blob: 707b5ef1e70a5a54279082c0caf52bb83a0f8790 [file] [log] [blame]
Matteo Scandolo485b7132017-06-30 11:46:47 -07001import unittest
2from mock import patch, MagicMock
3from grpc_client.models_accessor import GRPCModelsAccessor
4from grpc_client.resources import RESOURCES
5
6class FakeObj:
7 new = None
8 filter = None
9
10class FakeResource:
11 objects = FakeObj
12
13class FakeModel:
14 pass
15
16class FakeExistingModel:
17 pass
18
19mock_resources = {
20 'test-model': FakeResource
21}
22
23class GRPCModelsAccessor_Create_or_update_Test(unittest.TestCase):
24
25 def test_unkown_module(self):
26 """
27 [GRPCModelsAccessor] get_model_from_classname: If a model is not know by the grpc api, raise
28 """
29 data = {
30 "name": "test"
31 }
32 with self.assertRaises(Exception) as e:
33 GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data)
34 self.assertEqual(e.exception.message, "[XOS-TOSCA] The model your tring to create (name: test, class: i-do-not-exists) is not know by xos-core")
35
36 @patch.object(FakeResource.objects, "filter")
37 @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel))
38 def test_new_model(self, mock_filter):
39 """
40 [GRPCModelsAccessor] get_model_from_classname: should create a new model
41 """
42 data = {
43 "name": "test"
44 }
45 with patch.dict(RESOURCES, mock_resources, clear=True):
46 model = GRPCModelsAccessor.get_model_from_classname('test-model', data)
47 mock_filter.assert_called_with(name="test")
48 self.assertEqual(model, FakeModel)
49
50 @patch.object(FakeResource.objects, "filter", MagicMock(return_value=[FakeExistingModel]))
51 def test_existing_model(self):
52 """
53 [GRPCModelsAccessor] get_model_from_classname: should update an existing model
54 """
55 data = {
56 "name": "test"
57 }
58 with patch.dict(RESOURCES, mock_resources, clear=True):
59 model = GRPCModelsAccessor.get_model_from_classname('test-model', data)
60 self.assertEqual(model, FakeExistingModel)
61
62 @patch.object(FakeResource.objects, "filter", MagicMock(return_value=['a', 'b']))
63 def test_multiple_models(self):
64 """
65 [GRPCModelsAccessor] get_model_from_classname: should raise an exception if multiple instances are found
66 """
67 data = {
68 "name": "test"
69 }
70 with patch.dict(RESOURCES, mock_resources, clear=True):
71 with self.assertRaises(Exception) as e:
72 GRPCModelsAccessor.get_model_from_classname('test-model', data)
73 self.assertEqual(e.exception.message, "[XOS-Tosca] Model test has multiple instances, I can't handle it")
74
75 @patch.dict(RESOURCES, mock_resources, clear=True)
76 @patch.object(FakeResource.objects, "filter")
77 def _test_find_model_without_name_property(self, mock_filter):
78 """
79 [GRPCModelsAccessor] get_model_from_classname: should lookup a model by the first property
80 """
81 data = {
82 'foo': 'bar',
83 'something': 'else'
84 }
85 GRPCModelsAccessor.get_model_from_classname('test-model', data)
86 mock_filter.assert_called_with(foo="bar")
87
88if __name__ == '__main__':
89 unittest.main()