Coverage for tests/test_datatypes.py: 13%

47 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-01 15:16 -0700

1# This file is part of felis. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import unittest 

23 

24from pydantic import ValidationError 

25 

26from felis.datamodel import Column 

27 

28 

29class ColumnGenerator: 

30 """Generate column data for testing.""" 

31 

32 def __init__(self, name, id, db_name): 

33 self.name = name 

34 self.id = id 

35 self.db_name = db_name 

36 self.context = {"check_redundant_datatypes": True} 

37 

38 def col(self, datatype: str, db_datatype: str, length=None): 

39 return Column.model_validate( 

40 { 

41 "name": self.name, 

42 "@id": self.id, 

43 "datatype": datatype, 

44 f"{self.db_name}:datatype": db_datatype, 

45 "length": length, 

46 }, 

47 context=self.context, 

48 ) 

49 

50 

51class RedundantDatatypesTest(unittest.TestCase): 

52 """Test validation of redundant datatype definitions.""" 

53 

54 def test_mysql_datatypes(self) -> None: 

55 """Test that redundant datatype definitions raise an error.""" 

56 coldata = ColumnGenerator("test_col", "#test_col_id", "mysql") 

57 

58 with self.assertRaises(ValidationError): 

59 coldata.col("double", "DOUBLE") 

60 

61 with self.assertRaises(ValidationError): 

62 coldata.col("int", "INTEGER") 

63 

64 with self.assertRaises(ValidationError): 

65 coldata.col("boolean", "BIT(1)") 

66 

67 with self.assertRaises(ValidationError): 

68 coldata.col("float", "FLOAT") 

69 

70 with self.assertRaises(ValidationError): 

71 coldata.col("char", "CHAR", length=8) 

72 

73 with self.assertRaises(ValidationError): 

74 coldata.col("string", "VARCHAR", length=32) 

75 

76 with self.assertRaises(ValidationError): 

77 coldata.col("byte", "TINYINT") 

78 

79 with self.assertRaises(ValidationError): 

80 coldata.col("short", "SMALLINT") 

81 

82 with self.assertRaises(ValidationError): 

83 coldata.col("long", "BIGINT") 

84 

85 # These look like they should be equivalent, but the default is 

86 # actually ``BIT(1)`` for MySQL. 

87 coldata.col("boolean", "BOOLEAN") 

88 

89 with self.assertRaises(ValidationError): 

90 coldata.col("unicode", "NVARCHAR", length=32) 

91 

92 with self.assertRaises(ValidationError): 

93 coldata.col("timestamp", "TIMESTAMP") 

94 

95 # DM-42257: Felis does not handle unbounded text types properly. 

96 # coldata.col("text", "TEXT", length=32) 

97 

98 with self.assertRaises(ValidationError): 

99 coldata.col("binary", "LONGBLOB", length=1024) 

100 

101 with self.assertRaises(ValidationError): 

102 # Same type and length 

103 coldata.col("string", "VARCHAR(128)", length=128) 

104 

105 # Different types, which is okay 

106 coldata.col("double", "FLOAT") 

107 

108 # Same base type with different lengths, which is okay 

109 coldata.col("string", "VARCHAR(128)", length=32) 

110 

111 # Different string types, which is okay 

112 coldata.col("string", "CHAR", length=32) 

113 coldata.col("unicode", "CHAR", length=32) 

114 

115 

116if __name__ == "__main__": 116 ↛ 117line 116 didn't jump to line 117, because the condition on line 116 was never true

117 unittest.main()