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

39 statements  

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

1import pytest 

2 

3from prisma import Prisma 

4from prisma.enums import Role 

5 

6 

7@pytest.mark.asyncio 

8async def test_filtering_enums(client: Prisma) -> None: 

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

10 async with client.batch_() as batcher: 

11 batcher.lists.create({}) 

12 batcher.lists.create( 

13 data={ 

14 'roles': [], 

15 }, 

16 ) 

17 batcher.lists.create( 

18 data={ 

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

20 }, 

21 ) 

22 

23 model = await client.lists.find_first( 

24 where={ 

25 'roles': { 

26 'equals': None, 

27 }, 

28 }, 

29 ) 

30 assert model is not None 

31 assert model.roles == [] 

32 

33 model = await client.lists.find_first( 

34 where={ 

35 'roles': { 

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

37 }, 

38 }, 

39 ) 

40 assert model is not None 

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

42 

43 model = await client.lists.find_first( 

44 where={ 

45 'roles': { 

46 'has': Role.ADMIN, 

47 }, 

48 }, 

49 ) 

50 assert model is not None 

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

52 

53 model = await client.lists.find_first( 

54 where={ 

55 'roles': { 

56 'has': Role.EDITOR, 

57 }, 

58 }, 

59 ) 

60 assert model is None 

61 

62 model = await client.lists.find_first( 

63 where={ 

64 'roles': { 

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

66 }, 

67 }, 

68 ) 

69 assert model is not None 

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

71 

72 model = await client.lists.find_first( 

73 where={ 

74 'roles': { 

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

76 }, 

77 }, 

78 ) 

79 assert model is None 

80 

81 model = await client.lists.find_first( 

82 where={ 

83 'roles': { 

84 'has_every': [Role.USER], 

85 }, 

86 }, 

87 ) 

88 assert model is not None 

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

90 

91 count = await client.lists.count( 

92 where={ 

93 'roles': { 

94 'is_empty': True, 

95 }, 

96 }, 

97 ) 

98 assert count == 1 

99 

100 

101@pytest.mark.asyncio 

102async def test_updating_enum(client: Prisma) -> None: 

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

104 models = [ 

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

106 await client.lists.create( 

107 data={ 

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

109 }, 

110 ), 

111 ] 

112 

113 model = await client.lists.update( 

114 where={ 

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

116 }, 

117 data={ 

118 'roles': { 

119 'set': [Role.USER], 

120 }, 

121 }, 

122 ) 

123 assert model is not None 

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

125 

126 model = await client.lists.update( 

127 where={ 

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

129 }, 

130 data={ 

131 'roles': [Role.ADMIN], 

132 }, 

133 ) 

134 assert model is not None 

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