Coverage for databases/sync_tests/arrays/test_datetime.py: 100%

47 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-28 15:17 +0000

1from typing import List 

2from datetime import datetime, timezone, timedelta 

3 

4from prisma import Prisma 

5 

6 

7def _utcnow() -> datetime: 

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

9 now = datetime.now(timezone.utc) 

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

11 

12 

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

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

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

16 assert actual == expected 

17 

18 

19def test_updating_datetime(client: Prisma) -> None: 

20 """Updating a DateTime[] value""" 

21 now = _utcnow() 

22 models = [ 

23 client.lists.create({}), 

24 client.lists.create( 

25 data={ 

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

27 }, 

28 ), 

29 ] 

30 

31 model = client.lists.update( 

32 where={ 

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

34 }, 

35 data={ 

36 'dates': { 

37 'set': [now + timedelta(days=999)], 

38 }, 

39 }, 

40 ) 

41 assert model is not None 

42 _assert_datelist_equal(model.dates, [now + timedelta(days=999)]) 

43 

44 model = client.lists.update( 

45 where={ 

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

47 }, 

48 data={ 

49 'dates': [now + timedelta(hours=999)], 

50 }, 

51 ) 

52 assert model is not None 

53 _assert_datelist_equal(model.dates, [now + timedelta(hours=999)]) 

54 

55 

56def test_filtering_datetime(client: Prisma) -> None: 

57 """Searching for records by a DateTime[] value""" 

58 now = _utcnow() 

59 expected_objects = [now, now + timedelta(hours=1)] 

60 with client.batch_() as batcher: 

61 batcher.lists.create({}) 

62 batcher.lists.create( 

63 data={ 

64 'dates': [], 

65 }, 

66 ) 

67 batcher.lists.create( 

68 data={ 

69 'dates': expected_objects, 

70 }, 

71 ) 

72 

73 model = client.lists.find_first( 

74 where={ 

75 'dates': { 

76 'equals': None, 

77 }, 

78 }, 

79 ) 

80 assert model is not None 

81 assert model.dates == [] 

82 

83 model = client.lists.find_first( 

84 where={ 

85 'dates': { 

86 'equals': expected_objects, 

87 }, 

88 }, 

89 ) 

90 assert model is not None 

91 _assert_datelist_equal(model.dates, expected_objects) 

92 

93 model = client.lists.find_first( 

94 where={ 

95 'dates': { 

96 'has': now, 

97 }, 

98 }, 

99 ) 

100 assert model is not None 

101 _assert_datelist_equal(model.dates, expected_objects) 

102 

103 model = client.lists.find_first( 

104 where={ 

105 'dates': { 

106 'has': now + timedelta(seconds=5), 

107 }, 

108 }, 

109 ) 

110 assert model is None 

111 

112 model = client.lists.find_first( 

113 where={ 

114 'dates': { 

115 'has_some': [*expected_objects, now + timedelta(seconds=5)], 

116 }, 

117 }, 

118 ) 

119 assert model is not None 

120 _assert_datelist_equal(model.dates, expected_objects) 

121 

122 model = client.lists.find_first( 

123 where={ 

124 'dates': { 

125 'has_every': [*expected_objects, now + timedelta(minutes=10)], 

126 }, 

127 }, 

128 ) 

129 assert model is None 

130 

131 model = client.lists.find_first( 

132 where={ 

133 'dates': { 

134 'has_every': [*expected_objects[:2]], 

135 }, 

136 }, 

137 ) 

138 assert model is not None 

139 _assert_datelist_equal(model.dates, expected_objects) 

140 

141 count = client.lists.count( 

142 where={ 

143 'dates': { 

144 'is_empty': True, 

145 }, 

146 }, 

147 ) 

148 assert count == 1