__init__.py 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
import os
import sys
import datetime
import math
import tinctest
import re
from time import gmtime, strftime

import unittest2 as unittest
from fnmatch import fnmatch
from mpp.models import SQLTestCase

import tinctest

from tinctest.lib import run_shell_command, Gpdiff

from mpp.lib.PSQL import PSQL

@tinctest.skipLoading("Test model")
class GroupingFunctionTestCase(SQLTestCase):

    def __init__(self, methodName, baseline_result = None, sql_file = None, db_name = None):
        super(GroupingFunctionTestCase, self).__init__(methodName, baseline_result, sql_file, db_name)
        self.optimizer_mode='on'

    def _infer_metadata(self):
        super(GroupingFunctionTestCase, self)._infer_metadata()
        self.executemode= self._metadata.get('executemode', 'positivetest')

    def run_test(self):
        sql_file = self.sql_file
        ans_file = self.ans_file
        
        source_file = sys.modules[self.__class__.__module__].__file__
        source_dir = os.path.dirname(source_file)
        out_file = os.path.join(self.get_out_dir(), os.path.basename(sql_file).replace('.sql', '.out'))
        extrainfo = os.path.join(self.get_out_dir(), os.path.basename(sql_file).replace('.sql', '_extrainfo.diff'))


        self.gucs.add('optimizer=on')
        self.gucs.add('client_min_messages=\'log\'')
        self.gucs.add('optimizer_log=on')
        mod_sql_file = self._add_gucs_to_sql_file(sql_file)

        PSQL.run_sql_file(mod_sql_file, dbname = self.db_name, out_file = out_file)

        init_file_path = os.path.join(os.path.dirname(sql_file), 'init_file')
        init_files = []
        if os.path.exists(init_file_path):
            init_files.append(init_file_path)

        err = ""
        if self.executemode == 'expectedfallback':
            result_plan = self._checkforstring(out_file, 'Planner produced plan :0')
            if result_plan == False:
                err = "did not fallback properly"
        else:
            result_plan = self._checkforstring(out_file, 'Planner produced plan')
            if result_plan == True:
                err = "ORCA was not used when it's suppose to"

        if len(err) > 0:
            f = open(extrainfo, 'w')
            f.write(err)
            f.close()

        result = Gpdiff.are_files_equal(out_file, self.ans_file, match_sub = init_files)


        if len(err) < 1 and result:
            return True
        else:
            return False

    def _checkforstring(self, file, string):
        f = open(file, 'r')
        content = f.readlines()
        f.close()

        for x in content:
            x = x.strip()
            if string in x:
                return True
        return False