def test_count_by_group(self, mock_queryset): """ Tests the count_by_group function. """ options = ( ('high', 'High'), ('medium', 'Medium'), ('low', 'Low'), ) mock_queryset.model = Mock() mock_queryset.model._meta = MagicMock() mock_queryset.model.objects = mock_queryset mock_queryset.annotate.return_value = mock_queryset mock_queryset.filter.return_value = mock_queryset mock_queryset.values.return_value = mock_queryset mock_queryset.order_by.return_value = [{ 'level': value, 'level__count': 1 } for value, _ in options] actual = dbutils.count_by_group(mock_queryset, 'level', options) expected = {'level': {'high': 1, 'medium': 1, 'low': 1}} self.assertEqual(actual, expected) mock_queryset.order_by.return_value.clear() for value, _ in options: expected['level'][value] = 0 actual = dbutils.count_by_group(mock_queryset, 'level', options) self.assertEqual(actual, expected)
def _counts_by_field(queryset, field_name, choices): """ """ counts = count_by_group(queryset=queryset, column=field_name, options=choices) return counts[field_name]
def _counts_by_field(queryset, field_name, choices): """ Provides a REST API endpoint for GET requests for alert counts by level. """ counts = count_by_group( queryset=queryset, column=field_name, options=choices ) return counts[field_name]
def test_count_by_group(self, mock_queryset): """ Tests the count_by_group function. """ options = ( ('high', 'High'), ('medium', 'Medium'), ('low', 'Low'), ) mock_queryset.count = Mock(return_value=3) mock_queryset.filter = Mock(return_value=mock_queryset) actual = dbutils.count_by_group(mock_queryset, 'level', options) expected = {'level': {'high': 3, 'medium': 3, 'low': 3}} self.assertEqual(actual, expected)