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

23 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-20 18:03 +0000

1import pytest 

2from sqlalchemy.sql import func 

3 

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 

8from tests.test_fixtures import db, generate_user, testconfig # noqa 

9 

10 

11@pytest.fixture(autouse=True) 

12def _(testconfig): 

13 pass 

14 

15 

16def test_page_token_time_python(): 

17 now_ = now() 

18 assert now_ == dt_from_page_token(dt_to_page_token(now_)) 

19 

20 

21def test_page_token_time_db(db): 

22 generate_user() 

23 

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() 

28 

29 with session_scope() as session: 

30 # pull it back into python 

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

32 

33 # roundtrip page token 

34 roundtrip = dt_from_page_token(dt_to_page_token(joined)) 

35 

36 # make sure euqality is still equality 

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

38 assert user.joined == roundtrip