From 725bc754c33e1dc975a5b133f43db23112e46686 Mon Sep 17 00:00:00 2001 From: danlooo Date: Thu, 18 Dec 2025 16:33:05 +0100 Subject: [PATCH 1/5] Add tests --- .github/workflows/pylint.yml | 44 +++++++++++++++++++++++++----------- .vscode/settings.json | 11 +++++++++ test/__init__.py | 0 test/test.py | 23 +++++++++++++++++++ 4 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 test/__init__.py create mode 100644 test/test.py diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index c73e032..116cc06 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -3,21 +3,39 @@ name: Pylint on: [push] jobs: - build: + test: runs-on: ubuntu-latest strategy: matrix: python-version: ["3.8", "3.9", "3.10"] steps: - - uses: actions/checkout@v4 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v3 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install pylint - - name: Analysing the code with pylint - run: | - pylint $(git ls-files '*.py') + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pathtraits + - name: Unit tests + run: | + python -m unittest discover + lint: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10"] + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint + - name: Analysing the code with pylint + run: | + pylint $(git ls-files '*.py') diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3a7ba1c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + "./test", + "-p", + "*test.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} \ No newline at end of file diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..a2f29b3 --- /dev/null +++ b/test/test.py @@ -0,0 +1,23 @@ +# pylint: disable-all + +""" +Test module +""" + +import unittest +import tempfile +import pathtraits.db +import pathtraits.scan + + +class TestCLI(unittest.TestCase): + def test(self): + db_path = tempfile.mkstemp()[1] + pathtraits.scan.batch("test/example", db_path, False) + + db = pathtraits.db.TraitsDB(db_path) + self.assertTrue(db is not None) + + +if __name__ == "__main__": + unittest.main() From eeefcc61e4b89dd632ed3af7478df7b18ead0252 Mon Sep 17 00:00:00 2001 From: danlooo Date: Thu, 18 Dec 2025 16:45:10 +0100 Subject: [PATCH 2/5] Split ci to lint and test --- .github/workflows/pylint.yml | 41 ------------------------------- .github/workflows/python-lint.yml | 23 +++++++++++++++++ .github/workflows/python-test.yml | 23 +++++++++++++++++ 3 files changed, 46 insertions(+), 41 deletions(-) delete mode 100644 .github/workflows/pylint.yml create mode 100644 .github/workflows/python-lint.yml create mode 100644 .github/workflows/python-test.yml diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml deleted file mode 100644 index 116cc06..0000000 --- a/.github/workflows/pylint.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Pylint - -on: [push] - -jobs: - test: - runs-on: ubuntu-latest - strategy: - matrix: - python-version: ["3.8", "3.9", "3.10"] - steps: - - uses: actions/checkout@v4 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v3 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install pathtraits - - name: Unit tests - run: | - python -m unittest discover - lint: - runs-on: ubuntu-latest - strategy: - matrix: - python-version: ["3.8", "3.9", "3.10"] - steps: - - uses: actions/checkout@v4 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v3 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install pylint - - name: Analysing the code with pylint - run: | - pylint $(git ls-files '*.py') diff --git a/.github/workflows/python-lint.yml b/.github/workflows/python-lint.yml new file mode 100644 index 0000000..29b4744 --- /dev/null +++ b/.github/workflows/python-lint.yml @@ -0,0 +1,23 @@ +name: Lint Python + +on: [push] + +jobs: + lint: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.12"] + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint + - name: Analysing the code with pylint + run: | + pylint $(git ls-files '*.py') diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml new file mode 100644 index 0000000..9d938c2 --- /dev/null +++ b/.github/workflows/python-test.yml @@ -0,0 +1,23 @@ +name: Test Python + +on: [push] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.12"] + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pathtraits + - name: Unit tests + run: | + python -m unittest discover From 30a75dc092aceb4f785e77b86084a18ed9c8b897 Mon Sep 17 00:00:00 2001 From: danlooo Date: Thu, 18 Dec 2025 16:59:43 +0100 Subject: [PATCH 3/5] Add test_example --- pathtraits/access.py | 25 +------------------------ pathtraits/db.py | 24 ++++++++++++++++++++++++ test/test.py | 14 ++++++++++++-- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/pathtraits/access.py b/pathtraits/access.py index 9d14fc1..cb8a589 100644 --- a/pathtraits/access.py +++ b/pathtraits/access.py @@ -10,31 +10,8 @@ def get(path, db_path, verbose): if verbose: logging.basicConfig(level=logging.DEBUG) - abs_path = os.path.abspath(path) - leaf_dir = os.path.dirname(abs_path) if os.path.isfile(abs_path) else abs_path - dirs = leaf_dir.split("/") - db = TraitsDB(db_path) - - # get traits from path and its parents - dirs_data = [] - data = db.get("data", path=abs_path) - if data: - dirs_data.append(data) - for i in reversed(range(0, len(dirs))): - cur_path = "/".join(dirs[0 : i + 1]) - data = db.get("data", path=cur_path) - if data: - dirs_data.append(data) - - # inherit traits: children overwrite parent path traits - res = {} - for cur_data in reversed(dirs_data): - for k, v in cur_data.items(): - if v and k != "path": - res[k] = v - - # output + res = db.get_dict(path) if len(res) > 0: print(yaml.safe_dump(res)) else: diff --git a/pathtraits/db.py b/pathtraits/db.py index b0c4148..c5380ae 100644 --- a/pathtraits/db.py +++ b/pathtraits/db.py @@ -55,6 +55,30 @@ def get(self, table, cols="*", condition=None, **kwargs): res = {k: v for k, v in zip(keys, values)} return res + def get_dict(self, path): + abs_path = os.path.abspath(path) + leaf_dir = os.path.dirname(abs_path) if os.path.isfile(abs_path) else abs_path + dirs = leaf_dir.split("/") + + # get traits from path and its parents + dirs_data = [] + data = self.get("data", path=abs_path) + if data: + dirs_data.append(data) + for i in reversed(range(0, len(dirs))): + cur_path = "/".join(dirs[0 : i + 1]) + data = self.get("data", path=cur_path) + if data: + dirs_data.append(data) + + # inherit traits: children overwrite parent path traits + res = {} + for cur_data in reversed(dirs_data): + for k, v in cur_data.items(): + if v and k != "path": + res[k] = v + return res + def put_path_id(self, path): get_row_query = f"SELECT id FROM path WHERE path = '{path}' LIMIT 1;" res = self.execute(get_row_query).fetchone() diff --git a/test/test.py b/test/test.py index a2f29b3..0562c89 100644 --- a/test/test.py +++ b/test/test.py @@ -10,14 +10,24 @@ import pathtraits.scan -class TestCLI(unittest.TestCase): - def test(self): +class TestMain(unittest.TestCase): + def test_example(self): db_path = tempfile.mkstemp()[1] pathtraits.scan.batch("test/example", db_path, False) db = pathtraits.db.TraitsDB(db_path) self.assertTrue(db is not None) + source = db.get_dict("test/example/Europe/de.txt") + target = { + "description_TEXT": "Germany data", + "has_sidecar_meta_file_BOOL": 1, + "is_example_BOOL": 1, + "score_TEXT": "zero", + } + for k, v in target.items(): + self.assertEqual(source[k], v) + if __name__ == "__main__": unittest.main() From 26ee84f1af44fbfe0b973c42ba3d256c4cccc92e Mon Sep 17 00:00:00 2001 From: danlooo Date: Thu, 18 Dec 2025 17:02:19 +0100 Subject: [PATCH 4/5] Fix de txt path --- test/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.py b/test/test.py index 0562c89..9ed490c 100644 --- a/test/test.py +++ b/test/test.py @@ -18,7 +18,7 @@ def test_example(self): db = pathtraits.db.TraitsDB(db_path) self.assertTrue(db is not None) - source = db.get_dict("test/example/Europe/de.txt") + source = db.get_dict("test/example/EU/de.txt") target = { "description_TEXT": "Germany data", "has_sidecar_meta_file_BOOL": 1, From 7c35f4eebee7ea70a7798de722da64580e19df5f Mon Sep 17 00:00:00 2001 From: danlooo Date: Fri, 19 Dec 2025 09:41:49 +0100 Subject: [PATCH 5/5] Add test number of entries --- test/test.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test.py b/test/test.py index 9ed490c..7fde102 100644 --- a/test/test.py +++ b/test/test.py @@ -24,10 +24,15 @@ def test_example(self): "has_sidecar_meta_file_BOOL": 1, "is_example_BOOL": 1, "score_TEXT": "zero", + "score_REAL": 3.5, } for k, v in target.items(): self.assertEqual(source[k], v) + source = len(db.execute("SELECT * FROM data;").fetchall()) + target = 4 + self.assertEqual(source, target) + if __name__ == "__main__": unittest.main()