Skip to content

Commit 5fec044

Browse files
authored
Merge pull request #6 from danlooo/add-unit-tests
Add unit tests
2 parents a74504c + 7c35f4e commit 5fec044

File tree

8 files changed

+120
-47
lines changed

8 files changed

+120
-47
lines changed

.github/workflows/pylint.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/workflows/python-lint.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Lint Python
2+
3+
on: [push]
4+
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
python-version: ["3.12"]
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up Python ${{ matrix.python-version }}
14+
uses: actions/setup-python@v3
15+
with:
16+
python-version: ${{ matrix.python-version }}
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install pylint
21+
- name: Analysing the code with pylint
22+
run: |
23+
pylint $(git ls-files '*.py')

.github/workflows/python-test.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Test Python
2+
3+
on: [push]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
python-version: ["3.12"]
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up Python ${{ matrix.python-version }}
14+
uses: actions/setup-python@v3
15+
with:
16+
python-version: ${{ matrix.python-version }}
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install pathtraits
21+
- name: Unit tests
22+
run: |
23+
python -m unittest discover

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"python.testing.unittestArgs": [
3+
"-v",
4+
"-s",
5+
"./test",
6+
"-p",
7+
"*test.py"
8+
],
9+
"python.testing.pytestEnabled": false,
10+
"python.testing.unittestEnabled": true
11+
}

pathtraits/access.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,8 @@ def get(path, db_path, verbose):
1010
if verbose:
1111
logging.basicConfig(level=logging.DEBUG)
1212

13-
abs_path = os.path.abspath(path)
14-
leaf_dir = os.path.dirname(abs_path) if os.path.isfile(abs_path) else abs_path
15-
dirs = leaf_dir.split("/")
16-
1713
db = TraitsDB(db_path)
18-
19-
# get traits from path and its parents
20-
dirs_data = []
21-
data = db.get("data", path=abs_path)
22-
if data:
23-
dirs_data.append(data)
24-
for i in reversed(range(0, len(dirs))):
25-
cur_path = "/".join(dirs[0 : i + 1])
26-
data = db.get("data", path=cur_path)
27-
if data:
28-
dirs_data.append(data)
29-
30-
# inherit traits: children overwrite parent path traits
31-
res = {}
32-
for cur_data in reversed(dirs_data):
33-
for k, v in cur_data.items():
34-
if v and k != "path":
35-
res[k] = v
36-
37-
# output
14+
res = db.get_dict(path)
3815
if len(res) > 0:
3916
print(yaml.safe_dump(res))
4017
else:

pathtraits/db.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ def get(self, table, cols="*", condition=None, **kwargs):
5555
res = {k: v for k, v in zip(keys, values)}
5656
return res
5757

58+
def get_dict(self, path):
59+
abs_path = os.path.abspath(path)
60+
leaf_dir = os.path.dirname(abs_path) if os.path.isfile(abs_path) else abs_path
61+
dirs = leaf_dir.split("/")
62+
63+
# get traits from path and its parents
64+
dirs_data = []
65+
data = self.get("data", path=abs_path)
66+
if data:
67+
dirs_data.append(data)
68+
for i in reversed(range(0, len(dirs))):
69+
cur_path = "/".join(dirs[0 : i + 1])
70+
data = self.get("data", path=cur_path)
71+
if data:
72+
dirs_data.append(data)
73+
74+
# inherit traits: children overwrite parent path traits
75+
res = {}
76+
for cur_data in reversed(dirs_data):
77+
for k, v in cur_data.items():
78+
if v and k != "path":
79+
res[k] = v
80+
return res
81+
5882
def put_path_id(self, path):
5983
get_row_query = f"SELECT id FROM path WHERE path = '{path}' LIMIT 1;"
6084
res = self.execute(get_row_query).fetchone()

test/__init__.py

Whitespace-only changes.

test/test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# pylint: disable-all
2+
3+
"""
4+
Test module
5+
"""
6+
7+
import unittest
8+
import tempfile
9+
import pathtraits.db
10+
import pathtraits.scan
11+
12+
13+
class TestMain(unittest.TestCase):
14+
def test_example(self):
15+
db_path = tempfile.mkstemp()[1]
16+
pathtraits.scan.batch("test/example", db_path, False)
17+
18+
db = pathtraits.db.TraitsDB(db_path)
19+
self.assertTrue(db is not None)
20+
21+
source = db.get_dict("test/example/EU/de.txt")
22+
target = {
23+
"description_TEXT": "Germany data",
24+
"has_sidecar_meta_file_BOOL": 1,
25+
"is_example_BOOL": 1,
26+
"score_TEXT": "zero",
27+
"score_REAL": 3.5,
28+
}
29+
for k, v in target.items():
30+
self.assertEqual(source[k], v)
31+
32+
source = len(db.execute("SELECT * FROM data;").fetchall())
33+
target = 4
34+
self.assertEqual(source, target)
35+
36+
37+
if __name__ == "__main__":
38+
unittest.main()

0 commit comments

Comments
 (0)