Skip to content

Commit

Permalink
dsu import spec work
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Jan 30, 2024
1 parent 1a294e9 commit c3b95f1
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/dsu/presenters/import/all_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def import_entry_groups
entry_groups_hash[project_name] = {} unless entry_groups_hash.key?(project_name)

Date.parse(entry_group_entry[:entry_group]).to_s.tap do |time|
entry_groups_hash[project_name][time] = [] unless entry_groups_hash.key?(time)
entry_groups_hash[project_name][time] = [] unless entry_groups_hash[project_name].key?(time)
entry_groups_hash[project_name][time] << entry_group_entry[:entry_group_entry]
end
end
Expand Down
12 changes: 8 additions & 4 deletions lib/dsu/presenters/import/dates_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Import
class DatesPresenter < BasePresenterEx
include ImportFile

attr_reader :from, :to
attr_reader :from, :to, :import_file_path, :import_messages

def initialize(from:, to:, import_file_path:, options: {})
super(options: options)
Expand Down Expand Up @@ -41,8 +41,6 @@ def project_name

private

attr_reader :import_file_path

def import_entry_groups
@import_entry_groups ||= CSV.foreach(import_file_path,
headers: true, header_converters: :symbol).with_object({}) do |entry_group_entry, entry_groups_hash|
Expand All @@ -59,7 +57,7 @@ def import_entry_groups
entry_groups_hash[project_name] = {} unless entry_groups_hash.key?(project_name)

entry_group_time.to_date.to_s.tap do |time|
entry_groups_hash[project_name][time] = [] unless entry_groups_hash.key?(time)
entry_groups_hash[project_name][time] = [] unless entry_groups_hash[project_name].key?(time)
entry_groups_hash[project_name][time] << entry_group_entry[:entry_group_entry]
end
end
Expand All @@ -68,6 +66,12 @@ def import_entry_groups
def middle_of_day_for(date_string)
Time.parse(date_string).in_time_zone.middle_of_day
end

def importer_service
@importer_service ||= Services::EntryGroup::ImporterService.new(
import_projects: import_entry_groups, options: options
)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/locales/en/subcommands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ en:
$ dsu import all -i /path/to/import.csv
messages:
import_success: Entry group for %{date} imported successfully.
import_error: "Entry group for %{date} imported with an error: %{error}."
import_error: "Entry group for %{date} imported with an error: %{error}"
import_error_raised: "The entry groups failed to import. The following error was raised: \"%{error}\"."
nothing_to_import: No entry groups to import.
cancelled: Cancelled.
Expand Down
126 changes: 87 additions & 39 deletions spec/dsu/views/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@

let(:options) { {} }
let(:import_file_path) { 'spec/fixtures/files/import.csv' }
let(:presenter) do
Dsu::Presenters::Import::AllPresenter.new(import_file_path: import_file_path, options: options)
end

describe '#initialize' do
let(:presenter) do
Dsu::Presenters::Import::AllPresenter.new(import_file_path: import_file_path, options: options)
end

it 'does not raise an error' do
expect { import_view }.to_not raise_error
end
end

describe '#render' do
context 'when all entry groups are being imported' do
shared_examples 'the import file does not exist' do
context 'when the import file does not exist' do
let(:import_file_path) { 'spec/fixtures/files/does-not-exist.csv' }

Expand All @@ -26,7 +27,9 @@
end.chomp)).to include('Import file spec/fixtures/files/does-not-exist.csv does not exist.')
end
end
end

shared_examples 'there is nothing to import' do
context 'when there is nothing to import' do
let(:import_file_path) { 'spec/fixtures/files/nothing-to-import.csv' }

Expand All @@ -36,61 +39,106 @@
end.chomp)).to include('No entry groups to import.')
end
end
end

context 'when there is something to import' do
context 'when the user responds with "Y"' do
before do
stub_import_prompt(response: 'Y')
end

let(:expected_output) do
<<~OUTPUT
Entry group for 2023-12-31 imported successfully.
Entry group for 2024-01-01 imported successfully.
Entry group for 2024-01-02 imported successfully.
OUTPUT
end

