Skip to content

Issue 10 pytest #24

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

Merged
merged 2 commits into from
Mar 11, 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
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
- "3.11"
- "3.10"
- "3.9"
- "3.8"
os:
- ubuntu-latest
- macos-latest
Expand Down
36 changes: 31 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,44 @@ $ source .venv/bin/activate
$ pip install -e .[dev]
```

### Running the test
### About the tests

See README.md in the tests folder.
Tests are organized based on the structure of the [OpenAPI](https://data.oceannetworks.ca/OpenAPI) page.
Test files are named based on the API end point.
For example, `/location/tree` is tested in the test_location_tree.py file under discover_locations directory.

Before running the tests, create a `.env` file under the root directory and put TOKEN variable in the file.
If you are on Windows, make sure the encoding of `.env` file is UTF-8 after using the command below.

```shell
echo TOKEN=${YOUR_TOKEN} > .env
```

The default testing environment is PROD. If you are an internal developer,
add the following line to `.env` so that the tests are running against QA environment.

```shell
echo ONC_ENV=QA >> .env
```

If testing in PROD is desired, change ONC_ENV value from QA to PROD, or simply removing the line.

To run all tests, run

```shell
$ pytest # or pytest --cov=onc to get a coverage report
```

Or use tox

```shell
$ tox -e py310 # py38, py39, py311, py312. Make sure the specified python version is installed.
$ tox -e py310 # py39, py311, py312. Make sure the specified python version is installed.
```

To run specific tests, refer to [how to invoke pytest](https://docs.pytest.org/en/stable/how-to/usage.html#specifying-which-tests-to-run).
You can also use the built-in testing support of IDEs like [VS Code](https://code.visualstudio.com/docs/python/testing#_run-tests)
or [PyCharm](https://www.jetbrains.com/help/pycharm/pytest.html#run-pytest-test).

### Formatter

Code is formatted using [black](https://black.readthedocs.io/en/stable/) and [isort](https://pycqa.github.io/isort/).
Expand Down Expand Up @@ -135,7 +163,6 @@ The actual tox environments to run are specified in the [[gh]](https://github.co
```
[gh]
python =
3.8 = py38
3.9 = py39
3.10 = py310, format-check, lint
3.11 = py311
Expand All @@ -144,7 +171,6 @@ python =

In the config above, tox will run different set of tox environments on different python versions.

