Skip to content
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

Replace unittest with pytest #90

Merged
merged 2 commits into from
Feb 9, 2024
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
48 changes: 23 additions & 25 deletions tests/unit/test_class_inheritance/test_inheritance.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"""Testing class inheritance attributes changes."""
import unittest

from pyof.foundation.base import GenericStruct
from pyof.foundation.basic_types import UBInt8, UBInt16, UBInt32, UBInt64


class TestInheritance(unittest.TestCase):
class TestInheritance:
"""Testing GenericStruct class inheritance."""

def setUp(self):
def setup_method(self):
"""Basic Test Setup."""
class MyClassA(GenericStruct):
"""Example class."""
Expand Down Expand Up @@ -41,30 +39,30 @@ def test_modifications(self):
m1 = self.MyClassA()
m2 = self.MyClassB()
# Checking keys (attributes names) and its ordering
self.assertEqual([attr[0] for attr in m1.get_class_attributes()],
self.a_expected_names)
self.assertEqual([attr[0] for attr in m2.get_class_attributes()],
self.b_expected_names)
assert [attr[0] for attr in m1.get_class_attributes()] == \
self.a_expected_names
assert [attr[0] for attr in m2.get_class_attributes()] == \
self.b_expected_names

# Check if there is no shared attribute between instances
self.assertIsNot(m1, m2)
self.assertIsNot(m1.a1, m2.a1)
self.assertIsNot(m1.a2, m2.a2)
self.assertIsNot(m1.a3, m2.a4)
self.assertIsNot(m1.a4, m2.a4)
self.assertIsNot(m1.a5, m2.a5)
assert m1 is not m2
assert m1.a1 is not m2.a1
assert m1.a2 is not m2.a2
assert m1.a3 is not m2.a4
assert m1.a4 is not m2.a4
assert m1.a5 is not m2.a5

# Check attributes types on MyClassA
self.assertIsInstance(self.MyClassA.a1, UBInt8)
self.assertIsInstance(self.MyClassA.a2, UBInt16)
self.assertIsInstance(self.MyClassA.a3, UBInt8)
self.assertIsInstance(self.MyClassA.a4, UBInt16)
self.assertIsInstance(self.MyClassA.a5, UBInt32)
assert isinstance(self.MyClassA.a1, UBInt8)
assert isinstance(self.MyClassA.a2, UBInt16)
assert isinstance(self.MyClassA.a3, UBInt8)
assert isinstance(self.MyClassA.a4, UBInt16)
assert isinstance(self.MyClassA.a5, UBInt32)

# Check attributes types on MyClassA
self.assertIsInstance(self.MyClassB.a1, UBInt8)
self.assertIsInstance(self.MyClassB.a2, UBInt64)
self.assertIsInstance(self.MyClassB.a4, UBInt16)
self.assertIsInstance(self.MyClassB.a5, UBInt32)
self.assertIsInstance(self.MyClassB.a0, UBInt32)
self.assertIsInstance(self.MyClassB.b6, UBInt8)
assert isinstance(self.MyClassB.a1, UBInt8)
assert isinstance(self.MyClassB.a2, UBInt64)
assert isinstance(self.MyClassB.a4, UBInt16)
assert isinstance(self.MyClassB.a5, UBInt32)
assert isinstance(self.MyClassB.a0, UBInt32)
assert isinstance(self.MyClassB.b6, UBInt8)
78 changes: 38 additions & 40 deletions tests/unit/test_foundation/test_base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Test Base module of python-openflow."""
import unittest

from pyof.foundation import base, basic_types


class TestGenericStruct(unittest.TestCase):
class TestGenericStruct:
"""Testing GenericStruct class."""

def setUp(self):
def setup_method(self):
"""Basic Test Setup."""
class AttributeA(base.GenericStruct):
"""Example class."""
Expand Down Expand Up @@ -52,46 +50,46 @@ def test_basic_attributes(self):
"""[Foundation/Base/GenericStruct] - Attributes Creation."""
message1 = self.MyMessage()
message2 = self.MyMessage()
self.assertIsNot(message1, message2)
self.assertIsNot(message1.i, message2.i)
self.assertIsNot(message1.a, message2.a)
self.assertIsNot(message1.b, message2.b)
self.assertIsNot(message1.a.a1, message2.a.a1)
self.assertIsNot(message1.a.a2, message2.a.a2)
self.assertIsNot(message1.b.c, message2.b.c)
self.assertIsNot(message1.b.c.c1, message2.b.c.c1)
self.assertIsNot(message1.b.c.c2, message2.b.c.c2)


class TestGenericType(unittest.TestCase):
assert message1 is not message2
assert message1.i is not message2.i
assert message1.a is not message2.a
assert message1.b is not message2.b
assert message1.a.a1 is not message2.a.a1
assert message1.a.a2 is not message2.a.a2
assert message1.b.c is not message2.b.c
assert message1.b.c.c1 is not message2.b.c.c1
assert message1.b.c.c2 is not message2.b.c.c2


class TestGenericType:
"""Testing GenericType class."""

def test_basic_operator(self):
"""[Foundation/Base/GenericType] - Basic Operators."""
a = basic_types.UBInt32(1)
b = basic_types.UBInt32(2)

self.assertEqual(a + 1, 2)
self.assertEqual(1 + a, 2)
self.assertEqual(b + 1, 3)
self.assertEqual(1 + b, 3)

self.assertEqual(a - 1, 0)
self.assertEqual(1 - a, 0)
self.assertEqual(b - 1, 1)
self.assertEqual(1 - b, 1)

self.assertEqual(a & 1, 1)
self.assertEqual(1 & a, 1)
self.assertEqual(b & 1, 0)
self.assertEqual(1 & b, 0)

self.assertEqual(a | 1, 1)
self.assertEqual(1 | a, 1)
self.assertEqual(b | 1, 3)
self.assertEqual(1 | b, 3)

self.assertEqual(a ^ 1, 0)
self.assertEqual(1 ^ a, 0)
self.assertEqual(b ^ 1, 3)
self.assertEqual(1 ^ b, 3)
assert a + 1 == 2
assert 1 + a == 2
assert b + 1 == 3
assert 1 + b == 3

assert a - 1 == 0
assert 1 - a == 0
assert b - 1 == 1
assert 1 - b == 1

assert a & 1 == 1
assert 1 & a == 1
assert b & 1 == 0
assert 1 & b == 0

assert a | 1 == 1
assert 1 | a == 1
assert b | 1 == 3
assert 1 | b == 3

assert a ^ 1 == 0
assert 1 ^ a == 0
assert b ^ 1 == 3
assert 1 ^ b == 3
Loading
Loading