From 12f88dce6fed194e98b94c37af8e3ed7b1d12512 Mon Sep 17 00:00:00 2001 From: Bjorn Forsberg Date: Sat, 26 Aug 2023 15:32:42 +0200 Subject: [PATCH] Working + added test --- app/models/graphql/transactions_query.rb | 2 +- app/models/payment_history/api_importer.rb | 7 +---- app/models/user.rb | 2 +- .../payment_history/api_importer_test.rb | 31 +++++++++++++++++++ .../payment_history/csv_importer_test.rb | 4 +-- 5 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 test/models/payment_history/api_importer_test.rb diff --git a/app/models/graphql/transactions_query.rb b/app/models/graphql/transactions_query.rb index a808661..e83266f 100644 --- a/app/models/graphql/transactions_query.rb +++ b/app/models/graphql/transactions_query.rb @@ -1,4 +1,4 @@ -TransactionsQuery = ShopifyPartnerAPI.client.parse <<-'GRAPHQL' +Graphql::TransactionsQuery = ShopifyPartnerAPI.client.parse <<-'GRAPHQL' query($createdAtMin: DateTime, $cursor: String) { transactions(createdAtMin: $createdAtMin, after: $cursor, first: 100) { edges { diff --git a/app/models/payment_history/api_importer.rb b/app/models/payment_history/api_importer.rb index a8e4c7b..b1ce4d8 100644 --- a/app/models/payment_history/api_importer.rb +++ b/app/models/payment_history/api_importer.rb @@ -76,7 +76,7 @@ def import_new_payments created_at = Date.parse(node.created_at) - next if created_at <= last_calculated_metric_date + next if created_at <= user.calculate_from_date charge_type = lookup_charge_type(node.__typename) next if charge_type.nil? @@ -86,11 +86,6 @@ def import_new_payments payment_date: created_at ) - # STUPID: For my apps, I want Usage charges counted as "recurring" and not "one_time", others's don't - if USAGE_CHARGE_TYPES.include?(node.__typename) && user.count_usage_charges_as_recurring == true - record.charge_type = "recurring_revenue" - end - record.revenue = case node.__typename when "ReferralAdjustment", "ReferralTransaction" node.amount.amount diff --git a/app/models/user.rb b/app/models/user.rb index 8eddae4..f1efbc6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -30,7 +30,7 @@ def oldest_metric_date end def calculate_from_date - newest_metric_date || PaymentHistory.default_start_date + @calculate_from_date ||= newest_metric_date.presence || PaymentHistory.default_start_date end def clear_old_payments diff --git a/test/models/payment_history/api_importer_test.rb b/test/models/payment_history/api_importer_test.rb new file mode 100644 index 0000000..545e05e --- /dev/null +++ b/test/models/payment_history/api_importer_test.rb @@ -0,0 +1,31 @@ +require "test_helper" + +class PaymentHistory::ApiImporterTest < ActiveSupport::TestCase + setup do + @user = users(:regular) + end + + test "new" do + importer = PaymentHistory::ApiImporter.new(user: @user) + + assert importer.user == @user + end + + # test "import!" do + # # TODO: Work out how to stub the API call + # importer = PaymentHistory::ApiImporter.new(user: @user) + + # assert_difference "PaymentHistory.count", X do + # importer.import! + # end + # end + + test "import! fails with no api credentials" do + importer = PaymentHistory::ApiImporter.new(user: @user) + + assert_raise StandardError do + importer.import! + end + end + +end diff --git a/test/models/payment_history/csv_importer_test.rb b/test/models/payment_history/csv_importer_test.rb index 9f50047..a01f25e 100644 --- a/test/models/payment_history/csv_importer_test.rb +++ b/test/models/payment_history/csv_importer_test.rb @@ -15,7 +15,7 @@ class PaymentHistory::CsvImporterTest < ActiveSupport::TestCase assert importer.calculate_from_date == @user.calculate_from_date end - test "can import csv files" do + test "import! of csv files" do file = fixture_csv_file_for(@filename) importer = PaymentHistory::CsvImporter.new(user: @user, filename: @filename) @@ -27,7 +27,7 @@ class PaymentHistory::CsvImporterTest < ActiveSupport::TestCase assert_correct_last_payment end - test "can import zip files" do + test "import! of zip files" do zip_file = fixture_zip_file_for(@filename) importer = PaymentHistory::CsvImporter.new(user: @user, filename: zip_file.path)