Skip to content

Commit

Permalink
Merge pull request #193 from kytos-ng/upgrade/python
Browse files Browse the repository at this point in the history
Update test dependencies
  • Loading branch information
viniarck authored Mar 28, 2024
2 parents ac5e20d + e5e2ea2 commit f62f672
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 77 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ All notable changes to the ``topology`` project will be documented in this file.
[UNRELEASED] - Under development
********************************

Changed
=======
- Updated python environment installation from 3.9 to 3.11
- Updated test dependencies

[2023.2.0] - 2024-02-16
***********************

Expand Down
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ To install this NApp, first, make sure to have the same venv activated as you ha
$ git clone https://github.com/kytos-ng/topology.git
$ cd topology
$ python setup.py develop
$ python3 -m pip install --editable .
To install the kytos environment, please follow our
`development environment setup <https://github.com/kytos-ng/documentation/blob/master/tutorials/napps/development_environment_setup.rst>`_.

Requirements
============
Expand Down
6 changes: 3 additions & 3 deletions controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def upsert_switch(self, dpid: str, switch_dict: dict) -> Optional[dict]:
updated = self.db.switches.find_one_and_update(
{"_id": dpid},
{
"$set": model.dict(exclude={"inserted_at"}),
"$set": model.model_dump(exclude={"inserted_at"}),
"$setOnInsert": {"inserted_at": utc_now},
},
return_document=ReturnDocument.AFTER,
Expand Down Expand Up @@ -208,7 +208,7 @@ def upsert_link(self, link_id: str, link_dict: dict) -> dict:
updated = self.db.links.find_one_and_update(
{"_id": link_id},
{
"$set": model.dict(exclude={"inserted_at"}),
"$set": model.model_dump(exclude={"inserted_at"}),
"$setOnInsert": {"inserted_at": utc_now},
},
return_document=ReturnDocument.AFTER,
Expand Down Expand Up @@ -311,7 +311,7 @@ def upsert_interface_details(
"special_available_tags": special_available_tags,
"special_tags": special_tags,
"updated_at": utc_now
}).dict(exclude={"inserted_at"})
}).model_dump(exclude={"inserted_at"})
updated = self.db.interface_details.find_one_and_update(
{"_id": id_},
{
Expand Down
50 changes: 26 additions & 24 deletions db/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
# pylint: disable=no-name-in-module

from datetime import datetime
from typing import Dict, List, Optional
from typing import Dict, Optional

from pydantic import BaseModel, Field, conlist, validator
from pydantic import BaseModel, Field, field_validator
from typing_extensions import Annotated


class DocumentBaseModel(BaseModel):
"""DocumentBaseModel."""

id: str = Field(None, alias="_id")
inserted_at: Optional[datetime]
updated_at: Optional[datetime]
inserted_at: Optional[datetime] = None
updated_at: Optional[datetime] = None

def dict(self, **kwargs) -> dict:
def model_dump(self, **kwargs) -> dict:
"""Model to dict."""
values = super().dict(**kwargs)
values = super().model_dump(**kwargs)
if "id" in values and values["id"]:
values["_id"] = values["id"]
if "exclude" in kwargs and "_id" in kwargs["exclude"]:
Expand All @@ -37,28 +38,28 @@ class InterfaceSubDoc(BaseModel):
nni: bool = False
lldp: bool
switch: str
link: Optional[str]
link_side: Optional[str]
link: Optional[str] = None
link_side: Optional[str] = None
metadata: dict = {}
updated_at: Optional[datetime]
updated_at: Optional[datetime] = None


class SwitchDoc(DocumentBaseModel):
"""Switch DB Document Model."""

enabled: bool
data_path: Optional[str]
hardware: Optional[str]
manufacturer: Optional[str]
software: Optional[str]
connection: Optional[str]
ofp_version: Optional[str]
serial: Optional[str]
data_path: Optional[str] = None
hardware: Optional[str] = None
manufacturer: Optional[str] = None
software: Optional[str] = None
connection: Optional[str] = None
ofp_version: Optional[str] = None
serial: Optional[str] = None
metadata: dict = {}
interfaces: List[InterfaceSubDoc] = []
interfaces: list[InterfaceSubDoc] = []

@validator("interfaces", pre=True)
def preset_interfaces(cls, v, values, **kwargs) -> List[InterfaceSubDoc]:
@field_validator("interfaces", mode="before")
def preset_interfaces(cls, v, values, **kwargs) -> list[InterfaceSubDoc]:
"""Preset interfaces."""
if isinstance(v, dict):
return list(v.values())
Expand Down Expand Up @@ -104,7 +105,8 @@ class LinkDoc(DocumentBaseModel):

enabled: bool
metadata: dict = {}
endpoints: conlist(InterfaceIdSubDoc, min_items=2, max_items=2)
endpoints: Annotated[list[InterfaceIdSubDoc],
Field(min_length=2, max_length=2)]

@staticmethod
def projection() -> dict:
Expand All @@ -124,7 +126,7 @@ def projection() -> dict:
class InterfaceDetailDoc(DocumentBaseModel):
"""InterfaceDetail DB Document Model."""

available_tags: Dict[str, List[List[int]]]
tag_ranges: Dict[str, List[List[int]]]
special_available_tags: Dict[str, List[str]]
special_tags: Dict[str, List[str]]
available_tags: Dict[str, list[list[int]]]
tag_ranges: Dict[str, list[list[int]]]
special_available_tags: Dict[str, list[str]]
special_tags: Dict[str, list[str]]
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ class Linter(SimpleCommand):
def run(self):
"""Run yala."""
print('Yala is running. It may take several seconds...')
check_call('yala *.py controllers db tests', shell=True)
try:
check_call('yala *.py controllers db tests', shell=True)
print('No linter error found.')
except CalledProcessError:
print('Linter check failed. Fix the error(s) above and try again.')
sys.exit(-1)


class KytosInstall:
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def get_switch_mock(of_version, connection_state=ConnectionState.NEW,
address = Mock()
port = Mock()
socket = Mock()
switch.connection = Connection(address, port, socket)
transport = Mock()
switch.connection = Connection(address, port, socket, transport)
switch.connection.protocol.version = of_version
switch.connection.state = connection_state
return switch
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def test_document_base_model_dict() -> None:
utcnow = datetime.utcnow()
payload = {"_id": _id, "inserted_at": utcnow, "updated_at": utcnow}
model = DocumentBaseModel(**payload)
assert model.dict() == {**payload, **{"id": _id}}
assert "_id" not in model.dict(exclude={"_id"})
assert model.model_dump() == {**payload, **{"id": _id}}
assert "_id" not in model.model_dump(exclude={"_id"})


def test_switch_doc_preset_interfaces() -> None:
Expand Down
Loading

0 comments on commit f62f672

Please sign in to comment.