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

37 statements  

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

1from prisma import Json, Prisma 

2 

3 

4def test_updating_json(client: Prisma) -> None: 

5 """Updating a Json[] value""" 

6 models = [ 

7 client.lists.create({}), 

8 client.lists.create( 

9 data={ 

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

11 }, 

12 ), 

13 ] 

14 

15 model = client.lists.update( 

16 where={ 

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

18 }, 

19 data={ 

20 'json_objects': { 

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

22 }, 

23 }, 

24 ) 

25 assert model is not None 

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

27 

28 model = client.lists.update( 

29 where={ 

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

31 }, 

32 data={ 

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

34 }, 

35 ) 

36 assert model is not None 

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

38 

39 

40def test_filtering_json(client: Prisma) -> None: 

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

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

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

44 

45 with client.batch_() as batcher: 

46 batcher.lists.create({}) 

47 batcher.lists.create( 

48 data={ 

49 'json_objects': [], 

50 }, 

51 ) 

52 batcher.lists.create( 

53 data={ 

54 'json_objects': expected_objects, 

55 }, 

56 ) 

57 

58 model = client.lists.find_first( 

59 where={ 

60 'json_objects': { 

61 'equals': None, 

62 }, 

63 }, 

64 ) 

65 assert model is not None 

66 assert model.json_objects == [] 

67 

68 model = client.lists.find_first( 

69 where={ 

70 'json_objects': { 

71 'equals': expected_objects, 

72 }, 

73 }, 

74 ) 

75 assert model is not None 

76 assert model.json_objects == expected_raw 

77 

78 model = client.lists.find_first( 

79 where={ 

80 'json_objects': { 

81 'has': Json([]), 

82 }, 

83 }, 

84 ) 

85 assert model is not None 

86 assert model.json_objects == expected_raw 

87 

88 model = client.lists.find_first( 

89 where={ 

90 'json_objects': { 

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

92 }, 

93 }, 

94 ) 

95 assert model is None 

96 

97 model = client.lists.find_first( 

98 where={ 

99 'json_objects': { 

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

101 }, 

102 }, 

103 ) 

104 assert model is not None 

105 assert model.json_objects == expected_raw 

106 

107 model = client.lists.find_first( 

108 where={ 

109 'json_objects': { 

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

111 }, 

112 }, 

113 ) 

114 assert model is None 

115 

116 model = client.lists.find_first( 

117 where={ 

118 'json_objects': { 

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

120 }, 

121 }, 

122 ) 

123 assert model is not None 

124 assert model.json_objects == expected_raw 

125 

126 count = client.lists.count( 

127 where={ 

128 'json_objects': { 

129 'is_empty': True, 

130 }, 

131 }, 

132 ) 

133 assert count == 1