Skip to content

Commit

Permalink
main: tests for ColumnMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
balinthaller committed Dec 10, 2020
1 parent b9c4bb3 commit d5505e8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
9 changes: 5 additions & 4 deletions flyover/model/carte_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def get_description_text(description: DatabuilderDescription):
return description._source
return description._text


class ColumnType(Enum):
Expand Down Expand Up @@ -49,8 +49,8 @@ def from_databuilder(cls, column: DatabuilderColumnMetadata):
def from_frontmatter(cls, meta_dict):
return cls(
name=meta_dict["name"],
column_type=meta_dict["type"],
description=meta_dict["description"],
column_type=meta_dict.get("type"),
description=meta_dict.get("description"),
)

def to_frontmatter(self):
Expand All @@ -67,6 +67,7 @@ def __repr__(self) -> str:
self.description,
)


class TableMetadata:
def __init__(
self,
Expand Down Expand Up @@ -112,7 +113,7 @@ def from_frontmatter(cls, metadata, content):
]

return cls(
name=metadata.get("title"),
name=metadata["title"],
database=metadata.get("database", None),
description=content,
location=metadata.get("location", None),
Expand Down
103 changes: 103 additions & 0 deletions tests/model/test_carte_table_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import unittest
from unittest.mock import patch

from flyover.model.carte_table_model import TableMetadata, ColumnMetadata, TableType
from databuilder.models.table_metadata import ColumnMetadata as DatabuilderColumn
from databuilder.models.table_metadata import DescriptionMetadata as DatabuilderDescription
from databuilder.models.table_metadata import TableMetadata as DatabuilderTable



class TestColumnMetadata(unittest.TestCase):
def test_from_databuilder(self):
source_metadata = DatabuilderColumn(
"test-name",
"test-description",
"test-type",
1
)

result = ColumnMetadata.from_databuilder(source_metadata)

assert result.name == "test-name"
assert result.column_type == "test-type"
assert result.description == "test-description"

def test_from_frontmatter(self):
source_metadata = {
"name": "test-name",
"description": "test-description",
"type": "test-type"
}

result = ColumnMetadata.from_frontmatter(source_metadata)

assert result.name == "test-name"
assert result.description == "test-description"
assert result.column_type == "test-type"

def test_from_frontmatter_no_values(self):
source_metadata = {
"name": "test-name"
}

result = ColumnMetadata.from_frontmatter(source_metadata)

assert result.name == "test-name"
assert result.description is None
assert result.column_type is None

def test_from_frontmatter_raises_with_no_name(self):
self.assertRaises(KeyError, ColumnMetadata.from_frontmatter, {})

def test_to_frontmatter(self):
source = ColumnMetadata(
name="test-name",
column_type="test-type",
description="test-description"
)

result = source.to_frontmatter()

assert result == {
"name": "test-name",
"type": "test-type",
"description": "test-description"
}

class TestTableMetadata(unittest.TestCase):
def test_from_databuilder(self):
source_metadata = DatabuilderTable(
"test-connection",
"test-cluster",
"test-db",
"test-name",
"test-description",
[
DatabuilderColumn("test-col-1", "test-descr1", "test-type1", 1),
DatabuilderColumn("test-col-2", "test-descr2", "test-type2", 2),
],
False
)

result = TableMetadata.from_databuilder(source_metadata)

assert result.database == "test-db"
assert result.location == "test-connection://test-cluster.test-db/test-name"
assert result.connection == "test-connection"
assert len(result.columns) == 2
assert result.columns[0].__repr__() == ColumnMetadata(name="test-col-1", description="test-descr1", column_type="test-type1").__repr__()
assert result.columns[1].__repr__() == ColumnMetadata(name="test-col-2", description="test-descr2", column_type="test-type2").__repr__()
assert result.table_type == TableType.TABLE

def test_from_frontmatter(self):
pass

def test_from_frontmatter_no_values(self):
pass

def test_from_frontmatter_raises_with_no_name(self):
pass

def test_to_frontmatter(self):
pass
2 changes: 1 addition & 1 deletion tests/utils/test_frontmatter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import unittest
from unittest.mock import patch, create_autospec, mock_open
from unittest.mock import patch, mock_open
import pytest

import flyover.utils.frontmatter as frontmatter
Expand Down

0 comments on commit d5505e8

Please sign in to comment.