def test_verify_multi_db(stc, meth_mgr):
    global PKG, COMMAND, COMMAND_NAME, TEST_MULTI_DB_FILE
    result_file = os.path.join(os.getcwd(), TEST_MULTI_DB_FILE)

    # Simulate running through the TopologyTestGroupCommand and
    # TestGroupCommands
    ResultInterface.create_test()
    ResultInterface.start_test()

    with AutoCommand(".".join([PKG, COMMAND_NAME])) as cmd:
        verifydb.get_this_cmd = MagicMock(return_value=cmd)
        verifydb.get_active_results_db = MagicMock(return_value=result_file)
        verifydb.run(
            "Select TotalFrameCount from AnalyzerPortResults", "EQUAL", 4, 0, 0, 0, True, "", False, "", "", ""
        )

    ResultInterface.end_test()
    stm_test_result = meth_man_utils.get_stm_test_result()
    assert stm_test_result is not None
    json_results = stm_test_result.GetCollection("JsonResults")
    selected = None
    for result in json_results:
        obj = json.loads(result)
        assert ProviderConst.INFO in obj
        info = obj[ProviderConst.INFO]
        assert ProviderConst.CLASS in obj
        if obj[ProviderConst.CLASS] == EnumDataClass.table_db_query:
            selected = obj
            break
    assert selected is not None
    info = selected[ProviderConst.INFO]
    data = selected[ProviderConst.DATA]
    assert "Select TotalFrameCount from AnalyzerPortResults" == str(info[ProviderConst.SQL_QUERY])
    assert "[[298838], [287903], [294329], [289852]]" == str(data[ProviderConst.ROW])
    assert "[u'TotalFrameCount']" == str(data[ProviderConst.COLUMN_DISPLAY_NAMES])
def test_command_execute_with_sql_modlist(stc, tmpdir, meth_mgr):
    global PKG, COMMAND, COMMAND_NAME, TEST_DB_FILE

    # FIXME: Do the various checks to ensure the db file's result are properly
    # loaded into the JSON string
    result_file = os.path.join(os.getcwd(), TEST_DB_FILE)
    template_file = os.path.join(str(tmpdir), 'template.xml')
    create_template_file(template_file)

    # Simulate running through the TopologyTestGroupCommand and
    # TestGroupCommands
    ResultInterface.create_test()
    ResultInterface.start_test()

    with AutoCommand('.'.join([PKG, COMMAND_NAME])) as cmd:
        export_to_json.get_this_cmd = MagicMock(return_value=cmd)
        export_to_json.get_active_results_db = MagicMock(return_value=result_file)
        cmd.Set('ChartTemplateJsonFileName', template_file)
        cmd.SetCollection("Series",
                          ["Select TotalFrameCount from AnalyzerPortResults", "1, 2"])
        cmd.Set("SeriesDataType", "SINGLE")
        cmd.Set("CustomModifier", "{\"yAxis\": {\"title\": {\"text\": "
                "{{SELECT MIN(Id) FROM AnalyzerPortResults}}}}, \"xAxis\": "
                "{\"categories\": {{SELECT Id FROM AnalyzerPortResults}}}, "
                "\"series\": {\"data\": {{SELECT TotalFrameCount, "
                "TotalOctetCount FROM AnalyzerPortResults}}}}")
        export_to_json.run(cmd.Get('ChartTemplateJsonFileName'),
                           cmd.Get('Title'), cmd.Get('XAxisTitle'),
                           cmd.GetCollection('XAxisCategories'),
                           cmd.Get('YAxisTitle'),
                           cmd.GetCollection('YAxisCategories'),
                           cmd.GetCollection('Series'),
                           cmd.Get('SeriesDataType'),
                           cmd.Get('CustomModifier'),
                           cmd.Get('UseMultipleResultsDatabases'),
                           cmd.Get('UseSummary'),
                           "LEVEL_2")
    ResultInterface.end_test()
    stm_test_result = meth_man_utils.get_stm_test_result()
    assert stm_test_result is not None
    json_results = stm_test_result.GetCollection('JsonResults')
    selected = None
    for result in json_results:
        obj = json.loads(result)
        assert ProviderConst.INFO in obj
        assert ProviderConst.DATA_FORMAT in obj
        if obj[ProviderConst.DATA_FORMAT] == EnumDataFormat.chart:
            selected = obj
            break
    assert selected is not None
    data = selected[ProviderConst.DATA]
    x_cat = data[ChartConst.X_CAT]
    y_cat = data[ChartConst.Y_CAT]
    mod_list = data[ChartConst.MOD_LIST]
    assert mod_list == "{\"yAxis\": {\"title\": {\"text\": 1}}, \"xAxis\": " \
                       "{\"categories\": [1, 2]}, \"series\": " \
                       "{\"data\": [[1151261, 147361408], [1239945, 158712960]]}}"
    assert x_cat is None
    assert y_cat is None