context 'when all imports are successful' do
it "displays the 'imported successfully' message" do
expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do
import_view.render
end.chomp)).to include(expected_output.chomp)
end
end
shared_examples 'there is something to import' do
context 'when the user responds with "Y"' do
before do
stub_import_prompt(response: 'Y')
end

context 'when the user responds with "n"' do
before do
stub_import_prompt(response: 'n')
end
let(:expected_output) do
<<~OUTPUT
Entry group for 2023-12-31 imported successfully.
Entry group for 2024-01-01 imported successfully.
Entry group for 2024-01-02 imported successfully.
OUTPUT
end

it "displays the 'nothing to import' message" do
context 'when all imports are successful' do
it "displays the 'imported successfully' message" do
expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do
import_view.render
end.chomp)).to include('Cancelled.')
end.chomp)).to include(expected_output.chomp)
end
end
end

context 'when the import file has errors' do
context 'when the user responds with "n"' do
before do
stub_import_prompt(response: 'Y')
stub_import_prompt(response: 'n')
end

let(:import_file_path) { 'spec/fixtures/files/import-with-errors.csv' }
let(:expected_output) { /The entry groups failed to import/ }

it "displays the 'imported successfully' message" do
it "displays the 'nothing to import' message" do
expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do
import_view.render
end.chomp)).to match(expected_output)
end.chomp)).to include('Cancelled.')
end
end
end

shared_examples 'the import raises an error' do
before do
stub_import_prompt(response: 'Y')
end

let(:import_file_path) { 'spec/fixtures/files/import-with-errors.csv' }
let(:expected_output) { /The entry groups failed to import/ }

it "displays the 'imported successfully' message" do
expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do
import_view.render
end.chomp)).to match(expected_output)
end
end

shared_examples 'the import has errors' do
before do
stub_import_prompt(response: 'Y')
end

let(:options) { { merge: false } }
let(:expected_output) do
<<~OUTPUT
Entry group for 2023-12-31 imported with an error: Entries array contains duplicate entry: "Entry 2023-12-31...".
Entry group for 2024-01-01 imported successfully.
Entry group for 2024-01-02 imported successfully.
OUTPUT
end

let(:import_file_path) { 'spec/fixtures/files/import-with-duplicate-errors.csv' }

it "displays the 'imported successfully' message" do
expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do
import_view.render
end.chomp)).to include(expected_output.chomp)
end
end

context 'when all entry groups are being imported' do
let(:presenter) do
Dsu::Presenters::Import::AllPresenter.new(import_file_path: import_file_path, options: options)
end

it_behaves_like 'the import file does not exist'
it_behaves_like 'there is nothing to import'
it_behaves_like 'there is something to import'
it_behaves_like 'the import raises an error'
it_behaves_like 'the import has errors'
end

describe 'when entry groups are being imported by to/from dates' do
let(:presenter) { instance_double(Dsu::Presenters::Import::DatesPresenter) }
let(:presenter) do
Dsu::Presenters::Import::DatesPresenter.new(from: from, to: to, import_file_path: import_file_path, options: options)
end
let(:from) { Time.parse('2023-12-31').localtime }
let(:to) { Time.parse('2024-01-02').localtime }

it_behaves_like 'the import file does not exist'
it_behaves_like 'there is nothing to import'
it_behaves_like 'there is something to import'
it_behaves_like 'the import raises an error'
it_behaves_like 'the import has errors'
end
end
end
5 changes: 5 additions & 0 deletions spec/fixtures/files/import-with-duplicate-errors.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project_name,version,entry_group,entry_no,total_entries,entry_group_entry
default,20230613121411,2023-12-31,1,2,Entry 2023-12-31 duplicate
default,20230613121411,2023-12-31,2,2,Entry 2023-12-31 duplicate
default,20230613121411,2024-01-01,1,1,Entry 2024-01-01 1 of 2
default,20230613121411,2024-01-02,1,1,Entry 2024-01-02 2 of 2

0 comments on commit c3b95f1

Please sign in to comment.