Skip to content

Commit

Permalink
More unittest conversions (#167)
Browse files Browse the repository at this point in the history
* Convert the leaf-list tests to unittest

* Convert nested-containers tests to unittest

* Convert submodules tests to unittest

* Convert openconfig-bgp-juniper tests to unittest

* Fix a typo
  • Loading branch information
tarkatronic authored and robshakir committed Mar 30, 2018
1 parent 5352dd7 commit c0aa7d6
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 392 deletions.
Empty file added tests/leaf-list/__init__.py
Empty file.
257 changes: 125 additions & 132 deletions tests/leaf-list/run.py
Original file line number Diff line number Diff line change
@@ -1,140 +1,133 @@
#!/usr/bin/env python

import os
import sys
import getopt

TESTNAME = "leaflist"


# generate bindings in this folder
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "k", ["keepfiles"])
except getopt.GetoptError as e:
print(str(e))
sys.exit(127)

k = False
for o, a in opts:
if o in ["-k", "--keepfiles"]:
k = True

pythonpath = os.environ.get("PATH_TO_PYBIND_TEST_PYTHON") if \
os.environ.get('PATH_TO_PYBIND_TEST_PYTHON') is not None \
else sys.executable
pyangpath = os.environ.get('PYANGPATH') if \
os.environ.get('PYANGPATH') is not None else False
pyangbindpath = os.environ.get('PYANGBINDPATH') if \
os.environ.get('PYANGBINDPATH') is not None else False
assert pyangpath is not False, "could not find path to pyang"
assert pyangbindpath is not False, "could not resolve pyangbind directory"

this_dir = os.path.dirname(os.path.realpath(__file__))

cmd = "%s " % pythonpath
cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath)
cmd += " -f pybind -o %s/bindings.py" % this_dir
cmd += " -p %s" % this_dir
cmd += " %s/%s.yang" % (this_dir, TESTNAME)
os.system(cmd)

from bindings import leaflist

leaflist_instance = leaflist()

assert hasattr(leaflist_instance, "container") is True, \
"base container missing"

assert hasattr(leaflist_instance.container, "leaflist") is True, \
"leaf-list instance missing"

assert len(leaflist_instance.container.leaflist) == 0, \
"length of leaflist was not zero"

leaflist_instance.container.leaflist.append("itemOne")

assert len(leaflist_instance.container.leaflist) == 1, \
"did not succesfully append string to list"

assert leaflist_instance.container.leaflist[0] == "itemOne", \
"cannot successfully address an item from the list"

try:
leaflist_instance.container.leaflist.append(int(1))
except ValueError:
pass

assert len(leaflist_instance.container.leaflist) == 1, \
"appended an element to the list erroneously (%s, len %d vs. 1)" % \
(leaflist_instance.container.leaflist,
len(leaflist_instance.container.leaflist))

leaflist_instance.container.leaflist.append("itemTwo")
assert leaflist_instance.container.leaflist[1] == "itemTwo", \
"getitem did not return the correct value"

leaflist_instance.container.leaflist[1] = "indexOne"
assert leaflist_instance.container.leaflist[1] == "indexOne", \
"setitem did not set the correct node"

leaflist_instance.container.leaflist.insert(0, "indexZero")
assert leaflist_instance.container.leaflist[0] == "indexZero", \
"incorrectly set index 0 value"
assert len(leaflist_instance.container.leaflist) == 4, \
"list item was not added by insert()"

del leaflist_instance.container.leaflist[0]
assert len(leaflist_instance.container.leaflist) == 3, \
"list item not succesfully removed by delitem"

assert leaflist_instance.get() == \
{'container': {'leaflist': ['itemOne', 'indexOne', 'itemTwo'],
'listtwo': [], 'listthree': []}}, \
"get did not correctly return the dictionary"

try:
leaflist_instance.container.leaflist = ["itemOne", "itemTwo"]
except ValueError:
pass

assert leaflist_instance.container.leaflist == ["itemOne", "itemTwo"], \
"leaflist assignment did not function correctly"

passed = False
try:
leaflist_instance.container.leaflist = [1, 2]
except ValueError:
passed = True
assert passed is True, "an erroneous value was assigned to the list"

leaflist_instance.container.listtwo.append("a-valid-string")
assert len(leaflist_instance.container.listtwo) == 1, \
"restricted leaflist did not function correctly"

passed = False
try:
leaflist_instance.container.listtwo.append("broken-string")
except ValueError:
passed = True
assert passed is True, \
"an erroneous value was assigned to the list (restricted type)"

for i in [(1, True), ("fish", True), ([], False)]:
passed = False
import unittest

from tests.base import PyangBindTestCase


