Skip to content
Snippets Groups Projects
Commit a1666980 authored by Florian Bruhin's avatar Florian Bruhin
Browse files

Add first tests

parent 251791b6
No related branches found
No related tags found
No related merge requests found
__pycache__/
/.coverage
/cov.xml
This diff is collapsed.
......@@ -17,6 +17,12 @@ submit = 'taas_client.cli:app'
[tool.poetry.group.dev.dependencies]
pytest = "^7.1.3"
black = "^22.8.0"
pytest-httpx = "^0.21.0"
pytest-golden = "^0.2.2"
mypy = "^0.982"
flake8 = "^5.0.4"
pytest-cov = "^4.0.0"
time-machine = "^2.8.2"
[build-system]
requires = ["poetry-core"]
......
[pytest]
addopts = --strict-markers --strict-config --cov --cov-report=html --cov-report=xml:cov.xml --cov-report=term
#--cov-fail-under=100
xfail_strict = true
# pytest-golden
enable_assertion_pass_hook = true
input:
tests:
error: []
failed: []
passed: []
skipped: []
xfailed: []
xpassed: []
status: 0
commit: abcdef
output: |
Last change: 0 seconds ago
📫 ✔ Submission successful! (2022-10-12 13:37:00, abcdef)
import os
import textwrap
import pathlib
import datetime
import pytest
from pytest_httpx import HTTPXMock
from pytest_golden.plugin import GoldenTestFixture
from typer.testing import CliRunner
from time_machine import TimeMachineFixture
from taas_client import cli
runner = CliRunner()
FROZEN_TIME = datetime.datetime(2022, 10, 12, 13, 37).astimezone()
class TestArgumentParsing:
def test_no_args(self) -> None:
result = runner.invoke(cli.app, [])
print(result.stdout)
expected = """
Usage: upload-file [OPTIONS] NOTEBOOK [URL]
Try 'upload-file --help' for help.
╭─ Error ──────────────────────────────────────────────────────────────────────╮
│ Missing argument 'NOTEBOOK'. │
╰──────────────────────────────────────────────────────────────────────────────╯
"""
assert result.exit_code == 2
assert result.stdout == textwrap.dedent(expected).lstrip("\n")
def test_inexistent_file(self, tmp_path: pathlib.Path) -> None:
nb_path = tmp_path / "nb.ipynb"
result = runner.invoke(cli.app, [str(nb_path)])
print(result.stdout)
assert result.exit_code == 2
# Box drawing changes depending on the content length...
for line in [
"Usage: upload-file [OPTIONS] NOTEBOOK [URL]",
"Try 'upload-file --help' for help.",
"╭─ Error ──",
"│ Invalid value for 'NOTEBOOK': File",
f"'{nb_path}' does not",
"│ exist.",
"╰──",
]:
assert line in result.stdout
def test_dir(self, tmp_path: pathlib.Path) -> None:
nb_path = tmp_path / "nb.ipynb"
nb_path.mkdir()
result = runner.invoke(cli.app, [str(nb_path)])
print(result.stdout)
assert result.exit_code == 2
# Box drawing changes depending on the content length...
for line in [
"Usage: upload-file [OPTIONS] NOTEBOOK [URL]",
"Try 'upload-file --help' for help.",
"╭─ Error ──",
"│ Invalid value for 'NOTEBOOK': File",
f"'{nb_path}' is a directory.",
"╰──",
]:
assert line in result.stdout
def test_unreadable(self, tmp_path: pathlib.Path) -> None:
nb_path = tmp_path / "nb.ipynb"
nb_path.touch()
nb_path.chmod(0)
if os.access(nb_path, os.R_OK):
pytest.skip("failed to make file unreadable")
result = runner.invoke(cli.app, [str(nb_path)])
print(result.stdout)
assert result.exit_code == 2
# Box drawing changes depending on the content length...
for line in [
"Usage: upload-file [OPTIONS] NOTEBOOK [URL]",
"Try 'upload-file --help' for help.",
"╭─ Error ──",
"│ Invalid value for 'NOTEBOOK': File",
f"'{nb_path}' is not",
"│ readable.",
"╰──",
]:
assert line in result.stdout
def test_wrong_location(tmp_path: pathlib.Path) -> None:
nb_path = tmp_path / "nb.ipynb"
nb_path.touch()
result = runner.invoke(cli.app, [str(nb_path)])
print(result.stdout)
expected = f"""
Refusing to submit file from unexpected location:
{nb_path}
Make sure you're not accidentally working in the 'Originale' folder!
"""
assert result.exit_code == 1
assert result.stdout == textwrap.dedent(expected).lstrip("\n")
@pytest.fixture
def nb_arg(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> str:
"""Set up a valid notebook at the proper location."""
monkeypatch.setenv("HOME", str(tmp_path)) # POSIX
monkeypatch.setenv("USERPROFILE", str(tmp_path)) # Windows
lab_dir = tmp_path / "work" / "AutPy" / "1-getting-started" / "01-python-basic"
lab_dir.mkdir(parents=True)
lab_path = lab_dir / "python-basics.ipynb"
lab_path.touch()
ts = FROZEN_TIME.timestamp()
os.utime(lab_path, (ts, ts)) # atime, mtime
return str(lab_path)
@pytest.mark.golden_test("golden/*.yml")
def test_golden(golden: GoldenTestFixture, httpx_mock: HTTPXMock, time_machine: TimeMachineFixture, nb_arg: str) -> None:
httpx_mock.add_response(json=golden["input"]) # FIXME add URL?
time_machine.move_to(FROZEN_TIME)
result = runner.invoke(cli.app, [nb_arg])
print(result.stdout)
assert result.exit_code == 0
assert result.stdout == golden.out["output"]
from taas_client import __version__
def test_version():
assert __version__ == "0.2.0"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment