Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 17 | import unittest |
| 18 | from mock import patch, MagicMock |
| 19 | from grpc_client.models_accessor import GRPCModelsAccessor |
| 20 | from grpc_client.resources import RESOURCES |
| 21 | |
| 22 | class FakeObj: |
| 23 | new = None |
| 24 | filter = None |
| 25 | |
| 26 | class FakeResource: |
| 27 | objects = FakeObj |
| 28 | |
| 29 | class FakeModel: |
| 30 | pass |
| 31 | |
| 32 | class FakeExistingModel: |
| 33 | pass |
| 34 | |
| 35 | mock_resources = { |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 36 | 'username~pass': { |
| 37 | 'test-model': FakeResource |
| 38 | } |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 41 | USERNAME = 'username' |
| 42 | PASSWORD = 'pass' |
| 43 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 44 | class GRPCModelsAccessor_Create_or_update_Test(unittest.TestCase): |
| 45 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 46 | def test_unkown_user(self): |
| 47 | """ |
| 48 | [GRPCModelsAccessor] get_model_from_classname: If a user does not have orm classes, raise |
| 49 | """ |
| 50 | data = { |
| 51 | "name": "test" |
| 52 | } |
| 53 | with self.assertRaises(Exception) as e: |
| 54 | GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD) |
| 55 | self.assertEqual(e.exception.message, "[XOS-TOSCA] User 'username' does not have ready resources") |
| 56 | |
| 57 | @patch.dict(RESOURCES, mock_resources, clear=True) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 58 | def test_unkown_module(self): |
| 59 | """ |
| 60 | [GRPCModelsAccessor] get_model_from_classname: If a model is not know by the grpc api, raise |
| 61 | """ |
| 62 | data = { |
| 63 | "name": "test" |
| 64 | } |
| 65 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 66 | GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD) |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 67 | self.assertEqual(e.exception.message, "[XOS-TOSCA] The model you are trying to create (name: test, class: i-do-not-exists) is not know by xos-core") |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 68 | |
| 69 | @patch.object(FakeResource.objects, "filter") |
| 70 | @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel)) |
| 71 | def test_new_model(self, mock_filter): |
| 72 | """ |
| 73 | [GRPCModelsAccessor] get_model_from_classname: should create a new model |
| 74 | """ |
| 75 | data = { |
| 76 | "name": "test" |
| 77 | } |
| 78 | with patch.dict(RESOURCES, mock_resources, clear=True): |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 79 | model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 80 | mock_filter.assert_called_with(name="test") |
| 81 | self.assertEqual(model, FakeModel) |
| 82 | |
| 83 | @patch.object(FakeResource.objects, "filter", MagicMock(return_value=[FakeExistingModel])) |
| 84 | def test_existing_model(self): |
| 85 | """ |
| 86 | [GRPCModelsAccessor] get_model_from_classname: should update an existing model |
| 87 | """ |
| 88 | data = { |
| 89 | "name": "test" |
| 90 | } |
| 91 | with patch.dict(RESOURCES, mock_resources, clear=True): |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 92 | model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 93 | self.assertEqual(model, FakeExistingModel) |
| 94 | |
| 95 | @patch.object(FakeResource.objects, "filter", MagicMock(return_value=['a', 'b'])) |
| 96 | def test_multiple_models(self): |
| 97 | """ |
| 98 | [GRPCModelsAccessor] get_model_from_classname: should raise an exception if multiple instances are found |
| 99 | """ |
| 100 | data = { |
| 101 | "name": "test" |
| 102 | } |
| 103 | with patch.dict(RESOURCES, mock_resources, clear=True): |
| 104 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 105 | GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 106 | self.assertEqual(e.exception.message, "[XOS-Tosca] Model test has multiple instances, I can't handle it") |
| 107 | |
| 108 | @patch.dict(RESOURCES, mock_resources, clear=True) |
| 109 | @patch.object(FakeResource.objects, "filter") |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 110 | @patch.object(FakeResource.objects, "new") |
| 111 | def test_find_model_without_name_property(self, mock_new, mock_filter): |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 112 | """ |
| 113 | [GRPCModelsAccessor] get_model_from_classname: should lookup a model by the first property |
| 114 | """ |
| 115 | data = { |
| 116 | 'foo': 'bar', |
| 117 | 'something': 'else' |
| 118 | } |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 119 | GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 120 | mock_filter.assert_called_with(foo="bar") |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 121 | mock_new.assert_called() |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 122 | |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame^] | 123 | @patch.dict(RESOURCES, mock_resources, clear=True) |
| 124 | @patch.object(FakeResource.objects, "new") |
| 125 | def test_model_without_properties(self, mock_new): |
| 126 | """ |
| 127 | [GRPCModelsAccessor] get_model_from_classname: should create a new model if not properties are specified |
| 128 | """ |
| 129 | data = { |
| 130 | } |
| 131 | GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
| 132 | mock_new.assert_called() |
| 133 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 134 | if __name__ == '__main__': |
| 135 | unittest.main() |