def test_command_execute_with_sql(stc, tmpdir, meth_mgr):
    global PKG, COMMAND, COMMAND_NAME, TEST_DB_FILE

    # FIXME: Do the various checks to ensure the db file's result are properly
    # loaded into the JSON string
    result_file = os.path.join(os.getcwd(), TEST_DB_FILE)
    template_file = os.path.join(str(tmpdir), 'template.xml')
    create_template_file(template_file)

    # Simulate running through the TopologyTestGroupCommand and
    # TestGroupCommands
    ResultInterface.create_test()
    ResultInterface.start_test()

    with AutoCommand('.'.join([PKG, COMMAND_NAME])) as cmd:
        export_to_json.get_this_cmd = MagicMock(return_value=cmd)
        export_to_json.get_active_results_db = MagicMock(return_value=result_file)
        cmd.Set('ChartTemplateJsonFileName', template_file)
        cmd.Set("Title", "Select MIN(Id) From AnalyzerPortResults")
        cmd.Set("XAxisTitle", "Select MIN(Id) From AnalyzerPortResults")
        cmd.SetCollection("XAxisCategories",
                          ["Select Id From AnalyzerPortResults", "3", "4"])
        cmd.Set("YAxisTitle", "Select MIN(Id) From AnalyzerPortResults")
        cmd.SetCollection("YAxisCategories", ["Select Id From AnalyzerPortResults"])
        cmd.SetCollection("Series",
                          ["3",
                           "Select TotalFrameCount from AnalyzerPortResults", "1, 2"])
        cmd.Set("SeriesDataType", "SINGLE")
        cmd.Set("CustomModifier",
                "{\"yAxis\": {\"title\": {\"text\": \"Modified Label\"}}}")
        export_to_json.run("", "", "", "", "", "",
                           ["Select TotalFrameCount from AnalyzerPortResults"],
                           "", "", "", "", "")
    ResultInterface.end_test()
    stm_test_result = meth_man_utils.get_stm_test_result()
    assert stm_test_result is not None
    json_results = stm_test_result.GetCollection('JsonResults')
    selected = None
    for result in json_results:
        obj = json.loads(result)
        assert ProviderConst.INFO in obj
        assert ProviderConst.DATA_FORMAT in obj
        if obj[ProviderConst.DATA_FORMAT] == EnumDataFormat.chart:
            selected = obj
            break
    assert selected is not None
    data = selected[ProviderConst.DATA]
    assert "{u'text': u'1'}" == str(data[ChartConst.TITLE])
    assert "{u'text': u'1'}" == str(data[ChartConst.X_LAB])
    assert "[u'1', u'2', u'3', u'4']" == str(data[ChartConst.X_CAT])
    assert "{u'text': u'1'}" == str(data[ChartConst.Y_LAB])
    assert "[u'1', u'2']" == str(data[ChartConst.Y_CAT])
    assert "[{u'data': [3]}, {u'data': [1151261, 1239945]}, {u'data': [1, 2]}]" \
        == str(data[ChartConst.SERIES])
    assert "{u'yAxis': {u'title': {u'text': u'Modified Label'}}}" == \
        str(data[ChartConst.MOD_LIST])
