Skip to content

Commit

Permalink
spec for serializer and start of spec for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
yuenmichelle1 committed Jun 27, 2023
1 parent c5bf191 commit 738b79e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module ClassificationCounts
class DailyClassificationCount < ApplicationRecord
self.table_name = 'daily_classification_count'
attribute :classification_count, :integer
attribute :count, :integer
attribute :period, :datetime

def readonly?
true
Expand Down
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Application < Rails::Application
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")

# Use this to get Continuous Aggregates to Persist in specs. See: https://github.com/rails/rails/issues/38302
config.active_record.schema_format = :sql

# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/daily_classification_counts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

FactoryBot.define do
factory :daily_classification_count, class: 'ClassificationCounts::DailyClassificationCount' do
day { Date.today }
classification_count { 212 }
period { Date.today }
count { 212 }
end
end
23 changes: 23 additions & 0 deletions spec/queries/count_classifications_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe CountClassifications do
describe 'relation' do
let(:params) { {} }
let(:count_classifications) { described_class.new(params) }
it 'returns DailyClassificationCount if not given workflow or project ids' do
expect(count_classifications.counts.model).to be ClassificationCounts::DailyClassificationCount
end

it 'returns DailyWorkflowClassificationCount if workflow_id given' do
params[:workflow_id] = 2
expect(count_classifications.counts.model).to be ClassificationCounts::DailyWorkflowClassificationCount
end

it 'returns DailyProjectClassificationCount if workflow_id given' do
params[:project_id] = 2
expect(count_classifications.counts.model).to be ClassificationCounts::DailyProjectClassificationCount
end
end
end
6 changes: 3 additions & 3 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'

Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
Expand All @@ -21,12 +21,12 @@
abort e.to_s.strip
end
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.fixture_path = "#{Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
config.use_transactional_fixtures = false

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
Expand Down
30 changes: 30 additions & 0 deletions spec/serializers/classification_counts_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ClassificationCountsSerializer do
let(:classification_count) { build(:daily_classification_count) }
let(:count_serializer) { described_class.new([classification_count]) }

it 'returns total_count when period not given' do
serialized = count_serializer.as_json(serializer_options: {})
expect(serialized).to have_key(:total_count)
expect(serialized).not_to have_key(:data)
expect(serialized[:total_count]).to eq(classification_count.count)
end

it 'returns total_count and data when period is given' do
serialized = count_serializer.as_json(serializer_options: { period: 'year' })
expect(serialized).to have_key(:total_count)
expect(serialized).to have_key(:data)
expect(serialized[:data].size).to eq(1)
end

it 'sums up total_count correctly' do
classification_count2 = build(:daily_classification_count)
classification_counts = [classification_count, classification_count2]
serializer = described_class.new(classification_counts)
serialized = serializer.as_json(serializer_options: {})
expect(serialized[:total_count]).to eq(classification_counts.sum(&:count))
end
end

0 comments on commit 738b79e

Please sign in to comment.