diff options
Diffstat (limited to 'conductor/conductor/tests/unit/music/test_api.py')
-rw-r--r-- | conductor/conductor/tests/unit/music/test_api.py | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/conductor/conductor/tests/unit/music/test_api.py b/conductor/conductor/tests/unit/music/test_api.py new file mode 100644 index 0000000..07acac0 --- /dev/null +++ b/conductor/conductor/tests/unit/music/test_api.py @@ -0,0 +1,249 @@ +# +# ------------------------------------------------------------------------- +# Copyright (c) 2018 Intel Corporation Intellectual Property +# +# 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 unittest + +import mock +from conductor.common import rest +from conductor.common.music.api import MusicAPI +from oslo_config import cfg + + +class TestMusicApi(unittest.TestCase): + + def setUp(self): + cfg.CONF.set_override('debug', True, 'music_api') + self.mock_lock_id = mock.patch.object(MusicAPI, '_lock_id_create', + return_value='12345678') + self.mock_lock_acquire = mock.patch.object(MusicAPI, + '_lock_id_acquire', + return_value=True) + self.mock_lock_release = mock.patch.object(MusicAPI, + '_lock_id_release', + return_value=True) + self.mock_lock_id.start() + self.mock_lock_acquire.start() + self.mock_lock_release.start() + self.music_api = MusicAPI() + + def tearDown(self): + mock.patch.stopall() + + @mock.patch('conductor.common.rest.REST.request') + def test_lock_id_create(self, rest_mock): + self.mock_lock_id.stop() + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + response.text = '12345678' + rest_mock.return_value = response + self.assertEquals('12345678', self.music_api._lock_id_create('temp')) + self.mock_lock_id.start() + + @mock.patch('conductor.common.rest.REST.request') + def test_lock_id_acquire(self, rest_mock): + self.mock_lock_acquire.stop() + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + response.text = 'true' + rest_mock.return_value = response + self.assertEquals(True, self.music_api._lock_id_acquire('12345678')) + self.mock_lock_acquire.start() + + @mock.patch('conductor.common.rest.REST.request') + def test_lock_id_release(self, rest_mock): + self.mock_lock_release.stop() + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api._lock_id_release('12345678')) + self.mock_lock_release.start() + + def test_lock_name_generate(self): + expected = 'keyspace.votecount.pk_value' + self.assertEqual(expected, + self.music_api._lock_name_generate('keyspace', + 'votecount', + 'pk_value')) + + def test_lock_create(self): + expected = 'keyspace.votecount.pk_value' + self.assertEquals(expected, self.music_api.lock_create('keyspace', + 'votecount', + 'pk_value')) + + @mock.patch('conductor.common.rest.REST.request') + def test_lock_release(self, rest_mock): + self.mock_lock_release.stop() + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api.lock_release('test-lock-name')) + self.mock_lock_release.start() + + @mock.patch('conductor.common.rest.REST.request') + def test_lock_delete(self, rest_mock): + lock_name = 'test-lock-name' + self.music_api.lock_ids[lock_name] = self.music_api._lock_id_create( + lock_name) + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api.lock_delete('test-lock-name')) + + @mock.patch('conductor.common.rest.REST.request') + def test_keyspace_create(self, rest_mock): + lock_name = 'test-lock-name' + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api.keyspace_create('keyspace')) + + @mock.patch('conductor.common.rest.REST.request') + def test_keyspace_delete(self, rest_mock): + lock_name = 'test-lock-name' + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api.keyspace_delete('keyspace')) + + @mock.patch('conductor.common.rest.REST.request') + def test_row_create(self, rest_mock): + keyspace = 'test-keyspace' + kwargs = {'keyspace': keyspace, 'table': 'votecount', + 'pk_name': 'name'} + kwargs['pk_value'] = 'test-name' + kwargs['values'] = {'name': 'test-name', 'count': 0} + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api.row_create(**kwargs)) + + @mock.patch('conductor.common.rest.REST.request') + def test_row_update(self, rest_mock): + keyspace = 'test-keyspace' + kwargs = {'keyspace': keyspace, 'table': 'votecount', + 'pk_name': 'name'} + count = 2 + kwargs['pk_value'] = 'test-name2' + kwargs['values'] = {'count': count} + kwargs['atomic'] = True + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api.row_update(**kwargs)) + + @mock.patch('conductor.common.rest.REST.request') + def test_row_read(self, rest_mock): + keyspace = 'test-keyspace' + kwargs = {'keyspace': keyspace, 'table': 'votecount'} + response = mock.MagicMock() + response.status_code = 200 + response.json.return_value = {'row 1': {'count': 2}} + rest_mock.return_value = response + self.assertEquals({'row 1': {'count': 2}}, + self.music_api.row_read(**kwargs)) + + @mock.patch('conductor.common.rest.REST.request') + def test_row_delete(self, rest_mock): + keyspace = 'test-keyspace' + kwargs = {'keyspace': keyspace, 'table': 'votecount', + 'pk_name': 'name'} + kwargs['pk_value'] = 'test-name2' + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api.row_delete(**kwargs)) + + def test_table_path_generate(self): + keyspace = 'test-keyspace' + kwargs = { + 'keyspace': keyspace, + 'table': 'votecount' + } + expected = '/keyspaces/test-keyspace/tables/votecount/' + self.assertEqual(expected, + self.music_api._table_path_generate(**kwargs)) + + @mock.patch('conductor.common.rest.REST.request') + def test_table_create(self, rest_mock): + # Create the table + keyspace = 'test-keyspace' + kwargs = { + 'keyspace': keyspace, + 'table': 'votecount', + 'schema': { + 'name': 'text', + 'count': 'varint', + 'PRIMARY KEY': '(name)' + } + } + + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api.table_create(**kwargs)) + + @mock.patch('conductor.common.rest.REST.request') + def test_table_delete(self, rest_mock): + # Delete the table + keyspace = 'test-keyspace' + kwargs = { + 'keyspace': keyspace, + 'table': 'votecount' + } + + response = mock.MagicMock() + response.status_code = 200 + response.ok = True + rest_mock.return_value = response + self.assertEquals(True, self.music_api.table_delete(**kwargs)) + + def test_version(self): + with mock.patch.object(rest.REST, 'request', + autospec=True) as rest_mock: + response = mock.MagicMock() + response.status = 200 + response.text = 'MUSIC:2.2.14' + rest_mock.return_value = response + self.assertEquals('MUSIC:2.2.14', self.music_api.version()) + + def test_row_url_path(self): + keyspace = 'test-keyspace' + expected = '/keyspaces/test-keyspace/tables/votecount/rows' + self.assertEqual(expected, + self.music_api._row_url_path(keyspace, 'votecount', + None, None)) + expected += '?%s=%s' % ('pk_name', 'pk_value') + self.assertEqual(expected, + self.music_api._row_url_path(keyspace, 'votecount', + 'pk_name', 'pk_value')) + + +if __name__ == "__main__": + unittest.main() |