def test_command_execute_with_sql_mix_single_pair_multiple_dbs(stc, tmpdir, meth_mgr):
    global PKG, COMMAND, COMMAND_NAME, TEST_MULTIPLE_DB_FILE

    # FIXME: Do the various checks to ensure the db file's result are properly
    # loaded into the JSON string
    result_file = os.path.join(os.getcwd(), TEST_MULTIPLE_DB_FILE)
    template_file = os.path.join(str(tmpdir), 'template.xml')
    create_template_file(template_file)

    # Simulate running through the TopologyTestGroupCommand and
    # TestGroupCommands
    ResultInterface.create_test()
    ResultInterface.start_test()

    with AutoCommand('.'.join([PKG, COMMAND_NAME])) as cmd:
        export_to_json.get_this_cmd = MagicMock(return_value=cmd)
        export_to_json.get_active_results_db = MagicMock(return_value=result_file)
        cmd.Set('ChartTemplateJsonFileName', template_file)
        cmd.SetCollection("Series",
                          ["Select TotalFrameCount, Id from AnalyzerPortResults Where Id = 1",
                           "[3, 2], [5, 4]",
                           "7.1, 6.4",
                           "Select ParentHnd from AnalyzerPortResults Where Id = 1",
                           "Select Handle, Id, ParentHnd from AnalyzerPortResults Where Id = 1",
                           "blah"])
        export_to_json.run("", "", "", "", "", "",
                           [""],
                           "", "ALL_ITERATION", "")
    ResultInterface.end_test()
    stm_test_result = meth_man_utils.get_stm_test_result()
    assert stm_test_result is not None
    json_results = stm_test_result.GetCollection('JsonResults')
    selected = None
    for result in json_results:
        obj = json.loads(result)
        assert ProviderConst.INFO in obj
        assert ProviderConst.DATA_FORMAT in obj
        if obj[ProviderConst.DATA_FORMAT] == EnumDataFormat.chart:
            selected = obj
            break
    assert selected is not None
    data = selected[ProviderConst.DATA]
    expectedData = "[{u'data': [[298838, 1], [294329, 1]]}, {u'data': [[3, 2], [5, 4]]}, " + \
                   "{u'data': [7.1, 6.4]}, {u'data': [164282, 164282]}, " + \
                   "{u'data': [[164787, 1, 164282], [171335, 1, 164282]]}, {u'data': [u'blah']}]"
    assert expectedData == str(data[ChartConst.SERIES])
def test_command_execute_empty(stc, tmpdir, meth_mgr):
    global PKG, COMMAND, COMMAND_NAME, TEST_DB_FILE

    # FIXME: Do the various checks to ensure the db file's result are properly
    # loaded into the JSON string
    result_file = os.path.join(os.getcwd(), TEST_DB_FILE)
    template_file = os.path.join(str(tmpdir), 'template.xml')
    create_template_file(template_file)

    # Simulate running through the TopologyTestGroupCommand and
    # TestGroupCommands
    ResultInterface.create_test()
    ResultInterface.start_test()

    with AutoCommand('.'.join([PKG, COMMAND_NAME])) as cmd:
        export_to_json.get_this_cmd = MagicMock(return_value=cmd)
        export_to_json.get_active_results_db = MagicMock(return_value=result_file)
        cmd.Set('ChartTemplateJsonFileName', template_file)
        cmd.Set("Title", "")
        cmd.Set("XAxisTitle", "")
        cmd.SetCollection("XAxisCategories", [""])
        cmd.Set("YAxisTitle", "")
        cmd.SetCollection("YAxisCategories", [""])
        cmd.Set("SeriesDataType", "SINGLE")
        export_to_json.run("", "", "", "", "", "", "",
                           "", "", "", "", "")
    ResultInterface.end_test()
    stm_test_result = meth_man_utils.get_stm_test_result()
    assert stm_test_result is not None
    json_results = stm_test_result.GetCollection('JsonResults')
    selected = None
    for result in json_results:
        obj = json.loads(result)
        assert ProviderConst.INFO in obj
        assert ProviderConst.DATA_FORMAT in obj
        if obj[ProviderConst.DATA_FORMAT] == EnumDataFormat.chart:
            selected = obj
            break
    assert selected is not None
    data = selected[ProviderConst.DATA]
    assert "{u'text': u''}" == str(data[ChartConst.TITLE])
    assert "{u'text': u''}" == str(data[ChartConst.X_LAB])
    assert "[u'']" == str(data[ChartConst.X_CAT])
    assert "{u'text': u''}" == str(data[ChartConst.Y_LAB])
    assert "[u'']" == str(data[ChartConst.Y_CAT])
