mailcannon 0.0.8.pre.1 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,211 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe MailCannon::EnvelopeBagMapReduce do
4
-
5
- describe "#find_gem_root_path" do
6
-
7
- after(:all) do
8
- Gem::Specification.any_instance.unstub(:gem_dir)
9
- Gem::Specification.unstub(:find_by_name)
10
- end
11
-
12
- context "when gem is found" do
13
- it "returns the path" do
14
- mocked_gem_path = "/app/vendor/bundle/ruby/2.0.0/bundler/gems/mailcannon-2c266138c2eb"
15
- Gem::Specification.stub(:find_by_name).and_return(Gem::Specification.new)
16
- Gem::Specification.any_instance.stub(:gem_dir).and_return(mocked_gem_path)
17
- expect(MailCannon::EnvelopeBag.find_gem_root_path).to eq(mocked_gem_path)
18
- end
19
- end
20
-
21
- context "when gem is not found" do
22
- it "returns empty string" do
23
- Gem::Specification.stub(:find_by_name) do
24
- raise LoadError.new(message: /mailcannon/)
25
- end
26
- expect(MailCannon::EnvelopeBag.find_gem_root_path).to eq("")
27
- end
28
- end
29
-
30
- context "when dependence is not found" do
31
- it "raises error" do
32
- Gem::Specification.stub(:find_by_name) do
33
- raise LoadError.new(message: /some_other_gem/)
34
- end
35
- expect{ MailCannon::EnvelopeBag.find_gem_root_path }.to raise_error
36
- end
37
- end
38
-
39
- context "something else goes wrong" do
40
- it "raises exception" do
41
- Gem::Specification.stub(:find_by_name).and_raise(Exception.new)
42
- expect{ MailCannon::EnvelopeBag.find_gem_root_path }.to raise_exception
43
- end
44
- end
45
- end
46
-
47
- describe "#js_map" do
48
-
49
- before(:each) do
50
- File.stub(:read) { |path| path }
51
- MailCannon::EnvelopeBag.instance_variable_set(:@js_map, nil)
52
- end
53
-
54
- after(:each) do
55
- File.unstub(:read)
56
- MailCannon::EnvelopeBag.unstub(:find_gem_root_path)
57
- MailCannon::EnvelopeBag.instance_variable_set(:@js_map, nil)
58
- end
59
-
60
-
61
- context "when gem is found" do
62
- it "returns full gem file path" do
63
- MailCannon::EnvelopeBag.stub(:find_gem_root_path).and_return("path/to/gem")
64
- expect(MailCannon::EnvelopeBag.js_map).to eq("path/to/gem/lib/mailcannon/reduces/envelope_bag_map.js")
65
- end
66
- end
67
-
68
- context "when gem is not found" do
69
- it "returns file path relative to current location" do
70
- MailCannon::EnvelopeBag.stub(:find_gem_root_path).and_return("")
71
- expect(MailCannon::EnvelopeBag.js_map).to eq("lib/mailcannon/reduces/envelope_bag_map.js")
72
- end
73
- end
74
- end
75
-
76
- describe "#js_reduce" do
77
-
78
- before(:each) do
79
- File.stub(:read) { |path| path }
80
- MailCannon::EnvelopeBag.instance_variable_set(:@js_reduce, nil)
81
- end
82
-
83
- after(:each) do
84
- File.unstub(:read)
85
- MailCannon::EnvelopeBag.unstub(:find_gem_root_path)
86
- MailCannon::EnvelopeBag.instance_variable_set(:@js_reduce, nil)
87
- end
88
-
89
- context "when gem is found" do
90
- it "returns full gem file path" do
91
- MailCannon::EnvelopeBag.stub(:find_gem_root_path).and_return("path/to/gem")
92
- expect(MailCannon::EnvelopeBag.js_reduce).to eq("path/to/gem/lib/mailcannon/reduces/envelope_bag_reduce.js")
93
- end
94
- end
95
-
96
- context "when gem is not found" do
97
- it "returns file path relative to current location" do
98
- MailCannon::EnvelopeBag.stub(:find_gem_root_path).and_return("")
99
- expect(MailCannon::EnvelopeBag.js_reduce).to eq("lib/mailcannon/reduces/envelope_bag_reduce.js")
100
- end
101
- end
102
- end
103
-
104
-
105
- let!(:envelope_bag) { build(:empty_envelope_bag) }
106
- let(:envelope_a) { build(:envelope, envelope_bag_id: envelope_bag.id) }
107
- let(:envelope_b) { build(:envelope, envelope_bag_id: envelope_bag.id) }
108
- let(:envelope_c) { build(:envelope, envelope_bag_id: envelope_bag.id) }
109
-
110
- def insert_sample_events
111
- test_hash = [
112
- {envelope_id: envelope_a.id, envelope_bag_id: envelope_bag.id, email: 'foo1@bar.com', timestamp: 1322000092, unique_arg: 'my unique arg', event: 'delivered', target_id: '1'},
113
- {envelope_id: envelope_a.id, envelope_bag_id: envelope_bag.id, email: 'foo2@bar.com', timestamp: 1322000093, unique_arg: 'my unique arg', event: 'open', target_id: '2'},
114
- {envelope_id: envelope_a.id, envelope_bag_id: envelope_bag.id, email: 'foo3@bar.com', timestamp: 1322000094, unique_arg: 'my unique arg', event: 'bounce', type: 'bounce', target_id: '3'},
115
- {envelope_id: envelope_b.id, envelope_bag_id: envelope_bag.id, email: 'foo1@bar.com', timestamp: 1322000092, unique_arg: 'my unique arg', event: 'delivered', target_id: '1'},
116
- {envelope_id: envelope_b.id, envelope_bag_id: envelope_bag.id, email: 'foo2@bar.com', timestamp: 1322000093, unique_arg: 'my unique arg', event: 'click', target_id: '2'},
117
- {envelope_id: envelope_c.id, envelope_bag_id: envelope_bag.id, email: 'foo1@bar.com', timestamp: 1322000092, unique_arg: 'my unique arg', event: 'click', target_id: '1'}
118
- ]
119
- MailCannon::SendgridEvent.insert_bulk(test_hash)
120
- end
121
-
122
- before(:each) do
123
- envelope_a.save
124
- envelope_b.save
125
- envelope_c.save
126
- insert_sample_events
127
- end
128
-
129
- describe "#change_events_status_for_envelope" do
130
- it "sets events status (processed) to :lock(false)" do
131
- MailCannon::EnvelopeBag.change_events_status_for_envelope_bag(envelope_bag.id, nil, :lock)
132
- expect(envelope_a.reload.sendgrid_events.where(processed: false).count).to eq(3)
133
- expect(envelope_b.reload.sendgrid_events.where(processed: false).count).to eq(2)
134
- expect(envelope_c.reload.sendgrid_events.where(processed: false).count).to eq(1)
135
- end
136
-
137
- it "sets events status (processed) to :processed(true)" do
138
- MailCannon::EnvelopeBag.change_events_status_for_envelope_bag(envelope_bag.id, nil, :processed)
139
- expect(envelope_a.reload.sendgrid_events.where(processed: true).count).to eq(3)
140
- expect(envelope_b.reload.sendgrid_events.where(processed: true).count).to eq(2)
141
- expect(envelope_c.reload.sendgrid_events.where(processed: true).count).to eq(1)
142
- end
143
- end
144
-
145
- describe ".reduce_statistics_for_envelope_bag" do
146
- let(:expected_hash_a){
147
- {
148
- "posted"=>{"count"=>0.0, "targets"=>[]},
149
- "processed"=>{"count"=>0.0, "targets"=>[]},
150
- "delivered"=>{"count"=>2.0, "targets"=>["1", "1"]},
151
- "open"=>{"count"=>1.0, "targets"=>["2"]},
152
- "click"=>{"count"=>2.0, "targets"=>["2", "1"]},
153
- "deferred"=>{"count"=>0.0, "targets"=>[]},
154
- "spam_report"=>{"count"=>0.0, "targets"=>[]},
155
- "spam"=>{"count"=>0.0, "targets"=>[]},
156
- "unsubscribe"=>{"count"=>0.0, "targets"=>[]},
157
- "drop"=>{"count"=>0.0, "targets"=>[]},
158
- "hard_bounce"=>{"count"=>1.0, "targets"=>["3"]},
159
- "soft_bounce"=>{"count"=>0.0, "targets"=>[]},
160
- "unknown"=>{"count"=>0.0, "targets"=>[]}
161
- }
162
- }
163
-
164
- let(:expected_hash_b){
165
- {
166
- "posted"=>{"count"=>0.0, "targets"=>[]},
167
- "processed"=>{"count"=>0.0, "targets"=>[]},
168
- "delivered"=>{"count"=>4.0, "targets"=>["1", "1","1", "1"]},
169
- "open"=>{"count"=>2.0, "targets"=>["2", "2"]},
170
- "click"=>{"count"=>4.0, "targets"=>["2", "1","2", "1"]},
171
- "deferred"=>{"count"=>0.0, "targets"=>[]},
172
- "spam_report"=>{"count"=>0.0, "targets"=>[]},
173
- "spam"=>{"count"=>0.0, "targets"=>[]},
174
- "unsubscribe"=>{"count"=>0.0, "targets"=>[]},
175
- "drop"=>{"count"=>0.0, "targets"=>[]},
176
- "hard_bounce"=>{"count"=>2.0, "targets"=>["3","3"]},
177
- "soft_bounce"=>{"count"=>0.0, "targets"=>[]},
178
- "unknown"=>{"count"=>0.0, "targets"=>[]}
179
- }
180
- }
181
-
182
- it "measure reduce output availability" do
183
- envelope_bag.reduce_statistics
184
- start_time = Time.now
185
- while MailCannon::EnvelopeBagStatistic.count==0
186
- sleep 1
187
- end
188
- end_time = Time.now
189
- diff = end_time-start_time
190
- expect(diff<1.0).to be_true
191
- end
192
-
193
- it "creates an EnvelopeStatistic entry" do
194
- expect{ MailCannon::EnvelopeBag.reduce_statistics_for_envelope_bag(envelope_bag.id) }.to change{ MailCannon::EnvelopeBagStatistic.count }.from(0).to(1)
195
- end
196
-
197
- it "returns statistics hash/json" do
198
- envelope_bag.reduce_statistics
199
- expect(envelope_bag.stats).to eq(expected_hash_a)
200
- end
201
-
202
- it "merges recurring reduces" do
203
- envelope_bag.reduce_statistics
204
- expect(envelope_bag.stats).to eq(expected_hash_a)
205
- insert_sample_events
206
- envelope_bag.reduce_statistics
207
- expect(envelope_bag.stats).to eq(expected_hash_b)
208
- end
209
- end
210
-
211
- end
@@ -1,41 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe MailCannon::EnvelopeBagStatistic do
4
-
5
- let!(:envelope_bag) { build(:empty_envelope_bag) }
6
- let!(:envelope_a) { build(:envelope, envelope_bag_id: envelope_bag.id) }
7
-
8
- def insert_sample_events
9
- test_hash = [
10
- {envelope_id: envelope_a.id, envelope_bag_id: envelope_bag.id, email: 'foo1@bar.com', timestamp: 1322000092, unique_arg: 'my unique arg', event: 'delivered', target_id: '1'},
11
- {envelope_id: envelope_a.id, envelope_bag_id: envelope_bag.id, email: 'foo2@bar.com', timestamp: 1322000093, unique_arg: 'my unique arg', event: 'open', target_id: '2'},
12
- {envelope_id: envelope_a.id, envelope_bag_id: envelope_bag.id, email: 'foo3@bar.com', timestamp: 1322000094, unique_arg: 'my unique arg', event: 'bounce', target_id: '3'},
13
- ]
14
- MailCannon::SendgridEvent.insert_bulk(test_hash)
15
- end
16
-
17
- before(:each) do
18
- envelope_bag.save
19
- envelope_a.save
20
- insert_sample_events
21
- end
22
-
23
- describe "stats" do
24
- it "has expected keys" do
25
- envelope_bag.reduce_statistics
26
- expect(envelope_bag.stats).to have_key("posted")
27
- expect(envelope_bag.stats).to have_key("processed")
28
- expect(envelope_bag.stats).to have_key("delivered")
29
- expect(envelope_bag.stats).to have_key("open")
30
- expect(envelope_bag.stats).to have_key("click")
31
- expect(envelope_bag.stats).to have_key("deferred")
32
- expect(envelope_bag.stats).to have_key("spam_report")
33
- expect(envelope_bag.stats).to have_key("spam")
34
- expect(envelope_bag.stats).to have_key("unsubscribe")
35
- expect(envelope_bag.stats).to have_key("drop")
36
- expect(envelope_bag.stats).to have_key("soft_bounce")
37
- expect(envelope_bag.stats).to have_key("hard_bounce")
38
- expect(envelope_bag.stats).to have_key("unknown")
39
- end
40
- end
41
- end
@@ -1,17 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe MailCannon::EnvelopeBagReduceJob do
4
- describe "perform" do
5
-
6
- let(:bag_1) { create(:filled_envelope_bag) }
7
- let(:bag_2) { create(:filled_envelope_bag) }
8
-
9
- it "calls the reduce trigger for each envelope" do
10
- Sidekiq::Testing.inline! do
11
- MailCannon::EnvelopeBag.should_receive(:reduce_statistics_for_envelope_bag).with(bag_2.id.to_s).and_return(nil)
12
- MailCannon::EnvelopeBag.should_receive(:reduce_statistics_for_envelope_bag).with(bag_1.id.to_s).and_return(nil)
13
- MailCannon::EnvelopeBagReduceJob.perform_async([bag_2.id, bag_1.id])
14
- end
15
- end
16
- end
17
- end