class LeafListTests(PyangBindTestCase):
yang_files = ['leaflist.yang']

def setUp(self):
self.leaflist_obj = self.bindings.leaflist()

def test_container_exists(self):
self.assertTrue(hasattr(self.leaflist_obj, "container"), "Base container is missing.")

def test_leaflist_exists(self):
self.assertTrue(hasattr(self.leaflist_obj.container, "leaflist"), "Leaf-list instance is missing.")

def test_leaflist_length_is_zero(self):
self.assertEqual(len(self.leaflist_obj.container.leaflist), 0, "Length of leaflist was not zero.")

def test_append_to_leaflist(self):
self.leaflist_obj.container.leaflist.append("itemOne")
self.assertEqual(len(self.leaflist_obj.container.leaflist), 1, "Did not successfully append string to list.")

def test_retrieve_leaflist_item_value(self):
self.leaflist_obj.container.leaflist.append("itemOne")
self.assertEqual(self.leaflist_obj.container.leaflist[0], "itemOne",
"Cannot successfully address an item from the list.")

def test_append_int_to_string_leaflist(self):
allowed = True
try:
self.leaflist_obj.container.leaflist.append(1)
except ValueError:
allowed = False
self.assertFalse(allowed, "Appended an element to the list erroneously")

def test_getitem(self):
self.leaflist_obj.container.leaflist.append("itemOne")
self.leaflist_obj.container.leaflist.append("itemTwo")

self.assertEqual(self.leaflist_obj.container.leaflist[1], "itemTwo", "getitem did not return the correct value.")

def test_setitem(self):
self.leaflist_obj.container.leaflist.append("itemOne")
self.leaflist_obj.container.leaflist.append("itemTwo")
self.leaflist_obj.container.leaflist[1] = "indexOne"

self.assertEqual(self.leaflist_obj.container.leaflist[1], "indexOne", "setitem did not set the correct node.")

def test_insert(self):
self.leaflist_obj.container.leaflist.append("itemOne")
self.leaflist_obj.container.leaflist.append("itemTwo")
self.leaflist_obj.container.leaflist[1] = "indexOne"
self.leaflist_obj.container.leaflist.insert(0, "indexZero")

self.assertEqual(self.leaflist_obj.container.leaflist[0], "indexZero", "Incorrectly set index 0 value")

def test_leaflist_grows_from_various_modification_methods(self):
self.leaflist_obj.container.leaflist.append("itemOne")
self.leaflist_obj.container.leaflist.append("itemTwo")
self.leaflist_obj.container.leaflist[1] = "indexOne"
self.leaflist_obj.container.leaflist.insert(0, "indexZero")

self.assertEqual(len(self.leaflist_obj.container.leaflist), 4, "List item was not added by insert()")

def test_delete_item_from_leaflist(self):
self.leaflist_obj.container.leaflist.append("itemOne")
self.leaflist_obj.container.leaflist.append("itemTwo")
self.leaflist_obj.container.leaflist[1] = "indexOne"
self.leaflist_obj.container.leaflist.insert(0, "indexZero")

del self.leaflist_obj.container.leaflist[0]

self.assertEqual(len(self.leaflist_obj.container.leaflist), 3, "List item not successfully removed by delitem")

def test_get_full_leaflist(self):
self.leaflist_obj.container.leaflist.append("itemOne")
self.leaflist_obj.container.leaflist.append("itemTwo")
self.leaflist_obj.container.leaflist[1] = "indexOne"
self.leaflist_obj.container.leaflist.insert(0, "indexZero")
del self.leaflist_obj.container.leaflist[0]

self.assertEqual(
self.leaflist_obj.get(),
{'container': {'leaflist': ['itemOne', 'indexOne', 'itemTwo'],
'listtwo': [],
'listthree': []}},
"get did not correctly return the dictionary"
)

def test_leaflist_assignment(self):
self.leaflist_obj.container.leaflist = ["itemOne", "itemTwo"]

self.assertEqual(self.leaflist_obj.container.leaflist, ["itemOne", "itemTwo"],
"Leaflist assignment did not function correctly")

def test_leaflist_assignment_of_wrong_type(self):
allowed = True
try:
self.leaflist_obj.container.leaflist = [1, 2]
except ValueError:
allowed = False
self.assertFalse(allowed, "An erroneous value was assigned to the list.")

def test_restricted_string(self):
self.leaflist_obj.container.listtwo.append("a-valid-string")
self.assertEqual(len(self.leaflist_obj.container.listtwo), 1, "Restricted lefalist did not function correctly.")

