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_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_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_invalid_sql(stc, tmpdir):
    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("SeriesDataType", "SINGLE")

        cmd.SetCollection("XAxisCategories", ["SELECT test FROM test"])
        assert not export_to_json.run("", "", "", "", "", "", "",
                                      "", "", "", "", "")

        cmd.SetCollection("XAxisCategories", [])
        cmd.SetCollection("YAxisCategories", ["SELECT test FROM test"])
        assert not export_to_json.run("", "", "", "", "", "", "",
                                      "", "", "", "", "")

        cmd.SetCollection("YAxisCategories", [])
        cmd.SetCollection("Series",
                          ["SELECT test FROM test"])
        assert not export_to_json.run("", "", "", "", "", "", "",
                                      "", "", "", "", "")

        cmd.SetCollection("Series", [])
        cmd.Set("CustomModifier",
                "SELECT test FROM test")
        assert not export_to_json.run("", "", "", "", "", "", "",
                                      "", "", "", "", "")
def test_command_execute_get_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)

    db_list = []

    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)
        db_list = export_to_json.get_dbs(False, True)
        assert len(db_list) == 1
        assert db_list[0].endswith(TEST_MULTIPLE_DB_FILE)
        db_list = export_to_json.get_dbs(False, False)
        assert len(db_list) == 1
        assert db_list[0].endswith(TEST_MULTIPLE_DB_ITR_FILE)
        db_list = export_to_json.get_dbs(True, False)
        assert len(db_list) > 1
        db_list = export_to_json.get_dbs(True, True)
        assert len(db_list) > 1
def test_validation(stc, tmpdir):
    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
    template_file = os.path.join(str(tmpdir), 'template.xml')
    create_template_file(template_file)

    ChartTemplateJsonFileName = template_file
    Title = "Test Chart"
    XAxisTitle = "Time"
    XAxisCategories = ["1t", "2t", "3t"]
    YAxisTitle = "Routes"
    YAxisCategories = ["a", "b", "c"]
    Series = ["Select TotalFrameCount from AnalyzerPortResults"]
    SeriesDataType = "PAIR"
    CustomModifier = "{\"yAxis\": {\"title\": {\"text\": \"Modified Label\"}}}"
    UseMultipleResultsDatabases = "FALSE"
    UseSummary = "FALSE"
    ReportGroup = "LEVEL_1"
    assert export_to_json.validate(ChartTemplateJsonFileName, Title,
                                   XAxisTitle, XAxisCategories,
                                   YAxisTitle, YAxisCategories,
                                   Series, SeriesDataType, CustomModifier,
                                   UseMultipleResultsDatabases, UseSummary, ReportGroup) == ""

    assert "Invalid Chart Template" in \
        export_to_json.validate("", Title,
                                XAxisTitle, XAxisCategories,
                                YAxisTitle, YAxisCategories,
                                Series, SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)
    assert "1 is not of type u'string'" in \
        export_to_json.validate(ChartTemplateJsonFileName, 1,
                                XAxisTitle, XAxisCategories,
                                YAxisTitle, YAxisCategories,
                                Series, SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "Invalid Chart Template: """ in \
        export_to_json.validate("", Title,
                                XAxisTitle, XAxisCategories,
                                YAxisTitle, YAxisCategories,
                                Series, SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "[1, 2] is not of type u'string'" in \
        export_to_json.validate(ChartTemplateJsonFileName, Title,
                                [1, 2], XAxisCategories,
                                YAxisTitle, YAxisCategories,
                                Series, SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "1 is not of type u'array'" in \
        export_to_json.validate(ChartTemplateJsonFileName, Title,
                                XAxisTitle, 1,
                                YAxisTitle, YAxisCategories,
                                Series, SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "1 is not of type u'string'" in \
        export_to_json.validate(ChartTemplateJsonFileName, Title,
                                XAxisTitle, XAxisCategories,
                                1, YAxisCategories,
                                Series, SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "1 is not of type u'array'" in \
        export_to_json.validate(ChartTemplateJsonFileName, Title,
                                XAxisTitle, XAxisCategories,
                                YAxisTitle, 1,
                                Series, SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "1 is not of type u'array'" in \
        export_to_json.validate(ChartTemplateJsonFileName, Title,
                                XAxisTitle, XAxisCategories,
                                YAxisTitle, YAxisCategories,
                                1, SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "Invalid Series specified: []" in \
        export_to_json.validate(ChartTemplateJsonFileName, Title,
                                XAxisTitle, XAxisCategories,
                                YAxisTitle, YAxisCategories,
                                [], SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "'TEST' is not one of [u'SINGLE', u'PAIR']" in \
        export_to_json.validate(ChartTemplateJsonFileName, Title,
                                XAxisTitle, XAxisCategories,
                                YAxisTitle, YAxisCategories,
                                Series, "TEST", CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "Type Error: expected string or buffer" in \
        export_to_json.validate(ChartTemplateJsonFileName, Title,
                                XAxisTitle, XAxisCategories,
                                YAxisTitle, YAxisCategories,
                                Series, SeriesDataType, [1],
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)

    assert "Series data must be pair values" in \
        export_to_json.validate(ChartTemplateJsonFileName, Title,
                                XAxisTitle, XAxisCategories,
                                YAxisTitle, YAxisCategories,
                                ["[1, 2, 3]"], SeriesDataType, CustomModifier,
                                UseMultipleResultsDatabases, UseSummary, ReportGroup)