aboutsummaryrefslogtreecommitdiff
path: root/cpp/test/address_input_helper_test.cc
blob: 4014c914edc798d0a46348b4065a06e17d170aaa (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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright (C) 2013 Google Inc.
//
// 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.

#include <libaddressinput/address_input_helper.h>

#include <libaddressinput/address_data.h>
#include <libaddressinput/callback.h>
#include <libaddressinput/null_storage.h>
#include <libaddressinput/preload_supplier.h>
#include <libaddressinput/util/basictypes.h>
#include <libaddressinput/util/scoped_ptr.h>

#include <string>
#include <utility>

#include <gtest/gtest.h>

#include "mock_source.h"
#include "testdata_source.h"

namespace {

using i18n::addressinput::AddressData;
using i18n::addressinput::AddressInputHelper;
using i18n::addressinput::BuildCallback;
using i18n::addressinput::Callback;
using i18n::addressinput::MockSource;
using i18n::addressinput::NullStorage;
using i18n::addressinput::PreloadSupplier;
using i18n::addressinput::scoped_ptr;
using i18n::addressinput::TestdataSource;

class AddressInputHelperTest : public testing::Test {
 protected:
  AddressInputHelperTest()
      // Our PreloadSupplier loads all data for a country at a time.
      : supplier_(new TestdataSource(true), new NullStorage),
        address_input_helper_(&supplier_),
        loaded_(BuildCallback(this, &AddressInputHelperTest::Loaded)) {}

  // Helper method to test FillAddress that ensures the PreloadSupplier has the
  // necessary data preloaded.
  void FillAddress(AddressData* address) {
    const std::string& region_code = address->region_code;
    if (!region_code.empty()) {
      supplier_.LoadRules(region_code, *loaded_);
    }
    address_input_helper_.FillAddress(address);
  }

 private:
  // Used to preload data that we need.
  void Loaded(bool success, const std::string&, int) { ASSERT_TRUE(success); }

  PreloadSupplier supplier_;
  const AddressInputHelper address_input_helper_;
  const scoped_ptr<const PreloadSupplier::Callback> loaded_;
  DISALLOW_COPY_AND_ASSIGN(AddressInputHelperTest);
};

TEST_F(AddressInputHelperTest, AddressWithMissingPostalCode) {
  AddressData address;
  address.region_code = "CX";
  address.administrative_area = "WA";

  // There is only one postal code for Christmas Island
  AddressData expected = address;
  expected.postal_code = "6798";
  FillAddress(&address);
  EXPECT_EQ(expected, address);
}

TEST_F(AddressInputHelperTest, AddressWithPostalCodeMatchingAdmin) {
  AddressData address;
  address.region_code = "US";
  address.postal_code = "58098";
  // Other data should be left alone.
  address.address_line.push_back("10 High St");

  // North Dakota has post codes starting with 58.
  AddressData expected = address;
  expected.administrative_area = "ND";
  FillAddress(&address);
  EXPECT_EQ(expected, address);

  address.administrative_area = "CA";  // Override the admin area.
  // Now, since the admin area was already filled in, we don't fix it, even
  // though it was correct.
  expected.administrative_area = "CA";
  FillAddress(&address);
  EXPECT_EQ(expected, address);
}

TEST_F(AddressInputHelperTest, AddressWithPostalCodeMatchingLowerLevel) {
  AddressData address;
  address.region_code = "TW";
  address.postal_code = "53012";

  /* This matches 二水鄉 - Ershuei Township. */
  AddressData expected = address;
  /* This locality is in 彰化縣 - Changhua County. */
  expected.administrative_area = "\xE5\xBD\xB0\xE5\x8C\x96\xE7\xB8\xA3";
  expected.locality = "\xE4\xBA\x8C\xE6\xB0\xB4\xE9\x84\x89";
  FillAddress(&address);
  EXPECT_EQ(expected, address);

  // Override the admin area.
  address.administrative_area = "Already filled in";
  expected.administrative_area = "Already filled in";
  address.locality = "";
  // However, the locality will still be filled in.
  FillAddress(&address);
  EXPECT_EQ(expected, address);
}

TEST_F(AddressInputHelperTest, AddressWithPostalCodeMatchingLowerLevelLatin) {
  AddressData address;
  address.region_code = "TW";
  address.postal_code = "53012";
  address.language_code = "zh-Latn";

  /* This matches 二水鄉 - Ershuei Township. */
  AddressData expected = address;
  /* This locality is in 彰化縣 - Changhua County. */
  expected.locality = "Ershuei Township";
  expected.administrative_area = "Changhua County";
  FillAddress(&address);
  EXPECT_EQ(expected, address);

  // Override the admin area.
  address.administrative_area = "Already filled in";
  expected.administrative_area = "Already filled in";
  address.locality = "";
  // However, the locality will still be filled in.
  FillAddress(&address);
  EXPECT_EQ(expected, address);
}

TEST_F(AddressInputHelperTest, AddressWithPostalCodeMatchingDependentLocality) {
  AddressData address;
  address.region_code = "KR";
  // This matches Danwon-gu district.
  address.postal_code = "425-111";

  AddressData expected = address;
  /* The province is Gyeonggi - 경기도. */
  expected.administrative_area = "\xEA\xB2\xBD\xEA\xB8\xB0\xEB\x8F\x84";
  /* The city is Ansan-si - 안산시. */
  expected.locality = "\xEC\x95\x88\xEC\x82\xB0\xEC\x8B\x9C";
  /* The district is Danwon-gu - 단원구 */
  expected.dependent_locality = "\xEB\x8B\xA8\xEC\x9B\x90\xEA\xB5\xAC";

  FillAddress(&address);
  EXPECT_EQ(expected, address);

  AddressData address_ko_latn;
  address_ko_latn.region_code = "KR";
  address_ko_latn.postal_code = "425-111";
  address_ko_latn.language_code = "ko-latn";

  expected = address_ko_latn;
  /* The province is Gyeonggi - 경기도. */
  expected.administrative_area = "Gyeonggi";
  /* The city is Ansan-si - 안산시. */
  expected.locality = "Ansan-si";
  /* The district is Danwon-gu - 단원구 */
  expected.dependent_locality = "Danwon-gu";

  FillAddress(&address_ko_latn);
  EXPECT_EQ(expected, address_ko_latn);
}

TEST_F(AddressInputHelperTest, AddressWithPostalCodeMatchingMultipleValues) {
  AddressData address;
  address.region_code = "KR";
  // This matches Wando-gun and Ganjin-gun, both in Jeonnam province.
  address.postal_code = "527-111";

  AddressData expected = address;
  /* The province, Jeonnam - 전라남도 - is known, but we have several locality
   * matches so none of them are populated. */
  expected.administrative_area =
      "\xEC\xA0\x84\xEB\x9D\xBC\xEB\x82\xA8\xEB\x8F\x84";
  FillAddress(&address);
  EXPECT_EQ(expected, address);
}

TEST_F(AddressInputHelperTest, AddressWithInvalidPostalCode) {
  AddressData address;
  address.postal_code = "970";
  address.region_code = "US";

  // We don't expect any changes, since the postal code couldn't be determined
  // as valid.
  AddressData expected = address;
  FillAddress(&address);
  EXPECT_EQ(expected, address);
}

TEST_F(AddressInputHelperTest, AddressWithNoPostalCodeValidation) {
  AddressData address;
  address.postal_code = "123";
  address.region_code = "GA";

  // We don't expect any changes, since the postal code couldn't be determined
  // as valid - we have no information about postal codes in Gabon (or even that
  // they are in use).
  AddressData expected = address;
  FillAddress(&address);
  EXPECT_EQ(expected, address);
}

TEST_F(AddressInputHelperTest, AddressWithInvalidOrMissingRegionCode) {
  AddressData address;
  address.postal_code = "XXX";
  address.administrative_area = "YYY";

  // We don't expect any changes, since there was no region code.
  AddressData expected = address;
  FillAddress(&address);
  EXPECT_EQ(expected, address);

  address.region_code = "XXXX";
  expected.region_code = "XXXX";
  // Again, nothing should change.
  FillAddress(&address);
  EXPECT_EQ(expected, address);
}

class AddressInputHelperMockDataTest : public testing::Test {
 protected:
  AddressInputHelperMockDataTest()
      : source_(new MockSource),
        // Our PreloadSupplier loads all data for a country at a time.
        supplier_(source_, new NullStorage),
        address_input_helper_(&supplier_),
        loaded_(BuildCallback(this, &AddressInputHelperMockDataTest::Loaded)) {}

  // Helper method to test FillAddress that ensures the PreloadSupplier has the
  // necessary data preloaded.
  void FillAddress(AddressData* address) {
    const std::string& region_code = address->region_code;
    if (!region_code.empty()) {
      supplier_.LoadRules(region_code, *loaded_);
    }
    address_input_helper_.FillAddress(address);
  }

  MockSource* const source_;

 private:
  // Our mock source we assume will always succeed.
  void Loaded(bool success, const std::string&, int) { ASSERT_TRUE(success); }

  PreloadSupplier supplier_;
  const AddressInputHelper address_input_helper_;
  const scoped_ptr<const PreloadSupplier::Callback> loaded_;
  DISALLOW_COPY_AND_ASSIGN(AddressInputHelperMockDataTest);
};

TEST_F(AddressInputHelperMockDataTest,
       PostalCodeSharedAcrossDifferentHierarchies) {
  // Note that this data is in the format of data that would be returned from
  // the aggregate server.
  source_->data_.insert(std::make_pair(
      // We use KR since we need a country where we format all fields down to
      // dependent locality, or the hierarchy won't be loaded.
      "data/KR",
      "{\"data/KR\": "
      // The top-level ZIP expression must be present for sub-key matches to be
      // evaluated.
      "{\"id\":\"data/KR\", \"sub_keys\":\"A~B\", \"zip\":\"\\\\d{5}\"}, "
      "\"data/KR/A\": "
      "{\"id\":\"data/KR/A\", \"sub_keys\":\"A1\"}, "
      "\"data/KR/A/A1\": "
      "{\"id\":\"data/KR/A/A1\", \"zip\":\"1\"}, "
      "\"data/KR/B\": "
      "{\"id\":\"data/KR/B\", \"sub_keys\":\"B1\"}, "
      "\"data/KR/B/B1\": "
      "{\"id\":\"data/KR/B/B1\", \"zip\":\"12\"}}"));

  AddressData address;
  address.region_code = "KR";
  address.postal_code = "12345";
  address.administrative_area = "";

  AddressData expected = address;
  FillAddress(&address);
  // Nothing should have changed, since the ZIP code matches both of the cities,
  // and they aren't even in the same state.
  EXPECT_EQ(expected, address);
}

TEST_F(AddressInputHelperMockDataTest,
       PostalCodeSharedAcrossDifferentHierarchiesSameState) {
  // Create data where one state matches the ZIP code, but the other doesn't:
  // within the state which does, multiple cities and sub-cities match. The only
  // thing we can be certain of is therefore the state.
  source_->data_.insert(std::make_pair(
      // We use KR since we need a country where we format all fields down to
      // dependent locality, or the hierarchy won't be loaded.
      "data/KR",
      "{\"data/KR\": "
      // The top-level ZIP expression must be present for sub-key matches to be
      // evaluated.
      "{\"id\":\"data/KR\", \"sub_keys\":\"A~B\", \"zip\":\"\\\\d{5}\"}, "
      "\"data/KR/A\": "
      "{\"id\":\"data/KR/A\", \"sub_keys\":\"A1~A2\"}, "
      "\"data/KR/A/A1\": "
      "{\"id\":\"data/KR/A/A1\", \"sub_keys\":\"A1a\", \"zip\":\"1\"}, "
      // This key matches the ZIP code.
      "\"data/KR/A/A1/A1a\": "
      "{\"id\":\"data/KR/A/A1/A1a\", \"zip\":\"12\"}, "
      "\"data/KR/A/A2\": "
      "{\"id\":\"data/KR/A/A2\", \"sub_keys\":\"A2a\", \"zip\":\"1\"}, "
      // This key, also in state A, but in city A2, matches the ZIP code.
      "\"data/KR/A/A2/A2a\": "
      "{\"id\":\"data/KR/A/A2/A2a\", \"zip\":\"123\"}, "
      // This key, in state B, does not match the ZIP code.
      "\"data/KR/B\": "
      "{\"id\":\"data/KR/B\", \"zip\":\"2\"}}"));

  AddressData address;
  address.region_code = "KR";
  address.postal_code = "12345";
  address.administrative_area = "";

  AddressData expected = address;
  expected.administrative_area = "A";
  FillAddress(&address);
  // The ZIP code matches multiple city districts and cities; but only one
  // state, so we fill this in.
  EXPECT_EQ(expected, address);
}

}  // namespace