Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): add code coverage reporting #400

Merged
merged 26 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dcffab2
feat(tests): add code coverage reporting
guidojw Oct 13, 2023
8fb8a2d
fix: correct coverage path
guidojw Oct 13, 2023
db70b20
feat: add if-no-files-found on upload-artifact
guidojw Oct 13, 2023
4cfd948
refactor: use simplecov-lcov
guidojw Oct 13, 2023
bbba90b
chore: fixt lints
guidojw Oct 13, 2023
6a7d7d6
fix: remove test filters
guidojw Oct 13, 2023
e655b4b
feat(README): add badge
guidojw Oct 13, 2023
7a8ea9f
feat: add sentry_job spec
guidojw Oct 13, 2023
5d8273c
chore: add some tests
guidojw Oct 13, 2023
c915945
fix: tests
guidojw Oct 13, 2023
f6377e9
fix(activity_resource_spec): fix syntax error
guidojw Oct 13, 2023
1ccb7a1
fix: tests
guidojw Oct 14, 2023
9586b28
feat: add some tests to increase coverage
guidojw Oct 14, 2023
71f27a3
feat(groups_controller_spec): add filters test
guidojw Oct 14, 2023
7aea898
refactor: improve group filters
guidojw Oct 14, 2023
5e882e9
feat: user ical coverage
guidojw Oct 14, 2023
214387f
fix: readd it_behaves_like
guidojw Oct 14, 2023
45cf989
chore: remove redundant code
guidojw Oct 14, 2023
01a8dc8
refactor: try tokenless report uploading
guidojw Oct 14, 2023
49c7eaf
chore: comment test
guidojw Nov 4, 2023
2318cde
Merge branch 'staging' of https://github.com/csvalpha/amber-api into …
guidojw Mar 2, 2024
c6a7ca4
refactor: remove irrelevant test
guidojw Mar 2, 2024
3f25221
chore: update codecov-action
guidojw Mar 2, 2024
b15c2f1
Update README.md
lodewiges Oct 28, 2024
508777b
Fixed lint 1/2
lodewiges Oct 28, 2024
fe78702
Fixed lint 2/2
lodewiges Oct 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
required: false
type: string
secrets:
codecov_token:
description: Codecov token
required: true
rails_master_key:
description: The Rails master key
required: true
Expand Down Expand Up @@ -118,5 +121,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
Comment on lines +128 to +141
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these result in some kind of comment on PRs, too?

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method seems unused

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an endpoint, right? I suspect that's why it isn't covered by tests (or do we test endpoints too?) Did you check whether amber-ui depends on it?

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
guidojw marked this conversation as resolved.
Show resolved Hide resolved
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
Loading