This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
test_source_merge.py
242 lines (222 loc) · 7.79 KB
/
test_source_merge.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from unittest import main
from pandas import DataFrame
from lib.data_source import DataSource
from .profiled_test_case import ProfiledTestCase
# Synthetic data used for testing
TEST_AUX_DATA = DataFrame.from_records(
[
# Country with no subregions
{
"key": "AA",
"country_code": "AA",
"subregion1_code": None,
"subregion2_code": None,
"match_string": None,
},
# Country with one level-1 subregion
{
"key": "AB",
"country_code": "AB",
"subregion1_code": None,
"subregion2_code": None,
"match_string": None,
},
{
"key": "AB_1",
"country_code": "AB",
"subregion1_code": "1",
"subregion2_code": None,
"match_string": None,
},
# Country with five level-1 subregions
{
"key": "AC",
"country_code": "AC",
"subregion1_code": None,
"subregion2_code": None,
"match_string": None,
},
{
"key": "AC_1",
"country_code": "AC",
"subregion1_code": "1",
"subregion2_code": None,
"match_string": None,
},
{
"key": "AC_2",
"country_code": "AC",
"subregion1_code": "2",
"subregion2_code": None,
"match_string": None,
},
{
"key": "AC_3",
"country_code": "AC",
"subregion1_code": "3",
"subregion2_code": None,
"match_string": None,
},
{
"key": "AC_4",
"country_code": "AC",
"subregion1_code": "4",
"subregion2_code": None,
"match_string": None,
},
{
"key": "AC_5",
"country_code": "AC",
"subregion1_code": "5",
"subregion2_code": None,
"match_string": None,
},
# Country with one level-1 subregion and one level-2 subregion
{
"key": "AD",
"country_code": "AD",
"subregion1_code": None,
"subregion2_code": None,
"match_string": None,
},
{
"key": "AD_1",
"country_code": "AD",
"subregion1_code": "1",
"subregion2_code": None,
"match_string": None,
},
{
"key": "AD_1_1",
"country_code": "AD",
"subregion1_code": "1",
"subregion2_code": "1",
"match_string": None,
},
# Country with one level-1 subregion and five level-2 subregions
{
"key": "AE",
"country_code": "AE",
"subregion1_code": None,
"subregion2_code": None,
"match_string": None,
},
{
"key": "AE_1",
"country_code": "AE",
"subregion1_code": "1",
"subregion2_code": None,
"match_string": None,
},
{
"key": "AE_1_1",
"country_code": "AE",
"subregion1_code": "1",
"subregion2_code": "1",
"match_string": None,
},
{
"key": "AE_1_2",
"country_code": "AE",
"subregion1_code": "1",
"subregion2_code": "2",
"match_string": None,
},
{
"key": "AE_1_3",
"country_code": "AE",
"subregion1_code": "1",
"subregion2_code": "3",
"match_string": None,
},
{
"key": "AE_1_4",
"country_code": "AE",
"subregion1_code": "1",
"subregion2_code": "4",
"match_string": None,
},
{
"key": "AE_1_5",
"country_code": "AE",
"subregion1_code": "1",
"subregion2_code": "5",
"match_string": None,
},
]
)
TEST_METADATA_KEYS = set(TEST_AUX_DATA["key"].values)
class TestSourceMerge(ProfiledTestCase):
def test_merge_no_match(self):
aux = TEST_AUX_DATA.copy()
data_source = DataSource()
record = {"country_code": "__"}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertTrue(key is None)
def test_merge_by_key(self):
aux = TEST_AUX_DATA.copy()
data_source = DataSource()
record = {"key": "AE_1_2"}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, record["key"])
def test_merge_zero_subregions(self):
aux = TEST_AUX_DATA.copy()
data_source = DataSource()
record = {"country_code": "AA"}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, "AA")
def test_merge_one_subregion(self):
aux = TEST_AUX_DATA.copy()
data_source = DataSource()
record = {"country_code": "AB"}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertTrue(key is None)
record = {"country_code": "AB", "subregion1_code": None}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, "AB")
record = {"country_code": "AB", "subregion1_code": "1"}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, "AB_1")
def test_merge_null_vs_empty(self):
aux = TEST_AUX_DATA.copy()
data_source = DataSource()
# Only one record has null region1_code
record = {"country_code": "AD", "subregion1_code": None}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, "AD")
# Empty means "do not compare" rather than "filter non-null"
record = {"country_code": "AD", "subregion1_code": ""}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, None)
# There are multiple records that fit this merge, so it's ambiguous
record = {"country_code": "AD", "subregion1_code": "1"}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, None)
# Match fails because subregion1_code is not null
record = {"country_code": "AD", "subregion1_code": None, "subregion2_code": "1"}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, None)
# Match is exact so the merge is unambiguous
record = {"country_code": "AD", "subregion1_code": "1", "subregion2_code": "1"}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, "AD_1_1")
# Even though we don't have subregion1_code, there's only one record that matches
record = {"country_code": "AD", "subregion1_code": "", "subregion2_code": "1"}
key = data_source.merge(record, {"metadata": aux}, keys=TEST_METADATA_KEYS)
self.assertEqual(key, "AD_1_1")
if __name__ == "__main__":
sys.exit(main())