Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 1 | import unittest |
| 2 | from mock import patch, MagicMock |
| 3 | from grpc_client.models_accessor import GRPCModelsAccessor |
| 4 | from grpc_client.resources import RESOURCES |
| 5 | |
| 6 | class FakeObj: |
| 7 | new = None |
| 8 | filter = None |
| 9 | |
| 10 | class FakeResource: |
| 11 | objects = FakeObj |
| 12 | |
| 13 | class FakeModel: |
| 14 | pass |
| 15 | |
| 16 | class FakeExistingModel: |
| 17 | pass |
| 18 | |
| 19 | mock_resources = { |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 20 | 'username~pass': { |
| 21 | 'test-model': FakeResource |
| 22 | } |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 25 | USERNAME = 'username' |
| 26 | PASSWORD = 'pass' |
| 27 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 28 | class GRPCModelsAccessor_Create_or_update_Test(unittest.TestCase): |
| 29 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 30 | def test_unkown_user(self): |
| 31 | """ |
| 32 | [GRPCModelsAccessor] get_model_from_classname: If a user does not have orm classes, raise |
| 33 | """ |
| 34 | data = { |
| 35 | "name": "test" |
| 36 | } |
| 37 | with self.assertRaises(Exception) as e: |
| 38 | GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD) |
| 39 | self.assertEqual(e.exception.message, "[XOS-TOSCA] User 'username' does not have ready resources") |
| 40 | |
| 41 | @patch.dict(RESOURCES, mock_resources, clear=True) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 42 | def test_unkown_module(self): |
| 43 | """ |
| 44 | [GRPCModelsAccessor] get_model_from_classname: If a model is not know by the grpc api, raise |
| 45 | """ |
| 46 | data = { |
| 47 | "name": "test" |
| 48 | } |
| 49 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 50 | GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD) |
Matteo Scandolo | d12be21 | 2017-07-07 10:44:34 -0700 | [diff] [blame] | 51 | 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] | 52 | |
| 53 | @patch.object(FakeResource.objects, "filter") |
| 54 | @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel)) |
| 55 | def test_new_model(self, mock_filter): |
| 56 | """ |
| 57 | [GRPCModelsAccessor] get_model_from_classname: should create a new model |
| 58 | """ |
| 59 | data = { |
| 60 | "name": "test" |
| 61 | } |
| 62 | with patch.dict(RESOURCES, mock_resources, clear=True): |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 63 | model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 64 | mock_filter.assert_called_with(name="test") |
| 65 | self.assertEqual(model, FakeModel) |
| 66 | |
| 67 | @patch.object(FakeResource.objects, "filter", MagicMock(return_value=[FakeExistingModel])) |
| 68 | def test_existing_model(self): |
| 69 | """ |
| 70 | [GRPCModelsAccessor] get_model_from_classname: should update an existing model |
| 71 | """ |
| 72 | data = { |
| 73 | "name": "test" |
| 74 | } |
| 75 | with patch.dict(RESOURCES, mock_resources, clear=True): |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 76 | model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 77 | self.assertEqual(model, FakeExistingModel) |
| 78 | |
| 79 | @patch.object(FakeResource.objects, "filter", MagicMock(return_value=['a', 'b'])) |
| 80 | def test_multiple_models(self): |
| 81 | """ |
| 82 | [GRPCModelsAccessor] get_model_from_classname: should raise an exception if multiple instances are found |
| 83 | """ |
| 84 | data = { |
| 85 | "name": "test" |
| 86 | } |
| 87 | with patch.dict(RESOURCES, mock_resources, clear=True): |
| 88 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 89 | GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 90 | self.assertEqual(e.exception.message, "[XOS-Tosca] Model test has multiple instances, I can't handle it") |
| 91 | |
| 92 | @patch.dict(RESOURCES, mock_resources, clear=True) |
| 93 | @patch.object(FakeResource.objects, "filter") |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 94 | @patch.object(FakeResource.objects, "new") |
| 95 | def test_find_model_without_name_property(self, mock_new, mock_filter): |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 96 | """ |
| 97 | [GRPCModelsAccessor] get_model_from_classname: should lookup a model by the first property |
| 98 | """ |
| 99 | data = { |
| 100 | 'foo': 'bar', |
| 101 | 'something': 'else' |
| 102 | } |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 103 | GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 104 | mock_filter.assert_called_with(foo="bar") |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 105 | mock_new.assert_called() |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 106 | |
| 107 | if __name__ == '__main__': |
| 108 | unittest.main() |