Coverage for src/tests/test_utils.py: 100%
29 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-06-01 15:07 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-06-01 15:07 +0000
1import pytest
2from sqlalchemy.sql import func
4from couchers.db import session_scope
5from couchers.models import User
6from couchers.sql import couchers_select as select
7from couchers.utils import dt_from_page_token, dt_to_page_token, now, wrap_coordinate
8from tests.test_fixtures import db, generate_user, testconfig # noqa
11@pytest.fixture(autouse=True)
12def _(testconfig):
13 pass
16def test_page_token_time_python():
17 now_ = now()
18 assert now_ == dt_from_page_token(dt_to_page_token(now_))
21def test_page_token_time_db(db):
22 generate_user()
24 # generate a timestamp in postgres (note use of `func`)
25 with session_scope() as session:
26 user = session.execute(select(User)).scalar_one()
27 user.joined = func.now()
29 with session_scope() as session:
30 # pull it back into python
31 joined = session.execute(select(User)).scalar_one().joined
33 # roundtrip page token
34 roundtrip = dt_from_page_token(dt_to_page_token(joined))
36 # make sure euqality is still equality
37 user = session.execute(select(User).where(User.joined == roundtrip)).scalar_one()
38 assert user.joined == roundtrip
41def test_wrap_coordinate():
42 test_coords = [
43 ((-95, -185), (-85, 175)),
44 ((95, -180), (85, 180)), # Weird interaction in PostGIS where lng
45 # flips at -180 only when there is latitude overflow
46 ((90, -180), (90, -180)),
47 ((20, 185), (20, -175)),
48 ((0, 0), (0, 0)),
49 ((-1000, 0), (80, 0)),
50 ((1000, 0), (-80, 0)),
51 ((-180, 0), (0, 0)),
52 ((180, 0), (0, 0)),
53 ((-200, 0), (20, 0)),
54 ((200, 0), (-20, 0)),
55 ((-450, 0), (-90, 0)),
56 ((450, 0), (90, 0)),
57 ((-540, 0), (0, 0)),
58 ((540, 0), (0, 0)),
59 ((-90, 0), (-90, 0)),
60 ((90, 0), (90, 0)),
61 ((-1000, -1000), (80, 80)),
62 ((1000, 1000), (-80, -80)),
63 ((-100, -100), (-80, -100)),
64 ((100, 100), (80, 100)),
65 ((1000, 10), (-80, 10)),
66 ((-100.5, 10), (-79.5, 10)),
67 ((100.5, 10), (79.5, 10)),
68 ((-100, 10), (-80, 10)),
69 ((100, 10), (80, 10)),
70 ((-180, 10), (0, 10)),
71 ((180, 10), (0, 10)),
72 ((20, 10), (20, 10)),
73 ((-270, 10), (90, 10)),
74 ((270, 10), (-90, 10)),
75 ((-360, 10), (0, 10)),
76 ((360, 10), (0, 10)),
77 ((-90.1, 10), (-89.9, 10)),
78 ((90.1, 10), (89.9, 10)),
79 ((-90, 10), (-90, 10)),
80 ((90, 10), (90, 10)),
81 ((-80, -170), (-80, -170)),
82 ((80, 170), (80, 170)),
83 ((0, -180), (0, -180)),
84 ((0, 180), (0, 180)),
85 ((100, -180), (80, 180)),
86 ((100, 180), (80, 180)),
87 ((20, -180.1), (20, 179.9)),
88 ((20, 180.1), (20, -179.9)),
89 ((20, -180), (20, -180)),
90 ((20, 180), (20, 180)),
91 ((-90, -180), (-90, -180)),
92 ((90, 180), (90, 180)),
93 ((30, -190), (30, 170)),
94 ((30, 190), (30, -170)),
95 ((-95, -190), (-85, 170)),
96 ((95, 190), (85, -170)),
97 ((0, -200), (0, 160)),
98 ((0, 200), (0, -160)),
99 ((-100, -200), (-80, 160)),
100 ((100, 200), (80, -160)),
101 ((-200, -200), (20, 160)),
102 ((200, 200), (-20, -160)),
103 ((20, -200), (20, 160)),
104 ((20, 200), (20, -160)),
105 ((-90, 200), (-90, -160)),
106 ((90, 200), (90, -160)),
107 ((0, -270), (0, 90)),
108 ((0, 270), (0, -90)),
109 ((-45, -270), (-45, 90)),
110 ((45, 270), (45, -90)),
111 ((0, -360), (0, 0)),
112 ((0, 360), (0, 0)),
113 ((-90, -360), (-90, 0)),
114 ((90, 360), (90, 0)),
115 ((-500, -500), (-40, -140)),
116 ((500, 500), (40, 140)),
117 ((50, -500), (50, -140)),
118 ((50, 500), (50, 140)),
119 ((-500, 50), (-40, 50)),
120 ((500, 50), (40, 50)),
121 ((0, -540), (0, 180)),
122 ((0, 540), (0, 180)),
123 ((-45, -90), (-45, -90)),
124 ((45, 90), (45, 90)),
125 ]
127 with session_scope() as session:
128 for coords, coords_expected in test_coords:
129 coords_wrapped = wrap_coordinate(*coords)
130 assert coords_expected == coords_wrapped