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

40 statements  

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

1import pytest 

2 

3from prisma import Json, Prisma 

4 

5 

6@pytest.mark.asyncio 

7async def test_updating_json(client: Prisma) -> None: 

8 """Updating a Json[] value""" 

9 models = [ 

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

11 await client.lists.create( 

12 data={ 

13 'json_objects': [Json('foo'), Json(['foo', 'bar'])], 

14 }, 

15 ), 

16 ] 

17 

18 model = await client.lists.update( 

19 where={ 

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

21 }, 

22 data={ 

23 'json_objects': { 

24 'set': [Json.keys(hello=123)], 

25 }, 

26 }, 

27 ) 

28 assert model is not None 

29 assert model.json_objects == [{'hello': 123}] 

30 

31 model = await client.lists.update( 

32 where={ 

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

34 }, 

35 data={ 

36 'json_objects': [Json.keys(world=None)], 

37 }, 

38 ) 

39 assert model is not None 

40 assert model.json_objects == [{'world': None}] 

41 

42 

43@pytest.mark.asyncio 

44async def test_filtering_json(client: Prisma) -> None: 

45 """Searching for records by a Json[] value""" 

46 expected_raw: list[object] = [[], {'country': 'Scotland'}] 

47 expected_objects = [Json([]), Json.keys(country='Scotland')] 

48 

49 async with client.batch_() as batcher: 

50 batcher.lists.create({}) 

51 batcher.lists.create( 

52 data={ 

53 'json_objects': [], 

54 }, 

55 ) 

56 batcher.lists.create( 

57 data={ 

58 'json_objects': expected_objects, 

59 }, 

60 ) 

61 

62 model = await client.lists.find_first( 

63 where={ 

64 'json_objects': { 

65 'equals': None, 

66 }, 

67 }, 

68 ) 

69 assert model is not None 

70 assert model.json_objects == [] 

71 

72 model = await client.lists.find_first( 

73 where={ 

74 'json_objects': { 

75 'equals': expected_objects, 

76 }, 

77 }, 

78 ) 

79 assert model is not None 

80 assert model.json_objects == expected_raw 

81 

82 model = await client.lists.find_first( 

83 where={ 

84 'json_objects': { 

85 'has': Json([]), 

86 }, 

87 }, 

88 ) 

89 assert model is not None 

90 assert model.json_objects == expected_raw 

91 

92 model = await client.lists.find_first( 

93 where={ 

94 'json_objects': { 

95 'has': Json(['foo']), 

96 }, 

97 }, 

98 ) 

99 assert model is None 

100 

101 model = await client.lists.find_first( 

102 where={ 

103 'json_objects': { 

104 'has_some': [*expected_objects, Json(['foo'])], 

105 }, 

106 }, 

107 ) 

108 assert model is not None 

109 assert model.json_objects == expected_raw 

110 

111 model = await client.lists.find_first( 

112 where={ 

113 'json_objects': { 

114 'has_every': [*expected_objects, Json(['foo'])], 

115 }, 

116 }, 

117 ) 

118 assert model is None 

119 

120 model = await client.lists.find_first( 

121 where={ 

122 'json_objects': { 

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

124 }, 

125 }, 

126 ) 

127 assert model is not None 

128 assert model.json_objects == expected_raw 

129 

130 count = await client.lists.count( 

131 where={ 

132 'json_objects': { 

133 'is_empty': True, 

134 }, 

135 }, 

136 ) 

137 assert count == 1