Skip to content

Commit

Permalink
test: increase test coverage to 100%
Browse files Browse the repository at this point in the history
This is an effort to raise the code quality in the project by ensuring that most, if not all the code is tested. This will be the new standard moving forward.
  • Loading branch information
kennedykori committed Aug 26, 2022
1 parent 7c3456a commit 96f2911
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 38 deletions.
4 changes: 4 additions & 0 deletions app/imp/sql_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
SQLDataSource,
SQLDataSourceType,
SQLExtractMetadata,
SQLUploadChunk,
SQLUploadMetadata,
SupportedDBVendors,
)
from .exceptions import SQLDataError, SQLDataSourceDisposedError
Expand All @@ -12,5 +14,7 @@
"SQLDataSourceDisposedError",
"SQLDataSourceType",
"SQLExtractMetadata",
"SQLUploadChunk",
"SQLUploadMetadata",
"SupportedDBVendors",
]
12 changes: 6 additions & 6 deletions app/use_cases/fetch_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# =============================================================================


class DoFetchDataSourceTypeSources(Task[DataSourceType, Sequence[DataSource]]):
class DoFetchDataSources(Task[DataSourceType, Sequence[DataSource]]):
"""Fetches all the data sources of a given data source type."""

def __init__(self, data_source_type: DataSourceType):
Expand All @@ -45,7 +45,7 @@ def execute(self, an_input: Transport) -> Sequence[DataSource]:
return data_sources


class DoFetchDataSourceExtracts(Task[DataSource, Sequence[ExtractMetadata]]):
class DoFetchExtractMetadata(Task[DataSource, Sequence[ExtractMetadata]]):
"""Fetch all the extract metadata of a given data source."""

def __init__(self, data_source: DataSource):
Expand Down Expand Up @@ -98,9 +98,9 @@ def execute(
@staticmethod
def _data_source_types_to_tasks(
data_source_types: Iterable[DataSourceType],
) -> Sequence[DoFetchDataSourceTypeSources]:
) -> Sequence[DoFetchDataSources]:
return tuple(
DoFetchDataSourceTypeSources(data_source_type=_data_source_type)
DoFetchDataSources(data_source_type=_data_source_type)
for _data_source_type in data_source_types
)

