From 1c95684c5b42306d6dbb2a0a75a891470a4c4ced Mon Sep 17 00:00:00 2001 From: ThaHobbyist Date: Sat, 11 May 2024 17:15:25 +0530 Subject: Added testcases for all current classes and functions in pytest --- Testing/test.py | 24 ----- Testing/test_pyvnt.py | 37 ------- .../test_pyvnt.cpython-312-pytest-8.2.0.pyc | Bin 0 -> 645 bytes manual_tests/test.py | 24 +++++ manual_tests/test_pyvnt.py | 37 +++++++ manual_tests/treeTest.py | 11 ++ pyvnt/DictionaryElement/foamDS.py | 7 +- pyvnt/DictionaryElement/keyData.py | 7 +- pyvnt/Reference/basic.py | 4 +- pyvnt/test/treeTest.py | 11 -- .../test_basic.cpython-312-pytest-8.2.0.pyc | Bin 0 -> 25015 bytes .../test_keyData.cpython-312-pytest-8.2.0.pyc | Bin 0 -> 8895 bytes .../test_node.cpython-312-pytest-8.2.0.pyc | Bin 0 -> 9909 bytes .../test_pyvnt.cpython-312-pytest-8.2.0.pyc | Bin 0 -> 638 bytes tests/test_basic.py | 115 +++++++++++++++++++++ tests/test_keyData.py | 57 ++++++++++ tests/test_node.py | 56 ++++++++++ 17 files changed, 310 insertions(+), 80 deletions(-) delete mode 100755 Testing/test.py delete mode 100755 Testing/test_pyvnt.py create mode 100644 manual_tests/__pycache__/test_pyvnt.cpython-312-pytest-8.2.0.pyc create mode 100755 manual_tests/test.py create mode 100755 manual_tests/test_pyvnt.py create mode 100755 manual_tests/treeTest.py delete mode 100755 pyvnt/test/treeTest.py create mode 100644 tests/__pycache__/test_basic.cpython-312-pytest-8.2.0.pyc create mode 100644 tests/__pycache__/test_keyData.cpython-312-pytest-8.2.0.pyc create mode 100644 tests/__pycache__/test_node.cpython-312-pytest-8.2.0.pyc create mode 100644 tests/__pycache__/test_pyvnt.cpython-312-pytest-8.2.0.pyc create mode 100644 tests/test_basic.py create mode 100644 tests/test_keyData.py create mode 100644 tests/test_node.py diff --git a/Testing/test.py b/Testing/test.py deleted file mode 100755 index e98b25a..0000000 --- a/Testing/test.py +++ /dev/null @@ -1,24 +0,0 @@ -from pyvnt.DictionaryElement.foamDS import Foam -from pyvnt.DictionaryElement.keyData import KeyData - -def obj_constructor(name: str, parent = None, children: [] = None, *args): - ''' - Function to create a new node object with the given name - - Parameter: - name: Name of the Node that is to be created - parent: Parent Node of the current Node (Optional) - children: List of the children node(s) of the current Node (Optional) - ''' - - if children != None and args != None: - raise Exception("Both children and args cannot be given at the same time") - elif args == None: - return Foam(name, parent, children) - elif children == None: - return KeyData(name, parent, *args) - else: - raise Exception("Invalid arguments given") - -obj = obj_constructor("test_head") -print(obj) \ No newline at end of file diff --git a/Testing/test_pyvnt.py b/Testing/test_pyvnt.py deleted file mode 100755 index fbdae29..0000000 --- a/Testing/test_pyvnt.py +++ /dev/null @@ -1,37 +0,0 @@ -from pyvnt import * -from anytree import RenderTree, AsciiStyle - -''' -prop1 = EnumProp('val1', items={'PCG', 'PBiCG', 'PBiCGStab'}, default='PCG') -prop2 = EnumProp('val2', items={'PCG', 'PBiCG', 'PBiCGStab'}, default='PBiCG') -''' - -# set up automated tests for CI/CD in github - -# test for KeyData and Foam classes -''' -key1 = KeyData('solver', prop1, prop2) -print(key1.giveVal()) -head = Foam("test_head",None, None) - -# head.appendDict(args) - -child1 = Foam('test_child', head, None) -child2 = Foam('test_child2', child1, None, key1) -child3 = Foam('test_child3', child1, None, key1) -''' - -# Display tests -''' -print(head) -showTree(head) -print(RenderTree(child1).by_attr()) -''' - -# Test for Keydata class singularily -''' -key1 = KeyData('solver', prop1) -print(key1) - -''' - diff --git a/manual_tests/__pycache__/test_pyvnt.cpython-312-pytest-8.2.0.pyc b/manual_tests/__pycache__/test_pyvnt.cpython-312-pytest-8.2.0.pyc new file mode 100644 index 0000000..3463b13 Binary files /dev/null and b/manual_tests/__pycache__/test_pyvnt.cpython-312-pytest-8.2.0.pyc differ diff --git a/manual_tests/test.py b/manual_tests/test.py new file mode 100755 index 0000000..e98b25a --- /dev/null +++ b/manual_tests/test.py @@ -0,0 +1,24 @@ +from pyvnt.DictionaryElement.foamDS import Foam +from pyvnt.DictionaryElement.keyData import KeyData + +def obj_constructor(name: str, parent = None, children: [] = None, *args): + ''' + Function to create a new node object with the given name + + Parameter: + name: Name of the Node that is to be created + parent: Parent Node of the current Node (Optional) + children: List of the children node(s) of the current Node (Optional) + ''' + + if children != None and args != None: + raise Exception("Both children and args cannot be given at the same time") + elif args == None: + return Foam(name, parent, children) + elif children == None: + return KeyData(name, parent, *args) + else: + raise Exception("Invalid arguments given") + +obj = obj_constructor("test_head") +print(obj) \ No newline at end of file diff --git a/manual_tests/test_pyvnt.py b/manual_tests/test_pyvnt.py new file mode 100755 index 0000000..477429e --- /dev/null +++ b/manual_tests/test_pyvnt.py @@ -0,0 +1,37 @@ +from pyvnt import * +from anytree import RenderTree, AsciiStyle + + +prop1 = EnumProp('val1', items={'PCG', 'PBiCG', 'PBiCGStab'}, default='PCG') +prop2 = EnumProp('val2', items={'PCG', 'PBiCG', 'PBiCGStab'}, default='PBiCG') + + +# set up automated tests for CI/CD in github + +# test for KeyData and Foam classes +''' +key1 = KeyData('solver', prop1, prop2) +print(key1.giveVal()) +head = Foam("test_head",None, None) + +# head.appendDict(args) + +child1 = Foam('test_child', head, None) +child2 = Foam('test_child2', child1, None, key1) +child3 = Foam('test_child3', child1, None, key1) +''' + +# Display tests +''' +print(head) +showTree(head) +print(RenderTree(child1).by_attr()) +''' + +# Test for Keydata class singularily + +key1 = KeyData('solver', prop1) +print(key1) + + + diff --git a/manual_tests/treeTest.py b/manual_tests/treeTest.py new file mode 100755 index 0000000..656dd7d --- /dev/null +++ b/manual_tests/treeTest.py @@ -0,0 +1,11 @@ +from .pyvnt.DictionaryElement.foamDS import * +from .pyvnt.DictionaryElement.keyData import * +from .pyvnt.Reference.basic import * + +prop1 = EnumProp('val1', items={'PCG', 'PBiCG', 'PBiCGStab'}, default='PCG') + +key1 = KeyData('solver', prop1) + +head = Foam(name="test_head", children = [key1]) + +head.dispTree() \ No newline at end of file diff --git a/pyvnt/DictionaryElement/foamDS.py b/pyvnt/DictionaryElement/foamDS.py index 9a3ca5c..542cece 100755 --- a/pyvnt/DictionaryElement/foamDS.py +++ b/pyvnt/DictionaryElement/foamDS.py @@ -1,4 +1,5 @@ from anytree import Node, RenderTree, AsciiStyle, NodeMixin +from anytree.search import find_by_attr from typing import Any, Type from pyvnt.DictionaryElement.keyData import KeyData from pyvnt.Reference.errorClasses import * @@ -109,7 +110,7 @@ class Foam(NodeMixin): ''' if pos != None: - self.data.insert(data, pos) + self.data.insert(pos, data) else: self.data.append(data) @@ -119,7 +120,7 @@ class Foam(NodeMixin): ''' try: - self.data.pop(data) + self.data.remove(data) except: raise AttributeError(f"{data.name} does not exist in this node") @@ -129,7 +130,7 @@ class Foam(NodeMixin): ''' try: - self.data.pop(data) + self.data.remove(data) self.data.insert(data, pos) except: raise AttributeError(f"{data.name} does not exist in this node") diff --git a/pyvnt/DictionaryElement/keyData.py b/pyvnt/DictionaryElement/keyData.py index 39c5c7e..894615d 100755 --- a/pyvnt/DictionaryElement/keyData.py +++ b/pyvnt/DictionaryElement/keyData.py @@ -109,7 +109,7 @@ class KeyData(KeyParent): self.__dict__[newKey] = new """ - def replaceVal(self, old: ValueProperty | str, new: ValueProperty): # uses orderedDict instead of regular Dictionary + def replaceVal(self, old: ValueProperty or str, new: ValueProperty): # uses orderedDict instead of regular Dictionary ''' Function to insert and edit values in the class object once it is created @@ -129,9 +129,10 @@ class KeyData(KeyParent): newKey = new._ValueProperty__name if oldKey == newKey: - self.__dict__[newKey] = new + # self.__dict__[newKey] = new + self._privateDict[newKey] = new else: - if newKey != oldKey and newKey in self.__dict__.keys(): + if newKey != oldKey and newKey in self._privateDict.keys(): raise KeyRepeatError(newKey) else: self._privateDict = OrderedDict([(newKey, new) if k == oldKey else (k, v) for k, v in self._privateDict.items()]) diff --git a/pyvnt/Reference/basic.py b/pyvnt/Reference/basic.py index c4a2c93..b6279de 100755 --- a/pyvnt/Reference/basic.py +++ b/pyvnt/Reference/basic.py @@ -215,7 +215,7 @@ class EnumProp(ValueProperty): Parameters: val: The option that is to be removed ''' - if val != self.default: + if val != self.__default: self.__items.remove(val) else: raise IsDefaultError(val) @@ -227,7 +227,7 @@ class EnumProp(ValueProperty): Parameters: val: The new value of the property ''' - if val in self.items: + if val in self.__items: self.__default = val else: raise ValueOutofRangeError(val) diff --git a/pyvnt/test/treeTest.py b/pyvnt/test/treeTest.py deleted file mode 100755 index 656dd7d..0000000 --- a/pyvnt/test/treeTest.py +++ /dev/null @@ -1,11 +0,0 @@ -from .pyvnt.DictionaryElement.foamDS import * -from .pyvnt.DictionaryElement.keyData import * -from .pyvnt.Reference.basic import * - -prop1 = EnumProp('val1', items={'PCG', 'PBiCG', 'PBiCGStab'}, default='PCG') - -key1 = KeyData('solver', prop1) - -head = Foam(name="test_head", children = [key1]) - -head.dispTree() \ No newline at end of file diff --git a/tests/__pycache__/test_basic.cpython-312-pytest-8.2.0.pyc b/tests/__pycache__/test_basic.cpython-312-pytest-8.2.0.pyc new file mode 100644 index 0000000..6e4e529 Binary files /dev/null and b/tests/__pycache__/test_basic.cpython-312-pytest-8.2.0.pyc differ diff --git a/tests/__pycache__/test_keyData.cpython-312-pytest-8.2.0.pyc b/tests/__pycache__/test_keyData.cpython-312-pytest-8.2.0.pyc new file mode 100644 index 0000000..6ad8185 Binary files /dev/null and b/tests/__pycache__/test_keyData.cpython-312-pytest-8.2.0.pyc differ diff --git a/tests/__pycache__/test_node.cpython-312-pytest-8.2.0.pyc b/tests/__pycache__/test_node.cpython-312-pytest-8.2.0.pyc new file mode 100644 index 0000000..7de7234 Binary files /dev/null and b/tests/__pycache__/test_node.cpython-312-pytest-8.2.0.pyc differ diff --git a/tests/__pycache__/test_pyvnt.cpython-312-pytest-8.2.0.pyc b/tests/__pycache__/test_pyvnt.cpython-312-pytest-8.2.0.pyc new file mode 100644 index 0000000..b68eaf3 Binary files /dev/null and b/tests/__pycache__/test_pyvnt.cpython-312-pytest-8.2.0.pyc differ diff --git a/tests/test_basic.py b/tests/test_basic.py new file mode 100644 index 0000000..acfbea4 --- /dev/null +++ b/tests/test_basic.py @@ -0,0 +1,115 @@ +import pytest + +import pyvnt.Reference.basic as basic + +class TestEnum: + def setup_method(self, method): + self.items = {'PCG', 'PBiCG', 'PBiCGStab'} + self.eprop2 = basic.EnumProp('val2', items=self.items, default='PBiCG') + self.eprop1 = basic.EnumProp('val1', items=self.items, default='PCG') + + def teardown_method(self, method): + del self.eprop1 + del self.eprop2 + del self.items + + def test_enum_print(self): + assert str(self.eprop1) == f"EnumProp(name = val1, items = {self.items}, default = PCG)" + assert str(self.eprop2) == f"EnumProp(name = val2, items = {self.items}, default = PBiCG)" + + def test_enum_val(self): + assert self.eprop1.giveVal() == 'PCG' + assert self.eprop2.giveVal() == 'PBiCG' + + def test_enum_items(self): + assert self.eprop1.get_items() == self.items + assert self.eprop2.get_items() == self.items + + def test_enum_edit(self): + dummy_items = {'PCG', 'PBiCG', 'GMRES'} + self.eprop1.setProperties('val1', dummy_items, 'PCG') + assert self.eprop1.get_items() == dummy_items + assert self.eprop1.giveVal() == 'PCG' + + def test_enum_edit_fail(self): + dummy_items = {'PCG', 'PBiCG', 'GMRES'} + with pytest.raises(basic.DefaultOutofRangeError): + self.eprop1.setProperties('val1', dummy_items, 'PBiCGStab') + + with pytest.raises(basic.NotSetType): + self.eprop1.setProperties('val1', 'PCG', 'PCG') + + with pytest.raises(basic.NotStringType): + self.eprop1.setProperties('val1', {1, 2, 3}, 'PCG') + + def test_enum_change(self): + self.eprop1.set_default('PBiCG') + assert self.eprop1.giveVal() == 'PBiCG' + + def test_enum_change_fail(self): + with pytest.raises(basic.ValueOutofRangeError): + self.eprop1.set_default('GMRES') + + def test_enum_remove(self): + self.eprop1.remove_item('PBiCGStab') + assert self.eprop1.get_items() == {'PCG', 'PBiCG'} + + def test_enum_remove_fail(self): + with pytest.raises(basic.IsDefaultError): + self.eprop1.remove_item('PCG') + +class TestInt: + def setup_method(self, method): + self.iprop1 = basic.PropertyInt('val1', 5, 1, 10) + self.iprop2 = basic.PropertyInt('val2', 100, -100, 1000) + + def teardown_method(self, method): + del self.iprop1 + del self.iprop2 + + def test_int_print(self): + assert str(self.iprop1) == f"PropertyInt(name = val1, default = 5, minimum = 1, maximum = 10)" + assert str(self.iprop2) == f"PropertyInt(name = val2, default = 100, minimum = -100, maximum = 1000)" + + def test_int_edit(self): + self.iprop1.setProperties('val1', 10, 1, 10) + assert self.iprop1.giveVal() == 10 + + def test_int_edit_fail(self): + with pytest.raises(basic.DefaultOutofRangeError): + self.iprop1.setProperties('val1', 0, 1, 10) + + with pytest.raises(basic.InvalidRangeError): + self.iprop1.setProperties('val1', 2, 5, 1) + + def test_int_val(self): + assert self.iprop1.giveVal() == 5 + assert self.iprop2.giveVal() == 100 + +class TestFloat: + def setup_method(self, method): + self.fprop1 = basic.PropertyFloat('val1', 5.0, 1.0, 10.0) + self.fprop2 = basic.PropertyFloat('val2', 100.0, -100.0, 1000.0) + + def teardown_method(self, method): + del self.fprop1 + del self.fprop2 + + def test_float_print(self): + assert str(self.fprop1) == f"PropertyFloat(name = val1, default = 5.0, minimum = 1.0, maximum = 10.0)" + assert str(self.fprop2) == f"PropertyFloat(name = val2, default = 100.0, minimum = -100.0, maximum = 1000.0)" + + def test_float_edit(self): + self.fprop1.setProperties('val1', 10.0, 1.0, 10.0) + assert self.fprop1.giveVal() == 10.0 + + def test_float_edit_fail(self): + with pytest.raises(basic.DefaultOutofRangeError): + self.fprop1.setProperties('val1', 0.0, 1.0, 10.0) + + with pytest.raises(basic.InvalidRangeError): + self.fprop1.setProperties('val1', 2.0, 5.0, 1.0) + + def test_float_val(self): + assert self.fprop1.giveVal() == 5.0 + assert self.fprop2.giveVal() == 100.0 \ No newline at end of file diff --git a/tests/test_keyData.py b/tests/test_keyData.py new file mode 100644 index 0000000..ed8a687 --- /dev/null +++ b/tests/test_keyData.py @@ -0,0 +1,57 @@ +import pytest + +import pyvnt.DictionaryElement.keyData as keyData +from pyvnt.Reference.basic import * + +class TestKeyData: + def setup_method(self, method): + self.items = {'PCG', 'PBiCG', 'PBiCGStab'} + self.prop2 = EnumProp('val2', items=self.items, default='PBiCG') + self.prop1 = EnumProp('val1', items=self.items, default='PCG') + self.key1 = keyData.KeyData('solver', self.prop1, self.prop2) + + def teardown_method(self, method): + del self.key1 + del self.prop1 + del self.prop2 + del self.items + + def test_keyData_print(self): + assert str(self.key1) == f"KeyData(val1 : {str(self.prop1)}, val2 : {str(self.prop2)}, )" + + def test_keyData_val(self): + assert self.key1.giveVal() == f"solver : {self.prop1.giveVal()}, {self.prop2.giveVal()}, " + + def test_keyData_edit(self): + tmp_prop1 = PropertyInt('tmpval1', 2, 1, 10) + tmp_prop2 = PropertyInt('tmpval2', 3, 1, 10) + + self.key1.replaceVal('val1', tmp_prop1) + assert self.key1.giveVal() == f"solver : {tmp_prop1.giveVal()}, {self.prop2.giveVal()}, " + + self.key1.replaceVal(self.prop2, tmp_prop2) + assert self.key1.giveVal() == f"solver : {tmp_prop1.giveVal()}, {tmp_prop2.giveVal()}, " + + tmp_prop3 = PropertyInt('tmpval2', 4, 1, 10) + + self.key1.replaceVal(tmp_prop2, tmp_prop3) + assert self.key1.giveVal() == f"solver : {tmp_prop1.giveVal()}, {tmp_prop3.giveVal()}, " + + def test_keyData_edit_fail(self): + tmp_prop1 = PropertyInt('tmpval1', 2, 1, 10) + tmp_prop2 = PropertyInt('tmpval2', 3, 1, 10) + tmp_prop3 = PropertyInt('tmpval2', 4, 1, 10) + + with pytest.raises(keyData.KeyRepeatError): + self.key1.replaceVal('val1', tmp_prop1) + self.key1.replaceVal('val2', tmp_prop1) + + with pytest.raises(keyData.KeyRepeatError): + self.key1.replaceVal('val2', tmp_prop2) + self.key1.replaceVal('val2', tmp_prop3) + + def test_keyData_del(self): + self.key1.delVal('val1') + assert self.key1.giveVal() == f"solver : {self.prop2.giveVal()}, " + + \ No newline at end of file diff --git a/tests/test_node.py b/tests/test_node.py new file mode 100644 index 0000000..8c0fa32 --- /dev/null +++ b/tests/test_node.py @@ -0,0 +1,56 @@ +import pytest + +from pyvnt import * + +class TestNode: + def setup_method(self, method): + self.items = {'PCG', 'PBiCG', 'PBiCGStab'} + + self.eprop2 = EnumProp('val2', items=self.items, default='PBiCG') + self.eprop1 = EnumProp('val1', items=self.items, default='PCG') + + self.key1 = KeyData('solver', self.eprop1, self.eprop2) + self.key2 = KeyData('solver2', self.eprop2, self.eprop1) + + self.head = Foam("test_head", None, None) + self.chld1 = Foam("test_child", self.head, None, self.key2) + self.chld2 = Foam("test_child2", None, None) + + def teardown_method(self, method): + del self.head + del self.key1 + del self.eprop1 + del self.eprop2 + del self.items + + @pytest.mark.skip(reason = 'Complex to test') + def test_node_print(self): + assert str(self.head) == f"Foam(name : test_head, parent : None, children : ({self.chld1}, {self.chld2}, ), data : ({self.key1}, ), )" + + def test_node_add_child(self): + self.head.addChild(self.chld2) + assert self.head.children == (self.chld1, self.chld2, ) + + def test_node_set_parent(self): + self.chld2.setParent(self.head) + assert self.chld2.parent == self.head + + def test_node_get_child(self): + assert self.head.getChild('test_child') == self.chld1 + + def test_node_add_data(self): + self.chld2.addData(self.key2) + assert self.chld2.data == [self.key2] + + self.chld2.addData(self.key1, 0) + assert self.chld2.data == [self.key1, self.key2] + + def test_node_remove_data(self): + self.chld1.removeData(self.key2) + assert self.chld1.data == [] + + @pytest.mark.skip(reason = 'Complex to test') + def test_node_terminal_display(self): + pass + + \ No newline at end of file -- cgit