def test_restricted_string_invalid_value(self):
allowed = True
try:
leaflist_instance.container.listthree.append(i[0])
passed = True
self.leaflist_obj.container.listtwo.append("broken-string")
except ValueError:
pass
assert passed == i[1], \
"leaf-list of union type had invalid result (%s != %s for %s)" \
% (passed, i[1], i[0])
allowed = False
self.assertFalse(allowed, "An erroneous value was assigned to the list (restricted type)")

if not k:
os.system("/bin/rm %s/bindings.py" % this_dir)
os.system("/bin/rm %s/bindings.pyc" % this_dir)
def test_union_type(self):
for pair in [(1, True), ("fish", True), ([], False)]:
with self.subTest(pair=pair):
allowed = True
try:
self.leaflist_obj.container.listthree.append(pair[0])
except ValueError:
allowed = False
self.assertEqual(allowed, pair[1], "leaf-list of union type had invalid result (%s != %s for %s)" %
(allowed, pair[1], pair[0]))


if __name__ == '__main__':
main()
unittest.main()
Empty file.
82 changes: 24 additions & 58 deletions tests/nested-containers/run.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,39 @@
#!/usr/bin/env python

import os
import sys
import getopt
import unittest

TESTNAME = "nested"
from tests.base import PyangBindTestCase


# generate bindings in this folder
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "k", ["keepfiles"])
except getopt.GetoptError as e:
print(str(e))
sys.exit(127)
class NestedContainerTests(PyangBindTestCase):
yang_files = ['nested.yang']

k = False
for o, a in opts:
if o in ["-k", "--keepfiles"]:
k = True
def setUp(self):
self.nested_obj = self.bindings.nested()

pythonpath = os.environ.get("PATH_TO_PYBIND_TEST_PYTHON") if \
os.environ.get('PATH_TO_PYBIND_TEST_PYTHON') is not None \
else sys.executable
pyangpath = os.environ.get('PYANGPATH') if \
os.environ.get('PYANGPATH') is not None else False
pyangbindpath = os.environ.get('PYANGBINDPATH') if \
os.environ.get('PYANGBINDPATH') is not None else False
assert pyangpath is not False, "could not find path to pyang"
assert pyangbindpath is not False, "could not resolve pyangbind directory"
def test_subcontainer_is_not_changed_by_default(self):
self.assertFalse(self.nested_obj.container.subcontainer._changed(), "subcontainer was marked to changed")

this_dir = os.path.dirname(os.path.realpath(__file__))
def test_container_is_not_changed_by_default(self):
self.assertFalse(self.nested_obj.container._changed(), "container was marked to changed")

cmd = "%s " % pythonpath
cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath)
cmd += " -f pybind -o %s/bindings.py" % this_dir
cmd += " -p %s" % this_dir
cmd += " %s/%s.yang" % (this_dir, TESTNAME)
os.system(cmd)
def test_subcontainer_marked_changed(self):
self.nested_obj.container.subcontainer.a_leaf = 1
self.assertTrue(self.nested_obj.container.subcontainer._changed(),
"subcontainer not marked to changed after change")

from bindings import nested
def test_subcontainer_get(self):
self.nested_obj.container.subcontainer.a_leaf = 1
self.assertEqual(self.nested_obj.container.subcontainer.get(), {'a-leaf': 1}, "subcontainer get not correct")

test_instance = nested()
def test_container_get(self):
self.nested_obj.container.subcontainer.a_leaf = 1
self.assertEqual(self.nested_obj.container.get(), {'subcontainer': {'a-leaf': 1}}, "container get not correct")

assert test_instance.container.subcontainer._changed() is False, \
"subcontainer was marked to changed"

assert test_instance.container._changed() is False, \
"container was marked to changed"

test_instance.container.subcontainer.a_leaf = 1

assert test_instance.container.subcontainer._changed() is True, \
"subcontainer not marked to changed after change"

assert test_instance.container.subcontainer.get() == {'a-leaf': 1}, \
"subcontainer get not correct"

assert test_instance.container.get() == {'subcontainer': {'a-leaf': 1}}, \
"container get not correct"

assert test_instance.get() == \
{'container': {'subcontainer': {'a-leaf': 1}}}, \
"instance get not correct"

if not k:
os.system("/bin/rm %s/bindings.py" % this_dir)
os.system("/bin/rm %s/bindings.pyc" % this_dir)
def test_full_get(self):
self.nested_obj.container.subcontainer.a_leaf = 1
self.assertEqual(self.nested_obj.get(), {'container': {'subcontainer': {'a-leaf': 1}}}, "instance get not correct")


if __name__ == '__main__':
main()
unittest.main()
Empty file.
Loading

0 comments on commit c0aa7d6

Please sign in to comment.