From ce9fd3cb255f92a05eb36ce716d66254fc165bea Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 21 Jan 2015 17:11:21 +0100 Subject: [PATCH] Test start / stop. And check if the mocked function is really called --- docs/api/examples/post_vpcs.txt | 2 +- tests/api/base.py | 2 +- tests/api/test_vpcs.py | 18 +++++++++++------- tests/utils.py | 3 ++- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/docs/api/examples/post_vpcs.txt b/docs/api/examples/post_vpcs.txt index bcd8a539..395b109e 100644 --- a/docs/api/examples/post_vpcs.txt +++ b/docs/api/examples/post_vpcs.txt @@ -21,5 +21,5 @@ X-ROUTE: /vpcs "project_uuid": "a1e920ca-338a-4e9f-b363-aa607b09dd80", "script_file": null, "startup_script": null, - "uuid": "009a7260-e44c-4349-8df7-08668a3c4e17" + "uuid": "6370f75e-0a48-4e2b-95a8-0140da6ef1fb" } diff --git a/tests/api/base.py b/tests/api/base.py index e8fac3f8..6de05b45 100644 --- a/tests/api/base.py +++ b/tests/api/base.py @@ -42,7 +42,7 @@ class Query: self._port = port self._host = host - def post(self, path, body, **kwargs): + def post(self, path, body={}, **kwargs): return self._fetch("POST", path, body, **kwargs) def get(self, path, **kwargs): diff --git a/tests/api/test_vpcs.py b/tests/api/test_vpcs.py index 7b13e389..00ce14f4 100644 --- a/tests/api/test_vpcs.py +++ b/tests/api/test_vpcs.py @@ -19,7 +19,7 @@ import pytest import os from tests.api.base import server, loop, project from tests.utils import asyncio_patch -from unittest.mock import patch +from unittest.mock import patch, Mock from gns3server.modules.vpcs.vpcs_vm import VPCSVM @@ -99,11 +99,15 @@ def test_vpcs_delete_nio(server, vm): assert response.route == "/vpcs/{uuid}/ports/{port_id}/nio" -def test_vpcs_start(): - # assert True == False - pass +def test_vpcs_start(server, vm): + with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock: + response = server.post("/vpcs/{}/start".format(vm["uuid"])) + assert mock.called + assert response.status == 200 -def test_vpcs_stop(): - # assert True == False - pass +def test_vpcs_stop(server, vm): + with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.stop", return_value=True) as mock: + response = server.post("/vpcs/{}/stop".format(vm["uuid"])) + assert mock.called + assert response.status == 200 diff --git a/tests/utils.py b/tests/utils.py index 3ce32530..bc8dc5dc 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -39,7 +39,8 @@ class _asyncio_patch: def __enter__(self): """Used when enter in the with block""" self._patcher = patch(self.function, return_value=self._fake_anwser()) - self._patcher.start() + mock_class = self._patcher.start() + return mock_class def __exit__(self, *exc_info): """Used when leaving the with block"""