Skip to content

Commit

Permalink
Merge pull request #1 from FOSSEE/testcases
Browse files Browse the repository at this point in the history
Added testcases and github actions
  • Loading branch information
ThaHobbyist authored May 13, 2024
2 parents e6a3ff5 + f15ffbd commit 30c82e5
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 12 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/python-testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will install Python dependencies and run tests with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Package testing

on:
push:
branches: [ "main", "dev" ]
pull_request:
branches: [ "main", "dev" ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11"]

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
python -m pip install pytest anytree setuptools wheel
python setup.py sdist bdist_wheel
python setup.py install
- name: Test with pytest
run: |
pytest -v
Binary file not shown.
File renamed without changes.
8 changes: 4 additions & 4 deletions Testing/test_pyvnt.py → manual_tests/test_pyvnt.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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

Expand All @@ -29,9 +29,9 @@
'''

# Test for Keydata class singularily
'''

key1 = KeyData('solver', prop1)
print(key1)

'''


File renamed without changes.
7 changes: 4 additions & 3 deletions pyvnt/DictionaryElement/foamDS.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down Expand Up @@ -109,7 +110,7 @@ def addData(self, data: KeyData, pos: int = None):
'''

if pos != None:
self.data.insert(data, pos)
self.data.insert(pos, data)
else:
self.data.append(data)

Expand All @@ -119,7 +120,7 @@ def removeData(self, data: KeyData):
'''

try:
self.data.pop(data)
self.data.remove(data)
except:
raise AttributeError(f"{data.name} does not exist in this node")

Expand All @@ -129,7 +130,7 @@ def reorderData(self, data: KeyData, pos: int):
'''

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")
Expand Down
7 changes: 4 additions & 3 deletions pyvnt/DictionaryElement/keyData.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def replaceVal2(self, old: ValueProperty | str, new: ValueProperty):
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
Expand All @@ -129,9 +129,10 @@ def replaceVal(self, old: ValueProperty | str, new: ValueProperty): # uses order
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()])
Expand Down
4 changes: 2 additions & 2 deletions pyvnt/Reference/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def remove_item(self, val: str) -> None:
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)
Expand All @@ -227,7 +227,7 @@ def set_default(self, val: str) -> None:
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)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
115 changes: 115 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions tests/test_keyData.py
Original file line number Diff line number Diff line change
@@ -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()}, "


56 changes: 56 additions & 0 deletions tests/test_node.py
Original file line number Diff line number Diff line change
@@ -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


0 comments on commit 30c82e5

Please sign in to comment.