Coverage for databases/sync_tests/test_enum.py: 94%

31 statements  

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

1from dirty_equals import IsPartialDict 

2 

3from prisma import Prisma 

4from prisma.enums import Role 

5from prisma.models import Types 

6from prisma._compat import PYDANTIC_V2, model_json_schema 

7 

8 

9def test_enum_create(client: Prisma) -> None: 

10 """Creating a record with an enum value""" 

11 record = client.types.create({}) 

12 assert record.enum == Role.USER 

13 

14 record = client.types.create({'enum': Role.ADMIN}) 

15 assert record.enum == Role.ADMIN 

16 

17 

18# TODO: all other actions 

19 

20 

21def test_id5(client: Prisma) -> None: 

22 """Combined ID constraint with an Enum field""" 

23 model = client.id5.create( 

24 data={ 

25 'name': 'Robert', 

26 'role': Role.ADMIN, 

27 }, 

28 ) 

29 

30 found = client.id5.find_unique( 

31 where={ 

32 'name_role': { 

33 'name': 'Robert', 

34 'role': Role.ADMIN, 

35 }, 

36 }, 

37 ) 

38 assert found is not None 

39 assert found.name == model.name 

40 assert found.role == Role.ADMIN 

41 

42 found = client.id5.find_unique( 

43 where={ 

44 'name_role': { 

45 'name': 'Robert', 

46 'role': Role.USER, 

47 }, 

48 }, 

49 ) 

50 assert found is None 

51 

52 

53def test_unique6(client: Prisma) -> None: 

54 """Combined unique constraint with an Enum field""" 

55 model = client.unique6.create( 

56 data={ 

57 'name': 'Robert', 

58 'role': Role.ADMIN, 

59 }, 

60 ) 

61 

62 found = client.unique6.find_unique( 

63 where={ 

64 'name_role': { 

65 'name': 'Robert', 

66 'role': Role.ADMIN, 

67 }, 

68 }, 

69 ) 

70 assert found is not None 

71 assert found.name == model.name 

72 assert found.role == Role.ADMIN 

73 

74 found = client.unique6.find_unique( 

75 where={ 

76 'name_role': { 

77 'name': 'Robert', 

78 'role': Role.USER, 

79 }, 

80 }, 

81 ) 

82 assert found is None 

83 

84 

85def test_json_schema() -> None: 

86 """Ensure a JSON Schema definition can be created""" 

87 defs = { 

88 'Role': IsPartialDict( 

89 { 

90 'title': 'Role', 

91 'enum': ['USER', 'ADMIN', 'EDITOR'], 

92 'type': 'string', 

93 } 

94 ) 

95 } 

96 

97 if PYDANTIC_V2: 97 ↛ 111line 97 didn't jump to line 111, because the condition on line 97 was never false

98 assert model_json_schema(Types) == IsPartialDict( 

99 { 

100 '$defs': defs, 

101 'properties': IsPartialDict( 

102 { 

103 'enum': { 

104 '$ref': '#/$defs/Role', 

105 } 

106 } 

107 ), 

108 }, 

109 ) 

110 else: 

111 assert model_json_schema(Types) == IsPartialDict( 

112 definitions=defs, 

113 properties=IsPartialDict( 

114 { 

115 'enum': { 

116 '$ref': '#/definitions/Role', 

117 } 

118 } 

119 ), 

120 )