def test_status_start_date(self, mock_read_json): """ Test that the status start date is returned correctly. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual(datetime.datetime(DT_3AGO.year, DT_3AGO.month, DT_3AGO.day, 18, 20, 45), history.status_start_date('OpenBugsNone', 'red'))
def test_get_dates(self, mock_read_json): """ Test the reporting dates. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual( '2013-02-28 17:01:45,2013-02-28 17:16:45,2013-03-05 17:16:45', history.get_dates())
def test_status_start_date(self, mock_read_json): """ Test that the status start date is returned correctly. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual(datetime.datetime(2013, 2, 28, 17, 16, 45), history.status_start_date('OpenBugsNone', 'red'))
def test_long_history_length(self, mock_read_json): """ Test the length of a long history of a specific metric. """ mock_read_json.return_value = { "dates": ["2013-02-28 17:01:45"] * 2001, "metrics": { "OpenBugsNone": [{ "value": 10, "start": "2013-02-28 17:01:45", "end": "2013-02-28 17:01:45", "status": "yellow" }, { "value": 38, "start": "2013-02-28 17:16:45", "end": "2013-03-05 17:16:45", "status": "red" }] }, "statuses": [{ "yellow": 1 }, { "red": 1 }, { "red": 1 }] } history = CompactHistory('history_file_name.json') self.assertEqual(2000, len(history.long_history('OpenBugsNone')))
def test_add_metrics_invalid_numeric(self, mock_write_json, mock_read_json): """ Test that a metric with numeric value -1 does not record a value. """ mock_write_json.return_value = None mock_read_json.return_value = {"dates": [], "metrics": {}, "statuses": []} history = CompactHistory('history_file_name.json') class ExampleMetric(object): """ Fake a metric. """ @staticmethod def numerical_value(): """ Return the value. """ return -1 @staticmethod def stable_id(): """ Return the stable metric id. """ return 'ExampleMetric' @staticmethod def status(): """ Return the metric status. """ return 'green' history.add_metrics(datetime.datetime(2015, 1, 1), [ExampleMetric()]) self.assertTrue(history) mock_write_json.assert_called_once_with( { 'dates': ['2015-01-01 00:00:00'], 'metrics': {'ExampleMetric': [ {'start': '2015-01-01 00:00:00', 'end': '2015-01-01 00:00:00', 'status': 'green'}]}, 'statuses': [{'green': 1}] }, 'history_file_name.json' )
def test_recent_history_when_metric_added_later(self, mock_read_json): """ Test the recent history of a specific metric. """ mock_read_json.return_value = { "dates": [ "2013-01-15 12:01:00", "2013-02-28 17:01:45", "2013-02-28 17:16:45", "2013-03-05 17:16:45" ], "metrics": { "OpenBugsNone": [{ "value": 10, "start": "2013-02-28 17:01:45", "end": "2013-02-28 17:01:45", "status": "yellow" }, { "value": 38, "start": "2013-02-28 17:16:45", "end": "2013-03-05 17:16:45", "status": "red" }] }, "statuses": [{ "yellow": 1 }, { "red": 1 }, { "red": 1 }] } history = CompactHistory('history_file_name.json') self.assertEqual([None, 10, 38, 38], history.recent_history('OpenBugsNone'))
def test_status_start_date_no_history(self, mock_read_json): """ Test the status start date without history. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual(datetime.datetime(2013, 1, 1, 0, 0, 0), history.status_start_date('Foo', 'red', now=lambda: datetime.datetime(2013, 1, 1, 0, 0, 0)))
def test_statuses(self, mock_read_json): """ Test that the statuses are returned correctly. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual([{'yellow': 1, 'date': DATE1}, {'red': 1, 'date': DATE2}, {'red': 1, 'date': DATE3}], history.statuses())
def test_add_metrics_with_previous_history(self, mock_write_json, mock_read_json): """ Test that a metric with a new status adds a record. """ mock_write_json.return_value = None mock_read_json.return_value = { "dates": ["2013-02-28 17:01:45"], "metrics": { "ExampleMetric": [{ "value": 10, "start": "2013-02-28 17:01:45", "end": "2013-02-28 17:01:45", "status": "yellow" }] }, "statuses": [] } history = CompactHistory('history_file_name.json') class ExampleMetric(object): """ Fake a metric. """ @staticmethod def numerical_value(): """ Return the value. """ return 10 @staticmethod def stable_id(): """ Return the stable metric id. """ return 'ExampleMetric' @staticmethod def status(): """ Return the metric status. """ return 'green' history.add_metrics(datetime.datetime(2015, 1, 1), [ExampleMetric()]) self.assertTrue(history) mock_write_json.assert_called_once_with( { 'dates': ['2013-02-28 17:01:45', '2015-01-01 00:00:00'], 'metrics': { 'ExampleMetric': [{ 'value': 10, 'start': '2013-02-28 17:01:45', 'end': '2013-02-28 17:01:45', 'status': 'yellow' }, { 'start': '2015-01-01 00:00:00', 'end': '2015-01-01 00:00:00', 'status': 'green', 'value': 10 }] }, 'statuses': [{ 'green': 1 }] }, 'history_file_name.json')
def test_long_history(self, mock_read_json): """ Test that the empty history file has no long history. """ mock_read_json.return_value = { "dates": [], "metrics": {}, "statuses": [] } history = CompactHistory('history_file_name.json') self.assertEqual([], history.long_history('metric id'))
def test_status_start_date_no_history(self, mock_read_json): """ Test the status start date without history. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual( datetime.datetime(2013, 1, 1, 0, 0, 0), history.status_start_date( 'Foo', 'red', now=lambda: datetime.datetime(2013, 1, 1, 0, 0, 0)))
def test_get_dates_long_limit(self, mock_read_json): """ Test he reporting dates are limited to 2000 for long history. """ mock_read_json.return_value = { "dates": ["2013-02-28 17:01:45"] * 2001, "metrics": {}, "statuses": [] } history = CompactHistory('history_file_name.json') self.assertEqual(','.join(['2013-02-28 17:01:45'] * 2000), history.get_dates(long_history=True))
def test_long_history_length(self, mock_read_json): """ Test the length of a long history of a specific metric. """ mock_read_json.return_value = { "dates": ["2013-02-28 17:01:45"] * 2001, "metrics": {"OpenBugsNone": [ {"value": 10, "start": "2013-02-28 17:01:45", "end": "2013-02-28 17:01:45", "status": "yellow"}, {"value": 38, "start": "2013-02-28 17:16:45", "end": "2013-03-05 17:16:45", "status": "red"}]}, "statuses": [{"yellow": 1}, {"red": 1}, {"red": 1}] } history = CompactHistory('history_file_name.json') self.assertEqual(2000, len(history.long_history('OpenBugsNone')))
def test_add_report(self, mock_add_metrics, mock_read_json): """ Test that adding a report adds the metrics.""" mock_read_json.return_value = None quality_report = MagicMock() quality_report.date.return_value = 'quality_report.date()' quality_report.metrics.return_value = 'quality_report.metrics()' history = CompactHistory('unimportant') history.add_report(quality_report) self.assertTrue(history) mock_add_metrics.assert_called_once_with('quality_report.date()', 'quality_report.metrics()')
def test_persister(self, mock_read_json): """ Test if the persister can be changed. """ class OtherPersister(JsonPersister): """ Fake persister. """ write_json = None read_json = MagicMock() CompactHistory.set_persister(OtherPersister) self.assertTrue(CompactHistory('history_file_path')) mock_read_json.assert_not_called() OtherPersister.read_json.assert_called_once_with('history_file_path') CompactHistory.set_persister(FilePersister)
def test_statuses(self, mock_read_json): """ Test that the statuses are returned correctly. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual([{ 'yellow': 1, 'date': DATE1 }, { 'red': 1, 'date': DATE2 }, { 'red': 1, 'date': DATE3 }], history.statuses())
def test_add_metrics_invalid_numeric(self, mock_write_json, mock_read_json): """ Test that a metric with numeric value -1 does not record a value. """ mock_write_json.return_value = None mock_read_json.return_value = { "dates": [], "metrics": {}, "statuses": [] } history = CompactHistory('history_file_name.json') class ExampleMetric(object): """ Fake a metric. """ @staticmethod def numerical_value(): """ Return the value. """ return -1 @staticmethod def stable_id(): """ Return the stable metric id. """ return 'ExampleMetric' @staticmethod def status(): """ Return the metric status. """ return 'green' history.add_metrics(datetime.datetime(2015, 1, 1), [ExampleMetric()]) self.assertTrue(history) mock_write_json.assert_called_once_with( { 'dates': ['2015-01-01 00:00:00'], 'metrics': { 'ExampleMetric': [{ 'start': '2015-01-01 00:00:00', 'end': '2015-01-01 00:00:00', 'status': 'green' }] }, 'statuses': [{ 'green': 1 }] }, 'history_file_name.json')
def test_recent_history_when_metric_added_later(self, mock_read_json): """ Test the recent history of a specific metric. """ history = { "dates": [ "{y}-{m:02}-{d:02} 17:16:45".format(y=DT_3AGO.year, m=DT_3AGO.month, d=DT_3AGO.day), "{y}-{m:02}-{d:02} 17:21:45".format(y=DT_3AGO.year, m=DT_3AGO.month, d=DT_3AGO.day), "{y}-{m:02}-{d:02} 17:16:45".format(y=DT_NOW.year, m=DT_NOW.month, d=DT_NOW.day) ], "metrics": { "OpenBugsNone": [{ "value": 38, "start": "{y}-{m:02}-{d:02} 17:21:45".format(y=DT_3AGO.year, m=DT_3AGO.month, d=DT_3AGO.day), "end": "{y}-{m:02}-{d:02} 23:59:59".format(y=DT_NOW.year, m=DT_NOW.month, d=DT_NOW.day), "status": "red" }] }, "statuses": [{ "yellow": 1 }, { "red": 1 }, { "red": 1 }] } mock_read_json.return_value = history history = CompactHistory('history_file_name.json') self.assertEqual([None, 38, 38], history.recent_history('OpenBugsNone'))
def test_recent_history_when_metric_added_later(self, mock_read_json): """ Test the recent history of a specific metric. """ history = { "dates": [ "{y}-{m:02}-{d:02} 17:16:45".format(y=DT_3AGO.year, m=DT_3AGO.month, d=DT_3AGO.day), "{y}-{m:02}-{d:02} 17:21:45".format(y=DT_3AGO.year, m=DT_3AGO.month, d=DT_3AGO.day), "{y}-{m:02}-{d:02} 17:16:45".format(y=DT_NOW.year, m=DT_NOW.month, d=DT_NOW.day) ], "metrics": {"OpenBugsNone": [ {"value": 38, "start": "{y}-{m:02}-{d:02} 17:21:45".format(y=DT_3AGO.year, m=DT_3AGO.month, d=DT_3AGO.day), "end": "{y}-{m:02}-{d:02} 23:59:59".format(y=DT_NOW.year, m=DT_NOW.month, d=DT_NOW.day), "status": "red"}]}, "statuses": [{"yellow": 1}, {"red": 1}, {"red": 1}] } mock_read_json.return_value = history history = CompactHistory('history_file_name.json') self.assertEqual([None, 38, 38], history.recent_history('OpenBugsNone'))
def test_add_metrics_with_previous_history(self, mock_write_json, mock_read_json): """ Test that a metric with a new status adds a record. """ mock_write_json.return_value = None mock_read_json.return_value = { "dates": ["2013-02-28 17:01:45"], "metrics": { "ExampleMetric": [ {"value": 10, "start": "2013-02-28 17:01:45", "end": "2013-02-28 17:01:45", "status": "yellow"} ] }, "statuses": [] } history = CompactHistory('history_file_name.json') class ExampleMetric(object): """ Fake a metric. """ @staticmethod def numerical_value(): """ Return the value. """ return 10 @staticmethod def stable_id(): """ Return the stable metric id. """ return 'ExampleMetric' @staticmethod def status(): """ Return the metric status. """ return 'green' history.add_metrics(datetime.datetime(2015, 1, 1), [ExampleMetric()]) self.assertTrue(history) mock_write_json.assert_called_once_with({ 'dates': ['2013-02-28 17:01:45', '2015-01-01 00:00:00'], 'metrics': {'ExampleMetric': [ {'value': 10, 'start': '2013-02-28 17:01:45', 'end': '2013-02-28 17:01:45', 'status': 'yellow'}, {'start': '2015-01-01 00:00:00', 'end': '2015-01-01 00:00:00', 'status': 'green', 'value': 10}]}, 'statuses': [{'green': 1}]}, 'history_file_name.json')
def test_create_report_with_valid_history( # pylint: disable=too-many-arguments self, mock_add_report, mock_filename, mock_process, mock_resource_listdir, mock_write_file, mock_create_dir, mock_project): """ Tests if it does add a history record when it has history file name.""" mock_filename.return_value = 'history.json' mock_process.return_value = 'formatted report' mock_resource_listdir.return_value = [] mock_project.return_value = MagicMock(metric_sources=MagicMock( return_value=[CompactHistory('')])) reporter = quality_report.Reporter('folder').create_report( 'report_dir') mock_filename.assert_called_once() mock_add_report.assert_called_once_with(reporter) mock_write_file.assert_called() mock_create_dir.assert_called() self.assertTrue(reporter)
def test_long_history(self, mock_read_json): """ Test that the empty history file has no long history. """ mock_read_json.return_value = {"dates": [], "metrics": {}, "statuses": []} history = CompactHistory('history_file_name.json') self.assertEqual([], history.long_history('metric id'))
def test_url(self, mock_read_json): """ Test that the url is unspecified. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual("http://unspecified/", history.url())
def test_filename(self, mock_read_json): """ Test getting the filename. """ mock_read_json.retrun_value = '{"unimportant": "not used"}' history = CompactHistory('history_file_name.json') self.assertEqual('history_file_name.json', history.filename())
def test_filename(self, mock_read_json): """ Test that the filename is correct. """ mock_read_json.return_value = None history = CompactHistory('history.json') self.assertEqual('history.json', history.filename())
def test_recent_history(self, mock_read_json): """ Test the recent history of a specific metric. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual([10, 38, 38], history.recent_history('OpenBugsNone'))
def test_long_history(self, mock_read_json): """ Test the long history of a specific metric. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual([3, 10, 38, 38], history.long_history('OpenBugsNone'))
def test_get_dates(self, mock_read_json): """ Test the reporting dates. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual(DATE2 + ',' + DATE3 + ',' + DATE4, history.get_dates())
def test_get_dates_long_limit(self, mock_read_json): """ Test he reporting dates are limited to 2000 for long history. """ mock_read_json.return_value = {"dates": ["2013-02-28 17:01:45"] * 2001, "metrics": {}, "statuses": []} history = CompactHistory('history_file_name.json') self.assertEqual(','.join(['2013-02-28 17:01:45'] * 2000), history.get_dates(long_history=True))
def test_missing_recent_history(self, mock_read_json): """ Test the recent history of a non-existing metric. """ mock_read_json.return_value = COMPACT_HISTORY history = CompactHistory('history_file_name.json') self.assertEqual([], history.recent_history('Non existing'))