context
Some initial functionality for the remove-ds-store project has already been developed in a non-TDD fashion. Before moving any further, I wanted to make sure to at least add some functional tests to ensure most code paths are covered without spending too much time on full unit test coverage of code that will be subject to drastic change.
testing github API calls
The ghapi package used by remove-ds-store returns its own datatypes such as AttrDict instead of a python dict and L instead of a python list. This poses a challenge for testing by mocking the library directly since it would involve creating and returning these types, not to mention it couples the tests more tightly with this specific github client library.
Using vcrpy will intercept an API request/response at the HTTP level, “record” the interactions as a YAML “cassette” and continue using them for future tests based on configurable matching parameters. The default matching behavior of ['method', 'scheme', 'host', 'port', 'path', 'query'] seems fine.
A couple of limitations are that if the API changes, this would not be caught during automated testing (no worse than the direct mock approach), also some superfluous, recording-specific data (request timestamp, remaining requests, etc) ends up being tracked under version control.
functional test cases
The first function to test is get_repo which is supposed to check for a locally cached instance of the repo and if none is found, fetch it from the API (and cache it).
Clearly it wouldn’t be a good idea to use the same cache directory for local development and testing: the tests should run deterministically against a blank slate directory. pytest tmpdir will be useful here, but looking at the existing code it might be a good idea to refactor first.
1
2
3
LOCAL_BASE = "tmp/"
LOCAL_CONTENTS = os.path.join(LOCAL_BASE, "contents/")
LOCAL_REPOS = os.path.join(LOCAL_BASE, "repos_list.json")
Using functions instead of constants obviates the need for patching each constant and allows for only mocking the base directory instead…
1
2
3
4
5
6
7
8
def get_local_base_dir():
return "tmp/"
def get_local_contents_dir():
return os.path.join(get_local_base_dir(), "contents/")
def get_local_repos_dir():
return os.path.join(get_local_base_dir(), "repos_list.json")
Some notes on the testing code that follows:
- I didn’t find the record mode options in the vcrpy docs, but there is an enum in the source which is probably better to use than the raw strings anyway.
- To ensure that the request to cassette matching is working as expected (not just passing tests by actually hitting the API again for a “new” interaction), I first set the record mode to
ONCEand then toNONE. An “empty” cassette is used to ensure that no http requests are made. - The
repo_with_ds_store_headerfixture is unfortunately coupled with therepo_tree_with_ds_store.yamlcassette in a way that relies on the fixture values corresponding to those in the cassette. Not sure what the best way to avoid this is, but didn’t want to get too hung up on it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from unittest import mock
import os
import pytest
from vcr import mode, VCR
from repos import get_repo, Repo
CASSETTES_DIR = os.path.join(os.path.dirname(__file__), 'fixtures/vcr_cassettes')
vcr = VCR(
cassette_library_dir=CASSETTES_DIR,
record_mode=mode.NONE,
)
@pytest.fixture
def mock_cache_dir(tmpdir):
with mock.patch('repos.get_local_base_dir') as mock_dir:
mock_dir.return_value = tmpdir
yield mock_dir
@pytest.fixture
def repo_with_ds_store_header():
yield {'owner': {'login': 'caged'}, 'name': 'microsis'}
@vcr.use_cassette('repo_tree_with_ds_store.yaml')
def test_get_repo_fetching_with_ds_store(mock_cache_dir, repo_with_ds_store_header):
assert len([name for name in os.listdir(mock_cache_dir())]) == 0
repo = get_repo(repo_with_ds_store_header)
assert len([name for name in os.listdir(mock_cache_dir())]) == 1
assert repo.name == repo_with_ds_store_header['name']
assert len(repo.ds_store_paths()) == 1
assert repo.has_ds_store()
assert repo.gitignore()
@vcr.use_cassette('empty.yaml')
def test_get_repo_using_cache_with_ds_store(mock_cache_dir, repo_with_ds_store_header):
with vcr.use_cassette('repo_tree_with_ds_store.yaml'):
get_repo(repo_with_ds_store_header)
assert len([name for name in os.listdir(mock_cache_dir())]) == 1
repo = get_repo(repo_with_ds_store_header)
assert len([name for name in os.listdir(mock_cache_dir())]) == 1
assert repo.name == repo_with_ds_store_header['name']
assert len(repo.ds_store_paths()) == 1
assert repo.has_ds_store()
assert repo.gitignore()