Coverage for app/backend/src/tests/test_utils.py: 100%

41 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-19 15:47 +0000

1from datetime import UTC, datetime, timedelta, timezone 

2 

3from google.protobuf.timestamp_pb2 import Timestamp 

4from sqlalchemy import select, update 

5from sqlalchemy.sql import func 

6 

7from couchers.db import session_scope 

8from couchers.models import User 

9from couchers.utils import dt_from_page_token, dt_to_page_token, http_date, now, to_timezone, wrap_coordinate 

10from tests.fixtures.db import generate_user 

11from tests.fixtures.timewarp import FrozenTimewarp 

12 

13 

14def test_page_token_time_python(): 

15 now_ = now() 

16 assert now_ == dt_from_page_token(dt_to_page_token(now_)) 

17 

18 

19def test_page_token_time_db(db): 

20 user, _ = generate_user() 

21 

22 # generate a timestamp in postgres (note use of `func`) 

23 with session_scope() as session: 

24 session.execute(update(User).where(User.id == user.id).values(joined=func.now())) 

25 

26 with session_scope() as session: 

27 # pull it back into python 

28 joined = session.execute(select(User)).scalar_one().joined 

29 

30 # roundtrip page token 

31 roundtrip = dt_from_page_token(dt_to_page_token(joined)) 

32 

33 # make sure euqality is still equality 

34 user = session.execute(select(User).where(User.joined == roundtrip)).scalar_one() 

35 assert user.joined == roundtrip 

36 

37 

38def test_http_date_with_datetime(): 

39 """Test http_date with a specific datetime to verify usegmt=True is used""" 

40 dt = datetime(2024, 1, 15, 10, 30, 45, tzinfo=UTC) 

41 

42 result = http_date(dt) 

43 

44 assert result == "Mon, 15 Jan 2024 10:30:45 GMT" 

45 

46 

47def test_http_date_without_datetime(frozen_timewarp: FrozenTimewarp): 

48 """Test http_date with dt=None to verify it uses now() and usegmt=True""" 

49 frozen_timewarp.freeze_at(datetime(2024, 3, 20, 14, 25, 30, tzinfo=UTC)) 

50 

51 assert http_date() == "Wed, 20 Mar 2024 14:25:30 GMT" 

52 

53 

54def test_wrap_coordinate(): 

55 test_coords = [ 

56 ((-95, -185), (-85, 175)), 

57 ((95, -180), (85, 180)), # Weird interaction in PostGIS where lng 

58 # flips at -180 only when there is latitude overflow 

59 ((90, -180), (90, -180)), 

60 ((20, 185), (20, -175)), 

61 ((0, 0), (0, 0)), 

62 ((-1000, 0), (80, 0)), 

63 ((1000, 0), (-80, 0)), 

64 ((-180, 0), (0, 0)), 

65 ((180, 0), (0, 0)), 

66 ((-200, 0), (20, 0)), 

67 ((200, 0), (-20, 0)), 

68 ((-450, 0), (-90, 0)), 

69 ((450, 0), (90, 0)), 

70 ((-540, 0), (0, 0)), 

71 ((540, 0), (0, 0)), 

72 ((-90, 0), (-90, 0)), 

73 ((90, 0), (90, 0)), 

74 ((-1000, -1000), (80, 80)), 

75 ((1000, 1000), (-80, -80)), 

76 ((-100, -100), (-80, -100)), 

77 ((100, 100), (80, 100)), 

78 ((1000, 10), (-80, 10)), 

79 ((-100.5, 10), (-79.5, 10)), 

80 ((100.5, 10), (79.5, 10)), 

81 ((-100, 10), (-80, 10)), 

82 ((100, 10), (80, 10)), 

83 ((-180, 10), (0, 10)), 

84 ((180, 10), (0, 10)), 

85 ((20, 10), (20, 10)), 

86 ((-270, 10), (90, 10)), 

87 ((270, 10), (-90, 10)), 

88 ((-360, 10), (0, 10)), 

89 ((360, 10), (0, 10)), 

90 ((-90.1, 10), (-89.9, 10)), 

91 ((90.1, 10), (89.9, 10)), 

92 ((-90, 10), (-90, 10)), 

93 ((90, 10), (90, 10)), 

94 ((-80, -170), (-80, -170)), 

95 ((80, 170), (80, 170)), 

96 ((0, -180), (0, -180)), 

97 ((0, 180), (0, 180)), 

98 ((100, -180), (80, 180)), 

99 ((100, 180), (80, 180)), 

100 ((20, -180.1), (20, 179.9)), 

101 ((20, 180.1), (20, -179.9)), 

102 ((20, -180), (20, -180)), 

103 ((20, 180), (20, 180)), 

104 ((-90, -180), (-90, -180)), 

105 ((90, 180), (90, 180)), 

106 ((30, -190), (30, 170)), 

107 ((30, 190), (30, -170)), 

108 ((-95, -190), (-85, 170)), 

109 ((95, 190), (85, -170)), 

110 ((0, -200), (0, 160)), 

111 ((0, 200), (0, -160)), 

112 ((-100, -200), (-80, 160)), 

113 ((100, 200), (80, -160)), 

114 ((-200, -200), (20, 160)), 

115 ((200, 200), (-20, -160)), 

116 ((20, -200), (20, 160)), 

117 ((20, 200), (20, -160)), 

118 ((-90, 200), (-90, -160)), 

119 ((90, 200), (90, -160)), 

120 ((0, -270), (0, 90)), 

121 ((0, 270), (0, -90)), 

122 ((-45, -270), (-45, 90)), 

123 ((45, 270), (45, -90)), 

124 ((0, -360), (0, 0)), 

125 ((0, 360), (0, 0)), 

126 ((-90, -360), (-90, 0)), 

127 ((90, 360), (90, 0)), 

128 ((-500, -500), (-40, -140)), 

129 ((500, 500), (40, 140)), 

130 ((50, -500), (50, -140)), 

131 ((50, 500), (50, 140)), 

132 ((-500, 50), (-40, 50)), 

133 ((500, 50), (40, 50)), 

134 ((0, -540), (0, 180)), 

135 ((0, 540), (0, 180)), 

136 ((-45, -90), (-45, -90)), 

137 ((45, 90), (45, 90)), 

138 ] 

139 

140 for coords, coords_expected in test_coords: 

141 coords_wrapped = wrap_coordinate(*coords) 

142 assert coords_expected == coords_wrapped 

143 

144 

145def test_to_timezone(): 

146 utc_plus_one = timezone(offset=timedelta(hours=1)) 

147 

148 epoch_utc = to_timezone(Timestamp(seconds=0), UTC) 

149 assert epoch_utc.isoformat() == "1970-01-01T00:00:00+00:00" 

150 

151 # With Timestamp 

152 epoch_utc_plus_one = to_timezone(Timestamp(seconds=0), utc_plus_one) 

153 assert epoch_utc_plus_one.isoformat() == "1970-01-01T01:00:00+01:00" 

154 

155 # With datetime 

156 epoch_utc_plus_one = to_timezone(epoch_utc, utc_plus_one) 

157 assert epoch_utc_plus_one.isoformat() == "1970-01-01T01:00:00+01:00"