Coverage for databases/sync_tests/arrays/test_bytes.py: 100%
42 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1from prisma import Base64, Prisma
2from prisma.models import Lists
3from prisma._compat import model_dict, model_parse
6def test_updating_bytes(client: Prisma) -> None:
7 """Updating a Bytes[] value"""
8 models = [
9 client.lists.create({}),
10 client.lists.create(
11 data={
12 'bytes': [Base64.encode(b'foo'), Base64.encode(b'bar')],
13 },
14 ),
15 ]
17 model = client.lists.update(
18 where={
19 'id': models[1].id,
20 },
21 data={
22 'bytes': {
23 'set': [Base64.encode(b'd')],
24 },
25 },
26 )
27 assert model is not None
28 assert model.bytes == [Base64.encode(b'd')]
30 model = client.lists.update(
31 where={
32 'id': models[1].id,
33 },
34 data={
35 'bytes': [Base64.encode(b'e')],
36 },
37 )
38 assert model is not None
39 assert model.bytes == [Base64.encode(b'e')]
42def test_filtering_bytes(client: Prisma) -> None:
43 """Searching for records by a Bytes[] value"""
44 expected_objects = [Base64.encode(b'foo'), Base64.encode(b'bar')]
45 with client.batch_() as batcher:
46 batcher.lists.create({})
47 batcher.lists.create(
48 data={
49 'bytes': [],
50 },
51 )
52 batcher.lists.create(
53 data={
54 'bytes': expected_objects,
55 },
56 )
58 model = client.lists.find_first(
59 where={
60 'bytes': {
61 'equals': None,
62 },
63 },
64 )
65 assert model is not None
66 assert model.bytes == []
68 model = client.lists.find_first(
69 where={
70 'bytes': {
71 'equals': expected_objects,
72 },
73 },
74 )
75 assert model is not None
76 assert model.bytes == expected_objects
78 model = client.lists.find_first(
79 where={
80 'bytes': {
81 'has': Base64.encode(b'foo'),
82 },
83 },
84 )
85 assert model is not None
86 assert model.bytes == expected_objects
88 model = client.lists.find_first(
89 where={
90 'bytes': {
91 'has': Base64.encode(b'baz'),
92 },
93 },
94 )
95 assert model is None
97 model = client.lists.find_first(
98 where={
99 'bytes': {
100 'has_some': [*expected_objects, Base64.encode(b'fjhf')],
101 },
102 },
103 )
104 assert model is not None
105 assert model.bytes == expected_objects
107 model = client.lists.find_first(
108 where={
109 'bytes': {
110 'has_every': [*expected_objects, Base64.encode(b'prisma')],
111 },
112 },
113 )
114 assert model is None
116 model = client.lists.find_first(
117 where={
118 'bytes': {
119 'has_every': [*expected_objects[:2]],
120 },
121 },
122 )
123 assert model is not None
124 assert model.bytes == expected_objects
126 count = client.lists.count(
127 where={
128 'bytes': {
129 'is_empty': True,
130 },
131 },
132 )
133 assert count == 1
136def test_bytes_constructing(client: Prisma) -> None:
137 """A list of Base64 fields can be passed to the model constructor"""
138 record = client.lists.create({})
139 model = model_parse(
140 Lists,
141 {
142 **model_dict(record),
143 'bytes': [
144 Base64.encode(b'foo'),
145 Base64.encode(b'bar'),
146 ],
147 },
148 )
149 assert model.bytes == [Base64.encode(b'foo'), Base64.encode(b'bar')]