Coverage for src/tests/test_models.py: 95%

22 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-20 21:46 +0000

1from datetime import date, datetime 

2from unittest.mock import patch 

3 

4from couchers.models import User 

5 

6 

7class FakeDate(date): 

8 """A fake replacement for date that can be mocked for testing.""" 

9 

10 def __new__(cls, *args, **kwargs): 

11 return date.__new__(date, *args, **kwargs) 

12 

13 

14@patch("couchers.models.date", FakeDate) 

15def test_user_age(): 

16 FakeDate.today = classmethod(lambda cls: date(2019, 7, 5)) 

17 assert User(birthdate=date(1990, 7, 4)).age == 29 

18 assert User(birthdate=date(1990, 7, 31)).age == 28 

19 assert User(birthdate=date(1992, 2, 29)).age == 27 

20 

21 

22def test_user_display_joined(): 

23 assert User(joined=datetime(2020, 7, 10, 16, 34, 1, 1)).display_joined == datetime(2020, 7, 10, 16, 0, 0, 0) 

24 assert User(joined=datetime(2025, 7, 10, 16, 59, 1, 1)).display_joined == datetime(2025, 7, 10, 16, 0, 0, 0) 

25 assert User(joined=datetime(2020, 7, 10, 16, 0, 1, 1)).display_joined == datetime(2020, 7, 10, 16, 0, 0, 0) 

26 assert User(joined=datetime(2020, 7, 10, 0, 0, 0, 0)).display_joined == datetime(2020, 7, 10, 0, 0, 0, 0) 

27 

28 

29def test_user_display_last_active(): 

30 assert User(last_active=datetime(2020, 7, 10, 16, 34, 1, 1)).display_last_active == datetime( 

31 2020, 7, 10, 16, 0, 0, 0 

32 ) 

33 assert User(last_active=datetime(2025, 7, 10, 17, 59, 1, 1)).display_last_active == datetime( 

34 2025, 7, 10, 17, 0, 0, 0 

35 ) 

36 assert User(last_active=datetime(2020, 7, 10, 16, 0, 1, 1)).display_last_active == datetime( 

37 2020, 7, 10, 16, 0, 0, 0 

38 ) 

39 assert User(last_active=datetime(2020, 7, 10, 0, 0, 0, 0)).display_last_active == datetime(2020, 7, 10, 0, 0, 0, 0)