Coverage for tests/test_datatypes.py: 13%
47 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 02:43 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 02:43 -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/>.
22import unittest
24from pydantic import ValidationError
26from felis.datamodel import Column
29class ColumnGenerator:
30 """Generate column data for testing."""
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}
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 )
51class RedundantDatatypesTest(unittest.TestCase):
52 """Test validation of redundant datatype definitions."""
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")
58 with self.assertRaises(ValidationError):
59 coldata.col("double", "DOUBLE")
61 with self.assertRaises(ValidationError):
62 coldata.col("int", "INTEGER")
64 with self.assertRaises(ValidationError):
65 coldata.col("float", "FLOAT")
67 with self.assertRaises(ValidationError):
68 coldata.col("char", "CHAR", length=8)
70 with self.assertRaises(ValidationError):
71 coldata.col("string", "VARCHAR", length=32)
73 with self.assertRaises(ValidationError):
74 coldata.col("byte", "TINYINT")
76 with self.assertRaises(ValidationError):
77 coldata.col("short", "SMALLINT")
79 with self.assertRaises(ValidationError):
80 coldata.col("long", "BIGINT")
82 with self.assertRaises(ValidationError):
83 coldata.col("boolean", "BOOLEAN")
85 with self.assertRaises(ValidationError):
86 coldata.col("unicode", "NVARCHAR", length=32)
88 with self.assertRaises(ValidationError):
89 coldata.col("timestamp", "TIMESTAMP")
91 # DM-42257: Felis does not handle unbounded text types properly.
92 # coldata.col("text", "TEXT", length=32)
94 with self.assertRaises(ValidationError):
95 coldata.col("binary", "LONGBLOB", length=1024)
97 with self.assertRaises(ValidationError):
98 # Same type and length
99 coldata.col("string", "VARCHAR(128)", length=128)
101 # Check the old type mapping for MySQL, which is now okay
102 coldata.col("boolean", "BIT(1)")
104 # Different types, which is okay
105 coldata.col("double", "FLOAT")
107 # Same base type with different lengths, which is okay
108 coldata.col("string", "VARCHAR(128)", length=32)
110 # Different string types, which is okay
111 coldata.col("string", "CHAR", length=32)
112 coldata.col("unicode", "CHAR", length=32)
115if __name__ == "__main__": 115 ↛ 116line 115 didn't jump to line 116, because the condition on line 115 was never true
116 unittest.main()