def test_get_errors(self):
        mock = BZMock()
        mock.mock_get.update({
            'https://a.blazemeter.com/api/v4/data/labels?master_id=1': {
                "api_version":
                4,
                "error":
                None,
                "result": [{
                    "sessions": ["r-t-5746a8e38569a"],
                    "id": "ALL",
                    "name": "ALL"
                }, {
                    "sessions": ["r-t-5746a8e38569a"],
                    "id": "e843ff89a5737891a10251cbb0db08e5",
                    "name": "http://blazedemo.com/"
                }]
            },
            'https://a.blazemeter.com/api/v4/data/kpis?interval=1&from=0&master_ids%5B%5D=1&kpis%5B%5D=t&kpis%5B%5D=lt&kpis%5B%5D=by&kpis%5B%5D=n&kpis%5B%5D=ec&kpis%5B%5D=ts&kpis%5B%5D=na&labels%5B%5D=ALL&labels%5B%5D=e843ff89a5737891a10251cbb0db08e5':
            {
                "api_version":
                4,
                "error":
                None,
                "result": [{
                    "labelId":
                    "ALL",
                    "labelName":
                    "ALL",
                    "label":
                    "ALL",
                    "kpis": [{
                        "n": 1,
                        "na": 1,
                        "ec": 0,
                        "p90": 0,
                        "t_avg": 817,
                        "lt_avg": 82,
                        "by_avg": 0,
                        "n_avg": 1,
                        "ec_avg": 0,
                        "ts": 1464248743
                    }, {
                        "n": 1,
                        "na": 1,
                        "ec": 0,
                        "p90": 0,
                        "t_avg": 817,
                        "lt_avg": 82,
                        "by_avg": 0,
                        "n_avg": 1,
                        "ec_avg": 0,
                        "ts": 1464248744
                    }]
                }]
            },
            'https://a.blazemeter.com/api/v4/masters/1/reports/aggregatereport/data':
            {
                "api_version":
                4,
                "error":
                None,
                "result": [{
                    "labelName": "ALL",
                    "99line": 1050,
                    "90line": 836,
                    "95line": 912
                }, {
                    "labelName": "http://blazedemo.com",
                    "99line": 1050,
                    "90line": 836,
                    "95line": 912
                }]
            },
            'https://a.blazemeter.com/api/v4/data/kpis?interval=1&from=1464248744&master_ids%5B%5D=1&kpis%5B%5D=t&kpis%5B%5D=lt&kpis%5B%5D=by&kpis%5B%5D=n&kpis%5B%5D=ec&kpis%5B%5D=ts&kpis%5B%5D=na&labels%5B%5D=ALL&labels%5B%5D=e843ff89a5737891a10251cbb0db08e5':
            {
                "api_version":
                4,
                "error":
                None,
                "result": [{
                    "labelId":
                    "ALL",
                    "labelName":
                    "ALL",
                    "label":
                    "ALL",
                    "kpis": [{
                        "n": 1,
                        "na": 1,
                        "ec": 0,
                        "p90": 0,
                        "t_avg": 817,
                        "lt_avg": 82,
                        "by_avg": 0,
                        "n_avg": 1,
                        "ec_avg": 0,
                        "ts": 1464248744
                    }, {
                        "n": 1,
                        "na": 1,
                        "ec": 0,
                        "p90": 0,
                        "t_avg": 817,
                        "lt_avg": 82,
                        "by_avg": 0,
                        "n_avg": 1,
                        "ec_avg": 0,
                        "ts": 1464248745
                    }]
                }]
            },
            'https://a.blazemeter.com/api/v4/data/kpis?interval=1&from=1464248745&master_ids%5B%5D=1&kpis%5B%5D=t&kpis%5B%5D=lt&kpis%5B%5D=by&kpis%5B%5D=n&kpis%5B%5D=ec&kpis%5B%5D=ts&kpis%5B%5D=na&labels%5B%5D=ALL&labels%5B%5D=e843ff89a5737891a10251cbb0db08e5':
            {
                "api_version":
                4,
                "error":
                None,
                "result": [{
                    "labelId":
                    "ALL",
                    "labelName":
                    "ALL",
                    "label":
                    "ALL",
                    "kpis": [{
                        "n": 1,
                        "na": 1,
                        "ec": 0,
                        "p90": 0,
                        "t_avg": 817,
                        "lt_avg": 82,
                        "by_avg": 0,
                        "n_avg": 1,
                        "ec_avg": 0,
                        "ts": 1464248745
                    }]
                }]
            }
        })

        obj = ResultsFromBZA()
        obj.master = Master(data={"id": 1})
        mock.apply(obj.master)

        # set cumulative errors from BM
        mock.mock_get.update(
            self.get_errors_mock(
                {'ALL': {
                    "Not found": {
                        "count": 10,
                        "rc": "404"
                    }
                }}))

        # frame [0, 1464248744)
        res1 = list(obj.datapoints(False))
        self.assertEqual(1, len(res1))
        cumul = res1[0][DataPoint.CUMULATIVE]
        cur = res1[0][DataPoint.CURRENT]
        self.assertEqual(1, len(cumul.keys()))
        self.assertEqual(1, len(cur.keys()))
        errors_1 = {'Not found': {'count': 10, 'rc': u'404'}}
        self.assertEqual(self.convert_kpi_errors(cumul[""]["errors"]),
                         errors_1)  # all error data is written
        self.assertEqual(self.convert_kpi_errors(cur[""]["errors"]),
                         errors_1)  # to 'current' and 'cumulative'

        # frame [1464248744, 1464248745)
        res2 = list(obj.datapoints(False))
        self.assertEqual(1, len(res2))
        cumul = res2[0][DataPoint.CUMULATIVE]
        cur = res2[0][DataPoint.CURRENT]
        self.assertEqual(1, len(cumul.keys()))
        self.assertEqual(1, len(cur.keys()))
        self.assertEqual(self.convert_kpi_errors(cumul[""]["errors"]),
                         errors_1)  # the same errors,
        self.assertEqual(cur[""]["errors"], [])  # new errors not found

        mock.mock_get.update(
            self.get_errors_mock(
                {
                    "ALL": {
                        "Not found": {
                            "count": 11,
                            "rc": "404"
                        },  # one more error
                        "Found": {
                            "count": 2,
                            "rc": "200"
                        }
                    },  # new error message (error ID)
                    "label1": {
                        "Strange behaviour": {
                            "count": 666,
                            "rc": "666"
                        }
                    }
                },
                {  # new error label
                    "ALL": {
                        "assertion_example": {
                            "count": 33
                        }
                    }
                }))

        res3 = list(
            obj.datapoints(True))  # let's add the last timestamp [1464248745]
        self.assertEqual(1, len(res3))
        cumul = res3[0][DataPoint.CUMULATIVE]
        cur = res3[0][DataPoint.CURRENT]
        errors_all_full = {
            'Not found': {
                'count': 11,
                'rc': '404'
            },
            'Found': {
                'count': 2,
                'rc': '200'
            },
            'assertion_example': {
                'count': 33,
                'rc': 'All Assertions'
            }
        }
        errors_all_update = {
            'Not found': {
                'count': 1,
                'rc': '404'
            },
            'Found': {
                'count': 2,
                'rc': '200'
            },
            'assertion_example': {
                'count': 33,
                'rc': 'All Assertions'
            }
        }

        errors_label1 = {'Strange behaviour': {'count': 666, 'rc': '666'}}
        self.assertEqual(errors_label1,
                         self.convert_kpi_errors(cumul["label1"]["errors"]))
        self.assertEqual(errors_all_full,
                         self.convert_kpi_errors(cumul[""]["errors"]))
        self.assertEqual(errors_label1,
                         self.convert_kpi_errors(cur["label1"]["errors"]))
        self.assertEqual(errors_all_update,
                         self.convert_kpi_errors(cur[""]["errors"]))
    def test_datapoint(self):
        mock = BZMock()
        mock.mock_get.update({
            'https://a.blazemeter.com/api/v4/data/labels?master_id=1': {
                "api_version": 2,
                "error": None,
                "result": [{
                    "sessions": ["r-t-5746a8e38569a"],
                    "id": "ALL",
                    "name": "ALL"
                }, {
                    "sessions": ["r-t-5746a8e38569a"],
                    "id": "e843ff89a5737891a10251cbb0db08e5",
                    "name": "http://blazedemo.com/"}]},
            'https://a.blazemeter.com/api/v4/data/kpis?interval=1&from=0&master_ids%5B%5D=1&kpis%5B%5D=t&kpis%5B%5D=lt&kpis%5B%5D=by&kpis%5B%5D=n&kpis%5B%5D=ec&kpis%5B%5D=ts&kpis%5B%5D=na&labels%5B%5D=ALL&labels%5B%5D=e843ff89a5737891a10251cbb0db08e5': {
                "api_version": 2,
                "error": None,
                "result": [{
                    "labelId": "ALL",
                    "labelName": "ALL",
                    "label": "ALL",
                    "kpis": [{
                        "n": 1,
                        "na": 1,
                        "ec": 0,
                        "p90": 0,
                        "t_avg": 817,
                        "lt_avg": 82,
                        "by_avg": 0,
                        "n_avg": 1,
                        "ec_avg": 0,
                        "ts": 1464248743}]}]},
            'https://a.blazemeter.com/api/v4/masters/1/reports/aggregatereport/data': {
                "api_version": 2,
                "error": None,
                "result": [{
                    "labelId": "ALL",
                    "labelName": "ALL",
                    "samples": 152,
                    "avgResponseTime": 786,
                    "90line": 836,
                    "95line": 912,
                    "99line": 1050,
                    "minResponseTime": 531,
                    "maxResponseTime": 1148,
                    "avgLatency": 81,
                    "geoMeanResponseTime": None,
                    "stDev": 108,
                    "duration": 119,
                    "avgBytes": 0,
                    "avgThroughput": 1.2773109243697,
                    "medianResponseTime": 0,
                    "errorsCount": 0,
                    "errorsRate": 0,
                    "hasLabelPassedThresholds": None
                }, {
                    "labelId": "e843ff89a5737891a10251cbb0db08e5",
                    "labelName": "http://blazedemo.com/",
                    "samples": 152,
                    "avgResponseTime": 786,
                    "90line": 836,
                    "95line": 912,
                    "99line": 1050,
                    "minResponseTime": 531,
                    "maxResponseTime": 1148,
                    "avgLatency": 81,
                    "geoMeanResponseTime": None,
                    "stDev": 108,
                    "duration": 119,
                    "avgBytes": 0,
                    "avgThroughput": 1.2773109243697,
                    "medianResponseTime": 0,
                    "errorsCount": 0,
                    "errorsRate": 0,
                    "hasLabelPassedThresholds": None}]}})

        mock.mock_get.update(self.get_errors_mock({"ALL": {}}))

        obj = ResultsFromBZA()
        obj.master = Master(data={"id": 1})
        mock.apply(obj.master)
        res = list(obj.datapoints(True))
        cumulative_ = res[0][DataPoint.CUMULATIVE]
        total = cumulative_['']
        percentiles_ = total[KPISet.PERCENTILES]
        self.assertEquals(1.05, percentiles_['99.0'])
    def test_datapoint(self):
        mock = BZMock()
        mock.mock_get.update({
            'https://a.blazemeter.com/api/v4/data/labels?master_id=1': {
                "api_version":
                2,
                "error":
                None,
                "result": [{
                    "sessions": ["r-t-5746a8e38569a"],
                    "id": "ALL",
                    "name": "ALL"
                }, {
                    "sessions": ["r-t-5746a8e38569a"],
                    "id": "e843ff89a5737891a10251cbb0db08e5",
                    "name": "http://blazedemo.com/"
                }]
            },
            'https://a.blazemeter.com/api/v4/data/kpis?interval=1&from=0&master_ids%5B%5D=1&kpis%5B%5D=t&kpis%5B%5D=lt&kpis%5B%5D=by&kpis%5B%5D=n&kpis%5B%5D=ec&kpis%5B%5D=ts&kpis%5B%5D=na&labels%5B%5D=ALL&labels%5B%5D=e843ff89a5737891a10251cbb0db08e5':
            {
                "api_version":
                2,
                "error":
                None,
                "result": [{
                    "labelId":
                    "ALL",
                    "labelName":
                    "ALL",
                    "label":
                    "ALL",
                    "kpis": [{
                        "n": 1,
                        "na": 1,
                        "ec": 0,
                        "p90": 0,
                        "t_avg": 817,
                        "lt_avg": 82,
                        "by_avg": 0,
                        "n_avg": 1,
                        "ec_avg": 0,
                        "ts": 1464248743
                    }]
                }]
            },
            'https://a.blazemeter.com/api/v4/masters/1/reports/aggregatereport/data':
            {
                "api_version":
                2,
                "error":
                None,
                "result": [{
                    "labelId": "ALL",
                    "labelName": "ALL",
                    "samples": 152,
                    "avgResponseTime": 786,
                    "90line": 836,
                    "95line": 912,
                    "99line": 1050,
                    "minResponseTime": 531,
                    "maxResponseTime": 1148,
                    "avgLatency": 81,
                    "geoMeanResponseTime": None,
                    "stDev": 108,
                    "duration": 119,
                    "avgBytes": 0,
                    "avgThroughput": 1.2773109243697,
                    "medianResponseTime": 0,
                    "errorsCount": 0,
                    "errorsRate": 0,
                    "hasLabelPassedThresholds": None
                }, {
                    "labelId": "e843ff89a5737891a10251cbb0db08e5",
                    "labelName": "http://blazedemo.com/",
                    "samples": 152,
                    "avgResponseTime": 786,
                    "90line": 836,
                    "95line": 912,
                    "99line": 1050,
                    "minResponseTime": 531,
                    "maxResponseTime": 1148,
                    "avgLatency": 81,
                    "geoMeanResponseTime": None,
                    "stDev": 108,
                    "duration": 119,
                    "avgBytes": 0,
                    "avgThroughput": 1.2773109243697,
                    "medianResponseTime": 0,
                    "errorsCount": 0,
                    "errorsRate": 0,
                    "hasLabelPassedThresholds": None
                }]
            }
        })

        mock.mock_get.update(self.get_errors_mock({"ALL": {}}))

        obj = ResultsFromBZA()
        obj.master = Master(data={"id": 1})
        mock.apply(obj.master)
        res = list(obj.datapoints(True))
        cumulative_ = res[0][DataPoint.CUMULATIVE]
        total = cumulative_['']
        percentiles_ = total[KPISet.PERCENTILES]
        self.assertEquals(1.05, percentiles_['99.0'])
    def test_get_errors(self):
        mock = BZMock()
        mock.mock_get.update({
            'https://a.blazemeter.com/api/v4/data/labels?master_id=1': {
                "api_version": 4,
                "error": None,
                "result": [{
                    "sessions": ["r-t-5746a8e38569a"],
                    "id": "ALL",
                    "name": "ALL"
                }, {
                    "sessions": ["r-t-5746a8e38569a"],
                    "id": "e843ff89a5737891a10251cbb0db08e5",
                    "name": "http://blazedemo.com/"}]},
            'https://a.blazemeter.com/api/v4/data/kpis?interval=1&from=0&master_ids%5B%5D=1&kpis%5B%5D=t&kpis%5B%5D=lt&kpis%5B%5D=by&kpis%5B%5D=n&kpis%5B%5D=ec&kpis%5B%5D=ts&kpis%5B%5D=na&labels%5B%5D=ALL&labels%5B%5D=e843ff89a5737891a10251cbb0db08e5': {
                "api_version": 4,
                "error": None,
                "result": [{
                    "labelId": "ALL",
                    "labelName": "ALL",
                    "label": "ALL",
                    "kpis": [{
                        "n": 1, "na": 1, "ec": 0, "p90": 0, "t_avg": 817, "lt_avg": 82,
                        "by_avg": 0, "n_avg": 1, "ec_avg": 0, "ts": 1464248743
                    }, {"n": 1, "na": 1, "ec": 0, "p90": 0, "t_avg": 817, "lt_avg": 82,
                        "by_avg": 0, "n_avg": 1, "ec_avg": 0, "ts": 1464248744}]}]},
            'https://a.blazemeter.com/api/v4/masters/1/reports/aggregatereport/data': {
                "api_version": 4,
                "error": None,
                "result": [{
                    "labelName": "ALL", "99line": 1050, "90line": 836, "95line": 912}, {
                    "labelName": "http://blazedemo.com", "99line": 1050, "90line": 836, "95line": 912}]},
            'https://a.blazemeter.com/api/v4/data/kpis?interval=1&from=1464248744&master_ids%5B%5D=1&kpis%5B%5D=t&kpis%5B%5D=lt&kpis%5B%5D=by&kpis%5B%5D=n&kpis%5B%5D=ec&kpis%5B%5D=ts&kpis%5B%5D=na&labels%5B%5D=ALL&labels%5B%5D=e843ff89a5737891a10251cbb0db08e5': {
                "api_version": 4,
                "error": None,
                "result": [{
                    "labelId": "ALL",
                    "labelName": "ALL",
                    "label": "ALL",
                    "kpis": [{
                        "n": 1, "na": 1, "ec": 0, "p90": 0, "t_avg": 817, "lt_avg": 82,
                        "by_avg": 0, "n_avg": 1, "ec_avg": 0, "ts": 1464248744
                    }, {"n": 1, "na": 1, "ec": 0, "p90": 0, "t_avg": 817, "lt_avg": 82,
                        "by_avg": 0, "n_avg": 1, "ec_avg": 0, "ts": 1464248745}]}]},
            'https://a.blazemeter.com/api/v4/data/kpis?interval=1&from=1464248745&master_ids%5B%5D=1&kpis%5B%5D=t&kpis%5B%5D=lt&kpis%5B%5D=by&kpis%5B%5D=n&kpis%5B%5D=ec&kpis%5B%5D=ts&kpis%5B%5D=na&labels%5B%5D=ALL&labels%5B%5D=e843ff89a5737891a10251cbb0db08e5': {
                "api_version": 4,
                "error": None,
                "result": [{
                    "labelId": "ALL",
                    "labelName": "ALL",
                    "label": "ALL",
                    "kpis": [{
                        "n": 1, "na": 1, "ec": 0, "p90": 0, "t_avg": 817, "lt_avg": 82,
                        "by_avg": 0, "n_avg": 1, "ec_avg": 0, "ts": 1464248745}]}]}})

        obj = ResultsFromBZA()
        obj.master = Master(data={"id": 1})
        mock.apply(obj.master)

        # set cumulative errors from BM
        mock.mock_get.update(self.get_errors_mock({'ALL': {"Not found": {"count": 10, "rc": "404"}}}))

        # frame [0, 1464248744)
        res1 = list(obj.datapoints(False))
        self.assertEqual(1, len(res1))
        cumul = res1[0][DataPoint.CUMULATIVE]
        cur = res1[0][DataPoint.CURRENT]
        self.assertEqual(1, len(cumul.keys()))
        self.assertEqual(1, len(cur.keys()))
        errors_1 = {'Not found': {'count': 10, 'rc': u'404'}}
        self.assertEqual(self.convert_kpi_errors(cumul[""]["errors"]), errors_1)     # all error data is written
        self.assertEqual(self.convert_kpi_errors(cur[""]["errors"]), errors_1)       # to 'current' and 'cumulative'

        # frame [1464248744, 1464248745)
        res2 = list(obj.datapoints(False))
        self.assertEqual(1, len(res2))
        cumul = res2[0][DataPoint.CUMULATIVE]
        cur = res2[0][DataPoint.CURRENT]
        self.assertEqual(1, len(cumul.keys()))
        self.assertEqual(1, len(cur.keys()))
        self.assertEqual(self.convert_kpi_errors(cumul[""]["errors"]), errors_1)    # the same errors,
        self.assertEqual(cur[""]["errors"], [])                                     # new errors not found

        mock.mock_get.update(self.get_errors_mock({
            "ALL": {
                "Not found": {
                    "count": 11, "rc": "404"},          # one more error
                "Found": {
                    "count": 2, "rc": "200"}},          # new error message (error ID)
            "label1": {
                "Strange behaviour": {
                    "count": 666, "rc": "666"}}}, {     # new error label
            "ALL": {"assertion_example": {"count": 33}}}))

        res3 = list(obj.datapoints(True))    # let's add the last timestamp [1464248745]
        self.assertEqual(1, len(res3))
        cumul = res3[0][DataPoint.CUMULATIVE]
        cur = res3[0][DataPoint.CURRENT]
        errors_all_full = {
            'Not found': {'count': 11, 'rc': '404'},
            'Found': {'count': 2, 'rc': '200'},
            'assertion_example': {'count': 33, 'rc': 'All Assertions'}}
        errors_all_update = {
            'Not found': {'count': 1, 'rc': '404'},
            'Found': {'count': 2, 'rc': '200'},
            'assertion_example': {'count': 33, 'rc': 'All Assertions'}}

        errors_label1 = {'Strange behaviour': {'count': 666, 'rc': '666'}}
        self.assertEqual(errors_label1, self.convert_kpi_errors(cumul["label1"]["errors"]))
        self.assertEqual(errors_all_full, self.convert_kpi_errors(cumul[""]["errors"]))
        self.assertEqual(errors_label1, self.convert_kpi_errors(cur["label1"]["errors"]))
        self.assertEqual(errors_all_update, self.convert_kpi_errors(cur[""]["errors"]))