def test_verify_single_db(stc, meth_mgr):
    global PKG, COMMAND, COMMAND_NAME, TEST_DB_FILE
    result_file = os.path.join(os.getcwd(), TEST_DB_FILE)

    # Simulate running through the TopologyTestGroupCommand and
    # TestGroupCommands
    ResultInterface.create_test()
    ResultInterface.start_test()

    with AutoCommand('.'.join([PKG, COMMAND_NAME])) as cmd:
        verifymultidb.get_this_cmd = MagicMock(return_value=cmd)
        verifymultidb.get_active_results_db = MagicMock(return_value=result_file)
        verifymultidb.run(["Select TotalFrameCount from AnalyzerPortResults",
                           "Select TotalOctetCount from GeneratorPortResults"],
                          False, False, "GROUP_2", [], [], [], False)

    ResultInterface.end_test()
    stm_test_result = meth_man_utils.get_stm_test_result()
    assert stm_test_result is not None
    json_results = stm_test_result.GetCollection('JsonResults')
    selected = []

    for result in json_results:
        obj = json.loads(result)
        assert ProviderConst.INFO in obj
        info = obj[ProviderConst.INFO]
        assert ProviderConst.REPORT_GROUP in info
        assert info[ProviderConst.REPORT_GROUP] == "GROUP_2"
        assert ProviderConst.CLASS in obj
        if obj[ProviderConst.CLASS] == EnumDataClass.table_db_query:
            selected.append(obj)
    assert len(selected) > 0
    assert selected[0] is not None
    info = selected[0][ProviderConst.INFO]
    data = selected[0][ProviderConst.DATA]
    assert "Select TotalFrameCount from AnalyzerPortResults" == str(info[ProviderConst.SQL_QUERY])
    assert "[[1151261], [1239945]]" == str(data[ProviderConst.ROW])
    assert "[u'TotalFrameCount']" == str(data[ProviderConst.COLUMN_DISPLAY_NAMES])
    assert selected[1] is not None
    info = selected[1][ProviderConst.INFO]
    data = selected[1][ProviderConst.DATA]
    assert "Select TotalOctetCount from GeneratorPortResults" == str(info[ProviderConst.SQL_QUERY])
    assert "[[158712960], [147361408]]" == str(data[ProviderConst.ROW])
    assert "[u'TotalOctetCount']" == str(data[ProviderConst.COLUMN_DISPLAY_NAMES])
def test_command_execute_with_sql_multiple_dbs(stc, tmpdir, meth_mgr):
    global PKG, COMMAND, COMMAND_NAME, TEST_MULTIPLE_DB_FILE

    # FIXME: Do the various checks to ensure the db file's result are properly
    # loaded into the JSON string
    result_file = os.path.join(os.getcwd(), TEST_MULTIPLE_DB_FILE)
    template_file = os.path.join(str(tmpdir), 'template.xml')
    create_template_file(template_file)

    # Simulate running through the TopologyTestGroupCommand and
    # TestGroupCommands
    ResultInterface.create_test()
    ResultInterface.start_test()

    with AutoCommand('.'.join([PKG, COMMAND_NAME])) as cmd:
        export_to_json.get_this_cmd = MagicMock(return_value=cmd)
        export_to_json.get_active_results_db = MagicMock(return_value=result_file)
        cmd.Set('ChartTemplateJsonFileName', template_file)
        cmd.SetCollection("Series",
                          ["3",
                           "Select TotalFrameCount from AnalyzerPortResults", "1, 2"])
        cmd.Set("SeriesDataType", "SINGLE")
        cmd.Set("UseMultipleResultsDatabases", "TRUE")
        cmd.Set("UseSummary", "TRUE")
        export_to_json.run("", "", "", "", "", "",
                           ["Select TotalFrameCount from AnalyzerPortResults"],
                           "", "", "TRUE", "TRUE", "")
    ResultInterface.end_test()
    stm_test_result = meth_man_utils.get_stm_test_result()
    assert stm_test_result is not None
    json_results = stm_test_result.GetCollection('JsonResults')
    selected = None
    for result in json_results:
        obj = json.loads(result)
        assert ProviderConst.INFO in obj
        assert ProviderConst.DATA_FORMAT in obj
        if obj[ProviderConst.DATA_FORMAT] == EnumDataFormat.chart:
            selected = obj
            break
    assert selected is not None
    data = selected[ProviderConst.DATA]
    assert "[{u'data': [3]}, {u'data': [298838, 287903, 294329, 289852]}, {u'data': [1, 2]}]" \
        == str(data[ChartConst.SERIES])