- on Python 3.8 job, tox runs `py38` environment,
- on Python 3.9 job, tox runs `py39` environment,
- on Python 3.10 job, tox runs `py310`, `format-check` and `lint` environments,
- on Python 3.11 job, tox runs `py311` environment,
Expand Down
22 changes: 9 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ version = "2.3.5"
description = "Ocean 3.0 API Python Client Library"
readme = "README.md"
authors = [
{ name="ONC Data Team", email="data@oceannetworks.ca" },
{ name = "ONC Data Team", email = "data@oceannetworks.ca" },
]
license = {file = 'LICENSE.txt'}
requires-python = ">=3.8"
license = { file = 'LICENSE.txt' }
requires-python = ">=3.9"
dependencies = [
"requests",
'python-dateutil',
Expand All @@ -33,14 +33,13 @@ Source = "https://github.com/OceanNetworksCanada/api-python-client"

[project.optional-dependencies]
dev = [
"robotframework-pabot",
"ipykernel",
"robotframework",
"python-dotenv",
"tox",
"black",
"isort",
"pytest",
"pytest-cov",
"ruff",
]

Expand All @@ -51,14 +50,11 @@ src_paths = ["onc", "tests"]
[tool.ruff]
# Reference for the rules https://docs.astral.sh/ruff/rules/
select = [
"E", "W", # pycodestyle
"F", # pyflakes
"UP", # pyupgrade
"B", # flake8-bugbear
"SIM", # flake8-simplify
]
exclude = [
"tests/robot",
"E", "W", # pycodestyle
"F", # pyflakes
"UP", # pyupgrade
"B", # flake8-bugbear
"SIM", # flake8-simplify
]

[tool.pytest.ini_options]
Expand Down
117 changes: 0 additions & 117 deletions tests/README.md

This file was deleted.

20 changes: 20 additions & 0 deletions tests/archive_file/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest


@pytest.fixture
def params_location():
return {
"locationCode": "NCBC",
"deviceCategoryCode": "BPR",
"dateFrom": "2019-11-23",
"dateTo": "2019-11-26",
"fileExtension": "txt",
"rowLimit": 80000,
"page": 1,
}


@pytest.fixture
def params_location_multiple_pages(params_location):
# rowLimit should be less than the total number of rows.
return params_location | {"rowLimit": 2}
72 changes: 72 additions & 0 deletions tests/archive_file/test_archivefile_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pytest
import requests


@pytest.fixture
def params():
return {
"deviceCode": "BPR-Folger-59",
"dateFrom": "2019-11-23",
"dateTo": "2019-11-26",
"fileExtension": "txt",
"rowLimit": 80000,
"page": 1,
}


@pytest.fixture
def params_multiple_pages(params):
# rowLimit should be less than the total number of rows.
return params | {"rowLimit": 2}


def test_invalid_param_value(requester, params):
params_invalid_param_value = params | {"deviceCode": "XYZ123"}
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
requester.getListByDevice(params_invalid_param_value)


def test_invalid_params_missing_required(requester, params):
del params["deviceCode"]
with pytest.raises(requests.HTTPError, match=r"API Error 128"):
requester.getListByDevice(params)


def test_invalid_param_name(requester, params):
params_invalid_param_name = params | {"deviceCodes": "BPR-Folger-59"}
with pytest.raises(requests.HTTPError, match=r"API Error 129"):
requester.getListByDevice(params_invalid_param_name)


def test_no_data(requester, params):
params_no_data = params | {"dateFrom": "2000-01-01", "dateTo": "2000-01-02"}
data = requester.getListByDevice(params_no_data)

assert len(data["files"]) == 0


def test_valid_params_one_page(requester, params, params_multiple_pages):
data = requester.getListByDevice(params)
data_all_pages = requester.getListByDevice(params_multiple_pages, allPages=True)

assert (
len(data["files"]) > params_multiple_pages["rowLimit"]
), "Test should return at least `rowLimit` rows."

assert data["next"] is None, "Test should return only one page."

assert (
data_all_pages["files"] == data["files"]
), "Test should concatenate rows for all pages."

assert data_all_pages["next"] is None, "Test should return only one page."


def test_valid_params_multiple_pages(requester, params_multiple_pages):
data = requester.getListByDevice(params_multiple_pages)

assert (
len(data["files"]) == params_multiple_pages["rowLimit"]
), "Test should only return `rowLimit` rows."

assert data["next"] is not None, "Test should return multiple pages."
18 changes: 18 additions & 0 deletions tests/archive_file/test_archivefile_direct_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def test_valid_params_one_page(requester, params_location, util):
data = requester.getListByLocation(params_location)
result = requester.getDirectFiles(params_location)

assert util.get_download_files_num(requester) == len(data["files"])

assert result["stats"]["fileCount"] == len(data["files"])


def test_valid_params_multiple_pages(requester, params_location_multiple_pages, util):
result = requester.getDirectFiles(params_location_multiple_pages)

assert (
util.get_download_files_num(requester)
== params_location_multiple_pages["rowLimit"]
)

assert result["stats"]["fileCount"] == params_location_multiple_pages["rowLimit"]
27 changes: 27 additions & 0 deletions tests/archive_file/test_archivefile_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
import requests


def test_invalid_param_value(requester):
with pytest.raises(requests.HTTPError, match=r"API Error 96"):
requester.getFile("FAKEFILE.XYZ")


def test_invalid_params_missing_required(requester):
with pytest.raises(requests.HTTPError, match=r"API Error 128"):
requester.getFile()


def test_valid_params(requester, util):
filename = "BPR-Folger-59_20191123T000000.000Z.txt"

requester.getFile(filename)

assert (requester.outPath / filename).exists()

with pytest.raises(FileExistsError):
requester.getFile(filename)

requester.getFile(filename, overwrite=True)

assert util.get_download_files_num(requester) == 1
Loading