Skip to content

Commit 52c9dc3

Browse files
authored
Column Mock improvements (#37)
* made column mock nullable by default * array inner types use other mock columns * added BigQuery mock columns
1 parent 166f031 commit 52c9dc3

File tree

9 files changed

+57
-44
lines changed

9 files changed

+57
-44
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
## [Unreleased]
1313

1414
### Added
15+
* BigQuery column types
1516

1617
### Changed
18+
* ColumnMock nullable by default
1719

1820
### Breaking Changes
1921
* Path to dbt project.yml file is provided instead of manifest.json
22+
* Array types use other ColumnMock classes as inner type
2023

2124
### Fixed
2225
* Fixed generation of CTE names from references with hyphens

src/sql_mock/bigquery/column_mocks.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
from typing import Any
12
from sql_mock.column_mocks import BaseColumnMock
23

34

45
class BigQueryColumnMock(BaseColumnMock):
56
pass
67

78

9+
class Boolean(BigQueryColumnMock):
10+
dtype = 'Boolean'
11+
12+
813
class Int(BigQueryColumnMock):
914
dtype = "Integer"
1015

@@ -27,14 +32,18 @@ def __init__(self, default, precision, scale, nullable=False) -> None:
2732
super().__init__(default, nullable)
2833

2934

35+
class Timestamp(BigQueryColumnMock):
36+
dtype = 'Timestamp'
37+
38+
3039
class Array(BigQueryColumnMock):
3140
use_quotes_for_casting = False
3241

3342
def __init__(
3443
self,
35-
inner_dtype,
36-
default,
37-
nullable=False,
44+
inner_type: BigQueryColumnMock,
45+
default: Any,
46+
nullable: bool=False,
3847
) -> None:
39-
self.dtype = f"Array<{inner_dtype}>"
48+
self.dtype = f"Array<{inner_type.dtype}>"
4049
super().__init__(default, nullable)

src/sql_mock/clickhouse/column_mocks.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Any
12
from sql_mock.column_mocks import BaseColumnMock
23

34

@@ -47,9 +48,9 @@ class Array(ClickhouseColumnMock):
4748

4849
def __init__(
4950
self,
50-
inner_dtype,
51-
default,
52-
nullable=False,
51+
inner_type: ClickhouseColumnMock,
52+
default: Any,
53+
nullable: bool=False,
5354
) -> None:
54-
self.dtype = f"Array({inner_dtype})"
55+
self.dtype = f"Array({inner_type.dtype})"
5556
super().__init__(default, nullable)

src/sql_mock/column_mocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class BaseColumnMock:
1414
"""
1515

1616
dtype = None
17-
nullable = False
17+
nullable = True
1818
default = None
1919
use_quotes_for_casting = True
2020

21-
def __init__(self, default=None, nullable=False) -> None:
21+
def __init__(self, default=None, nullable=True) -> None:
2222
"""
2323
Initialize a BaseColumnMock instance.
2424

tests/sql_mock/bigquery/test_column_mocks.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from sql_mock.bigquery.column_mocks import Array, BigQueryColumnMock, Decimal
1+
from sql_mock.bigquery.column_mocks import Array, BigQueryColumnMock, Decimal, Int, String
22

33

4-
def test_init_not_nullable():
4+
def test_init_nullable():
55
"""
6-
...then nullable should be False and dtype be the same as passed.
6+
...then nullable should be True and dtype be the same as passed.
77
"""
88

99
class ColMock(BigQueryColumnMock):
@@ -13,22 +13,22 @@ class ColMock(BigQueryColumnMock):
1313

1414
assert column.default == 42
1515
assert column.dtype == "Integer"
16-
assert not column.nullable
16+
assert column.nullable
1717

1818

19-
def test_init_nullable():
19+
def test_init_not_nullable():
2020
"""
21-
...then nullable should be True"
21+
...then nullable should be False"
2222
"""
2323

2424
class ColMock(BigQueryColumnMock):
2525
dtype = "Integer"
2626

27-
column = ColMock(default=42, nullable=True)
27+
column = ColMock(default=42, nullable=False)
2828

2929
assert column.default == 42
3030
assert column.dtype == "Integer"
31-
assert column.nullable
31+
assert not column.nullable
3232

3333

3434
class TestDecimalColumn:
@@ -49,8 +49,8 @@ def test_decimal_initialization_nullable(self):
4949

5050
def test_array_column_inner_dtype():
5151
"""Ensure that the inner dtype is processed correctly"""
52-
string_array_col = Array(inner_dtype="String", default=["a", "b"], nullable=True)
53-
int_array_col = Array(inner_dtype="Integer", default=[1, 2], nullable=False)
52+
string_array_col = Array(inner_type=String, default=["a", "b"], nullable=True)
53+
int_array_col = Array(inner_type=Int, default=[1, 2], nullable=False)
5454

5555
assert string_array_col.dtype == "Array<String>"
5656
assert string_array_col.default == ["a", "b"]

tests/sql_mock/clickhouse/test_column_mocks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from sql_mock.clickhouse.column_mocks import Array, ClickhouseColumnMock, Decimal
1+
from sql_mock.clickhouse.column_mocks import Array, ClickhouseColumnMock, Decimal, Int, String
22

33

44
def test_init_not_nullable():
@@ -49,8 +49,8 @@ def test_decimal_initialization_nullable():
4949

5050
def test_array_column_inner_dtype():
5151
"""Ensure that the inner dtype is processed correctly"""
52-
string_array_col = Array(inner_dtype="String", default=["a", "b"], nullable=True)
53-
int_array_col = Array(inner_dtype="Integer", default=[1, 2], nullable=False)
52+
string_array_col = Array(inner_type=String, default=["a", "b"], nullable=True)
53+
int_array_col = Array(inner_type=Int, default=[1, 2], nullable=False)
5454

5555
assert string_array_col.dtype == "Nullable(Array(String))"
5656
assert string_array_col.default == ["a", "b"]

tests/sql_mock/redshift/test_column_mocks.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from sql_mock.redshift.column_mocks import RedshiftColumnMock, DECIMAL
22

33

4-
def test_init_not_nullable():
4+
def test_init_nullable():
55
"""
6-
...then nullable should be False and dtype be the same as passed.
6+
...then nullable should be True and dtype be the same as passed.
77
"""
88

99
class ColMock(RedshiftColumnMock):
@@ -13,22 +13,22 @@ class ColMock(RedshiftColumnMock):
1313

1414
assert column.default == 42
1515
assert column.dtype == "BIGINT"
16-
assert not column.nullable
16+
assert column.nullable
1717

1818

19-
def test_init_nullable():
19+
def test_init_not_nullable():
2020
"""
21-
...then nullable should be True"
21+
...then nullable should be False"
2222
"""
2323

2424
class ColMock(RedshiftColumnMock):
2525
dtype = "BIGINT"
2626

27-
column = ColMock(default=42, nullable=True)
27+
column = ColMock(default=42, nullable=False)
2828

2929
assert column.default == 42
3030
assert column.dtype == "BIGINT"
31-
assert column.nullable
31+
assert not column.nullable
3232

3333

3434
class TestDecimalColumn:

tests/sql_mock/snowflake/test_column_mocks.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from sql_mock.snowflake.column_mocks import DECIMAL, SnowflakeColumnMock
22

33

4-
def test_init_not_nullable():
4+
def test_init_nullable():
55
"""
6-
...then nullable should be False and dtype be the same as passed.
6+
...then nullable should be True and dtype be the same as passed.
77
"""
88

99
class ColMock(SnowflakeColumnMock):
@@ -13,22 +13,22 @@ class ColMock(SnowflakeColumnMock):
1313

1414
assert column.default == 42
1515
assert column.dtype == "INTEGER"
16-
assert not column.nullable
16+
assert column.nullable
1717

1818

19-
def test_init_nullable():
19+
def test_init_not_nullable():
2020
"""
21-
...then nullable should be True"
21+
...then nullable should be False"
2222
"""
2323

2424
class ColMock(SnowflakeColumnMock):
2525
dtype = "INTEGER"
2626

27-
column = ColMock(default=42, nullable=True)
27+
column = ColMock(default=42, nullable=False)
2828

2929
assert column.default == 42
3030
assert column.dtype == "INTEGER"
31-
assert column.nullable
31+
assert not column.nullable
3232

3333

3434
class TestDecimalColumn:

tests/sql_mock/test_column_mocks.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ def test_init_no_default_not_nullable():
1111
BaseColumnMock(default=None, nullable=False)
1212

1313

14-
def test_init_default_not_nullable():
14+
def test_init_default_nullable():
1515
"""
16-
...then it should set the default value and nullable should be False.
16+
...then it should set the default value and nullable should be True.
1717
"""
1818
column = BaseColumnMock(default=42)
1919
assert column.default == 42
20-
assert not column.nullable
20+
assert column.nullable
2121

2222

23-
def test_init_default_nullable():
23+
def test_init_default_not_nullable():
2424
"""
25-
...then it should set the default value and nullable should be True.
25+
...then it should set the default value and nullable should be False.
2626
"""
27-
column = BaseColumnMock(default="Hello", nullable=True)
27+
column = BaseColumnMock(default="Hello", nullable=False)
2828
assert column.default == "Hello"
29-
assert column.nullable
29+
assert not column.nullable
3030

3131

3232
def test_to_sql_with_value():

0 commit comments

Comments
 (0)