blob: b30011c58b8603103290c9f177919344ce388bd6 [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 = {
Matteo Scandolo21dde412017-07-11 18:54:12 -070020 'username~pass': {
21 'test-model': FakeResource
22 }
Matteo Scandolo485b7132017-06-30 11:46:47 -070023}
24
Matteo Scandolo21dde412017-07-11 18:54:12 -070025USERNAME = 'username'
26PASSWORD = 'pass'
27
Matteo Scandolo485b7132017-06-30 11:46:47 -070028class GRPCModelsAccessor_Create_or_update_Test(unittest.TestCase):
29
Matteo Scandolo21dde412017-07-11 18:54:12 -070030 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 Scandolo485b7132017-06-30 11:46:47 -070042 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 Scandolo21dde412017-07-11 18:54:12 -070050 GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD)
Matteo Scandolod12be212017-07-07 10:44:34 -070051 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 Scandolo485b7132017-06-30 11:46:47 -070052
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 Scandolo21dde412017-07-11 18:54:12 -070063 model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
Matteo Scandolo485b7132017-06-30 11:46:47 -070064 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 Scandolo21dde412017-07-11 18:54:12 -070076 model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
Matteo Scandolo485b7132017-06-30 11:46:47 -070077 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 Scandolo21dde412017-07-11 18:54:12 -070089 GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
Matteo Scandolo485b7132017-06-30 11:46:47 -070090 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 Scandolo21dde412017-07-11 18:54:12 -070094 @patch.object(FakeResource.objects, "new")
95 def test_find_model_without_name_property(self, mock_new, mock_filter):
Matteo Scandolo485b7132017-06-30 11:46:47 -070096 """
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 Scandolo21dde412017-07-11 18:54:12 -0700103 GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700104 mock_filter.assert_called_with(foo="bar")
Matteo Scandolo21dde412017-07-11 18:54:12 -0700105 mock_new.assert_called()
Matteo Scandolo485b7132017-06-30 11:46:47 -0700106
107if __name__ == '__main__':
108 unittest.main()