diff --git a/CHANGELOG.md b/CHANGELOG.md index a4ace187c..3d9cad46b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Changelog Fab-manager +- Add a test for statistics generation - Fix a bug: statistics not built for instances with plans created before v4.3.3 - Fix a bug: when requesting to send the sso migration code, the email was case-sensitive. - Fix a bug: the adminsys email was case-sensitive. diff --git a/test/fixtures/availabilities.yml b/test/fixtures/availabilities.yml index 24b340890..70ba0eb65 100644 --- a/test/fixtures/availabilities.yml +++ b/test/fixtures/availabilities.yml @@ -179,3 +179,13 @@ availability_18: updated_at: 2017-02-15 15:53:35.154433000 Z nb_total_places: 5 destroying: false + +availability_19: + id: 19 + start_at: <%= 1.day.from_now.utc.change({hour: 8}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> + end_at: <%= 1.day.from_now.utc.change({hour: 18}).strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> + available_type: machines + created_at: 2017-02-15 15:53:35.154433000 Z + updated_at: 2017-02-15 15:53:35.154433000 Z + nb_total_places: + destroying: false diff --git a/test/fixtures/machines_availabilities.yml b/test/fixtures/machines_availabilities.yml index 919de5bdf..1400c16a5 100644 --- a/test/fixtures/machines_availabilities.yml +++ b/test/fixtures/machines_availabilities.yml @@ -103,3 +103,8 @@ machines_availability_21: id: 21 machine_id: 2 availability_id: 16 + +machines_availability_22: + id: 22 + machine_id: 1 + availability_id: 19 diff --git a/test/services/statistic_service_test.rb b/test/services/statistic_service_test.rb new file mode 100644 index 000000000..680a10232 --- /dev/null +++ b/test/services/statistic_service_test.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'test_helper' + +class StatisticServiceTest < ActiveSupport::TestCase + setup do + @user = User.members.without_subscription.first + @admin = User.with_role(:admin).first + login_as(@admin, scope: :user) + end + + def test + machine_stats_count = Stats::Machine.all.count + subscription_stats_count = Stats::Subscription.all.count + + # Create a reservation to generate an invoice + machine = Machine.find(1) + availability = Availability.find(19) + post '/api/local_payment/confirm_payment', params: { + customer_id: @user.id, + items: [ + { + reservation: { + reservable_id: machine.id, + reservable_type: machine.class.name, + slots_attributes: [ + { + start_at: availability.start_at.to_s(:iso8601), + end_at: (availability.start_at + 1.hour).to_s(:iso8601), + availability_id: availability.id + } + ] + } + } + ] + }.to_json, headers: default_headers + + # Create a subscription to generate another invoice + plan = Plan.find_by(group_id: @user.group.id, type: 'Plan') + post '/api/local_payment/confirm_payment', + params: { + customer_id: @user.id, + items: [ + { + subscription: { + plan_id: plan.id + } + } + ] + }.to_json, headers: default_headers + + # Build the stats for today, we expect the above invoices (reservation+subscription) to appear in the resulting stats + StatisticService.new.generate_statistic( + start_date: DateTime.current.beginning_of_day, + end_date: DateTime.current.end_of_day + ) + + assert_equal machine_stats_count + 1, Stats::Machine.all.count + assert_equal subscription_stats_count + 1, Stats::Subscription.all.count + end +end