Coverage for databases/tests/arrays/push/test_datetime.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1from typing import List 

2from datetime import datetime, timezone, timedelta 

3 

4import pytest 

5 

6from prisma import Prisma 

7 

8 

9def _utcnow() -> datetime: 

10 # workaround for https://github.com/RobertCraigie/prisma-client-py/issues/129 

11 now = datetime.now(timezone.utc) 

12 return now.replace(microsecond=int(now.microsecond / 1000) * 1000) 

13 

14 

15def _assert_datelist_equal(actual: List[datetime], expected: List[datetime]) -> None: 

16 actual = [dt.replace(tzinfo=None) for dt in actual] 

17 expected = [dt.replace(tzinfo=None) for dt in expected] 

18 assert actual == expected 

19 

20 

21@pytest.mark.asyncio 

22async def test_pushing_datetime(client: Prisma) -> None: 

23 """Pushing a DateTime[] value""" 

24 now = _utcnow() 

25 models = [ 

26 await client.lists.create({}), 

27 await client.lists.create( 

28 data={ 

29 'dates': [now, now + timedelta(hours=3)], 

30 }, 

31 ), 

32 ] 

33 

34 model = await client.lists.update( 

35 where={ 

36 'id': models[0].id, 

37 }, 

38 data={ 

39 'dates': { 

40 'push': [now, now + timedelta(days=5)], 

41 }, 

42 }, 

43 ) 

44 assert model is not None 

45 _assert_datelist_equal(model.dates, [now, now + timedelta(days=5)]) 

46 

47 model = await client.lists.update( 

48 where={ 

49 'id': models[1].id, 

50 }, 

51 data={ 

52 'dates': { 

53 'push': [now + timedelta(hours=7)], 

54 }, 

55 }, 

56 ) 

57 assert model is not None 

58 _assert_datelist_equal(model.dates, [now, now + timedelta(hours=3), now + timedelta(hours=7)])