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

36 statements  

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

1from prisma import Prisma 

2from prisma.enums import Role 

3 

4 

5def test_filtering_enums(client: Prisma) -> None: 

6 """Searching for records by a Role[] enum value""" 

7 with client.batch_() as batcher: 

8 batcher.lists.create({}) 

9 batcher.lists.create( 

10 data={ 

11 'roles': [], 

12 }, 

13 ) 

14 batcher.lists.create( 

15 data={ 

16 'roles': [Role.USER, Role.ADMIN], 

17 }, 

18 ) 

19 

20 model = client.lists.find_first( 

21 where={ 

22 'roles': { 

23 'equals': None, 

24 }, 

25 }, 

26 ) 

27 assert model is not None 

28 assert model.roles == [] 

29 

30 model = client.lists.find_first( 

31 where={ 

32 'roles': { 

33 'equals': [Role.USER, Role.ADMIN], 

34 }, 

35 }, 

36 ) 

37 assert model is not None 

38 assert model.roles == [Role.USER, Role.ADMIN] 

39 

40 model = client.lists.find_first( 

41 where={ 

42 'roles': { 

43 'has': Role.ADMIN, 

44 }, 

45 }, 

46 ) 

47 assert model is not None 

48 assert model.roles == [Role.USER, Role.ADMIN] 

49 

50 model = client.lists.find_first( 

51 where={ 

52 'roles': { 

53 'has': Role.EDITOR, 

54 }, 

55 }, 

56 ) 

57 assert model is None 

58 

59 model = client.lists.find_first( 

60 where={ 

61 'roles': { 

62 'has_some': [Role.USER, Role.EDITOR], 

63 }, 

64 }, 

65 ) 

66 assert model is not None 

67 assert model.roles == [Role.USER, Role.ADMIN] 

68 

69 model = client.lists.find_first( 

70 where={ 

71 'roles': { 

72 'has_every': [Role.USER, Role.ADMIN, Role.EDITOR], 

73 }, 

74 }, 

75 ) 

76 assert model is None 

77 

78 model = client.lists.find_first( 

79 where={ 

80 'roles': { 

81 'has_every': [Role.USER], 

82 }, 

83 }, 

84 ) 

85 assert model is not None 

86 assert model.roles == [Role.USER, Role.ADMIN] 

87 

88 count = client.lists.count( 

89 where={ 

90 'roles': { 

91 'is_empty': True, 

92 }, 

93 }, 

94 ) 

95 assert count == 1 

96 

97 

98def test_updating_enum(client: Prisma) -> None: 

99 """Updating a Role[] enum value""" 

100 models = [ 

101 client.lists.create({}), 

102 client.lists.create( 

103 data={ 

104 'roles': [Role.USER, Role.ADMIN], 

105 }, 

106 ), 

107 ] 

108 

109 model = client.lists.update( 

110 where={ 

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

112 }, 

113 data={ 

114 'roles': { 

115 'set': [Role.USER], 

116 }, 

117 }, 

118 ) 

119 assert model is not None 

120 assert model.roles == [Role.USER] 

121 

122 model = client.lists.update( 

123 where={ 

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

125 }, 

126 data={ 

127 'roles': [Role.ADMIN], 

128 }, 

129 ) 

130 assert model is not None 

131 assert model.roles == [Role.ADMIN]