Skip to content
Snippets Groups Projects
Verified Commit 4cbff6df authored by Daniel Hornung's avatar Daniel Hornung
Browse files

TEST: Additional column type conversion checks.

parent c7b108bd
Branches
Tags
2 merge requests!89ENH: JsonSchemaExporter accepts do_not_create parameter.,!77F fix strict values in table columns
Pipeline #40251 passed with warnings
...@@ -44,7 +44,7 @@ from test_utils import BaseMockUpTest ...@@ -44,7 +44,7 @@ from test_utils import BaseMockUpTest
# For testing the table importer # For testing the table importer
IMPORTER_KWARGS = dict( IMPORTER_KWARGS = dict(
converters={'c': float, 'd': yes_no_converter, 'x': float}, # x does not exist converters={'c': float, 'd': yes_no_converter, 'x': float}, # x does not exist
datatypes={'a': str, 'b': int, 'x': int}, # x does not exist datatypes={'a': str, 'b': int, 'float': float, 'x': int}, # x does not exist
obligatory_columns=['a', 'b'], unique_keys=[('a', 'b')], obligatory_columns=['a', 'b'], unique_keys=[('a', 'b')],
existing_columns=['e'], existing_columns=['e'],
) )
...@@ -192,17 +192,29 @@ class TableImporterTest(unittest.TestCase): ...@@ -192,17 +192,29 @@ class TableImporterTest(unittest.TestCase):
def test_wrong_datatype(self): def test_wrong_datatype(self):
importer = TableImporter(**self.importer_kwargs) importer = TableImporter(**self.importer_kwargs)
df = pd.DataFrame([[None, 0, 2.0, 'yes'], df = pd.DataFrame([[1234, 0, 2.0, 3, 'yes'],
[5, 1, 2.0, 'yes']], [5678, 1, 2.0, 3, 'yes']],
columns=['a', 'b', 'c', 'd']) columns=['a', 'b', 'c', 'float', 'd'])
# wrong datatypes before
assert df["a"].dtype == int
assert df["float"].dtype == int
# strict = False by default, so this shouldn't raise an error # strict = False by default, so this shouldn't raise an error
importer.check_datatype(df) importer.check_datatype(df)
# The types should be correct now.
assert df["a"].dtype == pd.StringDtype
assert df["float"].dtype == float
# Reset since check_datatype changes datatypes # Resetting `df` since check_datatype may change datatypes
df = pd.DataFrame([[None, 0, 2.0, 'yes'], df = pd.DataFrame([[None, 0, 2.0, 'yes'],
[5, 1, 2.0, 'yes']], [5, 1, 2.0, 'yes']],
columns=['a', 'b', 'c', 'd']) columns=['a', 'b', 'c', 'd'])
# strict=True, so int in str column raises an error # strict=True, so number in str column raises an error
self.assertRaises(DataInconsistencyError, importer.check_datatype, df, None, True)
df = pd.DataFrame([[0],
[1]],
columns=['float'])
# strict=True, so int in float column raises an error
self.assertRaises(DataInconsistencyError, importer.check_datatype, df, None, True) self.assertRaises(DataInconsistencyError, importer.check_datatype, df, None, True)
# This is always wrong (float in int column) # This is always wrong (float in int column)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment