From 0c2129973c711ce959845d756e2f58ff9fcb4884 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 17 Nov 2022 16:35:59 +0100 Subject: [PATCH] (test) added a test on statistics export --- .../statistic_custom_aggregations.yml | 14 +++ test/fixtures/statistic_indices.yml | 13 +- test/fixtures/statistic_sub_types.yml | 14 +++ test/fixtures/statistic_type_sub_types.yml | 15 +++ test/fixtures/statistic_types.yml | 22 +++- .../exports/statistics_export_test.rb | 113 ++++++++++++++++++ 6 files changed, 183 insertions(+), 8 deletions(-) create mode 100644 test/integration/exports/statistics_export_test.rb diff --git a/test/fixtures/statistic_custom_aggregations.yml b/test/fixtures/statistic_custom_aggregations.yml index 146b437fe..56f076f0e 100644 --- a/test/fixtures/statistic_custom_aggregations.yml +++ b/test/fixtures/statistic_custom_aggregations.yml @@ -6,3 +6,17 @@ one: field: "available_hours" es_index: "fablab" es_type: "availabilities" + +two: + query: '{"size":0, "aggregations":{"%{aggs_name}":{"sum":{"field":"nb_total_places"}}}, "query":{"bool":{"must":[{"range":{"start_at":{"gte":"%{start_date}", "lte":"%{end_date}"}}}, {"match":{"available_type":"training"}}]}}}' + statistic_type_id: 3 + field: "available_tickets" + es_index: "fablab" + es_type: "availabilities" + +three: + query: '{"size":0, "aggregations":{"%{aggs_name}":{"avg":{"field":"ca", "script":"BigDecimal.valueOf(_value).setScale(1, RoundingMode.HALF_UP)", "missing": 0}}}, "query":{"bool":{"must":[{"range":{"date":{"gte":"%{start_date}", "lte":"%{end_date}"}}}]}}}' + statistic_type_id: 15 + field: "average_cart" + es_index: "stats" + es_type: "order" diff --git a/test/fixtures/statistic_indices.yml b/test/fixtures/statistic_indices.yml index aab5aa638..8a99a0b47 100644 --- a/test/fixtures/statistic_indices.yml +++ b/test/fixtures/statistic_indices.yml @@ -11,7 +11,7 @@ statistic_index_1: statistic_index_2: id: 2 es_type_key: machine - label: Créneaux machines + label: Heures machines created_at: 2016-04-04 14:11:33.359367000 Z updated_at: 2016-04-04 14:11:33.359367000 Z table: true @@ -29,7 +29,7 @@ statistic_index_3: statistic_index_4: id: 4 es_type_key: event - label: Ateliers/Stages + label: Évènements created_at: 2016-04-04 14:11:33.366381000 Z updated_at: 2016-04-04 14:11:33.366381000 Z table: true @@ -70,3 +70,12 @@ statistic_index_8: updated_at: 2017-02-13 14:08:36.211740000 Z table: true ca: true + +statistic_index_9: + id: 9 + es_type_key: order + label: Commandes + created_at: 2022-10-11 13:31:22.962460 + updated_at: 2022-10-11 13:31:22.962460 Z + table: true + ca: true diff --git a/test/fixtures/statistic_sub_types.yml b/test/fixtures/statistic_sub_types.yml index 38f6dc2ab..bb6ecc561 100644 --- a/test/fixtures/statistic_sub_types.yml +++ b/test/fixtures/statistic_sub_types.yml @@ -153,3 +153,17 @@ statistic_sub_type_22: - month created_at: 2016-04-04 15:18:28.864495000 Z updated_at: 2016-04-04 15:18:28.864495000 Z + +statistic_sub_type_23: + id: 23 + key: paid-processed + label: Payée et/ou traitée + created_at: 2022-10-12 08:55:05.167910 Z + updated_at: 2022-10-12 08:55:05.167910 Z + +statistic_sub_type_24: + id: 24 + key: aborted + label: Interrompue + created_at: 2022-10-12 08:55:05.209986 Z + updated_at: 2022-10-12 08:55:05.209986 Z diff --git a/test/fixtures/statistic_type_sub_types.yml b/test/fixtures/statistic_type_sub_types.yml index 751a34706..d031bd900 100644 --- a/test/fixtures/statistic_type_sub_types.yml +++ b/test/fixtures/statistic_type_sub_types.yml @@ -243,3 +243,18 @@ statistic_type_sub_type_35: statistic_sub_type_id: 22 created_at: 2016-04-04 15:18:28.867317000 Z updated_at: 2016-04-04 15:18:28.867317000 Z + +statistic_type_sub_type_36: + id: 36 + statistic_type_id: 15 + statistic_sub_type_id: 23 + created_at: 2022-10-12 08:55:05.187077 + updated_at: 2022-10-12 08:55:05.187077 + +statistic_type_sub_type_37: + id: 37 + statistic_type_id: 15 + statistic_sub_type_id: 24 + created_at: 2022-10-12 08:55:05.211284 + updated_at: 2022-10-12 08:55:05.211284 + diff --git a/test/fixtures/statistic_types.yml b/test/fixtures/statistic_types.yml index 9fe87f1b5..6d15df106 100644 --- a/test/fixtures/statistic_types.yml +++ b/test/fixtures/statistic_types.yml @@ -92,8 +92,8 @@ statistic_type_9: statistic_type_10: id: 10 statistic_index_id: 1 - key: '2592000' - label: 'Durée : one month' + key: '2629746' + label: 'Durée : un mois' graph: true created_at: 2016-04-04 15:15:21.110438000 Z updated_at: 2016-04-04 15:15:21.110438000 Z @@ -102,8 +102,8 @@ statistic_type_10: statistic_type_11: id: 11 statistic_index_id: 1 - key: '5184000' - label: 'Durée : 2 months' + key: '5259492' + label: 'Durée : 2 mois' graph: true created_at: 2016-04-04 15:17:24.950033000 Z updated_at: 2016-04-04 15:17:24.950033000 Z @@ -133,8 +133,18 @@ statistic_type_14: id: 14 statistic_index_id: 1 key: '31556952' - label: 'Durée : 1 year' + label: 'Durée : 1 an' graph: true created_at: 2016-04-04 15:17:24.950033000 Z updated_at: 2016-04-04 15:17:24.950033000 Z - simple: true \ No newline at end of file + simple: true + +statistic_type_15: + id: 15 + statistic_index_id: 9 + key: store + label: Boutique + graph: true + created_at: 2022-10-11 13:31:23.004224 Z + updated_at: 2022-10-11 13:31:23.004224 Z + simple: true diff --git a/test/integration/exports/statistics_export_test.rb b/test/integration/exports/statistics_export_test.rb new file mode 100644 index 000000000..90fc5df44 --- /dev/null +++ b/test/integration/exports/statistics_export_test.rb @@ -0,0 +1,113 @@ +# frozen_string_literal: true + +require 'test_helper' +require 'rubyXL' + +module Exports; end + +class Exports::StatisticsExportTest < ActionDispatch::IntegrationTest + setup do + admin = User.with_role(:admin).first + login_as(admin, scope: :user) + end + + test 'export machine reservations statistics to Excel' do + # Build the stats for the June 2015, a machine reservation should have happened at the time + ::Statistics::BuilderService.generate_statistic({ start_date: '2015-06-01'.to_date.beginning_of_day, + end_date: '2015-06-30'.to_date.end_of_day }) + # Create a new export + post '/stats/machine/export', { + params: { + type_key: 'booking', + body: '{"query":{"bool":{"must":[{"term":{"type":"booking"}},{"range":{"date":{"gte":"2015-06-01T02:00:00+02:00",' \ + '"lte":"2015-06-30T23:59:59+02:00"}}}]}},"sort":[{"date":{"order":"desc"}}],"aggs":{"total_ca":{"sum":{"field":"ca"}}, ' \ + '"average_age":{"avg":{"field":"age"}},"total_stat":{"sum":{"field":"stat"}}}}' + } + } + + # Check response format & status + assert_equal 200, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the export was created correctly + res = json_response(response.body) + e = Export.where(id: res[:export_id]).first + assert_not_nil e, 'Export was not created in database' + + # Run the worker + worker = StatisticsExportWorker.new + worker.perform(e.id) + + # notification + assert_not_empty Notification.where(attached_object: e) + + # resulting XLSX file + assert FileTest.exist?(e.file), 'XLSX file was not generated' + workbook = RubyXL::Parser.parse(e.file) + + # test worksheet + assert_not_nil workbook[StatisticIndex.find_by(es_type_key: 'machine').label] + + # test data + reservation = Reservation.find(2) + wb = workbook[StatisticIndex.find_by(es_type_key: 'machine').label] + assert_equal 1, wb.sheet_data[0][1].value + assert_equal 15.0, wb.sheet_data[1][1].value + assert_equal reservation.user.statistic_profile.age.to_i, wb.sheet_data[2][1].value + assert_equal reservation.created_at.to_date, wb.sheet_data[5][0].value.to_date + assert_equal reservation.user.profile.full_name, wb.sheet_data[5][1].value + assert_equal reservation.user.email, wb.sheet_data[5][2].value + assert_equal reservation.user.profile.phone, wb.sheet_data[5][3].value + assert_equal I18n.t("export.#{reservation.user.statistic_profile.str_gender}"), wb.sheet_data[5][4].value + assert_equal reservation.user.statistic_profile.age.to_i, wb.sheet_data[5][5].value + assert_equal reservation.reservable.name, wb.sheet_data[5][6].value + assert_equal reservation.invoice_items.first.invoice.total / 100.0, wb.sheet_data[5][7].value + + # Clean XLSX file + require 'fileutils' + FileUtils.rm(e.file) + end + + test 'export global statistics to Excel' do + # Build the stats for the June 2015 + ::Statistics::BuilderService.generate_statistic({ start_date: '2015-06-01'.to_date.beginning_of_day, + end_date: '2015-06-30'.to_date.end_of_day }) + # Create a new export + post '/stats/global/export', { + params: { + type_key: 'booking', + body: '{"query":{"bool":{"must":[{"range":{"date":{"gte":"2015-06-01T02:00:00+02:00","lte":"2015-06-30T23:59:59+02:00"}}}]}}}' + } + } + + # Check response format & status + assert_equal 200, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the export was created correctly + res = json_response(response.body) + e = Export.where(id: res[:export_id]).first + assert_not_nil e, 'Export was not created in database' + + # Run the worker + worker = StatisticsExportWorker.new + worker.perform(e.id) + + # notification + assert_not_empty Notification.where(attached_object: e) + + # resulting XLSX file + assert FileTest.exist?(e.file), 'XLSX file was not generated' + workbook = RubyXL::Parser.parse(e.file) + + puts e.file + + # test worksheets + StatisticIndex.where(table: true).includes(:statistic_fields, statistic_types: [:statistic_sub_types]).each do |index| + index.statistic_types.each do |type| + sheet_name = "#{index.label} - #{type.label}".gsub(%r{[*|\\:"<>?/]}, '').truncate(31) + assert_not_nil workbook[sheet_name], "#{sheet_name} not found" + end + end + end +end