def test_command_execute_default(stc, tmpdir, meth_mgr):
    global PKG, COMMAND, COMMAND_NAME, TEST_MULTIPLE_DB_FILE

    # FIXME: Do the various checks to ensure the db file's result are properly
    # loaded into the JSON string
    result_file = os.path.join(os.getcwd(), TEST_MULTIPLE_DB_FILE)
    template_file = os.path.join(str(tmpdir), 'template.xml')
    create_template_file(template_file)

    # Simulate running through the TopologyTestGroupCommand and
    # TestGroupCommands
    ResultInterface.create_test()
    ResultInterface.start_test()

    with AutoCommand('.'.join([PKG, COMMAND_NAME])) as cmd:
        export_to_json.get_this_cmd = MagicMock(return_value=cmd)
        export_to_json.get_active_results_db = MagicMock(return_value=result_file)
        cmd.Set('ChartTemplateJsonFileName', template_file)
        export_to_json.run("", "", "", "", "", "", "",
                           "", "SUMMARY", "GROUP_1")
    ResultInterface.end_test()
    stm_test_result = meth_man_utils.get_stm_test_result()
    assert stm_test_result is not None
    json_results = stm_test_result.GetCollection('JsonResults')
    selected = None
    for result in json_results:
        obj = json.loads(result)
        info = obj[ProviderConst.INFO]
        assert ProviderConst.REPORT_GROUP in info
        assert info[ProviderConst.REPORT_GROUP] == "GROUP_1"
        assert ProviderConst.DATA_FORMAT in obj
        if obj[ProviderConst.DATA_FORMAT] == EnumDataFormat.chart:
            selected = obj
            break
    assert selected is not None
    data = selected[ProviderConst.DATA]
    assert "{u'text': u'Result Node Param Handles'}" == str(data[ChartConst.TITLE])
    assert "{u'text': u'Id'}" == str(data[ChartConst.X_LAB])
    assert "[u'1', u'2', u'3', u'4']" == str(data[ChartConst.X_CAT])
    assert "{u'text': u'Handle'}" == str(data[ChartConst.Y_LAB])
    assert "None" == str(data[ChartConst.Y_CAT])
    assert "[{u'data': [164769, 164771, 164774, 171322]}]" == str(data[ChartConst.SERIES])
    assert "{u'series': [{u'name': u'Handles'}]}" == str(data[ChartConst.MOD_LIST])
def test_command_execute_with_sql_pair(stc, tmpdir, meth_mgr):
    global PKG, COMMAND, COMMAND_NAME, TEST_DB_FILE

    # FIXME: Do the various checks to ensure the db file's result are properly
    # loaded into the JSON string
    result_file = os.path.join(os.getcwd(), TEST_DB_FILE)
    template_file = os.path.join(str(tmpdir), 'template.xml')
    create_template_file(template_file)

    # Simulate running through the TopologyTestGroupCommand and
    # TestGroupCommands
    ResultInterface.create_test()
    ResultInterface.start_test()

    with AutoCommand('.'.join([PKG, COMMAND_NAME])) as cmd:
        export_to_json.get_this_cmd = MagicMock(return_value=cmd)
        export_to_json.get_active_results_db = MagicMock(return_value=result_file)
        cmd.Set('ChartTemplateJsonFileName', template_file)
        cmd.SetCollection("XAxisCategories", [])
        cmd.SetCollection("Series",
                          ["Select TotalFrameCount, Id from AnalyzerPortResults",
                           "Select Id, TotalFrameCount From AnalyzerPortResults"])
        export_to_json.run("", "", "", "", "", "",
                           [""], "", "SUMMARY", "")
    ResultInterface.end_test()
    stm_test_result = meth_man_utils.get_stm_test_result()
    assert stm_test_result is not None
    json_results = stm_test_result.GetCollection('JsonResults')
    selected = None
    for result in json_results:
        obj = json.loads(result)
        assert ProviderConst.INFO in obj
        assert ProviderConst.DATA_FORMAT in obj
        if obj[ProviderConst.DATA_FORMAT] == EnumDataFormat.chart:
            selected = obj
            break
    assert selected is not None
    data = selected[ProviderConst.DATA]
    assert "[{u'data': [[1151261, 1], [1239945, 2]]}, {u'data': [[1, 1151261], [2, 1239945]]}]" \
        == str(data[ChartConst.SERIES])