Skip to content

Commit

Permalink
Updated for v2 (#28)
Browse files Browse the repository at this point in the history
* Updated for v2

* Updated tests for v2
  • Loading branch information
jarredSafegraph authored Feb 9, 2024
1 parent 684a6a3 commit 97360c3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 3 additions & 3 deletions placekey/placekey.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
TUPLE_REGEX = '[' + ALPHABET + REPLACEMENT_CHARS + ']{3}'
WHERE_REGEX = re.compile(
'^' + '-'.join([FIRST_TUPLE_REGEX, TUPLE_REGEX, TUPLE_REGEX]) + '$')
WHAT_REGEX = re.compile('^[' + ALPHABET + ']{3,}(-[' + ALPHABET + ']{3,})?$')

WHAT_REGEX_V1 = re.compile('^[' + ALPHABET + ']{3,}(-[' + ALPHABET + ']{3,})?$')
WHAT_REGEX_V2 = re.compile('^[01][abcdefghijklmnopqrstuvwxyz234567]{9}$')

def _get_header_int():
"""
Expand Down Expand Up @@ -341,7 +341,7 @@ def placekey_format_is_valid(placekey):
return False

if what:
return _where_part_is_valid(where) and bool(WHAT_REGEX.match(what))
return _where_part_is_valid(where) and (bool(WHAT_REGEX_V1.match(what)) or bool(WHAT_REGEX_V2.match(what)))
else:
return _where_part_is_valid(where)

Expand Down
2 changes: 1 addition & 1 deletion placekey/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_lookup_placekeys(self):
self.assertListEqual(
self.pk_api.lookup_placekeys(places),
[
{'query_id': 'place_0', 'placekey': '22g@5vg-7gq-5mk'},
{'query_id': 'place_0', 'placekey': '0rsdbudq45@5vg-7gq-5mk'},
{'query_id': 'thisqueryidaloneiscustom', 'placekey': '227-223@5vg-82n-pgk'},
{'query_id': 'place_2', 'placekey': '@5vg-82n-kzz'}
]
Expand Down
6 changes: 6 additions & 0 deletions placekey/tests/test_placekey.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ def test_placekey_format_is_valid(self):
'long poi encoding with where')
self.assertTrue(pk.placekey_format_is_valid('22222222-zzzzzzzzz@5vg-7gq-tvz'),
'long address and poi encoding with where')
self.assertTrue(pk.placekey_format_is_valid('0rsdbudq45@5vg-7gt-tn5'),
'version 2 encoding with where beginning with 0')
self.assertTrue(pk.placekey_format_is_valid('1rsdbudq45@5vg-7gt-tn5'),
'version 2 encoding beginning with 1')

self.assertFalse(pk.placekey_format_is_valid('@abc'), 'short where part')
self.assertFalse(pk.placekey_format_is_valid('abc-xyz'), 'short where part')
Expand All @@ -238,6 +242,8 @@ def test_placekey_format_is_valid(self):
'short address encoding')
self.assertFalse(pk.placekey_format_is_valid('222-zz@abc-234-xyz'),
'short poi encoding')
self.assertFalse(pk.placekey_format_is_valid('7rsdbudq45@5vg-7gt-tn5'),
'version 2 encoding illegal leading character(7)')

self.assertFalse(pk.placekey_format_is_valid('@abc-234-xyz'), 'invalid where value')
self.assertFalse(pk.placekey_format_is_valid('@@5vg-7gq-tvz'), 'multiple @ in placekey')
Expand Down

0 comments on commit 97360c3

Please sign in to comment.