Skip to content

adding automated tests #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test

on:
- push
- pull_request

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox
run: tox
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![PYPI](https://github.com/cedadev/ceda-directory-tree/actions/workflows/release.yml/badge.svg)](https://pypi.org/project/ceda-directory-tree/)
[![Test](https://github.com/cedadev/ceda-directory-tree/actions/workflows/test.yml/badge.svg)](https://github.com/cedadev/ceda-directory-tree/actions/workflows/test.yml)

# CEDA directory-tree
Data structure for storing and searching directory trees.
Expand Down
7 changes: 7 additions & 0 deletions directory_tree/dataset_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ def add_child(self, child_name: str, **kwargs) -> None:

for part in child_name.split("/"):
for child in node.children:
if child_name == child.name:
child = DatasetNode(name=part, parent=node, **kwargs)
node = child
node.dataset = True
return
if part == child.name:
node = child
if part != node.name:
child = DatasetNode(name=part, parent=node, **kwargs)
node = child
if not node.dataset:
node.update(**kwargs)
node.dataset = True

def search_all(self, query: str) -> List[Node]:
Expand Down
7 changes: 7 additions & 0 deletions directory_tree/directory_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def __init__(self, name=None, parent=None, children=None, **kwargs):
name = ""
super().__init__(name, parent=parent, children=children, **kwargs)

def update(self, **kwargs):
"""
Update the values of an existing node with new values.
"""
for key, value in kwargs.items():
self.__setattr__(key, value)

@staticmethod
def valid_node(value: str) -> bool:
"""
Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='ceda-directory_tree',
version='1.0.1',
version='1.0.2',
description='Python package to create a virtual tree data structure to represent a directory tree to be used for efficient search',
author='Tommy Godfrey',
author_email='tommy.godfrey@stfc.ac.uk',
Expand All @@ -28,6 +28,10 @@
'sphinx',
'sphinx-rtd-theme',
'sphinxcontrib-programoutput'
],
'dev': [
'pytest',
'requests'
]
},
# See:
Expand Down
9 changes: 9 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# encoding: utf-8
"""

"""
__author__ = 'Richard Smith'
__date__ = '23 Nov 2021'
__copyright__ = 'Copyright 2018 United Kingdom Research and Innovation'
__license__ = 'BSD - see LICENSE file in top-level package directory'
__contact__ = 'richard.d.smith@stfc.ac.uk'
69 changes: 69 additions & 0 deletions tests/test_dataset_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# encoding: utf-8
"""

"""
__author__ = 'Richard Smith'
__date__ = '23 Nov 2021'
__copyright__ = 'Copyright 2018 United Kingdom Research and Innovation'
__license__ = 'BSD - see LICENSE file in top-level package directory'
__contact__ = 'richard.d.smith@stfc.ac.uk'

import pytest

from directory_tree import DatasetNode

@pytest.fixture
def tree():
return DatasetNode()


def test_add_child(tree):

dataset_path = '/badc/faam/a/b/c/'
tree.add_child(dataset_path)

node = tree.search('/badc/faam/a/b/c/d/e')

assert node.dataset
assert node.directory_path() == dataset_path


def test_extra_args(tree):
dataset_path = '/badc/faam/a/b/c/'
tree.add_child(dataset_path, description_file='file1')

node = tree.search('/badc/faam/a/b/c/d/e')

assert node.dataset
assert node.directory_path() == dataset_path
assert node.description_file == 'file1'

def test_search_all(tree):

path1 = '/badc/faam/a/'
path2 = '/badc/faam/a/b/c/'

tree.add_child(path1)
tree.add_child(path2)

nodes = tree.search_all('/badc/faam/a/b/c/d/e/')

assert len(nodes) == 2
assert nodes[0].directory_path() == path1
assert nodes[1].directory_path() == path2


def test_out_of_order_add_child(tree):

path1 = '/badc/faam/a/'
path2 = '/badc/faam/a/b/c/'

tree.add_child(path2, description_file='file2')
tree.add_child(path1, description_file='file1')

nodes = tree.search_all('/badc/faam/a/b/c/d/e/')

assert nodes[0].description_file == 'file1'
assert nodes[1].description_file == 'file2'


17 changes: 17 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tox]
envlist = py36, py37, py38, flake8

[gh-actions]
python =
3.8: py38
3.7: py37
3.6: py36


[testenv]
setenv =
PYTHONPATH = {toxinidir}
deps = pytest
-r{toxinidir}/requirements.txt
extras = dev
commands = pytest {posargs}