API for rectangle & co

Ref #498
This commit is contained in:
Julien Duponchelle committed 2016-06-20 18:45:31 +02:00
1 parent 6949976d12
commit 85ebac7eb3
12 files changed
+534 -6

No files matched your search

+74
View File
@@ -0,0 +1,74 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from unittest.mock import MagicMock
import pytest
import uuid
from tests.utils import AsyncioMagicMock
from gns3server.controller.item import Item
from gns3server.controller.project import Project
@pytest.fixture
def project(controller, async_run):
return async_run(controller.add_project())
@pytest.fixture
def item(project):
return Item(project, None, svg="<svg></svg>")
def test_init_without_uuid(project):
item = Item(project, None, svg="<svg></svg>")
assert item.id is not None
def test_init_with_uuid(project):
id = str(uuid.uuid4())
item = Item(project, id, svg="<svg></svg>")
assert item.id == id
def test_json(project):
i = Item(project, None, svg="<svg></svg>")
assert i.__json__() == {
"item_id": i.id,
"project_id": project.id,
"x": i.x,
"y": i.y,
"z": i.z
}
assert i.__json__(topology_dump=True) == {
"item_id": i.id,
"x": i.x,
"y": i.y,
"z": i.z
}
def test_update(item, project, async_run, controller):
controller._notification = AsyncioMagicMock()
project.dump = MagicMock()
async_run(item.update(x=42))
assert item.x == 42
controller._notification.emit.assert_called_with("item.updated", item.__json__())
assert project.dump.called
+26
View File
@@ -213,6 +213,32 @@ def test_deleteLink(async_run, project, controller):
assert len(project._links) == 0
def test_addItem(async_run, project, controller):
controller.notification.emit = MagicMock()
item = async_run(project.add_item(None, svg="<svg></svg>"))
assert len(project._items) == 1
controller.notification.emit.assert_any_call("item.created", item.__json__())
def test_getItem(async_run, project):
item = async_run(project.add_item(None))
assert project.get_item(item.id) == item
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
project.get_item("test")
def test_deleteItem(async_run, project, controller):
assert len(project._items) == 0
item = async_run(project.add_item())
assert len(project._items) == 1
controller._notification = MagicMock()
async_run(project.delete_item(item.id))
controller.notification.emit.assert_any_call("item.deleted", item.__json__())
assert len(project._items) == 0
def test_delete(async_run, project, controller):
assert os.path.exists(project.path)
async_run(project.delete())
+7 -2
View File
@@ -37,7 +37,8 @@ def test_project_to_topology_empty(tmpdir):
"topology": {
"nodes": [],
"links": [],
"computes": []
"computes": [],
"items": []
},
"type": "topology",
"version": __version__
@@ -57,11 +58,14 @@ def test_basic_topology(tmpdir, async_run, controller):
async_run(link.add_node(node1, 0, 0))
async_run(link.add_node(node2, 0, 0))
item = async_run(project.add_item(svg="<svg></svg>"))
topo = project_to_topology(project)
assert len(topo["topology"]["nodes"]) == 2
assert node1.__json__(topology_dump=True) in topo["topology"]["nodes"]
assert topo["topology"]["links"][0] == link.__json__(topology_dump=True)
assert topo["topology"]["computes"][0] == compute.__json__(topology_dump=True)
assert topo["topology"]["items"][0] == item.__json__(topology_dump=True)
def test_load_topology(tmpdir):
@@ -72,7 +76,8 @@ def test_load_topology(tmpdir):
"topology": {
"nodes": [],
"links": [],
"computes": []
"computes": [],
"items": []
},
"type": "topology",
"version": __version__}