Skip to content

Commit

Permalink
Add some unit tests to validate CI pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
danilopeixoto committed Dec 12, 2021
1 parent 9d3c13a commit 116826d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
- name: install-package
run: pip install -e .[development]
- name: check-format
run: autopep8 --recursive --exit-code --diff setup.py metastore/
run: autopep8 --recursive --exit-code --diff setup.py metastore/ tests/
- name: check-lint
run: pylint setup.py metastore/
run: pylint setup.py metastore/ tests/
test:
runs-on: ubuntu-latest
environment: production
Expand All @@ -37,9 +37,9 @@ jobs:
- name: install-package
run: pip install -e .[development]
- name: test-package
run: pytest --suppress-no-test-exit-code
run: pytest
- name: check-coverage
run: pytest --suppress-no-test-exit-code --cov --cov-fail-under 80
run: pytest --cov --cov-fail-under 80
build-publish:
runs-on: ubuntu-latest
environment: production
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ pip install -e .[development]
Format source code:

```
autopep8 --recursive --in-place setup.py metastore/
autopep8 --recursive --in-place setup.py metastore/ tests/
```

Lint source code:

```
pylint setup.py metastore/
pylint setup.py metastore/ tests/
```

Test package:
Expand All @@ -61,9 +61,11 @@ pytest
Report test coverage:

```
pytest --cov
pytest --cov --cov-fail-under 80
```

> **Note** Set the `--cov-fail-under` flag to 80% to validate the code coverage metric.
Build package:

```
Expand Down
10 changes: 7 additions & 3 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Use the `-e, --editable` flag to install the package in development mode.
Format source code:

```
autopep8 --recursive --in-place setup.py metastore/
autopep8 --recursive --in-place setup.py metastore/ tests/
```

Lint source code:

```
pylint setup.py metastore/
pylint setup.py metastore/ tests/
```

Test package:
Expand All @@ -37,7 +37,11 @@ pytest
Report test coverage:

```
pytest --cov
pytest --cov --cov-fail-under 80
```

```{note}
Set the `--cov-fail-under` flag to 80% to validate the code coverage metric.
```

Build package:
Expand Down
23 changes: 23 additions & 0 deletions metastore/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
Utility functions.
'''

from datetime import datetime, timezone


def timezone_aware(date: datetime) -> datetime:
'''
Convert naive to timezone-aware datetime (UTC timezone).
Arguments:
date (datetime): Datetime object.
Returns:
datetime: A timezone-aware datetime.
'''

return date.replace(tzinfo=timezone.utc) if date.tzinfo is None else date


__all__ = [
'timezone_aware'
]
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_package_info(path: str) -> Dict[str, str]:
Arguments:
path (str): Path to directory defining package main module.
Returns:
(Dict[str, str]) A dictionary containing package information.
Dict[str, str]: A dictionary containing package information.
'''

with open(
Expand All @@ -33,7 +33,7 @@ def parse_long_description() -> str:
Get package long description.
Returns:
(str) A string representing package long description.
str: A string representing package long description.
'''

with open(convert_path('README.md'), encoding='utf-8') as file:
Expand Down Expand Up @@ -76,11 +76,10 @@ def parse_long_description() -> str:
'pylint>=2.12.0',
'pytest>=6.2.0',
'pytest-cov>=3.0.0',
'pytest-custom-exit-code>=0.3.0',
'sphinx>=4.3.0',
'myst-parser>=0.15.0',
'pydata-sphinx-theme>=0.7.0',
'twine >= 3.7.0'
'twine>=3.7.0'
]
},
packages=find_packages(),
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
Test utility functions.
'''

from datetime import datetime, timezone

from metastore import utils


def test_timezone_aware_should_return_same_timezone_aware_datetime_with_timezone_aware_datetime_argument(): # pylint: disable=C0301
'''
Test converting naive to timezone-aware datetime (UTC timezone)
with timezone-aware datetime argument.
Raises:
AssertionError: Expected value does not match the returned value.
'''

expected_datetime = datetime.now(tz=timezone.utc)
actual_datetime = utils.timezone_aware(expected_datetime)

assert actual_datetime == expected_datetime


def test_timezone_aware_should_return_new_timezone_aware_datetime_with_naive_timezone_datetime_argument(): # pylint: disable=C0301
'''
Test converting naive to timezone-aware datetime (UTC timezone)
with naive datetime argument.
Raises:
AssertionError: Expected value does not match the returned value.
'''

input_datetime = datetime.now()
expected_datetime = input_datetime.replace(tzinfo=timezone.utc)
actual_datetime = utils.timezone_aware(input_datetime)

assert actual_datetime == expected_datetime

0 comments on commit 116826d

Please sign in to comment.