Browse Source

Add raise unit test for session in sqlalchemy

Change-Id: I1fc045010732f178abb4c5ea75e02b96e7a83b3d
gengchc2 4 months ago
parent
commit
ac48abe735

+ 17
- 0
freezer_api/tests/unit/sqlalchemy/v1/test_session.py View File

@@ -18,8 +18,11 @@
18 18
 """Tests for manipulating job via the DB API"""
19 19
 
20 20
 import copy
21
+import mock
22
+from mock import patch
21 23
 from oslo_config import cfg
22 24
 
25
+from freezer_api.common import exceptions as freezer_api_exc
23 26
 from freezer_api.tests.unit import common
24 27
 from freezer_api.tests.unit.sqlalchemy import base
25 28
 
@@ -31,6 +34,7 @@ class DbSessionTestCase(base.DbTestCase):
31 34
     def setUp(self):
32 35
         super(DbSessionTestCase, self).setUp()
33 36
         self.fake_session_0 = common.get_fake_session_0()
37
+        self.fake_user_id = self.fake_session_0.get('user_id')
34 38
         self.fake_session_0.pop('session_id')
35 39
         self.fake_session_2 = common.get_fake_session_2()
36 40
         self.fake_session_2.pop('session_id')
@@ -405,3 +409,16 @@ class DbSessionTestCase(base.DbTestCase):
405 409
         for index in range(len(result)):
406 410
             sessionmap = result[index]
407 411
             self.assertEqual(100, sessionmap['hold_off'])
412
+
413
+    @patch('freezer_api.db.sqlalchemy.api.get_session')
414
+    def test_raise_add_session_exist(self, mock_get_session):
415
+        mock_get_session.return_value = mock.MagicMock()
416
+        self.assertRaises(freezer_api_exc.DocumentExists,
417
+                          self.dbapi.add_session, self.fake_user_id,
418
+                          self.fake_session_0)
419
+
420
+    def test_raise_update_session_noexist(self):
421
+        self.assertRaises(freezer_api_exc.DocumentNotFound,
422
+                          self.dbapi.update_session, self.fake_user_id,
423
+                          self.fake_session_id,
424
+                          self.fake_session_0)

+ 20
- 0
freezer_api/tests/unit/sqlalchemy/v2/test_session.py View File

@@ -18,7 +18,10 @@
18 18
 """Tests for manipulating job via the DB API"""
19 19
 
20 20
 import copy
21
+import mock
22
+from mock import patch
21 23
 
24
+from freezer_api.common import exceptions as freezer_api_exc
22 25
 from freezer_api.tests.unit import common
23 26
 from freezer_api.tests.unit.sqlalchemy import base
24 27
 
@@ -28,6 +31,8 @@ class DbSessionTestCase(base.DbTestCase):
28 31
     def setUp(self):
29 32
         super(DbSessionTestCase, self).setUp()
30 33
         self.fake_session_0 = common.get_fake_session_0()
34
+        self.fake_project_id = self.fake_session_0.get('project_id')
35
+        self.fake_user_id = self.fake_session_0.get('user_id')
31 36
         self.fake_session_0.pop('session_id')
32 37
         self.fake_session_2 = common.get_fake_session_2()
33 38
         self.fake_session_2.pop('session_id')
@@ -461,3 +466,18 @@ class DbSessionTestCase(base.DbTestCase):
461 466
         for index in range(len(result)):
462 467
             sessionmap = result[index]
463 468
             self.assertEqual(100, sessionmap['hold_off'])
469
+
470
+    @patch('freezer_api.db.sqlalchemy.api.get_session')
471
+    def test_raise_add_session_exist(self, mock_get_session):
472
+        mock_get_session.return_value = mock.MagicMock()
473
+        self.assertRaises(freezer_api_exc.DocumentExists,
474
+                          self.dbapi.add_session, self.fake_user_id,
475
+                          self.fake_session_0,
476
+                          project_id=self.fake_project_id)
477
+
478
+    def test_raise_update_session_noexist(self):
479
+        self.assertRaises(freezer_api_exc.DocumentNotFound,
480
+                          self.dbapi.update_session, self.fake_user_id,
481
+                          self.fake_session_id,
482
+                          self.fake_session_0,
483
+                          project_id=self.fake_project_id)

Loading…
Cancel
Save