curds: Create a single place to get a path to a data file

It's useful to have a way to override where data gets placed in the
filesystem so we don't accidentally clobber production data with test
data while running tests.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-02-12 11:54:46 -05:00
parent 0705f86d81
commit 2c022163ad
3 changed files with 44 additions and 1 deletions

View File

@ -8,4 +8,4 @@ clean:
.PHONY: trier
trier:
python trier/generate_tracks.py
python -m unittest discover -v curds/
EMMENTAL_TESTING=1 python -m unittest discover -v curds/

12
curds/data.py Normal file
View File

@ -0,0 +1,12 @@
# Copyright 2019 (c) Anna Schumaker.
import os
import xdg.BaseDirectory
__resource = "emmental"
if os.environ.get("EMMENTAL_TESTING"):
__resource = "emmental-testing"
emmental_data = xdg.BaseDirectory.save_data_path(__resource)
def data_file_path(filename):
return os.path.join(emmental_data, filename)

31
curds/test_data.py Normal file
View File

@ -0,0 +1,31 @@
# Copyright 2019 (c) Anna Schumaker.
import data
import os
import unittest
import xdg.BaseDirectory
xdg_data_home = xdg.BaseDirectory.xdg_data_home
testing_data = os.path.join(xdg_data_home, "emmental-testing")
class Test:
def __init__(self):
self.one = 1
self.two = 2
self.list = [ 3, 4, 5]
class TestDataModule(unittest.TestCase):
def setUp(self):
path = os.path.join(testing_data, "test")
if os.path.exists(path):
os.remove(path)
def test_dir(self):
self.assertEqual(data.emmental_data, testing_data)
self.assertTrue(os.path.exists(testing_data))
self.assertTrue(os.path.isdir(testing_data))
def test_data_file_path(self):
path = os.path.join(testing_data, "test")
self.assertEqual(data.data_file_path("test"), path)
self.assertFalse(os.path.exists(path))