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

49 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 

7def test_create(client: Prisma) -> None: 

8 """Basic record creation""" 

9 post = client.post.create( 

10 { 

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

12 'published': True, 

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

14 } 

15 ) 

16 assert isinstance(post.id, str) 

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

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

19 assert post.published is True 

20 assert_time_like_now(post.created_at) 

21 assert_time_like_now(post.updated_at) 

22 

23 user = client.user.create( 

24 { 

25 'name': 'Robert', 

26 } 

27 ) 

28 assert user.name == 'Robert' 

29 assert isinstance(user.id, str) 

30 

31 

32def test_create_with_relationship(client: Prisma) -> None: 

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

34 post = client.post.create( 

35 { 

36 'published': False, 

37 'title': 'Post 1', 

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

39 }, 

40 include={'author': True}, 

41 ) 

42 assert post.author is not None 

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

44 

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

46 assert found is not None 

47 assert found.name == 'Bob' 

48 

49 

50def test_create_missing_required_args(client: Prisma) -> None: 

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

52 with pytest.raises(TypeError): 

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

54 

55 with pytest.raises(errors.MissingRequiredValueError): 

56 client.post.create( 

57 { # type: ignore[typeddict-item] 

58 'published': False, 

59 } 

60 ) 

61 

62 

63def test_create_unique_violation(client: Prisma) -> None: 

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

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

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

67 assert user.name == 'Robert' 

68 

69 with pytest.raises(errors.UniqueViolationError): 

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

71 

72 

73def test_setting_field_to_null(client: Prisma) -> None: 

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

75 post = client.post.create( 

76 data={ 

77 'title': 'Post', 

78 'published': False, 

79 'description': None, 

80 }, 

81 ) 

82 assert post.description is None 

83 

84 

85def test_setting_non_nullable_field_to_null(client: Prisma) -> None: 

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

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

88 client.post.create( 

89 data={ 

90 'title': 'Post', 

91 'published': None, # type: ignore 

92 }, 

93 ) 

94 

95 assert exc.match(r'published') 

96 

97 

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

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

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

101 client.post.create( 

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

103 ) 

104 

105 assert exc.match(r'author') 

106 

107 

108def test_required_relation_key_field(client: Prisma) -> None: 

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

110 user = client.user.create( 

111 data={ 

112 'name': 'Robert', 

113 }, 

114 ) 

115 profile = client.profile.create( 

116 data={ 

117 'description': 'My bio!', 

118 'country': 'Scotland', 

119 'user_id': user.id, 

120 }, 

121 include={ 

122 'user': True, 

123 }, 

124 ) 

125 assert profile.user is not None 

126 assert profile.user.id == user.id 

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