refactor

Change-Id: I833f751ab3f307f6996d0822bef7a749dc6f6d2f
diff --git a/test/test_grpc_models_accessor.py b/test/test_grpc_models_accessor.py
new file mode 100644
index 0000000..707b5ef
--- /dev/null
+++ b/test/test_grpc_models_accessor.py
@@ -0,0 +1,89 @@
+import unittest
+from mock import patch, MagicMock
+from grpc_client.models_accessor import GRPCModelsAccessor
+from grpc_client.resources import RESOURCES
+
+class FakeObj:
+    new = None
+    filter = None
+
+class FakeResource:
+    objects = FakeObj
+
+class FakeModel:
+    pass
+
+class FakeExistingModel:
+    pass
+
+mock_resources = {
+    'test-model': FakeResource
+}
+
+class GRPCModelsAccessor_Create_or_update_Test(unittest.TestCase):
+
+    def test_unkown_module(self):
+        """
+        [GRPCModelsAccessor] get_model_from_classname: If a model is not know by the grpc api, raise
+        """
+        data = {
+            "name": "test"
+        }
+        with self.assertRaises(Exception) as e:
+            GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data)
+        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")
+
+    @patch.object(FakeResource.objects, "filter")
+    @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel))
+    def test_new_model(self, mock_filter):
+        """
+        [GRPCModelsAccessor] get_model_from_classname: should create a new model
+        """
+        data = {
+            "name": "test"
+        }
+        with patch.dict(RESOURCES, mock_resources, clear=True):
+            model = GRPCModelsAccessor.get_model_from_classname('test-model', data)
+            mock_filter.assert_called_with(name="test")
+            self.assertEqual(model, FakeModel)
+
+    @patch.object(FakeResource.objects, "filter", MagicMock(return_value=[FakeExistingModel]))
+    def test_existing_model(self):
+        """
+        [GRPCModelsAccessor] get_model_from_classname: should update an existing model
+        """
+        data = {
+            "name": "test"
+        }
+        with patch.dict(RESOURCES, mock_resources, clear=True):
+            model = GRPCModelsAccessor.get_model_from_classname('test-model', data)
+            self.assertEqual(model, FakeExistingModel)
+
+    @patch.object(FakeResource.objects, "filter", MagicMock(return_value=['a', 'b']))
+    def test_multiple_models(self):
+        """
+        [GRPCModelsAccessor] get_model_from_classname: should raise an exception if multiple instances are found
+        """
+        data = {
+            "name": "test"
+        }
+        with patch.dict(RESOURCES, mock_resources, clear=True):
+            with self.assertRaises(Exception) as e:
+                GRPCModelsAccessor.get_model_from_classname('test-model', data)
+            self.assertEqual(e.exception.message, "[XOS-Tosca] Model test has multiple instances, I can't handle it")
+
+    @patch.dict(RESOURCES, mock_resources, clear=True)
+    @patch.object(FakeResource.objects, "filter")
+    def _test_find_model_without_name_property(self, mock_filter):
+        """
+        [GRPCModelsAccessor] get_model_from_classname: should lookup a model by the first property
+        """
+        data = {
+            'foo': 'bar',
+            'something': 'else'
+        }
+        GRPCModelsAccessor.get_model_from_classname('test-model', data)
+        mock_filter.assert_called_with(foo="bar")
+
+if __name__ == '__main__':
+    unittest.main()
\ No newline at end of file