Skip to content

Commit

Permalink
feat(tests): add code coverage reporting (#400)
Browse files Browse the repository at this point in the history
* feat(tests): add code coverage reporting

* fix: correct coverage path

* feat: add if-no-files-found on upload-artifact

* refactor: use simplecov-lcov

* chore: fixt lints

* fix: remove test filters

* feat(README): add badge

* feat: add sentry_job spec

* chore: add some tests

* fix: tests

* fix(activity_resource_spec): fix syntax error

* fix: tests

* feat: add some tests to increase coverage

* feat(groups_controller_spec): add filters test

* refactor: improve group filters

* feat: user ical coverage

* fix: readd it_behaves_like

* chore: remove redundant code

* refactor: try tokenless report uploading

* chore: comment test

* refactor: remove irrelevant test

* chore: update codecov-action

* Update README.md

Co-authored-by: Matteo Bronkhorst <[email protected]>

* Fixed lint 1/2

* Fixed lint 2/2

---------

Co-authored-by: lodewiges <[email protected]>
Co-authored-by: Matteo Bronkhorst <[email protected]>
  • Loading branch information
3 people authored Oct 28, 2024
1 parent b4437f5 commit 153319a
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 126 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,21 @@ jobs:
env:
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
run: |
mkdir coverage
docker run -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_HOST=localhost -e \
RAILS_MASTER_KEY --network=host app bin/ci.sh spec
RAILS_MASTER_KEY --network=host -v "$(pwd)"'/coverage:/app/coverage' app bin/ci.sh spec
- name: Upload coverage report to Codecov
if: ${{ !cancelled() }}
uses: codecov/codecov-action@54bcd8715eee62d40e33596ef5e8f0f48dbbccab # v4.1.0
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Upload coverage report artifact
if: ${{ !cancelled() }}
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
name: coverage
path: coverage/
if-no-files-found: error
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ group :test do
gem 'rubocop-rails', require: false
gem 'rubocop-rspec', require: false
gem 'simplecov', require: false
gem 'simplecov-lcov', '~> 0.8.0', require: false
gem 'terminal-notifier-guard'
gem 'timecop'
gem 'vcr'
Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ GEM
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov-lcov (0.8.0)
simplecov_json_formatter (0.1.4)
slack-notifier (2.4.0)
spoon (0.0.6)
Expand Down Expand Up @@ -596,6 +597,7 @@ DEPENDENCIES
sidekiq (~> 6.0)
sidekiq-scheduler (~> 5.0, >= 5.0.2)
simplecov
simplecov-lcov (~> 0.8.0)
slack-notifier (~> 2.0)
spring
spring-commands-rspec
Expand All @@ -609,4 +611,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
2.2.33
2.4.14
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Alpha AMBER API
================
[![Continuous Integration](https://github.com/csvalpha/amber-api/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/csvalpha/amber-api/actions/workflows/continuous-integration.yml)
[![Continuous Delivery](https://github.com/csvalpha/amber-api/actions/workflows/continuous-delivery.yml/badge.svg)](https://github.com/csvalpha/amber-api/actions/workflows/continuous-delivery.yml)
[![codecov](https://codecov.io/github/csvalpha/amber-api/graph/badge.svg)](https://codecov.io/github/csvalpha/amber-api)

## Prerequisites
If you're going to run the project with Docker, you only need to install the following prerequisites:
Expand Down
4 changes: 2 additions & 2 deletions app/resources/v1/application_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class V1::ApplicationResource < JSONAPI::Resource
def self.creatable_fields(_context)
[]
end

# :nocov:

def self.updatable_fields(context)
Expand All @@ -22,7 +21,6 @@ def self.updatable_fields(context)
def self.searchable_fields
[]
end

# :nocov:

def self.apply_filter(records, filter, value, options)
Expand Down Expand Up @@ -79,9 +77,11 @@ def current_user
context.fetch(:user)
end

# :nocov:
def current_application
context.fetch(:application)
end
# :nocov:

def self.current_user_or_application(options)
options.fetch(:context).fetch(:user) || options.fetch(:context).fetch(:application)
Expand Down
6 changes: 2 additions & 4 deletions app/resources/v1/group_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ def description_camofied
has_many :permissions

filter :active, apply: ->(records, _value, _options) { records.active }
filter :kind, apply: ->(records, value, _options) { records.where(kind: value[0]) }
filter :administrative, (lambda do |records, value, _options|
records.where(administrative: value[0])
end)
filter :kind
filter :administrative

def fetchable_fields
return super - [:avatar] if current_user
Expand Down
14 changes: 14 additions & 0 deletions spec/jobs/sentry_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe SentryJob, type: :job do
describe '#perform' do
let(:job) { described_class.new }

before do
allow(Raven).to receive(:send_event)
job.perform('Example event')
end

it { expect(Raven).to have_received(:send_event).with('Example event') }
end
end
6 changes: 6 additions & 0 deletions spec/requests/v1/activities_controller/ical_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
categories: 'algemeen,sociëteit,vorming,invalid_cat,birthdays' })
end

let(:group) { create(:group, name: 'Leden') }
let(:membership) { create(:membership, user:, group:, start_date: 1.month.ago) }

let(:activity) { create(:activity, category: 'algemeen') }
let(:second_activity) { create(:activity, category: 'vorming') }

Expand All @@ -103,12 +106,15 @@
end

before do
group
membership
activity
second_activity
filtered_activity
end

it_behaves_like '200 OK'
it { expect(ical_events.map(&:summary).to_s).to include("Verjaardag #{user.full_name}") }
it { expect(ical_events.map(&:summary).to_s).to include(activity.title) }
it { expect(request.body).not_to include(filtered_activity.title) }
it { expect(ical_events.count).to eq 2 + birthday_users.count }
Expand Down
14 changes: 14 additions & 0 deletions spec/requests/v1/activities_controller/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,19 @@
it_behaves_like 'a publicly visible index request' do
let(:model_name) { :activity }
end

describe 'sortable attributes' do
context 'when valid' do
let(:record_url) { '/v1/activities?sort=-form.respond_until' }

it_behaves_like '200 OK'
end

context 'when invalid' do
let(:record_url) { '/v1/activities?sort=form.id' }

it_behaves_like '400 Bad Request'
end
end
end
end
2 changes: 1 addition & 1 deletion spec/requests/v1/daily_verses_controller/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:cache) { Rails.cache }
let(:record_url) { '/v1/daily_verses' }
let(:request) do
VCR.use_cassette('retrieve_daily_verse') do
VCR.use_cassette('retrieve_daily_verse', tag: :daily_verse) do
get record_url
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/requests/v1/groups_controller/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,31 @@
}
end
end

describe 'filters' do
include_context 'when authenticated' do
let(:user) { create(:user, user_permission_list: [record_permission]) }
end

let(:filtered_request) do
get("#{record_url}?filter[administrative]=true")
end

let(:administrative_group) { create(:group, administrative: true) }
let(:expected_ids) { [administrative_group.id] }

subject(:request) { filtered_request }

before do
administrative_group
end

it_behaves_like '200 OK'
it { expect(json_object_ids).to match_array(expected_ids) }
it { expect(json_object_ids.count).to equal(1) }
it { expect(json_object_ids).to include(administrative_group.id) }
it { expect(json_object_ids).not_to include(records[0].id) }
it { expect(json_object_ids).not_to include(records[1].id) }
end
end
end
20 changes: 20 additions & 0 deletions spec/resources/v1/activity_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,24 @@
it { expect(filtered.length).to eq 1 }
end
end

describe 'sort' do
let(:sorted) { described_class.apply_sort(records, sort, context) }

describe 'form.respond_until' do
let(:records) { Activity.all }
let(:sort) { { 'form.respond_until' => :desc } }
let(:form) { create(:form, respond_until: 1.day.from_now) }
let(:other_form) { create(:form, respond_until: 2.days.from_now) }
let(:activity) { create(:activity, form:) }
let(:other_activity) { create(:activity, form: other_form) }

before do
activity
other_activity
end

it { expect(sorted).to match_array [other_activity, activity] }
end
end
end
2 changes: 1 addition & 1 deletion spec/resources/v1/group_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
end

describe 'administrative' do
let(:filter) { { administrative: ['true'] } }
let(:filter) { { administrative: [true] } }
let(:group) { create(:group, administrative: true) }
let(:other_group) { create(:group, administrative: false) }

Expand Down
8 changes: 6 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'simplecov'
require 'simplecov-lcov'

SimpleCov.formatter = SimpleCov::Formatter::LcovFormatter
SimpleCov.start 'rails' do
add_filter 'app/jobs/sentry_job.rb'

minimum_coverage 95
minimum_coverage_by_file 95
end
Expand Down Expand Up @@ -30,4 +30,8 @@
VCR.configure do |config|
config.cassette_library_dir = 'spec/vcr_cassettes'
config.hook_into :webmock
config.filter_sensitive_data('<TOKEN>', :daily_verse) do
Base64.strict_encode64("#{Rails.application.config.x.daily_verse_user}:"\
"#{Rails.application.config.x.daily_verse_password}")
end
end
Loading

0 comments on commit 153319a

Please sign in to comment.