Skip to content

Commit

Permalink
tests: partially cover InfrastructureDrawer
Browse files Browse the repository at this point in the history
Add first tests to partially cover InfrastructureDrawer class. Some
refactoring is done to share some code in new tests.lib module.
  • Loading branch information
rezib committed Dec 3, 2024
1 parent e31a18c commit e469407
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 45 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

import os
import unittest
from pathlib import Path

from racksdb.drawers.base import Drawer, DefaultEquipmentColorSet, DefaultRackColorSet
from racksdb.drawers.parameters import DrawingParameters
from racksdb.generic.db import DBDictsLoader

from ..lib.common import drawing_schema_path


class FakeEquipmentType:
def __init__(self, id):
Expand Down Expand Up @@ -39,21 +39,10 @@ def __init__(self, id, tags):

class TestDrawer(unittest.TestCase):
def setUp(self):
# Try relative path and system paths sequentially for both the schema
# and example database. If none of these paths exist, gently skip the
# test with meaningful message.
current_dir = os.path.dirname(os.path.realpath(__file__))
drawings_schema_paths = [
Path(current_dir).joinpath("../../schemas/drawings.yml"),
Path("/usr/share/racksdb/schemas/drawings.yml"),
]
self.drawings_schema_path = None
for drawings_schema_path in drawings_schema_paths:
if drawings_schema_path.exists():
self.drawings_schema_path = drawings_schema_path
break
if self.drawings_schema_path is None:
self.skipTest("Unable to find drawings schema file to run test")
try:
self.drawings_schema_path = drawing_schema_path()
except FileNotFoundError as err:
self.skipTest(err)
parameters_raw = {
"colors": {
"equipments": [
Expand Down
69 changes: 69 additions & 0 deletions racksdb/tests/drawers/test_infrastructure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright (c) 2023 Rackslab
#
# This file is part of RacksDB.
#
# SPDX-License-Identifier: GPL-3.0-or-later

import unittest
import tempfile
from pathlib import Path

from racksdb import RacksDB
from racksdb.drawers.infrastructure import InfrastructureDrawer
from racksdb.drawers.parameters import DrawingParameters
from racksdb.generic.db import DBDictsLoader
from racksdb.errors import RacksDBError

from ..lib.common import drawing_schema_path, schema_path, db_path


class TestInfrastructureDrawer(unittest.TestCase):
def setUp(self):
try:
self.drawings_schema_path = drawing_schema_path()
self.schema_path = schema_path()
self.db_path = db_path()
except FileNotFoundError as err:
self.skipTest(err)
self.parameters = DrawingParameters.load(
DBDictsLoader({}), self.drawings_schema_path
)
self.db = RacksDB.load(schema=self.schema_path, db=self.db_path)

def test_draw_png(self):
with tempfile.TemporaryDirectory() as tmpdir:
filename = Path(tmpdir) / "output.png"
drawer = InfrastructureDrawer(
self.db, "mercury", filename, "png", self.parameters, None, "yaml"
)
drawer.draw()
self.assertTrue(filename.exists())

def test_draw_svg(self):
with tempfile.TemporaryDirectory() as tmpdir:
filename = Path(tmpdir) / "output.svg"
drawer = InfrastructureDrawer(
self.db, "mercury", filename, "svg", self.parameters, None, "yaml"
)
drawer.draw()
self.assertTrue(filename.exists())

def test_draw_pdf(self):
with tempfile.TemporaryDirectory() as tmpdir:
filename = Path(tmpdir) / "output.pdf"
drawer = InfrastructureDrawer(
self.db, "mercury", filename, "pdf", self.parameters, None, "yaml"
)
drawer.draw()
self.assertTrue(filename.exists())

def test_draw_not_existing_infrastructure(self):
with tempfile.TemporaryDirectory() as tmpdir:
filename = Path(tmpdir) / "output.png"
with self.assertRaisesRegex(
RacksDBError, "^Unable to find infrastructure fail in database$"
):
InfrastructureDrawer(
self.db, "fail", filename, "png", self.parameters, None, "yaml"
)
self.assertFalse(filename.exists())
Empty file added racksdb/tests/lib/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions racksdb/tests/lib/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (c) 2023 Rackslab
#
# This file is part of RacksDB.
#
# SPDX-License-Identifier: GPL-3.0-or-later

import os
from pathlib import Path

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


def _first_path(paths, error):
for path in paths:
if path.exists():
return path
raise FileNotFoundError(error)


def drawing_schema_path():
return _first_path(
[
Path(CURRENT_DIR).joinpath("../../../schemas/drawings.yml"),
Path("/usr/share/racksdb/schemas/drawings.yml"),
],
"Unable to find drawing schema to run tests",
)


def schema_path():
return _first_path(
[
Path(CURRENT_DIR).joinpath("../../schemas/racksdb.yml"),
Path("/usr/share/racksdb/schemas/racksdb.yml"),
],
"Unable to find schema to run tests",
)


def db_path():
return _first_path(
[
Path(CURRENT_DIR).joinpath("../../../examples/db"),
Path("/usr/share/doc/racksdb/examples/db"),
],
"Unable to find database to run tests",
)
35 changes: 7 additions & 28 deletions racksdb/tests/tests_db_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,20 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

import os
from pathlib import Path
import unittest

from racksdb import RacksDB

from ..lib.common import schema_path, db_path


class TestDBLoad(unittest.TestCase):
def setUp(self):
# Try relative path and system paths sequentially for both the schema
# and example database. If none of these paths exist, gently skip the
# test with meaningful message.
current_dir = os.path.dirname(os.path.realpath(__file__))
schema_paths = [
Path(current_dir).joinpath("../../schemas/racksdb.yml"),
Path("/usr/share/racksdb/schemas/racksdb.yml"),
]
self.schema_path = None
for schema_path in schema_paths:
if schema_path.exists():
self.schema_path = schema_path
break
if self.schema_path is None:
self.skipTest("Unable to find schema file to run test")
db_paths = [
Path(current_dir).joinpath("../../examples/db"),
Path("/usr/share/doc/racksdb/examples/db"),
]
self.db_path = None
for db_path in db_paths:
if db_path.exists():
self.db_path = db_path
break
if self.db_path is None:
self.skipTest("Unable to find db file to run test")
try:
self.schema_path = schema_path()
self.db_path = db_path()
except FileNotFoundError as err:
self.skipTest(err)

def test_load(self):
RacksDB.load(schema=self.schema_path, db=self.db_path)
Expand Down

0 comments on commit e469407

Please sign in to comment.