aboutsummaryrefslogtreecommitdiffstats
path: root/spec/services/parent_notifier_spec.rb
blob: d2dc6b1849833c731bd0d46fbdebc6381b2d747c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
RSpec.describe ParentNotifier do
  let(:workbench_import) { create(:workbench_import) }

  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

      ParentNotifier.new(Import::Base).notify_when_finished(netex_imports)
    end

    it "doesn't call #notify_parent if its `notified_parent_at` is set" do
      netex_import = create(
        :netex_import,
        parent: workbench_import,
        status: :failed,
        notified_parent_at: DateTime.now
      )

      expect(netex_import).not_to receive(:notify_parent)

      ParentNotifier.new(Import::Base).notify_when_finished
    end
  end

  describe ".objects_pending_notification" do
    it "includes imports with a parent and `notified_parent_at` unset" do
      netex_import = create(
        :netex_import,
        parent: workbench_import,
        status: :successful,
        notified_parent_at: nil
      )

      Import::Base.where(id: netex_import).update_all notified_parent_at: nil

      expect(
        ParentNotifier.new(Import::Base).objects_pending_notification
      ).to eq([netex_import])
    end

    it "doesn't include imports without a parent" do
      create(:import, parent: nil)

      expect(
        ParentNotifier.new(Import::Base).objects_pending_notification
      ).to be_empty
    end

    it "doesn't include imports that aren't finished" do
      [:new, :pending, :running].each do |status|
        create(
          :netex_import,
          parent: workbench_import,
          status: status,
          notified_parent_at: nil
        )
      end

      expect(
        ParentNotifier.new(Import::Base).objects_pending_notification
      ).to be_empty
    end

    it "doesn't include imports that have already notified their parent" do
      create(
        :netex_import,
        parent: workbench_import,
        status: :successful,
        notified_parent_at: DateTime.now
      )

      expect(
        ParentNotifier.new(Import::Base).objects_pending_notification
      ).to be_empty
    end
  end
end