#1715 - Added Link class in physical_layer.py. Also added NIC class in physical_layer.py for #1672. Added attributes and public API functions. test_physical_layer.py ready to house the tests once logic has been implemented.
This commit is contained in:
0
tests/unit_tests/_primaite/_simulator/__init__.py
Normal file
0
tests/unit_tests/_primaite/_simulator/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import re
|
||||
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.physical_layer import generate_mac_address
|
||||
|
||||
|
||||
def test_mac_address_generation():
|
||||
"""Tests random mac address generation."""
|
||||
mac_address = generate_mac_address()
|
||||
assert re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac_address)
|
||||
|
||||
|
||||
def test_mac_address_with_oui():
|
||||
"""Tests random mac address generation with oui."""
|
||||
oui = "aa:bb:cc"
|
||||
mac_address = generate_mac_address(oui=oui)
|
||||
assert re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac_address)
|
||||
assert mac_address[:8] == oui
|
||||
|
||||
|
||||
def test_invalid_oui_mac_address():
|
||||
"""Tests random mac address generation fails with invalid oui."""
|
||||
invalid_oui = "aa-bb-cc"
|
||||
with pytest.raises(ValueError):
|
||||
generate_mac_address(oui=invalid_oui)
|
||||
79
tests/unit_tests/_primaite/_simulator/test_core.py
Normal file
79
tests/unit_tests/_primaite/_simulator/test_core.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from typing import Callable, Dict, List, Literal, Tuple
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from primaite.simulator.core import SimComponent
|
||||
|
||||
|
||||
class TestIsolatedSimComponent:
|
||||
"""Test the SimComponent class in isolation."""
|
||||
|
||||
def test_data_validation(self):
|
||||
"""
|
||||
Test that our derived class does not interfere with pydantic data validation.
|
||||
|
||||
This test may seem like it's simply validating pydantic data validation, but
|
||||
actually it is here to give us assurance that any custom functionality we add
|
||||
to the SimComponent does not interfere with pydantic.
|
||||
"""
|
||||
|
||||
class TestComponent(SimComponent):
|
||||
name: str
|
||||
size: Tuple[float, float]
|
||||
|
||||
def describe_state(self) -> Dict:
|
||||
return {}
|
||||
|
||||
comp = TestComponent(name="computer", size=(5, 10))
|
||||
assert isinstance(comp, TestComponent)
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
invalid_comp = TestComponent(name="computer", size="small") # noqa
|
||||
|
||||
def test_serialisation(self):
|
||||
"""Validate that our added functionality does not interfere with pydantic."""
|
||||
|
||||
class TestComponent(SimComponent):
|
||||
name: str
|
||||
size: Tuple[float, float]
|
||||
|
||||
def describe_state(self) -> Dict:
|
||||
return {}
|
||||
|
||||
comp = TestComponent(name="computer", size=(5, 10))
|
||||
dump = comp.model_dump()
|
||||
assert dump == {"name": "computer", "size": (5, 10)}
|
||||
|
||||
def test_apply_action(self):
|
||||
"""Validate that we can override apply_action behaviour and it updates the state of the component."""
|
||||
|
||||
class TestComponent(SimComponent):
|
||||
name: str
|
||||
status: Literal["on", "off"] = "off"
|
||||
|
||||
def describe_state(self) -> Dict:
|
||||
return {}
|
||||
|
||||
def _possible_actions(self) -> Dict[str, Callable[[List[str]], None]]:
|
||||
return {
|
||||
"turn_off": self._turn_off,
|
||||
"turn_on": self._turn_on,
|
||||
}
|
||||
|
||||
def _turn_off(self, options: List[str]) -> None:
|
||||
assert len(options) == 0, "This action does not support options."
|
||||
self.status = "off"
|
||||
|
||||
def _turn_on(self, options: List[str]) -> None:
|
||||
assert len(options) == 0, "This action does not support options."
|
||||
self.status = "on"
|
||||
|
||||
comp = TestComponent(name="computer", status="off")
|
||||
|
||||
assert comp.status == "off"
|
||||
comp.apply_action(["turn_on"])
|
||||
assert comp.status == "on"
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
comp.apply_action(["do_nothing"])
|
||||
Reference in New Issue
Block a user