Coverage for src/tests/core/domain/value_objects/test_period.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for testing the value object Period.""" 

2 

3import datetime 

4 

5import pytest 

6 

7from kwai_core.domain.value_objects.period import Period 

8from kwai_core.domain.value_objects.timestamp import Timestamp 

9 

10 

11def test_period_delta(): 

12 """Test get_delta of a period.""" 

13 period = Period( 

14 start_date=Timestamp.create_now(), 

15 end_date=Timestamp.create_with_delta(days=1), 

16 ) 

17 

18 assert period.delta.days == 1, "The period should be one day" 

19 

20 

21def test_initialize_period(): 

22 """Test the initialization of a period.""" 

23 period = Period( 

24 start_date=Timestamp(timestamp=datetime.datetime(1, 1, 1, 20, 0)), 

25 end_date=Timestamp(timestamp=datetime.datetime(1, 1, 1, 21, 0)), 

26 ) 

27 assert period.delta.total_seconds() / (60 * 60) == 1, ( 

28 "The period should be one hour" 

29 ) 

30 

31 

32def test_wrong_period(): 

33 """Test if a period fails when the end date is before the start date.""" 

34 with pytest.raises(ValueError): 

35 Period( 

36 start_date=Timestamp.create_now(), 

37 end_date=Timestamp.create_with_delta(days=-1), 

38 ) 

39 

40 

41def test_create_with_delta(): 

42 """Test the create_with_delta factory method.""" 

43 period = Period.create_from_delta(hours=1) 

44 

45 assert period.delta.total_seconds() / (60 * 60) == 1, ( 

46 "The period should be one hour" 

47 )