From bbd2efbab7bd53f2844a6008cc9fe97ee52ff19e Mon Sep 17 00:00:00 2001 From: Mjumbe Poe Date: Sat, 16 Jun 2018 18:42:39 -0400 Subject: [PATCH] Ensure that numeric precision is included only if not None (#796) * Ensure that numeric precision is included only if not None * Add unit test for data_type of field with empty numeric precision * Add numeric fields to the schema_tests integration tests --- dbt/schema.py | 5 +++- .../008_schema_tests_test/seed.sql | 4 ++++ test/unit/test_schema.py | 23 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/unit/test_schema.py diff --git a/dbt/schema.py b/dbt/schema.py index b66cf95a821..944ede0e0fc 100644 --- a/dbt/schema.py +++ b/dbt/schema.py @@ -57,7 +57,10 @@ def string_type(cls, size): def numeric_type(cls, dtype, size): # This could be decimal(...), numeric(...), number(...) # Just use whatever was fed in here -- don't try to get too clever - return "{}({})".format(dtype, size) + if size is None: + return dtype + else: + return "{}({})".format(dtype, size) def __repr__(self): return "".format(self.name, self.data_type) diff --git a/test/integration/008_schema_tests_test/seed.sql b/test/integration/008_schema_tests_test/seed.sql index 3de2d1a234b..8f1801504ca 100644 --- a/test/integration/008_schema_tests_test/seed.sql +++ b/test/integration/008_schema_tests_test/seed.sql @@ -3,6 +3,10 @@ create table {schema}.seed ( id INTEGER, first_name VARCHAR(11), email VARCHAR(31), + + net_worth NUMERIC(12, 2) DEFAULT '100.00', + fav_number NUMERIC DEFAULT '3.14159265', + ip_address VARCHAR(15), updated_at TIMESTAMP WITHOUT TIME ZONE ); diff --git a/test/unit/test_schema.py b/test/unit/test_schema.py new file mode 100644 index 00000000000..a7c3d94fff0 --- /dev/null +++ b/test/unit/test_schema.py @@ -0,0 +1,23 @@ +import unittest + +import dbt.schema + + +class TestNumericType(unittest.TestCase): + + def test__numeric_type(self): + col = dbt.schema.Column( + 'fieldname', + 'numeric', + numeric_size='12,2') + + self.assertEqual(col.data_type, 'numeric(12,2)') + + def test__numeric_type_with_no_precision(self): + # PostgreSQL, at least, will allow empty numeric precision + col = dbt.schema.Column( + 'fieldname', + 'numeric', + numeric_size=None) + + self.assertEqual(col.data_type, 'numeric')