Coverage for tests/test_nullable.py: 100%

68 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from typing import Optional 1mnopqr

2 

3import pytest 1mnopqr

4from sqlalchemy.exc import IntegrityError 1mnopqr

5from sqlmodel import Field, Session, SQLModel, create_engine 1mnopqr

6 

7 

8def test_nullable_fields(clear_sqlmodel, caplog): 1mnopqr

9 class Hero(SQLModel, table=True): 1fabcde

10 primary_key: Optional[int] = Field( 1fabcde

11 default=None, 

12 primary_key=True, 

13 ) 

14 required_value: str 1fabcde

15 optional_default_ellipsis: Optional[str] = Field(default=...) 1fabcde

16 optional_default_none: Optional[str] = Field(default=None) 1fabcde

17 optional_non_nullable: Optional[str] = Field( 1fabcde

18 nullable=False, 

19 ) 

20 optional_nullable: Optional[str] = Field( 1fabcde

21 nullable=True, 

22 ) 

23 optional_default_ellipses_non_nullable: Optional[str] = Field( 1fabcde

24 default=..., 

25 nullable=False, 

26 ) 

27 optional_default_ellipses_nullable: Optional[str] = Field( 1fabcde

28 default=..., 

29 nullable=True, 

30 ) 

31 optional_default_none_non_nullable: Optional[str] = Field( 1fabcde

32 default=None, 

33 nullable=False, 

34 ) 

35 optional_default_none_nullable: Optional[str] = Field( 1fabcde

36 default=None, 

37 nullable=True, 

38 ) 

39 default_ellipses_non_nullable: str = Field(default=..., nullable=False) 1fabcde

40 optional_default_str: Optional[str] = "default" 1fabcde

41 optional_default_str_non_nullable: Optional[str] = Field( 1fabcde

42 default="default", nullable=False 

43 ) 

44 optional_default_str_nullable: Optional[str] = Field( 1fabcde

45 default="default", nullable=True 

46 ) 

47 str_default_str: str = "default" 1fabcde

48 str_default_str_non_nullable: str = Field(default="default", nullable=False) 1fabcde

49 str_default_str_nullable: str = Field(default="default", nullable=True) 1fabcde

50 str_default_ellipsis_non_nullable: str = Field(default=..., nullable=False) 1fabcde

51 str_default_ellipsis_nullable: str = Field(default=..., nullable=True) 1fabcde

52 

53 engine = create_engine("sqlite://", echo=True) 1fabcde

54 SQLModel.metadata.create_all(engine) 1fabcde

55 

56 create_table_log = [ 1abcde

57 message for message in caplog.messages if "CREATE TABLE hero" in message 

58 ][0] 

59 assert "primary_key INTEGER NOT NULL," in create_table_log 1fabcde

60 assert "required_value VARCHAR NOT NULL," in create_table_log 1fabcde

61 assert "optional_default_ellipsis VARCHAR," in create_table_log 1fabcde

62 assert "optional_default_none VARCHAR," in create_table_log 1fabcde

63 assert "optional_non_nullable VARCHAR NOT NULL," in create_table_log 1fabcde

64 assert "optional_nullable VARCHAR," in create_table_log 1fabcde

65 assert ( 1fabcde

66 "optional_default_ellipses_non_nullable VARCHAR NOT NULL," in create_table_log 

67 ) 

68 assert "optional_default_ellipses_nullable VARCHAR," in create_table_log 1fabcde

69 assert "optional_default_none_non_nullable VARCHAR NOT NULL," in create_table_log 1fabcde

70 assert "optional_default_none_nullable VARCHAR," in create_table_log 1fabcde

71 assert "default_ellipses_non_nullable VARCHAR NOT NULL," in create_table_log 1fabcde

72 assert "optional_default_str VARCHAR," in create_table_log 1fabcde

73 assert "optional_default_str_non_nullable VARCHAR NOT NULL," in create_table_log 1fabcde

74 assert "optional_default_str_nullable VARCHAR," in create_table_log 1fabcde

75 assert "str_default_str VARCHAR NOT NULL," in create_table_log 1fabcde

76 assert "str_default_str_non_nullable VARCHAR NOT NULL," in create_table_log 1fabcde

77 assert "str_default_str_nullable VARCHAR," in create_table_log 1fabcde

78 assert "str_default_ellipsis_non_nullable VARCHAR NOT NULL," in create_table_log 1fabcde

79 assert "str_default_ellipsis_nullable VARCHAR," in create_table_log 1fabcde

80 

81 

82# Test for regression in https://github.com/tiangolo/sqlmodel/issues/420 

83def test_non_nullable_optional_field_with_no_default_set(clear_sqlmodel, caplog): 1mnopqr

84 class Hero(SQLModel, table=True): 1lghijk

85 primary_key: Optional[int] = Field( 1lghijk

86 default=None, 

87 primary_key=True, 

88 ) 

89 

90 optional_non_nullable_no_default: Optional[str] = Field(nullable=False) 1lghijk

91 

92 engine = create_engine("sqlite://", echo=True) 1lghijk

93 SQLModel.metadata.create_all(engine) 1lghijk

94 

95 create_table_log = [ 1ghijk

96 message for message in caplog.messages if "CREATE TABLE hero" in message 

97 ][0] 

98 assert "primary_key INTEGER NOT NULL," in create_table_log 1lghijk

99 assert "optional_non_nullable_no_default VARCHAR NOT NULL," in create_table_log 1lghijk

100 

101 # We can create a hero with `None` set for the optional non-nullable field 

102 hero = Hero(primary_key=123, optional_non_nullable_no_default=None) 1lghijk

103 # But we cannot commit it. 

104 with Session(engine) as session: 1lghijk

105 session.add(hero) 1lghijk

106 with pytest.raises(IntegrityError): 1lghijk

107 session.commit() 1lghijk

108 

109 

110def test_nullable_primary_key(clear_sqlmodel, caplog): 1mnopqr

111 # Probably the weirdest corner case, it shouldn't happen anywhere, but let's test it 

112 class Hero(SQLModel, table=True): 1xstuvw

113 nullable_integer_primary_key: Optional[int] = Field( 1xstuvw

114 default=None, 

115 primary_key=True, 

116 nullable=True, 

117 ) 

118 

119 engine = create_engine("sqlite://", echo=True) 1xstuvw

120 SQLModel.metadata.create_all(engine) 1xstuvw

121 

122 create_table_log = [ 1stuvw

123 message for message in caplog.messages if "CREATE TABLE hero" in message 

124 ][0] 

125 assert "nullable_integer_primary_key INTEGER," in create_table_log 1xstuvw