def test_empty_test(self):
        class Test(TestCase):
            def test_foo(self):
                pass

            def test_bar(self):
                pass

        self.test_runner.setup_test_environment()
        self.test_runner.run_suite(TestLoader().loadTestsFromTestCase(
            testCaseClass=Test)
        )
        self.test_runner.teardown_test_environment()

        # check for empty tests
        self.assertIsNotNone(RequestQueryCountManager, 'queries')
        self.assertIsInstance(RequestQueryCountManager.queries,
                              TestResultQueryContainer)
        self.assertEqual(RequestQueryCountManager.queries.total, 0)

        # check if files are generated
        self.assertTrue(path.exists(
            RequestQueryCountConfig.get_setting('SUMMARY_PATH'))
        )
        self.assertTrue(path.isfile(
            RequestQueryCountConfig.get_setting('SUMMARY_PATH'))
        )
        self.assertTrue(path.exists(
            RequestQueryCountConfig.get_setting('DETAIL_PATH'))
        )
        self.assertTrue(path.isfile(
            RequestQueryCountConfig.get_setting('DETAIL_PATH'))
        )
 def tearDown(self):
     try:
         os.remove(RequestQueryCountConfig.get_setting('DETAIL_PATH'))
     except FileNotFoundError:
         pass
     try:
         os.remove(RequestQueryCountConfig.get_setting('SUMMARY_PATH'))
     except FileNotFoundError:
         pass
Example #3
0
    def test_json_exists(self):
        class Test(TestCase):
            def test_foo(self):
                self.client.get('/url-1')

        self.assertFalse(path.exists(
            RequestQueryCountConfig.get_setting('DETAIL_PATH'))
        )
        self.test_runner.run_tests(
            None,
            TestLoader().loadTestsFromTestCase(testCaseClass=Test)
        )
        self.assertTrue(path.exists(
            RequestQueryCountConfig.get_setting('DETAIL_PATH'))
        )
Example #4
0
 def wrapped(self, *args, **kwargs):
     result = func(self, *args, **kwargs)
     if not RequestQueryCountConfig.enabled():
         return result
     cls.save_json('SUMMARY_PATH', cls.queries, False)
     cls.save_json('DETAIL_PATH', cls.queries, True)
     cls.queries = None
     return result
Example #5
0
    def save_json(cls, setting_name, container, detail):
        summary_path = os.path.realpath(RequestQueryCountConfig.get_setting(
            setting_name))
        os.makedirs(os.path.dirname(summary_path), exist_ok=True)

        with open(summary_path, 'w') as json_file:
            json.dump(container.get_json(detail=detail), json_file,
                      ensure_ascii=False, indent=4, sort_keys=True)
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     if not RequestQueryCountConfig.enabled():
         raise MiddlewareNotUsed()
     self.force_debug_cursor = False
     self.initial_queries = 0
     self.final_queries = None
     self.connection = connections[DEFAULT_DB_ALIAS]
    def add_arguments(self, parser):
        super().add_arguments(parser)
        parser.add_argument('--last-count-file',
                            dest='last_count_file',
                            help='JSON summary file to compare against.',
                            required=True)

        summary_path = RequestQueryCountConfig.get_setting('SUMMARY_PATH')
        parser.add_argument('--query-count-file',
                            dest='query_count_file',
                            help='JSON summary file for current run.',
                            default=summary_path)

        help_msg = 'Threshold tolerance, which is computed in percentage. ' \
                   'Defaults to {}%%'.format(self.INCREASE_THRESHOLD)
        parser.add_argument('--query-count-threhold',
                            dest='query_count_threshold',
                            type=float, default=self.INCREASE_THRESHOLD,
                            help=help_msg)
Example #8
0
        def wrapped(self, *args, **kwargs):
            if (not hasattr(cls, 'queries') or not
                    RequestQueryCountConfig.enabled()):
                return tear_down(self, *args, **kwargs)

            container = cls.get_testcase_container()

            test_method = getattr(self, self._testMethodName)

            exclusions = (
                getattr(self.__class__, "__querycount_exclude__", []) +
                getattr(test_method, "__querycount_exclude__", [])
            )

            all_queries = cls.queries
            current_queries = container.filter_by(exclusions)
            all_queries.add(self.id(), current_queries)

            return tear_down(self, *args, **kwargs)
Example #9
0
 def test_default_enabled(self):
     self.assertTrue(RequestQueryCountConfig.enabled())
Example #10
0
 def test_override_enabled(self):
     self.assertTrue(RequestQueryCountConfig.enabled())
Example #11
0
 def test_override_disabled(self):
     self.assertFalse(RequestQueryCountConfig.enabled())
Example #12
0
 def wrapped(self, *args, **kwargs):
     result = set_up(self, *args, **kwargs)
     if RequestQueryCountConfig.enabled():
         setattr(local, cls.LOCAL_TESTCASE_CONTAINER_NAME,
                 TestCaseQueryContainer())
     return result
Example #13
0
 def wrapped(self, *args, **kwargs):
     result = func(self, *args, **kwargs)
     if not RequestQueryCountConfig.enabled():
         return result
     cls.queries = TestResultQueryContainer()
     return result