Coverage for databases/tests/test_create.py: 100%

57 statements  

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

1import pytest 

2 

3from prisma import Prisma, errors 

4from lib.testing import assert_time_like_now 

5 

6 

7@pytest.mark.asyncio 

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

9 """Basic record creation""" 

10 post = await client.post.create( 

11 { 

12 'title': 'Hi from Prisma!', 

13 'published': True, 

14 'description': 'Prisma is a database toolkit that makes databases easy.', 

15 } 

16 ) 

17 assert isinstance(post.id, str) 

18 assert post.title == 'Hi from Prisma!' 

19 assert post.description == 'Prisma is a database toolkit that makes databases easy.' 

20 assert post.published is True 

21 assert_time_like_now(post.created_at, threshold=60) 

22 assert_time_like_now(post.updated_at, threshold=60) 

23 

24 user = await client.user.create( 

25 { 

26 'name': 'Robert', 

27 } 

28 ) 

29 assert user.name == 'Robert' 

30 assert isinstance(user.id, str) 

31 

32 

33@pytest.mark.asyncio 

34async def test_create_with_relationship(client: Prisma) -> None: 

35 """Creating a record with a nested relationship record creation""" 

36 post = await client.post.create( 

37 { 

38 'published': False, 

39 'title': 'Post 1', 

40 'author': {'create': {'name': 'Bob'}}, 

41 }, 

42 include={'author': True}, 

43 ) 

44 assert post.author is not None 

45 assert post.author.name == 'Bob' 

46 

47 found = await client.user.find_unique(where={'id': post.author.id}) 

48 assert found is not None 

49 assert found.name == 'Bob' 

50 

51 

52@pytest.mark.asyncio 

53async def test_create_missing_required_args(client: Prisma) -> None: 

54 """Trying to create a record with a missing required field raises an error""" 

55 with pytest.raises(TypeError): 

56 await client.post.create() # type: ignore[call-arg] 

57 

58 with pytest.raises(errors.MissingRequiredValueError): 

59 await client.post.create( 

60 { # type: ignore[typeddict-item] 

61 'published': False, 

62 } 

63 ) 

64 

65 

66@pytest.mark.asyncio 

67async def test_create_unique_violation(client: Prisma) -> None: 

68 """Creating the same record twice raises an error""" 

69 user = await client.user.create({'name': 'Robert', 'id': 'user-1'}) 

70 assert user.id == 'user-1' 

71 assert user.name == 'Robert' 

72 

73 with pytest.raises(errors.UniqueViolationError): 

74 await client.user.create({'name': 'Robert', 'id': 'user-1'}) 

75 

76 

77@pytest.mark.asyncio 

78async def test_setting_field_to_null(client: Prisma) -> None: 

79 """Creating a field with a None value sets the database record to None""" 

80 post = await client.post.create( 

81 data={ 

82 'title': 'Post', 

83 'published': False, 

84 'description': None, 

85 }, 

86 ) 

87 assert post.description is None 

88 

89 

90@pytest.mark.asyncio 

91async def test_setting_non_nullable_field_to_null(client: Prisma) -> None: 

92 """Attempting to create a record with a non-nullable field set to null raises an error""" 

93 with pytest.raises(errors.MissingRequiredValueError) as exc: 

94 await client.post.create( 

95 data={ 

96 'title': 'Post', 

97 'published': None, # type: ignore 

98 }, 

99 ) 

100 

101 assert exc.match(r'published') 

102 

103 

104@pytest.mark.asyncio 

105async def test_nullable_relational_field(client: Prisma) -> None: 

106 """Relational fields cannot be set to None""" 

107 with pytest.raises(errors.MissingRequiredValueError) as exc: 

108 await client.post.create( 

109 data={'title': 'Post', 'published': False, 'author': None} # type: ignore 

110 ) 

111 

112 assert exc.match(r'author') 

113 

114 

115@pytest.mark.asyncio 

116async def test_required_relation_key_field(client: Prisma) -> None: 

117 """Explicitly passing a field used as a foreign key connects the relation""" 

118 user = await client.user.create( 

119 data={ 

120 'name': 'Robert', 

121 }, 

122 ) 

123 profile = await client.profile.create( 

124 data={ 

125 'description': 'My bio!', 

126 'country': 'Scotland', 

127 'user_id': user.id, 

128 }, 

129 include={ 

130 'user': True, 

131 }, 

132 ) 

133 assert profile.user is not None 

134 assert profile.user.id == user.id 

135 assert profile.user.name == 'Robert'