aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorTeddy Wing2017-07-25 13:02:26 +0200
committerTeddy Wing2017-08-01 19:19:01 +0200
commiteb90176182c8caa3456a5582d2eb0623ce2b210c (patch)
treeb04d25f23437effc1c4c188cac7a92ffe7aa7edb /spec
parent0e49a76f4510a1bef43445f4e3621e313bd5ea66 (diff)
downloadchouette-core-eb90176182c8caa3456a5582d2eb0623ce2b210c.tar.bz2
Add a service that notifies parent imports of sub-import status
A new service, `ParentImportNotifier`, will be called by a Cron job every 5 minutes. This service will query all finished sub-imports that haven't yet notified their parent, and tell them to do so. factories/workbench_imports.rb: Add a `WorkbenchImport` factory. This is currently incomplete. We'll want to correctly initialise the `file` attribute eventually. import.rb: factories/imports.rb: In order to get the test setup to work correctly, we skip the `before_create` callback on `Import`. This is because that callback initialises `status` to "new". We want to be able to set any status value in our tests to test things properly. Thus the callback messes with our buttermilk. For all imports created by the factory, skip this callback. In order to skip the callback, I had to put it in its own method and pass the method name to `#skip_callback`. That's why the callback is now in a new method in `Import`. Adds an empty `#notify_parent` method in `Import`. For now this doesn't do anything, but eventually it will be filled in to perform the actual notification. I this changeset, all we're doing is setting up the connecting logic to notify the right things from the Cron job. import_spec.rb: Remove the `.class_name` validation because we removed that previously in `Import`. The `belongs_to` is now a polymorphic association, with no class name specified. parent_import_notifier_spec.rb: Needed to use `create` instead of `build_stubbed` in a lot of these cases so that I could validate the query. Really don't like this because now this set of tests is SUPER SLOW. If you have any ideas for how to speed it up, let's do that. Refs #3511
Diffstat (limited to 'spec')
-rw-r--r--spec/factories/imports.rb4
-rw-r--r--spec/factories/workbench_imports.rb9
-rw-r--r--spec/models/import_spec.rb2
-rw-r--r--spec/services/parent_import_notifier_spec.rb93
4 files changed, 107 insertions, 1 deletions
diff --git a/spec/factories/imports.rb b/spec/factories/imports.rb
index aaf56914f..a59bb5795 100644
--- a/spec/factories/imports.rb
+++ b/spec/factories/imports.rb
@@ -10,6 +10,10 @@ FactoryGirl.define do
started_at nil
ended_at nil
+ after(:build) do |import|
+ import.class.skip_callback(:create, :before, :initialize_fields)
+ end
+
factory :workbench_import, class: WorkbenchImport do
file {File.open(Rails.root.join('spec', 'fixtures', 'terminated_job.json'))}
end
diff --git a/spec/factories/workbench_imports.rb b/spec/factories/workbench_imports.rb
new file mode 100644
index 000000000..ea582846a
--- /dev/null
+++ b/spec/factories/workbench_imports.rb
@@ -0,0 +1,9 @@
+# require 'spec/support/file_fixtures'
+
+FactoryGirl.define do
+ # include FileFixtures
+
+ factory :workbench_import, class: WorkbenchImport, parent: :import do
+ # file { file_fixture('OFFRE_TRANSDEV_2017030112251.zip') }
+ end
+end
diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb
index c56858b44..88d339ea0 100644
--- a/spec/models/import_spec.rb
+++ b/spec/models/import_spec.rb
@@ -2,7 +2,7 @@ RSpec.describe Import, :type => :model do
it { should belong_to(:referential) }
it { should belong_to(:workbench) }
- it { should belong_to(:parent).class_name(described_class.to_s) }
+ it { should belong_to(:parent) }
it { should enumerize(:status).in("aborted", "canceled", "failed", "new", "pending", "running", "successful") }
diff --git a/spec/services/parent_import_notifier_spec.rb b/spec/services/parent_import_notifier_spec.rb
new file mode 100644
index 000000000..5888002ad
--- /dev/null
+++ b/spec/services/parent_import_notifier_spec.rb
@@ -0,0 +1,93 @@
+RSpec.describe ParentImportNotifier do
+ describe ".notify_when_finished" do
+ it "calls #notify_parent on finished imports" do
+ workbench_import = build_stubbed(:workbench_import)
+
+ finished_statuses = [:failed, :successful, :aborted, :canceled]
+ netex_imports = []
+
+ finished_statuses.each do |status|
+ netex_imports << build_stubbed(
+ :netex_import,
+ parent: workbench_import,
+ status: status
+ )
+ end
+
+ netex_imports.each do |netex_import|
+ expect(netex_import).to receive(:notify_parent)
+ end
+
+ ParentImportNotifier.notify_when_finished(netex_imports)
+ end
+
+ it "doesn't call #notify_parent if its `notified_parent_at` is set" do
+ workbench_import = create(:workbench_import)
+
+ netex_import = create(
+ :netex_import,
+ parent: workbench_import,
+ status: :failed,
+ notified_parent_at: DateTime.now
+ )
+
+ expect(netex_import).not_to receive(:notify_parent)
+
+ ParentImportNotifier.notify_when_finished
+ end
+ end
+
+ describe ".imports_pending_notification" do
+ it "includes imports with a parent and `notified_parent_at` unset" do
+ workbench_import = create(:workbench_import)
+ netex_import = create(
+ :netex_import,
+ parent: workbench_import,
+ status: :successful,
+ notified_parent_at: nil
+ )
+
+ expect(
+ ParentImportNotifier.imports_pending_notification
+ ).to eq([netex_import])
+ end
+
+ it "doesn't include imports without a parent" do
+ create(:import, parent: nil)
+
+ expect(
+ ParentImportNotifier.imports_pending_notification
+ ).to be_empty
+ end
+
+ it "doesn't include imports that aren't finished" do
+ workbench_import = create(:workbench_import)
+ [:new, :pending, :running].each do |status|
+ create(
+ :netex_import,
+ parent: workbench_import,
+ status: status,
+ notified_parent_at: nil
+ )
+ end
+
+ expect(
+ ParentImportNotifier.imports_pending_notification
+ ).to be_empty
+ end
+
+ it "doesn't include imports that have already notified their parent" do
+ workbench_import = create(:workbench_import)
+ create(
+ :netex_import,
+ parent: workbench_import,
+ status: :successful,
+ notified_parent_at: DateTime.now
+ )
+
+ expect(
+ ParentImportNotifier.imports_pending_notification
+ ).to be_empty
+ end
+ end
+end