Skip to content

Commit

Permalink
Start adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
forsbergplustwo committed Aug 24, 2023
1 parent 3698aba commit a8c342b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
9 changes: 7 additions & 2 deletions app/models/payment_history/csv_importer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "zip"

class PaymentHistory::CsvImporter
attr_accessor :user, :calculate_from_date, :temp_files
attr_accessor :user, :calculate_from_date, :filename, :temp_files

CSV_READER_OPTIONS = {
converters: :all,
Expand Down Expand Up @@ -62,13 +62,14 @@ class PaymentHistory::CsvImporter
def initialize(user:, filename:)
@user = user
@calculate_from_date = user.calculate_from_date
@filename = filename
@temp_files = {}
@temp_files[:csv] = prepare_csv_file(filename)
@rows_processed_count = 0
@batch_of_payments = []
end

def import!
prepare_csv_file
clear_old_payments
import_new_payments
rescue => e
Expand All @@ -79,6 +80,10 @@ def import!

private

def prepare_csv_file
@temp_files[:csv] = prepare_csv_file(@filename)
end

def clear_old_payments
user.payment_histories.where("payment_date > ?", calculate_from_date).delete_all
end
Expand Down
23 changes: 23 additions & 0 deletions test/models/payment_history/csv_importer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "test_helper"

class PaymentHistory::CsvImporterTest < ActiveSupport::TestCase

setup do
@user = users(:regular)
@filename = "filename.zip"
end

test "new" do
@user = users(:regular)
@filename = "filename.zip"

@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" do
# end
end
7 changes: 4 additions & 3 deletions test/models/payment_history_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "test_helper"

class PaymentHistoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end

test "default_start_date" do
PaymentHistory.default_start_date == 4.years.ago.to_date
end
end

0 comments on commit a8c342b

Please sign in to comment.