Browse Source

Merge "Add db api tests for scheduled jobs"

tags/9.0.0.0b1
Zuul 1 week ago
parent
commit
7698c3def0
2 changed files with 111 additions and 0 deletions
  1. 2
    0
      mistral/db/v2/sqlalchemy/api.py
  2. 109
    0
      mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py

+ 2
- 0
mistral/db/v2/sqlalchemy/api.py View File

@@ -1221,6 +1221,8 @@ def delete_delayed_calls(session=None, **kwargs):
1221 1221
     return _delete_all(models.DelayedCall, **kwargs)
1222 1222
 
1223 1223
 
1224
+# Scheduled jobs.
1225
+
1224 1226
 @b.session_aware()
1225 1227
 def create_scheduled_job(values, session=None):
1226 1228
     job = models.ScheduledJob()

+ 109
- 0
mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py View File

@@ -2654,6 +2654,115 @@ class CronTriggerTest(SQLAlchemyTest):
2654 2654
         self.assertIn("'name': 'trigger1'", s)
2655 2655
 
2656 2656
 
2657
+SCHEDULED_JOBS = [
2658
+    {
2659
+        'run_after': 30,
2660
+        'func_name': 'test_module.test_func',
2661
+        'func_args': {
2662
+            'server': 'localhost',
2663
+            'database': 'test',
2664
+            'timeout': 600,
2665
+            'verbose': True
2666
+        },
2667
+        'execute_at': datetime.datetime(2019, 7, 6, 15, 1, 0)
2668
+    },
2669
+    {
2670
+        'run_after': 50,
2671
+        'target_factory_func_name': 'test_target_factory_func',
2672
+        'func_name': 'test_func',
2673
+        'func_args': {
2674
+            'server': 'localhost',
2675
+            'database': 'test',
2676
+            'timeout': 600,
2677
+            'verbose': True
2678
+        },
2679
+        'execute_at': datetime.datetime(2019, 7, 6, 20, 30, 0)
2680
+    },
2681
+]
2682
+
2683
+
2684
+class ScheduledJobTest(SQLAlchemyTest):
2685
+    def setUp(self):
2686
+        super(ScheduledJobTest, self).setUp()
2687
+
2688
+        db_api.delete_scheduled_jobs()
2689
+
2690
+    def test_create_and_get_scheduled_job(self):
2691
+        created = db_api.create_scheduled_job(SCHEDULED_JOBS[0])
2692
+
2693
+        fetched = db_api.get_scheduled_job(created.id)
2694
+
2695
+        self.assertEqual(created, fetched)
2696
+
2697
+    def test_create_scheduled_job_duplicate_without_auth(self):
2698
+        cfg.CONF.set_default('auth_enable', False, group='pecan')
2699
+        db_api.create_scheduled_job(SCHEDULED_JOBS[0])
2700
+        db_api.create_scheduled_job(SCHEDULED_JOBS[0])
2701
+
2702
+    def test_update_scheduled_job(self):
2703
+        created = db_api.create_scheduled_job(SCHEDULED_JOBS[0])
2704
+
2705
+        self.assertIsNone(created.updated_at)
2706
+
2707
+        updated = db_api.update_scheduled_job(
2708
+            created.id,
2709
+            {'captured_at': datetime.datetime(2019, 7, 6, 20, 30, 0)}
2710
+        )
2711
+
2712
+        self.assertEqual(
2713
+            datetime.datetime(2019, 7, 6, 20, 30, 0),
2714
+            updated[0].captured_at
2715
+        )
2716
+
2717
+        fetched = db_api.get_scheduled_job(created.id)
2718
+
2719
+        self.assertEqual(updated[0], fetched)
2720
+        self.assertIsNotNone(fetched.updated_at)
2721
+
2722
+    def test_get_scheduled_jobs(self):
2723
+        created0 = db_api.create_scheduled_job(SCHEDULED_JOBS[0])
2724
+        created1 = db_api.create_scheduled_job(SCHEDULED_JOBS[1])
2725
+
2726
+        fetched = db_api.get_scheduled_jobs()
2727
+
2728
+        self.assertEqual(2, len(fetched))
2729
+        self._assert_single_item(fetched, func_name=created0['func_name'])
2730
+        self._assert_single_item(fetched, func_name=created1['func_name'])
2731
+
2732
+    def test_delete_scheduled_job(self):
2733
+        created = db_api.create_scheduled_job(SCHEDULED_JOBS[0])
2734
+
2735
+        fetched = db_api.get_scheduled_job(created.id)
2736
+
2737
+        self.assertEqual(created, fetched)
2738
+
2739
+        db_api.delete_scheduled_job(created.id)
2740
+
2741
+        self.assertRaises(
2742
+            exc.DBEntityNotFoundError,
2743
+            db_api.get_environment,
2744
+            created.id
2745
+        )
2746
+
2747
+    def test_get_scheduled_jobs_count(self):
2748
+        res = db_api.get_scheduled_jobs_count()
2749
+        self.assertEqual(0, res)
2750
+
2751
+        created0 = db_api.create_scheduled_job(SCHEDULED_JOBS[0])
2752
+        created1 = db_api.create_scheduled_job(SCHEDULED_JOBS[1])
2753
+
2754
+        res = db_api.get_scheduled_jobs_count()
2755
+        self.assertEqual(2, res)
2756
+
2757
+        db_api.delete_scheduled_job(created0.id)
2758
+        res = db_api.get_scheduled_jobs_count()
2759
+        self.assertEqual(1, res)
2760
+
2761
+        db_api.delete_scheduled_job(created1.id)
2762
+        res = db_api.get_scheduled_jobs_count()
2763
+        self.assertEqual(0, res)
2764
+
2765
+
2657 2766
 ENVIRONMENTS = [
2658 2767
     {
2659 2768
         'name': 'env1',

Loading…
Cancel
Save