Browse Source

Save total logs count for bulk upload

pull/1777/head
Kat 3 years ago
parent
commit
194f95b213
  1. 4
      app/services/bulk_upload/lettings/validator.rb
  2. 1
      app/services/bulk_upload/processor.rb
  3. 4
      app/services/bulk_upload/sales/validator.rb
  4. 5
      db/migrate/20230713140231_add_total_logs_count_to_bulk_upload.rb
  5. 3
      db/schema.rb
  6. 4
      spec/services/bulk_upload/lettings/validator_spec.rb
  7. 10
      spec/services/bulk_upload/processor_spec.rb
  8. 27
      spec/services/bulk_upload/sales/validator_spec.rb

4
app/services/bulk_upload/lettings/validator.rb

@ -73,6 +73,10 @@ class BulkUpload::Lettings::Validator
row_parsers.any?(&:log_already_exists?) row_parsers.any?(&:log_already_exists?)
end end
def total_logs_count
csv_parser.body_rows.count
end
private private
# n^2 algo # n^2 algo

1
app/services/bulk_upload/processor.rb

@ -10,6 +10,7 @@ class BulkUpload::Processor
download download
@bulk_upload.update!(total_logs_count: validator.total_logs_count)
return send_failure_mail(errors: validator.errors.full_messages) if validator.invalid? return send_failure_mail(errors: validator.errors.full_messages) if validator.invalid?
validator.call validator.call

4
app/services/bulk_upload/sales/validator.rb

@ -67,6 +67,10 @@ class BulkUpload::Sales::Validator
errors.count == errors.where(category: "soft_validation").count && errors.count.positive? errors.count == errors.where(category: "soft_validation").count && errors.count.positive?
end end
def total_logs_count
csv_parser.body_rows.count
end
private private
# n^2 algo # n^2 algo

5
db/migrate/20230713140231_add_total_logs_count_to_bulk_upload.rb

@ -0,0 +1,5 @@
class AddTotalLogsCountToBulkUpload < ActiveRecord::Migration[7.0]
def change
add_column :bulk_uploads, :total_logs_count, :integer
end
end

3
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_07_10_101532) do ActiveRecord::Schema[7.0].define(version: 2023_07_13_140231) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -40,6 +40,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_10_101532) do
t.text "filename" t.text "filename"
t.integer "needstype" t.integer "needstype"
t.text "choice" t.text "choice"
t.integer "total_logs_count"
t.index ["identifier"], name: "index_bulk_uploads_on_identifier", unique: true t.index ["identifier"], name: "index_bulk_uploads_on_identifier", unique: true
t.index ["user_id"], name: "index_bulk_uploads_on_user_id" t.index ["user_id"], name: "index_bulk_uploads_on_user_id"
end end

4
spec/services/bulk_upload/lettings/validator_spec.rb

@ -65,6 +65,10 @@ RSpec.describe BulkUpload::Lettings::Validator do
it "is valid" do it "is valid" do
expect(validator).to be_valid expect(validator).to be_valid
end end
it "returns correct total logs count" do
expect(validator.total_logs_count).to be(1)
end
end end
context "and file has too few valid headers" do context "and file has too few valid headers" do

10
spec/services/bulk_upload/processor_spec.rb

@ -33,6 +33,7 @@ RSpec.describe BulkUpload::Processor do
BulkUpload::Lettings::Validator, BulkUpload::Lettings::Validator,
invalid?: true, invalid?: true,
call: nil, call: nil,
total_logs_count: 1,
errors: [], errors: [],
) )
end end
@ -58,6 +59,13 @@ RSpec.describe BulkUpload::Processor do
expect(mock_validator).not_to have_received(:call) expect(mock_validator).not_to have_received(:call)
end end
it "sets total number of logs on bulk upload" do
processor.call
bulk_upload.reload
expect(bulk_upload.total_logs_count).to eq(1)
end
end end
context "when the bulk upload processing throws an error" do context "when the bulk upload processing throws an error" do
@ -74,6 +82,7 @@ RSpec.describe BulkUpload::Processor do
instance_double( instance_double(
BulkUpload::Lettings::Validator, BulkUpload::Lettings::Validator,
invalid?: false, invalid?: false,
total_logs_count: 1,
) )
end end
@ -119,6 +128,7 @@ RSpec.describe BulkUpload::Processor do
BulkUpload::Lettings::Validator, BulkUpload::Lettings::Validator,
invalid?: false, invalid?: false,
call: nil, call: nil,
total_logs_count: 1,
any_setup_errors?: true, any_setup_errors?: true,
) )
end end

27
spec/services/bulk_upload/sales/validator_spec.rb

@ -246,4 +246,31 @@ RSpec.describe BulkUpload::Sales::Validator do
end end
end end
end end
describe "#total_logs_count?" do
around do |example|
Timecop.freeze(Time.zone.local(2023, 2, 22)) do
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
Singleton.__init__(FormHandler)
end
context "when all logs are valid" do
let(:target_path) { file_fixture("completed_2022_23_sales_bulk_upload.csv") }
before do
target_array = File.open(target_path).readlines
target_array[0..118].each do |line|
file.write line
end
file.rewind
end
it "returns correct total logs count" do
expect(validator.total_logs_count).to be(1)
end
end
end
end end

Loading…
Cancel
Save