|
1 |
| -import pluscodes |
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
2 | 4 | import unittest
|
| 5 | +import openlocationcode as olc |
| 6 | + |
| 7 | +class TestValidity(unittest.TestCase): |
| 8 | + def setUp(self): |
| 9 | + self.testdata = [] |
| 10 | + headermap = {0: 'code', 1: 'isValid', 2: 'isShort', 3: 'isFull'} |
| 11 | + tests_fn = 'test_data/validityTests.csv' |
| 12 | + with open(tests_fn, "r") as fin: |
| 13 | + for line in fin: |
| 14 | + if line.startswith('#'): |
| 15 | + continue |
| 16 | + td = line.strip().split(',') |
| 17 | + assert len(td) == len(headermap), 'Wrong format of testing data: {0}'.format(line) |
| 18 | + # all values should be booleans except the code |
| 19 | + for i in range(1, len(headermap)): |
| 20 | + td[i] = (td[i] == 'true') |
| 21 | + self.testdata.append({headermap[i]: v for i, v in enumerate(td)}) |
| 22 | + |
| 23 | + def test_validcodes(self): |
| 24 | + for td in self.testdata: |
| 25 | + self.assertEqual(olc.isValid(td['code']), td['isValid'], td) |
| 26 | + |
| 27 | + def test_fullcodes(self): |
| 28 | + for td in self.testdata: |
| 29 | + self.assertEqual(olc.isFull(td['code']), td['isFull'], td) |
| 30 | + |
| 31 | + def test_shortcodes(self): |
| 32 | + for td in self.testdata: |
| 33 | + self.assertEqual(olc.isShort(td['code']), td['isShort'], td) |
3 | 34 |
|
4 |
| -class TestValalidity(unittest.TestCase): |
5 |
| - def test_validfullcodes(self): |
6 |
| - self.assertTrue(pluscodes.isValid('8FWC2345+G6') and pluscodes.isFull('8FWC2345+G6')) |
7 |
| - self.assertTrue(pluscodes.isValid('8FWC2345+G6G') and pluscodes.isFull('8FWC2345+G6G')) |
8 |
| - self.assertTrue(pluscodes.isValid('8fwc2345+') and pluscodes.isFull('8fwc2345+')) |
9 |
| - self.assertTrue(pluscodes.isValid('8FWCX400+') and pluscodes.isFull('8FWCX400+')) |
10 |
| - self.assertFalse(pluscodes.isShort('8FWCX400+')) |
11 |
| - self.assertFalse(pluscodes.isShort('8fwc2345+')) |
12 |
| - |
13 |
| - def test_validshortcodes(self): |
14 |
| - self.assertTrue(pluscodes.isValid('WC2345+G6g') and pluscodes.isShort('WC2345+G6g')) |
15 |
| - self.assertTrue(pluscodes.isValid('2345+G6') and pluscodes.isShort('2345+G6')) |
16 |
| - self.assertTrue(pluscodes.isValid('45+G6') and pluscodes.isShort('45+G6')) |
17 |
| - self.assertTrue(pluscodes.isValid('+G6') and pluscodes.isShort('+G6')) |
18 |
| - self.assertFalse(pluscodes.isFull('45+G6')) |
19 |
| - self.assertFalse(pluscodes.isFull('WC2345+G6g')) |
20 |
| - |
21 |
| - def test_invalidcodes(self): |
22 |
| - self.assertFalse(pluscodes.isValid('G+')) |
23 |
| - self.assertFalse(pluscodes.isValid('+')) |
24 |
| - self.assertFalse(pluscodes.isValid('8FWC2345+G')) |
25 |
| - self.assertFalse(pluscodes.isValid('8FWC2_45+G6')) |
26 |
| - self.assertFalse(pluscodes.isValid('8FWC2η45+G6')) |
27 |
| - self.assertFalse(pluscodes.isValid('8FWC2345+G6+')) |
28 |
| - self.assertFalse(pluscodes.isValid('8FWC2300+G6')) |
29 |
| - self.assertFalse(pluscodes.isValid('WC2300+G6g')) |
30 |
| - self.assertFalse(pluscodes.isValid('WC2345+G')) |
31 | 35 |
|
32 | 36 | class TestShorten(unittest.TestCase):
|
| 37 | + def setUp(self): |
| 38 | + self.testdata = [] |
| 39 | + headermap = {0: 'fullcode', 1: 'lat', 2: 'lng', 3: 'shortcode'} |
| 40 | + tests_fn = 'test_data/shortCodeTests.csv' |
| 41 | + with open(tests_fn, "r") as fin: |
| 42 | + for line in fin: |
| 43 | + if line.startswith('#'): |
| 44 | + continue |
| 45 | + td = line.strip().split(',') |
| 46 | + assert len(td) == len(headermap), 'Wrong format of testing data: {0}'.format(line) |
| 47 | + td[1] = float(td[1]) |
| 48 | + td[2] = float(td[2]) |
| 49 | + self.testdata.append({headermap[i]: v for i, v in enumerate(td)}) |
| 50 | + |
33 | 51 | def test_full2short(self):
|
34 |
| - self.assertEqual('+2VX', pluscodes.shorten('9C3W9QCJ+2VX',51.3701125,-1.217765625)) |
35 |
| - self.assertEqual('CJ+2VX', pluscodes.shorten('9C3W9QCJ+2VX',51.3708675,-1.217765625)) |
36 |
| - self.assertEqual('CJ+2VX', pluscodes.shorten('9C3W9QCJ+2VX',51.3701125,-1.217010625)) |
37 |
| - self.assertEqual('9QCJ+2VX', pluscodes.shorten('9C3W9QCJ+2VX',51.3852125,-1.217765625)) |
38 |
| - |
39 |
| -class TestEncode(unittest.TestCase): |
40 |
| - def test_encode(self): |
41 |
| - self.assertEqual('7FG49Q00+', pluscodes.encode(20.375,2.775,6)) |
42 |
| - self.assertEqual('7FG49QCJ+2V', pluscodes.encode(20.3700625,2.7821875)) |
43 |
| - self.assertEqual('7FG49QCJ+2VX', pluscodes.encode(20.3701125,2.782234375,11)) |
44 |
| - self.assertEqual('7FG49QCJ+2VXGJ', pluscodes.encode(20.3701135,2.78223535156,13)) |
45 |
| - self.assertEqual('8FVC2222+22', pluscodes.encode(47.0,8.0)) |
46 |
| - self.assertEqual('4VCPPQGP+Q9', pluscodes.encode(-41.2730625,174.7859375)) |
47 |
| - self.assertEqual('62G20000+', pluscodes.encode(0.5,-179.5,4)) |
48 |
| - self.assertEqual('22220000+', pluscodes.encode(-90,-180,4)) |
49 |
| - self.assertEqual('CFX30000+', pluscodes.encode(90,1,4)) |
50 |
| - self.assertEqual('62H20000+', pluscodes.encode(1,180,4)) |
| 52 | + for td in self.testdata: |
| 53 | + self.assertEqual(td['shortcode'], olc.shorten(td['fullcode'], td['lat'], td['lng']), td) |
| 54 | + |
| 55 | + |
| 56 | +class TestEncoding(unittest.TestCase): |
| 57 | + def setUp(self): |
| 58 | + self.testdata = [] |
| 59 | + headermap = {0: 'code', 1: 'lat', 2: 'lng', 3: 'latLo', 4: 'lngLo', 5: 'latHi', 6: 'longHi'} |
| 60 | + tests_fn = 'test_data/encodingTests.csv' |
| 61 | + with open(tests_fn, "r") as fin: |
| 62 | + for line in fin: |
| 63 | + if line.startswith('#'): |
| 64 | + continue |
| 65 | + td = line.strip().split(',') |
| 66 | + assert len(td) == len(headermap), 'Wrong format of testing data: {0}'.format(line) |
| 67 | + # all values should be numbers except the code |
| 68 | + for i in range(1, len(headermap)): |
| 69 | + td[i] = float(td[i]) |
| 70 | + self.testdata.append({headermap[i]: v for i, v in enumerate(td)}) |
| 71 | + |
| 72 | + def test_encoding(self): |
| 73 | + for td in self.testdata: |
| 74 | + codelength = len(td['code']) - 1 |
| 75 | + if '0' in td['code']: |
| 76 | + codelength = td['code'].index("0") |
| 77 | + self.assertEqual(td['code'], olc.encode(td['lat'], td['lng'], codelength), td) |
| 78 | + |
| 79 | + def test_decoding(self): |
| 80 | + precision = 10 |
| 81 | + for td in self.testdata: |
| 82 | + decoded = olc.decode(td['code']) |
| 83 | + self.assertEqual(round(decoded.latitudeLo, precision), round(td['latLo'], precision), td) |
| 84 | + self.assertEqual(round(decoded.longitudeLo, precision), round(td['lngLo'], precision), td) |
| 85 | + self.assertEqual(round(decoded.latitudeHi, precision), round(td['latHi'], precision), td) |
| 86 | + self.assertEqual(round(decoded.longitudeHi, precision), round(td['longHi'], precision), td) |
| 87 | + |
51 | 88 |
|
52 | 89 | if __name__ == '__main__':
|
53 | 90 | unittest.main()
|
0 commit comments