Added unit test for db/api

Change-Id: Ibec0a3dfa253113728eb2b4df3690d16a3933f99
This commit is contained in:
leilei9547
2014-08-11 14:31:24 -07:00
parent a5bb37b1e8
commit c8c879d0c4
6 changed files with 193 additions and 26 deletions

View File

@@ -116,5 +116,5 @@ def patch_machine(session, updater, machine_id, **kwargs):
@utils.wrap_to_dict(RESP_FIELDS)
def del_machine(session, deleter, machine_id, **kwargs):
"""Delete a machine."""
machine = utils.get_db_object(session, models.Switch, id=machine_id)
machine = utils.get_db_object(session, models.Machine, id=machine_id)
return utils.del_db_object(session, machine)

View File

@@ -302,7 +302,9 @@ def filter_output(filter_callbacks, filters, obj, missing_ok=False):
if missing_ok:
continue
else:
return False
raise exception.InvalidResponse(
'%s is not in %s' % (callback_key, obj)
)
if not callback_value(
filters[callback_key], obj[callback_key]
):
@@ -378,6 +380,7 @@ def output_validates(**kwargs_validators):
_output_validates(kwargs_validators, obj_item)
else:
_output_validates(kwargs_validators, obj)
return obj
return wrapper
return decorator

View File

@@ -26,7 +26,6 @@ from sqlalchemy import DateTime
from sqlalchemy import Enum
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy import Float
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
@@ -254,7 +253,7 @@ class FieldMixin(HelperMixin):
class InstallerMixin(HelperMixin):
name = Column(String(80))
instance_name = Column(String(80), unique=True)
settings = Column(MutableDict.as_mutable(JSONEncoded), default={})
settings = Column(JSONEncoded, default={})
def validate(self):
if not self.name:

View File

@@ -0,0 +1,182 @@
# Copyright 2014 Huawei Technologies Co. Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import logging
import os
import unittest2
from compass.db.api import database
from compass.db.api import machine
from compass.db.api import switch
from compass.db.api import user as user_api
from compass.db import exception
from compass.utils import flags
from compass.utils import logsetting
from compass.utils import setting_wrapper as setting
reload(setting)
os.environ['COMPASS_IGNORE_SETTING'] = 'true'
class BaseTest(unittest2.TestCase):
"""Base Class for unit test."""
def setUp(self):
super(BaseTest, self).setUp()
database.init('sqlite://')
database.create_db()
self.user_object = (
user_api.get_user_object(
setting.COMPASS_ADMIN_EMAIL
)
)
def tearDown(self):
super(BaseTest, self).setUp()
database.drop_db()
class TestGetMachine(BaseTest):
"""Test get machine."""
def setUp(self):
super(TestGetMachine, self).setUp()
logsetting.init()
def tearDown(self):
super(TestGetMachine, self).tearDown()
def test_get_machine(self):
switch.add_switch_machine(
self.user_object,
1,
mac='28:6e:d4:46:c4:25',
port=1
)
get_machine = machine.get_machine(
self.user_object,
1
)
self.assertIsNotNone(get_machine)
class TestListMachines(BaseTest):
"""Test list machines."""
def setUp(self):
super(TestListMachines, self).setUp()
logsetting.init()
def tearDown(self):
super(TestListMachines, self).tearDown()
def test_list_machines(self):
switch.add_switch_machine(
self.user_object,
1,
mac='28:6e:d4:46:c4:25',
port=1
)
list_machine = machine.list_machines(self.user_object)
self.assertIsNotNone(list_machine)
class TestUpdateMachine(BaseTest):
"""Test update machine."""
def setUp(self):
super(TestUpdateMachine, self).setUp()
logsetting.init()
def tearDown(self):
super(TestUpdateMachine, self).tearDown()
def test_update_machine(self):
switch.add_switch_machine(
self.user_object,
1,
mac='28:6e:d4:46:c4:25',
port=1
)
machine.update_machine(
self.user_object,
1,
tag='test'
)
update_machine = machine.list_machines(self.user_object)
expected = {'tag': 'test'}
self.assertTrue(
item in update_machine[0].items() for item in expected.items()
)
class TestPatchMachine(BaseTest):
"""Test patch machine."""
def setUp(self):
super(TestPatchMachine, self).setUp()
logsetting.init()
def tearDown(self):
super(TestPatchMachine, self).tearDown()
def test_patch_machine(self):
switch.add_switch_machine(
self.user_object,
1,
mac='28:6e:d4:46:c4:25',
port=1
)
machine.patch_machine(
self.user_object,
1,
tag={'patched_tag': 'test'}
)
patch_machine = machine.list_machines(self.user_object)
expected = {'patched_tag': 'test'}
self.assertTrue(
item in patch_machine[0].items() for item in expected.items()
)
class TestDelMachine(BaseTest):
"""Test delete machine."""
def setUp(self):
super(TestDelMachine, self).setUp()
logsetting.init()
def tearDown(self):
super(TestDelMachine, self).tearDown()
def test_del_machine(self):
switch.add_switch_machine(
self.user_object,
1,
mac='28:6e:d4:46:c4:25',
port=1
)
machine.del_machine(
self.user_object,
1
)
del_machine = machine.list_machines(self.user_object)
self.assertEqual([], del_machine)
if __name__ == '__main__':
flags.init()
logsetting.init()
unittest2.main()

View File

@@ -17,20 +17,15 @@ import logging
import os
import unittest2
os.environ['COMPASS_IGNORE_SETTING'] = 'true'
from compass.utils import setting_wrapper as setting
reload(setting)
from compass.db.api import database
from compass.db.api import permission
from compass.db.api import user as user_api
from compass.db import exception
from compass.utils import flags
from compass.utils import logsetting
from compass.utils import setting_wrapper as setting
os.environ['COMPASS_IGNORE_SETTING'] = 'true'
class BaseTest(unittest2.TestCase):
@@ -82,8 +77,5 @@ class TestGetPermission(BaseTest):
get_permission = permission.get_permission(self.user_object, 1)
self.assertIsNotNone(get_permission)
if __name__ == '__main__':
flags.init()
logsetting.init()
unittest2.main()

View File

@@ -17,19 +17,13 @@ import logging
import os
import unittest2
os.environ['COMPASS_IGNORE_SETTING'] = 'true'
from compass.utils import setting_wrapper as setting
reload(setting)
from compass.db.api import database
from compass.db.api import user as user_api
from compass.db import exception
from compass.utils import flags
from compass.utils import logsetting
from compass.utils import setting_wrapper as setting
os.environ['COMPASS_IGNORE_SETTING'] = 'true'
class BaseTest(unittest2.TestCase):
@@ -331,8 +325,5 @@ class TestUpdatePermissions(BaseTest):
item in add_permission[0].items() for item in expected.items()
)
if __name__ == '__main__':
flags.init()
logsetting.init()
unittest2.main()