~tieong/download-course

613f7142de29fcab0cd5e2aa5c74f79ab0c7f690 — Thomas Ieong 2 years ago ffddff5
Added some tests
4 files changed, 92 insertions(+), 0 deletions(-)

A tests/__init__.py
M tests/conftest.py
A tests/unit/__init__.py
A tests/unit/test_config_json.py
A tests/__init__.py => tests/__init__.py +0 -0
M tests/conftest.py => tests/conftest.py +16 -0
@@ 1,1 1,17 @@
#!/usr/bin/python3

import pytest
import json


@pytest.fixture
def tmp_json(tmp_path):
    json_path = tmp_path / "REPLACE_ME-courses.json"
    return json_path


@pytest.fixture
def tmp_json_populated(tmp_json):
    test_string = '{"20442.pdf" : "2457524dsg54gsdsdg5"}'
    tmp_json.write_text(test_string, encoding='utf-8')
    return tmp_json, json.loads(test_string)

A tests/unit/__init__.py => tests/unit/__init__.py +0 -0
A tests/unit/test_config_json.py => tests/unit/test_config_json.py +76 -0
@@ 0,0 1,76 @@
#!/usr/bin/python3

import pytest
import json

from download_course.utils.config_json import (
    init_config,
    read_config,
    update_config,
    update_etag
)

@pytest.mark.parametrize("json_exists", [True, False])
def test_init_config(tmp_json, monkeypatch, json_exists):
    """If the json doesn't exists init_config should create it"""
    monkeypatch.setattr(
        "download_course.utils.config_json.STORAGE_FILE",
        tmp_json
    )
    test_string = "Hello!"
    if json_exists:
        tmp_json.write_text(test_string, encoding='utf-8')
    init_config()
    if json_exists:
        assert tmp_json.read_text(encoding='utf-8') == test_string
    else:
        assert tmp_json.read_text(encoding='utf-8') == '{}'
    assert tmp_json.exists() is True

def test_read_config(tmp_json_populated, monkeypatch):
    monkeypatch.setattr(
        "download_course.utils.config_json.STORAGE_FILE",
        tmp_json_populated[0]
    )
    config = read_config()
    assert config == tmp_json_populated[1]


@pytest.mark.parametrize(
    "json_dict",
    [
        {
            "foo.pdf": "3dfg5543q",
            "bar.pdf": "3sfdf5543q",
            "baz.pdf": "3df55qd43q",
        },
        {"bar.csv": "jumhjmh646fq"}
    ]
)
def test_update_config(tmp_json_populated, monkeypatch, json_dict):
    monkeypatch.setattr(
        "download_course.utils.config_json.STORAGE_FILE",
        tmp_json_populated[0]
    )
    update_config(json_dict)
    config = json.loads(tmp_json_populated[0].read_text(encoding='utf-8'))
    assert len(config) == len(json_dict) + 1 # tmp_json_populated is one
    for key in json_dict:
        assert key in config


@pytest.mark.parametrize(
    "file_name, etag",
    [
        ("20442.pdf", "jumhjmh646fq")
    ]
)
def test_update_etag(tmp_json_populated, file_name, etag, monkeypatch):
    monkeypatch.setattr(
        "download_course.utils.config_json.STORAGE_FILE",
        tmp_json_populated[0]
    )
    update_etag(file_name, etag)
    config = tmp_json_populated[0]
    json_config = json.loads(config.read_text(encoding='utf-8'))
    assert json_config["20442.pdf"] == "jumhjmh646fq"