Coverage for databases/tests/arrays/test_bytes.py: 100%

46 statements  

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

1import pytest 

2 

3from prisma import Base64, Prisma 

4from prisma.models import Lists 

5from prisma._compat import model_dict, model_parse 

6 

7 

8@pytest.mark.asyncio 

9async def test_updating_bytes(client: Prisma) -> None: 

10 """Updating a Bytes[] value""" 

11 models = [ 

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

13 await client.lists.create( 

14 data={ 

15 'bytes': [Base64.encode(b'foo'), Base64.encode(b'bar')], 

16 }, 

17 ), 

18 ] 

19 

20 model = await client.lists.update( 

21 where={ 

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

23 }, 

24 data={ 

25 'bytes': { 

26 'set': [Base64.encode(b'd')], 

27 }, 

28 }, 

29 ) 

30 assert model is not None 

31 assert model.bytes == [Base64.encode(b'd')] 

32 

33 model = await client.lists.update( 

34 where={ 

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

36 }, 

37 data={ 

38 'bytes': [Base64.encode(b'e')], 

39 }, 

40 ) 

41 assert model is not None 

42 assert model.bytes == [Base64.encode(b'e')] 

43 

44 

45@pytest.mark.asyncio 

46async def test_filtering_bytes(client: Prisma) -> None: 

47 """Searching for records by a Bytes[] value""" 

48 expected_objects = [Base64.encode(b'foo'), Base64.encode(b'bar')] 

49 async with client.batch_() as batcher: 

50 batcher.lists.create({}) 

51 batcher.lists.create( 

52 data={ 

53 'bytes': [], 

54 }, 

55 ) 

56 batcher.lists.create( 

57 data={ 

58 'bytes': expected_objects, 

59 }, 

60 ) 

61 

62 model = await client.lists.find_first( 

63 where={ 

64 'bytes': { 

65 'equals': None, 

66 }, 

67 }, 

68 ) 

69 assert model is not None 

70 assert model.bytes == [] 

71 

72 model = await client.lists.find_first( 

73 where={ 

74 'bytes': { 

75 'equals': expected_objects, 

76 }, 

77 }, 

78 ) 

79 assert model is not None 

80 assert model.bytes == expected_objects 

81 

82 model = await client.lists.find_first( 

83 where={ 

84 'bytes': { 

85 'has': Base64.encode(b'foo'), 

86 }, 

87 }, 

88 ) 

89 assert model is not None 

90 assert model.bytes == expected_objects 

91 

92 model = await client.lists.find_first( 

93 where={ 

94 'bytes': { 

95 'has': Base64.encode(b'baz'), 

96 }, 

97 }, 

98 ) 

99 assert model is None 

100 

101 model = await client.lists.find_first( 

102 where={ 

103 'bytes': { 

104 'has_some': [*expected_objects, Base64.encode(b'fjhf')], 

105 }, 

106 }, 

107 ) 

108 assert model is not None 

109 assert model.bytes == expected_objects 

110 

111 model = await client.lists.find_first( 

112 where={ 

113 'bytes': { 

114 'has_every': [*expected_objects, Base64.encode(b'prisma')], 

115 }, 

116 }, 

117 ) 

118 assert model is None 

119 

120 model = await client.lists.find_first( 

121 where={ 

122 'bytes': { 

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

124 }, 

125 }, 

126 ) 

127 assert model is not None 

128 assert model.bytes == expected_objects 

129 

130 count = await client.lists.count( 

131 where={ 

132 'bytes': { 

133 'is_empty': True, 

134 }, 

135 }, 

136 ) 

137 assert count == 1 

138 

139 

140@pytest.mark.asyncio 

141async def test_bytes_constructing(client: Prisma) -> None: 

142 """A list of Base64 fields can be passed to the model constructor""" 

143 record = await client.lists.create({}) 

144 model = model_parse( 

145 Lists, 

146 { 

147 **model_dict(record), 

148 'bytes': [ 

149 Base64.encode(b'foo'), 

150 Base64.encode(b'bar'), 

151 ], 

152 }, 

153 ) 

154 assert model.bytes == [Base64.encode(b'foo'), Base64.encode(b'bar')]