Commit 1abb2dfa authored by Alan Trick's avatar Alan Trick

fix programming bug with by-month trackstats metrics, better test coverage

parent 762c6dd3
Pipeline #39000254 (#14) passed with stage
in 1 minute and 28 seconds
......@@ -18,4 +18,4 @@ demo:
django-admin runserver
coverage:
pytest --cov=django_adminstats tests/
pytest --cov=django_adminstats --cov-report=html tests/
......@@ -16,7 +16,3 @@ def register_model(model):
def register_metric(metric, cls=None):
from .trackstats import MetricRegistration
register(MetricRegistration(metric, cls))
def get_registry_ids():
return REGISTRY.keys()
......@@ -18,17 +18,17 @@ class Registration(metaclass=abc.ABCMeta):
@abc.abstractmethod
def key(self) -> str:
"""A unique string for this statistics registration"""
...
raise NotImplementedError
@property
@abc.abstractmethod
def label(self) -> str:
"""A human-readable name for these statistics"""
...
raise NotImplementedError
@abc.abstractmethod
def get_queryset(self):
...
raise NotImplementedError
def get_span_queryset(self, start, end, _period_step):
qs = self.get_queryset().annotate(
......
......@@ -20,18 +20,12 @@ class MetricRegistration(Registration):
self.metric = metric
self.model = model
lazy_func = getattr(metric, '_setupfunc')
if lazy_func:
contents = lazy_func.__closure__[1].cell_contents
self.ref = contents['ref']
domain = contents['domain']
lazy_func = getattr(domain, '_setupfunc')
if lazy_func:
contents = lazy_func.__closure__[1].cell_contents
self.domain_ref = contents['ref']
else:
self.domain_ref = metric.domain.ref
else:
self.ref = metric.ref
contents = lazy_func.__closure__[1].cell_contents
self.ref = contents['ref']
domain = contents['domain']
lazy_func = getattr(domain, '_setupfunc')
contents = lazy_func.__closure__[1].cell_contents
self.domain_ref = contents['ref']
@property
def key(self):
......@@ -94,7 +88,7 @@ class MetricRegistration(Registration):
if period_step == Step.DAY:
kwargs['period'] = trackstats.models.Period.DAY
elif period_step == Step.MONTH:
kwargs['period'] == trackstats.models.Period.MONTH
kwargs['period'] = trackstats.models.Period.MONTH
else:
# trackstats doesn't actually support year
kwargs['period'] = trackstats.models.Period.LIFETIME
......
......@@ -101,6 +101,13 @@
metric: 1
period: 86400
value: 2
- model: trackstats.statisticbydate
pk: 4
fields:
date: 2018-11-1
metric: 1
period: 2592000
value: 7
- model: trackstats.statisticbydateandobject
pk: 1
fields:
......
......@@ -123,6 +123,12 @@ def test_pageview_lookup_filter_options():
assert expected == qsp.options(models.PageView)
def test_modelregistry_query_options():
reg = registry.REGISTRY['tests.pageview']
expected = ['id', 'page', 'ip_address', 'date']
assert expected == reg.query_options('', is_filter=False)
def test_trackstats_nomodel_options():
reg = registry.REGISTRY['trackstats:page.view']
assert [] == reg.query_options('', is_filter=False)
......
......@@ -7,7 +7,7 @@ from django_adminstats import Step, registry
from . import models
class QueryModelDemoTests(TestCase):
class QueryModelTests(TestCase):
"""Test querying model data"""
fixtures = ['demo']
......@@ -48,7 +48,7 @@ class QueryModelDemoTests(TestCase):
assert expected == actual
class QueryTrackstatsDemoTests(TestCase):
class QueryTrackstatsTests(TestCase):
"""Test querying trackstats data"""
fixtures = ['demo']
......@@ -73,6 +73,33 @@ class QueryTrackstatsDemoTests(TestCase):
period_step=Step.DAY, axis_text='', group_text='', filter_text='')
assert expected == actual
@staticmethod
def test_bad_axis():
reg = registry.REGISTRY['trackstats:page.view']
try:
reg.get_data(
start=date(2018, 11, 1), end=date(2018, 11, 12),
period_step=Step.DAY, axis_text='period', group_text='',
filter_text='')
except ValueError:
pass
else:
assert False, 'Function should have raised ValueError because ' \
'axis query isn\'t supported in tracstats'
@staticmethod
def test_by_month():
reg = registry.REGISTRY['trackstats:page.view']
# note: we're only going to get results for november, because
# we don't have stats in the fixtures for any other month
expected = {'': {
date(2018, 11, 1): '7',
}}
actual = reg.get_data(
start=date(2018, 10, 1), end=date(2018, 12, 1),
period_step=Step.MONTH, axis_text='', group_text='', filter_text='')
assert expected == actual
# Note: this is not the desired behavior, but it is the current behavior
@staticmethod
def test_group_by_path():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment