-
Notifications
You must be signed in to change notification settings - Fork 482
/
contact_rollups_processed_test.rb
163 lines (138 loc) · 5.64 KB
/
contact_rollups_processed_test.rb
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require 'test_helper'
class ContactRollupsProcessedTest < ActiveSupport::TestCase
test 'import_from_raw_table creates one row per email' do
assert 0, ContactRollupsRaw.count
assert 0, ContactRollupsProcessed.count
# Create 6 records for 3 unique emails
create :contact_rollups_raw, email: 'email1@example.domain'
create_list :contact_rollups_raw, 2, email: 'email2@example.domain'
create_list :contact_rollups_raw, 3, email: 'email3@example.domain'
ContactRollupsProcessed.import_from_raw_table
# Note: having unique email addresses is already guaranteed by table constraint
assert_equal 3, ContactRollupsProcessed.count
end
test 'import_from_raw_table inserts records by batch' do
unique_email_count = 15
batch_sizes = [1, 5, 7, 11, 20]
ContactRollupsRaw.delete_all
create_list :contact_rollups_raw, unique_email_count
batch_sizes.each do |batch_size|
ContactRollupsProcessed.delete_all
ContactRollupsProcessed.import_from_raw_table(batch_size)
assert_equal unique_email_count, ContactRollupsProcessed.count, "Failed with batch size of #{batch_size}"
end
end
test 'import_from_raw_table combines data from multiple records' do
assert 0, ContactRollupsRaw.count
assert 0, ContactRollupsProcessed.count
base_time = Time.now.utc
email = 'email@example.domain'
create :contact_rollups_raw, email: email,
data: nil, data_updated_at: base_time - 1.day
create :contact_rollups_raw, email: email,
sources: "#{CDO.dashboard_db_name}.email_preferences", data: {opt_in: 1}, data_updated_at: base_time
ContactRollupsProcessed.import_from_raw_table
assert_equal 1, ContactRollupsProcessed.count
data = ContactRollupsProcessed.first.data
assert_equal 1, data['opt_in']
assert_equal base_time.to_i, Time.parse(data['updated_at']).to_i
end
test 'import_from_raw_table calls all extraction functions' do
assert 0, ContactRollupsRaw.count
create :contact_rollups_raw
# Each extraction function will be called once per unique email address
ContactRollupsProcessed.expects(:extract_field).once
ContactRollupsProcessed.expects(:extract_updated_at).once
ContactRollupsProcessed.import_from_raw_table
end
test 'extract_field' do
table = "#{CDO.dashboard_db_name}.email_preferences"
field = 'opt_in'
test_cases = [
{input: [{}, nil, nil], expected_output: nil},
{input: [{table => {}}, table, field], expected_output: nil},
{input: [{"#{CDO.dashboard_db_name}.another_table" => {opt_in: 1}}, table, field], expected_output: nil},
{input: [{table => {'opt_in' => 0}}, table, field], expected_output: {opt_in: 0}},
{input: [{table => {'opt_in' => 1}}, table, field], expected_output: {opt_in: 1}},
{input: [{table => {'opt_in' => nil}}, table, field], expected_output: {opt_in: nil}}
]
test_cases.each_with_index do |test, index|
output = ContactRollupsProcessed.extract_field(*test[:input])
assert_equal test[:expected_output], output, "Test index #{index} failed"
end
end
test 'extract_updated_at' do
base_time = Time.now.utc - 7.days
tests = [
{
input: {'table1' => {'data_updated_at' => base_time}},
expected_output: {updated_at: base_time}
},
{
input: {
'table1' => {'data_updated_at' => base_time - 1.day},
'table2' => {'data_updated_at' => base_time + 1.day},
'table3' => {'data_updated_at' => base_time},
},
expected_output: {updated_at: base_time + 1.day}
}
]
# Test valid inputs
tests.each_with_index do |test, index|
output = ContactRollupsProcessed.extract_updated_at(test[:input])
assert_equal test[:expected_output], output, "Test index #{index} failed"
end
# Test invalid input
assert_raise StandardError do
ContactRollupsProcessed.extract_updated_at({'table' => {}})
end
end
test 'parse_contact_data parses valid input' do
time_str = '2020-03-11 15:01:26'
time_parsed = Time.find_zone('UTC').parse(time_str)
tests = [
{
input: format('[{"sources": "table1", "data": null, "data_updated_at": "%s"}]', time_str),
expected_output: {'table1' => {'data_updated_at' => time_parsed}}
},
{
input: format('[{"sources": "table1", "data": {}, "data_updated_at": "%s"}]', time_str),
expected_output: {'table1' => {'data_updated_at' => time_parsed}}
},
{
input: format('[{"sources": "table1", "data": {"opt_in": 1}, "data_updated_at": "%s"}]', time_str),
expected_output: {'table1' => {'opt_in' => 1, 'data_updated_at' => time_parsed}}
},
{
input: format('['\
'{"sources": "table1", "data": {"opt_in": 1}, "data_updated_at": "%s"},'\
'{"sources": "table2", "data": {"state": "WA"}, "data_updated_at": "%s"}]',
time_str, time_str
),
expected_output: {
'table1' => {'opt_in' => 1, 'data_updated_at' => time_parsed},
'table2' => {'state' => 'WA', 'data_updated_at' => time_parsed}
}
}
]
tests.each_with_index do |test, index|
output = ContactRollupsProcessed.parse_contact_data test[:input]
assert_equal test[:expected_output], output, "Test index #{index} failed"
end
end
test 'parse_contact_data throws exception for invalid input' do
test_inputs = [
nil,
'',
'[{}]',
'[{"sources": "table"}]',
'[{"data_updated_at" => null}]',
'[{"data_updated_at" => "invalid date"}]'
]
test_inputs.each do |input|
assert_raises do
ContactRollupsProcessed.parse_contact_data input
end
end
end
end