Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column Mock improvements #37

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
* BigQuery column types

### Changed
* ColumnMock nullable by default

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

### Fixed
* Fixed generation of CTE names from references with hyphens
Expand Down
17 changes: 13 additions & 4 deletions src/sql_mock/bigquery/column_mocks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from typing import Any
from sql_mock.column_mocks import BaseColumnMock


class BigQueryColumnMock(BaseColumnMock):
pass


class Boolean(BigQueryColumnMock):
dtype = 'Boolean'


class Int(BigQueryColumnMock):
dtype = "Integer"

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


class Timestamp(BigQueryColumnMock):
dtype = 'Timestamp'


class Array(BigQueryColumnMock):
use_quotes_for_casting = False

def __init__(
self,
inner_dtype,
default,
nullable=False,
inner_type: BigQueryColumnMock,
default: Any,
nullable: bool=False,
) -> None:
self.dtype = f"Array<{inner_dtype}>"
self.dtype = f"Array<{inner_type.dtype}>"
super().__init__(default, nullable)
9 changes: 5 additions & 4 deletions src/sql_mock/clickhouse/column_mocks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from sql_mock.column_mocks import BaseColumnMock


Expand Down Expand Up @@ -47,9 +48,9 @@ class Array(ClickhouseColumnMock):

def __init__(
self,
inner_dtype,
default,
nullable=False,
inner_type: ClickhouseColumnMock,
default: Any,
nullable: bool=False,
) -> None:
self.dtype = f"Array({inner_dtype})"
self.dtype = f"Array({inner_type.dtype})"
super().__init__(default, nullable)
4 changes: 2 additions & 2 deletions src/sql_mock/column_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class BaseColumnMock:
"""

dtype = None
nullable = False
nullable = True
default = None
use_quotes_for_casting = True

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

Expand Down
20 changes: 10 additions & 10 deletions tests/sql_mock/bigquery/test_column_mocks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from sql_mock.bigquery.column_mocks import Array, BigQueryColumnMock, Decimal
from sql_mock.bigquery.column_mocks import Array, BigQueryColumnMock, Decimal, Int, String


def test_init_not_nullable():
def test_init_nullable():
"""
...then nullable should be False and dtype be the same as passed.
...then nullable should be True and dtype be the same as passed.
"""

class ColMock(BigQueryColumnMock):
Expand All @@ -13,22 +13,22 @@ class ColMock(BigQueryColumnMock):

assert column.default == 42
assert column.dtype == "Integer"
assert not column.nullable
assert column.nullable


def test_init_nullable():
def test_init_not_nullable():
"""
...then nullable should be True"
...then nullable should be False"
"""

class ColMock(BigQueryColumnMock):
dtype = "Integer"

column = ColMock(default=42, nullable=True)
column = ColMock(default=42, nullable=False)

assert column.default == 42
assert column.dtype == "Integer"
assert column.nullable
assert not column.nullable


class TestDecimalColumn:
Expand All @@ -49,8 +49,8 @@ def test_decimal_initialization_nullable(self):

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

assert string_array_col.dtype == "Array<String>"
assert string_array_col.default == ["a", "b"]
Expand Down
6 changes: 3 additions & 3 deletions tests/sql_mock/clickhouse/test_column_mocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sql_mock.clickhouse.column_mocks import Array, ClickhouseColumnMock, Decimal
from sql_mock.clickhouse.column_mocks import Array, ClickhouseColumnMock, Decimal, Int, String


def test_init_not_nullable():
Expand Down Expand Up @@ -49,8 +49,8 @@ def test_decimal_initialization_nullable():

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

assert string_array_col.dtype == "Nullable(Array(String))"
assert string_array_col.default == ["a", "b"]
Expand Down
14 changes: 7 additions & 7 deletions tests/sql_mock/redshift/test_column_mocks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from sql_mock.redshift.column_mocks import RedshiftColumnMock, DECIMAL


def test_init_not_nullable():
def test_init_nullable():
"""
...then nullable should be False and dtype be the same as passed.
...then nullable should be True and dtype be the same as passed.
"""

class ColMock(RedshiftColumnMock):
Expand All @@ -13,22 +13,22 @@ class ColMock(RedshiftColumnMock):

assert column.default == 42
assert column.dtype == "BIGINT"
assert not column.nullable
assert column.nullable


def test_init_nullable():
def test_init_not_nullable():
"""
...then nullable should be True"
...then nullable should be False"
"""

class ColMock(RedshiftColumnMock):
dtype = "BIGINT"

column = ColMock(default=42, nullable=True)
column = ColMock(default=42, nullable=False)

assert column.default == 42
assert column.dtype == "BIGINT"
assert column.nullable
assert not column.nullable


class TestDecimalColumn:
Expand Down
14 changes: 7 additions & 7 deletions tests/sql_mock/snowflake/test_column_mocks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from sql_mock.snowflake.column_mocks import DECIMAL, SnowflakeColumnMock


def test_init_not_nullable():
def test_init_nullable():
"""
...then nullable should be False and dtype be the same as passed.
...then nullable should be True and dtype be the same as passed.
"""

class ColMock(SnowflakeColumnMock):
Expand All @@ -13,22 +13,22 @@ class ColMock(SnowflakeColumnMock):

assert column.default == 42
assert column.dtype == "INTEGER"
assert not column.nullable
assert column.nullable


def test_init_nullable():
def test_init_not_nullable():
"""
...then nullable should be True"
...then nullable should be False"
"""

class ColMock(SnowflakeColumnMock):
dtype = "INTEGER"

column = ColMock(default=42, nullable=True)
column = ColMock(default=42, nullable=False)

assert column.default == 42
assert column.dtype == "INTEGER"
assert column.nullable
assert not column.nullable


class TestDecimalColumn:
Expand Down
14 changes: 7 additions & 7 deletions tests/sql_mock/test_column_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ def test_init_no_default_not_nullable():
BaseColumnMock(default=None, nullable=False)


def test_init_default_not_nullable():
def test_init_default_nullable():
"""
...then it should set the default value and nullable should be False.
...then it should set the default value and nullable should be True.
"""
column = BaseColumnMock(default=42)
assert column.default == 42
assert not column.nullable
assert column.nullable


def test_init_default_nullable():
def test_init_default_not_nullable():
"""
...then it should set the default value and nullable should be True.
...then it should set the default value and nullable should be False.
"""
column = BaseColumnMock(default="Hello", nullable=True)
column = BaseColumnMock(default="Hello", nullable=False)
assert column.default == "Hello"
assert column.nullable
assert not column.nullable


def test_to_sql_with_value():
Expand Down