Coverage for tests/test_field_sa_column.py: 100%

49 statements  

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

1from typing import Optional 1abcdef

2 

3import pytest 1abcdef

4from sqlalchemy import Column, Integer, String 1abcdef

5from sqlmodel import Field, SQLModel 1abcdef

6 

7 

8def test_sa_column_takes_precedence() -> None: 1abcdef

9 class Item(SQLModel, table=True): 1mnopqr

10 id: Optional[int] = Field( 1mnopqr

11 default=None, 

12 sa_column=Column(String, primary_key=True, nullable=False), 

13 ) 

14 

15 # It would have been nullable with no sa_column 

16 assert Item.id.nullable is False # type: ignore 1mnopqr

17 assert isinstance(Item.id.type, String) # type: ignore 1mnopqr

18 

19 

20def test_sa_column_no_sa_args() -> None: 1abcdef

21 with pytest.raises(RuntimeError): 1stuvwx

22 

23 class Item(SQLModel, table=True): 1stuvwx

24 id: Optional[int] = Field( 1stuvwx

25 default=None, 

26 sa_column_args=[Integer], 

27 sa_column=Column(Integer, primary_key=True), 

28 ) 

29 

30 

31def test_sa_column_no_sa_kargs() -> None: 1abcdef

32 with pytest.raises(RuntimeError): 1yzABCD

33 

34 class Item(SQLModel, table=True): 1yzABCD

35 id: Optional[int] = Field( 1yzABCD

36 default=None, 

37 sa_column_kwargs={"primary_key": True}, 

38 sa_column=Column(Integer, primary_key=True), 

39 ) 

40 

41 

42def test_sa_column_no_type() -> None: 1abcdef

43 with pytest.raises(RuntimeError): 1EFGHIJ

44 

45 class Item(SQLModel, table=True): 1EFGHIJ

46 id: Optional[int] = Field( 1EFGHIJ

47 default=None, 

48 sa_type=Integer, 

49 sa_column=Column(Integer, primary_key=True), 

50 ) 

51 

52 

53def test_sa_column_no_primary_key() -> None: 1abcdef

54 with pytest.raises(RuntimeError): 1KLMNOP

55 

56 class Item(SQLModel, table=True): 1KLMNOP

57 id: Optional[int] = Field( 1KLMNOP

58 default=None, 

59 primary_key=True, 

60 sa_column=Column(Integer, primary_key=True), 

61 ) 

62 

63 

64def test_sa_column_no_nullable() -> None: 1abcdef

65 with pytest.raises(RuntimeError): 1QRSTUV

66 

67 class Item(SQLModel, table=True): 1QRSTUV

68 id: Optional[int] = Field( 1QRSTUV

69 default=None, 

70 nullable=True, 

71 sa_column=Column(Integer, primary_key=True), 

72 ) 

73 

74 

75def test_sa_column_no_foreign_key() -> None: 1abcdef

76 with pytest.raises(RuntimeError): 1ghijkl

77 

78 class Team(SQLModel, table=True): 1ghijkl

79 id: Optional[int] = Field(default=None, primary_key=True) 1ghijkl

80 name: str 1ghijkl

81 

82 class Hero(SQLModel, table=True): 1ghijkl

83 id: Optional[int] = Field(default=None, primary_key=True) 1ghijkl

84 team_id: Optional[int] = Field( 1ghijkl

85 default=None, 

86 foreign_key="team.id", 

87 sa_column=Column(Integer, primary_key=True), 

88 ) 

89 

90 

91def test_sa_column_no_unique() -> None: 1abcdef

92 with pytest.raises(RuntimeError): 1WXYZ01

93 

94 class Item(SQLModel, table=True): 1WXYZ01

95 id: Optional[int] = Field( 1WXYZ01

96 default=None, 

97 unique=True, 

98 sa_column=Column(Integer, primary_key=True), 

99 ) 

100 

101 

102def test_sa_column_no_index() -> None: 1abcdef

103 with pytest.raises(RuntimeError): 1234567

104 

105 class Item(SQLModel, table=True): 1234567

106 id: Optional[int] = Field( 1234567

107 default=None, 

108 index=True, 

109 sa_column=Column(Integer, primary_key=True), 

110 ) 

111 

112 

113def test_sa_column_no_ondelete() -> None: 1abcdef

114 with pytest.raises(RuntimeError): 189!#$%

115 

116 class Item(SQLModel, table=True): 189!#$%

117 id: Optional[int] = Field( 189!#$%

118 default=None, 

119 sa_column=Column(Integer, primary_key=True), 

120 ondelete="CASCADE", 

121 )