Expand Down Expand Up @@ -133,8 +133,8 @@ def execute(
@staticmethod
def _data_sources_to_tasks(
data_sources: Iterable[DataSource],
) -> Sequence[DoFetchDataSourceExtracts]:
) -> Sequence[DoFetchExtractMetadata]:
return tuple(
DoFetchDataSourceExtracts(data_source=_data_source)
DoFetchExtractMetadata(data_source=_data_source)
for _data_source in data_sources
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ strictSetInference = true
typeCheckingMode = "basic"

[tool.pytest.ini_options]
addopts = "--cov=app --cov-fail-under=85 --cov-report=html --cov-report=term-missing -n auto --junitxml='junitxml_report/report.xml' -v --durations=10 --cache-clear -p no:sugar"
addopts = "--cov=app --cov-fail-under=90 --cov-report=html --cov-report=term-missing -n auto --junitxml='junitxml_report/report.xml' -v --durations=10 --cache-clear"
console_output_style = "progress"
log_cli = 1
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
Expand Down
27 changes: 21 additions & 6 deletions tests/core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,17 @@ def execute(self, an_input: Any) -> Any:


class FakeTransport(Transport):
"""A fake transport that returns empty results."""
"""A fake transport that returns dummy data."""

def __init__(self):
self._is_closed: bool = False
def __init__(
self,
is_closed: bool = False,
fetch_data_source_extracts_count: int = 0,
fetch_data_sources_count: int = 0,
):
self._is_closed: bool = is_closed
self._data_sources_count: int = fetch_data_sources_count
self._extracts_count: int = fetch_data_source_extracts_count

@property
def is_disposed(self) -> bool:
Expand All @@ -131,12 +138,16 @@ def fetch_data_source_extracts(
data_source: DataSource,
**options: TransportOptions,
) -> Sequence[ExtractMetadata]:
return tuple()
return tuple(
FakeExtractMetadataFactory.create_batch(size=self._extracts_count)
)

def fetch_data_sources(
self, data_source_type: DataSourceType, **options: TransportOptions
) -> Sequence[DataSource]:
return tuple()
return tuple(
FakeDataSourceFactory.create_batch(size=self._data_sources_count)
)

def mark_upload_as_complete(
self, upload_metadata: UploadMetadata, **options: TransportOptions
Expand Down Expand Up @@ -306,7 +317,7 @@ class FakeExtractMetadataFactory(ExtractMetadataFactory):
preferred_uploads_name = factory.LazyAttribute(
lambda _o: "%s" % _o.name.lower().replace(" ", "_")
)
data_source = factory.SubFactory(FakeDataSource)
data_source = factory.SubFactory(FakeDataSourceFactory)

class Meta:
model = FakeExtractMetadata
Expand All @@ -317,6 +328,10 @@ class FakeTransportFactory(factory.Factory):
A factory for creating fake transport instances that return empty results.
"""

is_closed: bool = False
fetch_data_source_extracts_count: int = 0
fetch_data_sources_count: int = 0

class Meta:
model = FakeTransport

Expand Down
160 changes: 139 additions & 21 deletions tests/core/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
FakeDataSourceType,
FakeDataSourceTypeFactory,
FakeExtractMetadata,
FakeExtractMetadataFactory,
FakeUploadChunk,
FakeUploadMetadata,
)


Expand Down Expand Up @@ -89,21 +92,9 @@ def test_get_required_fields_class_method(self) -> None:
)


class TestFakeDataSourceInterface(TestCase):
class TestDataSourceInterface(TestCase):
"""Tests for the ``DataSource`` interface default implementations."""

def test_string_representation(self) -> None:
"""
Assert that the default ``DataSource.__str__()`` implementation
returns the expected value.
"""
data_source = FakeDataSource(
id="1",
name="Some data source",
data_source_type=FakeDataSourceTypeFactory(),
)
assert str(data_source) == "1::Some data source"

def test_of_mapping_class_method(self) -> None:
"""
Assert that the ``DataSource.of_mapping()`` class method returns
Expand Down Expand Up @@ -136,8 +127,20 @@ def test_of_mapping_class_method(self) -> None:
assert data_source2.name == "Some other data source"
assert data_source2.description is None

def test_string_representation(self) -> None:
"""
Assert that the default ``DataSource.__str__()`` implementation
returns the expected value.
"""
data_source = FakeDataSource(
id="1",
name="Some data source",
data_source_type=FakeDataSourceTypeFactory(),
)
assert str(data_source) == "1::Some data source"


class TestFakeDataSourceTypeInterface(TestCase):
class TestDataSourceTypeInterface(TestCase):
"""Tests for the ``DataSourceType`` interface default implementations."""

def test_string_representation(self) -> None:
Expand All @@ -152,15 +155,20 @@ def test_string_representation(self) -> None:
class TestExtractMetadataInterface(TestCase):
"""Tests for the ``ExtractMetadata`` interface default implementations."""

def test_string_representation(self) -> None:
"""
Assert that the default ``ExtractMetadata.__str__()`` implementation
returns the expected value.
"""
extract = FakeExtractMetadata(
def setUp(self) -> None:
super().setUp()
self._extract = FakeExtractMetadata(
id="1", name="Some data", data_source=FakeDataSourceFactory()
)
assert str(extract) == "1::Some data"

def test_get_upload_meta_extra_init_kwargs(self) -> None:
"""
Assert that the default implementation of
``ExtractMetadata.get_upload_meta_extra_init_kwargs()`` instance
method returns ``None``.
"""

assert self._extract.get_upload_meta_extra_init_kwargs() is None

def test_of_mapping_class_method(self) -> None:
"""
Expand Down Expand Up @@ -195,3 +203,113 @@ def test_of_mapping_class_method(self) -> None:
assert extract2.name == "Some other data"
assert extract2.description is None
assert extract2.preferred_uploads_name == "some_data"

def test_string_representation(self) -> None:
"""
Assert that the default ``ExtractMetadata.__str__()`` implementation
returns the expected value.
"""
assert str(self._extract) == "1::Some data"


class TestUploadChunkInterface(TestCase):
"""Tests for the ``UploadChunk`` interface default implementations."""

def setUp(self) -> None:
super().setUp()
self._upload_chunk = FakeUploadChunk(
id="1", chunk_index=0, chunk_content=b"Bla bla bla ..."
)

def test_of_mapping_class_method(self) -> None:
"""
Assert that the ``UploadChunk.of_mapping()`` class method returns
the expected value.
"""

upload_chunk1 = FakeUploadChunk.of_mapping(
{"id": "1", "chunk_index": 0, "chunk_content": b"Bla bla bla ..."}
)
upload_chunk2 = FakeUploadChunk.of_mapping(
{
"id": "2",
"chunk_index": 1,
"chunk_content": b"Bla bla bla bla ...",
}
)

assert upload_chunk1 is not None
assert upload_chunk1.id == "1"
assert upload_chunk1.chunk_index == 0
assert upload_chunk1.chunk_content == b"Bla bla bla ..."
assert upload_chunk2 is not None
assert upload_chunk2.id == "2"
assert upload_chunk2.chunk_index == 1
assert upload_chunk2.chunk_content == b"Bla bla bla bla ..."

def test_string_representation(self) -> None:
"""
Assert that the default ``UploadChunk.__str__()`` implementation
returns the expected value.
"""
assert str(self._upload_chunk) == "Chunk 0"


class TestUploadMetadataInterface(TestCase):
"""Tests for the ``UploadMetadata`` interface default implementations."""

def setUp(self) -> None:
super().setUp()
self._extract_metadata = FakeExtractMetadataFactory()
self._upload_metadata = FakeUploadMetadata(
id="1",
org_unit_code="12345",
org_unit_name="Test Facility",
content_type="application/json",
extract_metadata=self._extract_metadata,
)

def test_get_upload_meta_extra_init_kwargs(self) -> None:
"""
Assert that the default implementation of
``UploadMetadata.get_upload_chunk_extra_init_kwargs()`` instance
method returns ``None``.
"""

kwargs = self._upload_metadata.get_upload_chunk_extra_init_kwargs()
assert kwargs is None

def test_of_mapping_class_method(self) -> None:
"""
Assert that the ``UploadMetadata.of_mapping()`` class method returns
the expected value.
"""

org_unit_code = "12345"
org_unit_name = "Test Facility"
content_type = "application/json"
upload_metadata = FakeUploadMetadata.of_mapping(
{
"id": "1",
"org_unit_code": org_unit_code,
"org_unit_name": org_unit_name,
"content_type": content_type,
"extract_metadata": self._extract_metadata,
}
)

assert upload_metadata is not None
assert upload_metadata.id == "1"
assert upload_metadata.org_unit_code == org_unit_code
assert upload_metadata.org_unit_name == org_unit_name
assert upload_metadata.content_type == content_type
assert upload_metadata.extract_metadata == self._extract_metadata

def test_string_representation(self) -> None:
"""
Assert that the default ``UploadMetadata.__str__()`` implementation
returns the expected value.
"""
assert str(self._upload_metadata) == "Upload {} for extract {}".format(
self._upload_metadata.id, str(self._extract_metadata)
)
20 changes: 20 additions & 0 deletions tests/imp/sql_data/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
SQLDataSource,
SQLDataSourceType,
SQLExtractMetadata,
SQLUploadChunk,
SQLUploadMetadata,
SupportedDBVendors,
)
from tests.core.factories import (
DataSourceFactory,
DataSourceTypeFactory,
ExtractMetadataFactory,
UploadChunkFactory,
UploadMetadataFactory,
)


Expand Down Expand Up @@ -85,3 +89,19 @@ class SQLExtractMetadataFactory(ExtractMetadataFactory):

class Meta:
model = SQLExtractMetadata


class SQLUploadChunkFactory(UploadChunkFactory):
"""A factory for ``SQLUploadChunk`` instances."""

class Meta:
model = SQLUploadChunk


class SQLUploadMetadataFactory(UploadMetadataFactory):
"""A factory for ``SQLUploadMetadata`` instances."""

extract_metadata = factory.SubFactory(SQLExtractMetadataFactory)

class Meta:
model = SQLUploadMetadata
Loading

0 comments on commit 96f2911

Please sign in to comment.