Skip to content

Commit

Permalink
Cleanup + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
forsbergplustwo committed Aug 27, 2023
1 parent da38de1 commit 76e1917
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 24 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ group :development, :test do
gem "standard"

gem "mocha"
gem "http_logger"

# TODO: Add these back when Ruby 2.7+ is supported
# gem "standard-rails"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ GEM
activesupport (>= 4.2)
high_voltage (3.1.2)
hike (1.2.3)
http_logger (0.7.0)
hub (1.12.4)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -349,6 +350,7 @@ DEPENDENCIES
graphql-client
groupdate
high_voltage
http_logger
hub
intercom-rails
jbuilder (~> 2.0)
Expand Down
2 changes: 1 addition & 1 deletion app/models/graphql/transactions_query.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Graphql::TransactionsQuery = ShopifyPartnerAPI.client.parse <<~'GRAPHQL'
query($createdAtMin: DateTime, $cursor: String) {
transactions(createdAtMin: $createdAtMin, after: $cursor, first: 100) {
transactions(createdAtMin: $createdAtMin, after: $cursor, first: 1) {
edges {
cursor
node {
Expand Down
3 changes: 2 additions & 1 deletion app/models/payment_history/api_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def import!
def import_new_payments
cursor = ""
has_next_page = true
created_at_min = user.calculate_from_date.strftime("%Y-%m-%dT%H:%M:%S.%L%z") # ISO-8601
created_at_min = user.calculate_from_date.iso8601 # ISO-8601
throttle_start_time = Time.zone.now

while has_next_page == true
Expand Down Expand Up @@ -87,6 +87,7 @@ def fetch_from_api(cursor, created_at_min)
variables: {createdAtMin: created_at_min, cursor: cursor},
context: {access_token: user.partner_api_access_token, organization_id: user.partner_api_organization_id}
)
Rails.logger.info(results.inspect)
raise StandardError.new(results.errors.messages.map { |k, v| "#{k}=#{v}" }.join("&")) if results.errors.any?
results
end
Expand Down
29 changes: 29 additions & 0 deletions test/fixtures/files/graphql/recurring.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"data": {
"transactions": {
"edges": [
{
"cursor": "",
"node": {
"__typename": "AppSubscriptionSale",
"id": "gid://partners/AppSubscriptionSale/242565501",
"created_at": "2023-02-01T00:00:00.000000Z",
"net_amount": {
"amount": "23.81"
},
"app": {
"name": "Recurring Subscription App"
},
"shop": {
"myshopify_domain": "recurring.myshopify.com"
}
}
}
],
"page_info": {
"has_next_page": false
}
}
},
"errors": []
}
9 changes: 0 additions & 9 deletions test/fixtures/files/payouts/mixed.csv

This file was deleted.

2 changes: 2 additions & 0 deletions test/fixtures/files/payouts/recurring.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Payout Period,Payout Date,Shop,Shop Country,Charge Creation Time,Charge Type,Category,Partner Sale,Shopify Fee,Processing Fee,Regulatory Operating Fee,Partner Share,App Title,Theme Name,Tax Description,Charge ID,Partner Sale Currency,Partner Sale In Payout Currency,Payout Currency
,2023-02-01 00:00:00 UTC,recurring.myshopify.com,US,2023-02-01 00:00:00 UTC,App sale – 30-day subscription,App revenue,29.0,4.35,0.84,0.0,23.81,Recurring Subscription App,,,gid://shopify/AppSubscription/29942382912,USD,29.0,USD
46 changes: 39 additions & 7 deletions test/models/payment_history/api_importer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,52 @@ class PaymentHistory::ApiImporterTest < ActiveSupport::TestCase
assert importer.user == @user
end

# test "import!" do
# # TODO: Work out how to stub the API call
# importer = PaymentHistory::ApiImporter.new(user: @user)
test "import!" do
importer = PaymentHistory::ApiImporter.new(user: @user)

ShopifyPartnerAPI.client.expects(:query).with(
Graphql::TransactionsQuery,
variables: {
createdAtMin: @user.calculate_from_date.iso8601,
cursor: ""},
context: {
access_token: @user.partner_api_access_token,
organization_id: @user.partner_api_organization_id
}
).returns(fixture_graphql_file_for("recurring.json"))

assert_difference "PaymentHistory.count", 1 do
importer.import!
end

# assert_difference "PaymentHistory.count", X do
# importer.import!
# end
# end
last_payment = PaymentHistory.last
assert last_payment.valid?
assert last_payment.user == @user
assert last_payment.app_title = "Recurring Subscription App"
assert last_payment.revenue == 23.81
assert last_payment.charge_type == "recurring_revenue"
assert last_payment.payment_date == Date.parse("2023-02-01")
assert last_payment.shop == "recurring.myshopify.com"
# Not possible to get this data from the API
# assert last_payment.shop_country == "US"
end

test "import! fails with no api credentials" do
@user.partner_api_access_token = nil
@user.partner_api_organization_id = nil

importer = PaymentHistory::ApiImporter.new(user: @user)

assert_raise StandardError do
importer.import!
end
end

private

def fixture_graphql_file_for(filename)
# Requires json file key names to be in underscore format (not camelCase)
json = File.read(Rails.root.join("test", "fixtures", "files", "graphql", filename))
JSON.parse(json, object_class: OpenStruct)
end
end
16 changes: 10 additions & 6 deletions test/models/payment_history/csv_importer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
class PaymentHistory::CsvImporterTest < ActiveSupport::TestCase
setup do
@user = users(:regular)
@filename = "mixed.csv"
@filename = "recurring.csv"
end

test "new" do
importer = PaymentHistory::CsvImporter.new(user: @user, filename: @filename)

assert importer.user == @user
assert importer.filename == @filename
assert importer.calculate_from_date == @user.calculate_from_date
end

test "import! of csv files" do
Expand All @@ -21,7 +20,7 @@ class PaymentHistory::CsvImporterTest < ActiveSupport::TestCase
importer = PaymentHistory::CsvImporter.new(user: @user, filename: @filename)
importer.expects(:fetch_from_s3).returns(file)

assert_difference "PaymentHistory.count", 8 do
assert_difference "PaymentHistory.count", 1 do
importer.import!
end
assert_correct_last_payment
Expand All @@ -33,7 +32,7 @@ class PaymentHistory::CsvImporterTest < ActiveSupport::TestCase
importer = PaymentHistory::CsvImporter.new(user: @user, filename: zip_file.path)
importer.expects(:fetch_from_s3).returns(zip_file)

assert_difference "PaymentHistory.count", 8 do
assert_difference "PaymentHistory.count", 1 do
importer.import!
end
assert_correct_last_payment
Expand All @@ -42,6 +41,11 @@ class PaymentHistory::CsvImporterTest < ActiveSupport::TestCase
zip_file.unlink
end


# test "Import! of different types of payments" do
# # TODO: Add tests for all types of payments
# end

private

def fixture_csv_file_for(filename)
Expand All @@ -63,8 +67,8 @@ def assert_correct_last_payment
assert last_payment.app_title = "Recurring Subscription App"
assert last_payment.revenue == 23.81
assert last_payment.charge_type == "recurring_revenue"
assert last_payment.payment_date == Date.parse("2023-07-01")
assert last_payment.shop == "monthly.myshopify.com"
assert last_payment.payment_date == Date.parse("2023-02-01")
assert last_payment.shop == "recurring.myshopify.com"
assert last_payment.shop_country == "US"
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "mocha"
require "mocha/minitest"

# TODO: Remove this when Rails 4 is no longer used
Expand Down

0 comments on commit 76e1917

Please sign in to comment.