Milimetric has uploaded a new change for review.
https://gerrit.wikimedia.org/r/81153
Change subject: test coverage is now over 90 percent
......................................................................
test coverage is now over 90 percent
Change-Id: I0e625cb126ca264c6aca8c394554daad3d8b88d4
---
A tests/test_core_classes.py
M tests/test_metrics/test_pages_created.py
M tests/test_models/test_aggregate_report.py
M tests/test_models/test_metric_report.py
M tests/test_models/test_multi_project_metric_report.py
A tests/test_models/test_report.py
M tests/test_models/test_run_report.py
A tests/test_models/test_user.py
A tests/test_utils/test_one_off_functions.py
M wikimetrics/database.py
M wikimetrics/metrics/namespace_edits.py
M wikimetrics/metrics/pages_created.py
M wikimetrics/models/report_nodes/report.py
13 files changed, 257 insertions(+), 62 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/analytics/wikimetrics
refs/changes/53/81153/1
diff --git a/tests/test_core_classes.py b/tests/test_core_classes.py
new file mode 100644
index 0000000..8c0ae79
--- /dev/null
+++ b/tests/test_core_classes.py
@@ -0,0 +1,33 @@
+import os
+from unittest import TestCase
+from nose.tools import assert_equals, assert_true
+from wikimetrics.configurables import db
+from wikimetrics.database import get_host_projects, get_host_projects_map
+
+
+class DatabaseSetupTest(TestCase):
+
+ def test_get_host_projects(self):
+ (host_one, projects) = get_host_projects(1)
+ assert_equals(host_one, 1)
+ assert_true('enwiki' in projects)
+ print projects
+
+ def test_get_host_projects_map(self):
+ project_host_map = get_host_projects_map()
+ assert_true('enwiki' in project_host_map)
+ assert_true('arwiki' in project_host_map)
+ assert_true('commonswiki' in project_host_map)
+
+ #def test_get_fresh_project_host_map(self):
+ #project_host_map_cache_file = 'project_host_map.json'
+ ## make sure any cached file is deleted
+ #if os.path.exists(project_host_map_cache_file):
+ #os.remove(project_host_map_cache_file)
+
+ #db.get_project_host_map(usecache=True)
+ #assert_true(os.path.exists(project_host_map_cache_file))
+
+ #os.remove(project_host_map_cache_file)
+ #db.get_project_host_map(usecache=False)
+ #assert_true(os.path.exists(project_host_map_cache_file))
diff --git a/tests/test_metrics/test_pages_created.py
b/tests/test_metrics/test_pages_created.py
index ece6213..23902b5 100644
--- a/tests/test_metrics/test_pages_created.py
+++ b/tests/test_metrics/test_pages_created.py
@@ -30,5 +30,3 @@
)
results = metric(list(self.cohort), self.mwSession)
assert_equal(results[self.evan_id]["pages_created"], 2)
-
-
diff --git a/tests/test_models/test_aggregate_report.py
b/tests/test_models/test_aggregate_report.py
index ca536dc..1c0f929 100644
--- a/tests/test_models/test_aggregate_report.py
+++ b/tests/test_models/test_aggregate_report.py
@@ -12,10 +12,10 @@
def test_basic_response(self):
cohort = self.session.query(Cohort).get(self.test_cohort_id)
metric = metric_classes['NamespaceEdits'](
- name = 'NamespaceEdits',
- namespaces = [0, 1, 2],
- start_date = '2013-06-01',
- end_date = '2013-09-01',
+ name='NamespaceEdits',
+ namespaces=[0, 1, 2],
+ start_date='2013-06-01',
+ end_date='2013-09-01',
)
ar = AggregateReport(
cohort,
@@ -33,25 +33,14 @@
.filter(PersistentReport.id == ar.persistent_id)\
.one()\
.result_key
- inner_key = self.session.query(PersistentReport)\
- .filter(PersistentReport.id == ar.children[0].persistent_id)\
- .one()\
- .result_key
assert_equals(
- result
- [aggregate_key]
- [Aggregation.IND]
- [0]
- [self.test_mediawiki_user_id]
- ['edits'],
+
result[aggregate_key][Aggregation.IND][0][self.test_mediawiki_user_id]
+ ['edits'],
2
)
assert_equals(
- result
- [aggregate_key]
- [Aggregation.AVG]
- ['edits'],
+ result[aggregate_key][Aggregation.AVG]['edits'],
Decimal(1.25)
)
@@ -61,10 +50,10 @@
def test_finish(self):
cohort = self.session.query(Cohort).get(self.test_cohort_id)
metric = metric_classes['NamespaceEdits'](
- name = 'NamespaceEdits',
- namespaces = [0, 1, 2],
- start_date = '2013-06-01',
- end_date = '2013-09-01',
+ name='NamespaceEdits',
+ namespaces=[0, 1, 2],
+ start_date='2013-06-01',
+ end_date='2013-09-01',
)
ar = AggregateReport(
cohort,
@@ -117,10 +106,10 @@
def test_repr(self):
cohort = self.session.query(Cohort).get(self.test_cohort_id)
metric = metric_classes['NamespaceEdits'](
- name = 'NamespaceEdits',
- namespaces = [0, 1, 2],
- start_date = '2013-06-01',
- end_date = '2013-09-01',
+ name='NamespaceEdits',
+ namespaces=[0, 1, 2],
+ start_date='2013-06-01',
+ end_date='2013-09-01',
)
ar = AggregateReport(
cohort,
diff --git a/tests/test_models/test_metric_report.py
b/tests/test_models/test_metric_report.py
index c183375..11aa623 100644
--- a/tests/test_models/test_metric_report.py
+++ b/tests/test_models/test_metric_report.py
@@ -10,10 +10,10 @@
def test_basic_response(self):
metric = metric_classes['NamespaceEdits'](
- name = 'NamespaceEdits',
- namespaces = [0, 1, 2],
- start_date = '2013-06-01',
- end_date = '2013-09-01',
+ name='NamespaceEdits',
+ namespaces=[0, 1, 2],
+ start_date='2013-06-01',
+ end_date='2013-09-01',
)
mr = MetricReport(
metric,
@@ -30,10 +30,10 @@
def test_repr(self):
metric = metric_classes['NamespaceEdits'](
- name = 'NamespaceEdits',
- namespaces = [0, 1, 2],
- start_date = '2013-06-01',
- end_date = '2013-09-01',
+ name='NamespaceEdits',
+ namespaces=[0, 1, 2],
+ start_date='2013-06-01',
+ end_date='2013-09-01',
)
mr = MetricReport(
metric,
diff --git a/tests/test_models/test_multi_project_metric_report.py
b/tests/test_models/test_multi_project_metric_report.py
index 25bf91c..767999b 100644
--- a/tests/test_models/test_multi_project_metric_report.py
+++ b/tests/test_models/test_multi_project_metric_report.py
@@ -11,10 +11,10 @@
def test_basic_response(self):
cohort = self.session.query(Cohort).get(self.test_cohort_id)
metric = metric_classes['NamespaceEdits'](
- name = 'NamespaceEdits',
- namespaces = [0, 1, 2],
- start_date = '2013-06-01',
- end_date = '2013-09-01',
+ name='NamespaceEdits',
+ namespaces=[0, 1, 2],
+ start_date='2013-06-01',
+ end_date='2013-09-01',
)
mr = MultiProjectMetricReport(cohort, metric, 'enwiki')
@@ -33,10 +33,10 @@
def test_finish(self):
cohort = self.session.query(Cohort).get(self.test_cohort_id)
metric = metric_classes['NamespaceEdits'](
- name = 'NamespaceEdits',
- namespaces = [0, 1, 2],
- start_date = '2013-06-01',
- end_date = '2013-09-01',
+ name='NamespaceEdits',
+ namespaces=[0, 1, 2],
+ start_date='2013-06-01',
+ end_date='2013-09-01',
)
mr = MultiProjectMetricReport(cohort, metric, 'enwiki')
@@ -55,10 +55,10 @@
def test_repr(self):
cohort = self.session.query(Cohort).get(self.test_cohort_id)
metric = metric_classes['NamespaceEdits'](
- name = 'NamespaceEdits',
- namespaces = [0, 1, 2],
- start_date = '2013-06-01',
- end_date = '2013-09-01',
+ name='NamespaceEdits',
+ namespaces=[0, 1, 2],
+ start_date='2013-06-01',
+ end_date='2013-09-01',
)
mr = MultiProjectMetricReport(cohort, metric, 'enwiki')
diff --git a/tests/test_models/test_report.py b/tests/test_models/test_report.py
new file mode 100644
index 0000000..180f7cd
--- /dev/null
+++ b/tests/test_models/test_report.py
@@ -0,0 +1,82 @@
+# 101-108, 114, 130-145, 153
+from nose.tools import assert_equals, assert_true
+from wikimetrics.metrics import metric_classes
+from wikimetrics.models import (
+ Report, ReportNode, ReportLeaf, PersistentReport, MetricReport,
+)
+from wikimetrics.models import queue_task
+from ..fixtures import QueueDatabaseTest, DatabaseTest
+
+
+class ReportTest(QueueDatabaseTest):
+
+ # TODO: figure out a way to mock current_task so we can test
ReportNode:run()
+ #def test_report_node_run(self):
+
+ #edits_metric = metric_classes['NamespaceEdits'](
+ #name = 'NamespaceEdits',
+ #namespaces = [0, 1, 2],
+ #start_date = '2013-06-01',
+ #end_date = '2013-09-01',
+ #)
+ #bytes_metric = metric_classes['NamespaceEdits'](
+ #name = 'BytesAdded',
+ #namespaces = [0, 1, 2],
+ #start_date = '2013-06-01',
+ #end_date = '2013-09-01',
+ #)
+ #children = [
+ #MetricReport(edits_metric, [self.test_mediawiki_user_id],
'enwiki'),
+ #MetricReport(bytes_metric, [self.test_mediawiki_user_id],
'enwiki'),
+ #]
+ #report_node = ReportNode(children=children)
+ #results = report_node.run()
+
+ #assert_equals(len(results), 2)
+
+ def test_report_node_finish(self):
+ report_node = ReportNode()
+ report_node.finish([1, 2, 3])
+ assert_true(True)
+
+ def test_report_run(self):
+ report = Report()
+ report.run()
+ assert_true(True)
+
+
+class ReportWithoutQueueTest(DatabaseTest):
+
+ def test_repr(self):
+ r = Report()
+ assert_true(str(r).find('Report') >= 0)
+
+
+class QueueTaskTest(QueueDatabaseTest):
+
+ def test_queue_task(self):
+ fr = FakeReport()
+ result = queue_task(fr)
+ assert_equals(result, 'hello world')
+
+ def test_set_status(self):
+ fr = FakeReport()
+ fr.set_status('STARTED')
+ pr_started = self.session.query(PersistentReport).get(fr.persistent_id)
+ assert_equals(pr_started.status, 'STARTED')
+ assert_equals(pr_started.queue_result_key, None)
+
+ def test_set_status_and_task(self):
+ fr = FakeReport()
+ fr.set_status('WORKING', task_id=1)
+ pr_working = self.session.query(PersistentReport).get(fr.persistent_id)
+ assert_equals(pr_working.status, 'WORKING')
+ assert_equals(pr_working.queue_result_key, '1')
+
+
+class FakeReport(Report):
+ """
+ This just helps with some of the tests above
+ """
+ def run(self):
+ return 'hello world'
diff --git a/tests/test_models/test_run_report.py
b/tests/test_models/test_run_report.py
index 11561cf..72adbea 100644
--- a/tests/test_models/test_run_report.py
+++ b/tests/test_models/test_run_report.py
@@ -1,4 +1,4 @@
-from nose.tools import assert_equals, assert_true
+from nose.tools import assert_equals, assert_true, raises
from celery.exceptions import SoftTimeLimitExceeded
from wikimetrics.models import (
RunReport, Aggregation, PersistentReport
@@ -162,3 +162,28 @@
print('Successes: {0}'.format(successes))
assert_true(successes == trials, 'all of the trials must succeed')
+
+ # TODO: This is weird, the exception seems to be thrown
+ # But the line is still showing as not covered by tests
+ @raises(Exception)
+ def test_invalid_metric(self):
+ run_report = RunReport()
+ run_report.parse_request([{
+ 'name': 'Edits - test',
+ 'cohort': {
+ 'id': self.test_cohort_id,
+ },
+ 'metric': {
+ 'name': 'NamespaceEdits',
+ 'namespaces': 'blah blah',
+ },
+ }])
+
+ def test_run_report_finish(self):
+ run_report = RunReport([])
+ result = run_report.finish([])
+ assert_equals(result[run_report.result_key], 'Finished')
+
+ def test_run_report_repr(self):
+ run_report = RunReport([])
+ assert_true(str(run_report).find('RunReport') >= 0)
diff --git a/tests/test_models/test_user.py b/tests/test_models/test_user.py
new file mode 100644
index 0000000..3521c67
--- /dev/null
+++ b/tests/test_models/test_user.py
@@ -0,0 +1,32 @@
+from nose.tools import assert_equals, assert_true, raises
+from wikimetrics.models import User
+from ..fixtures import DatabaseTest
+
+
+class UserTest(DatabaseTest):
+
+ def test_logout(self):
+ user = self.session.query(User).get(self.test_web_user_id)
+ user.logout(self.session)
+ user = self.session.query(User).get(self.test_web_user_id)
+ assert_equals(user.authenticated, False)
+ assert_equals(user.active, False)
+
+ def test_detach_from(self):
+ user = self.session.query(User).get(self.test_web_user_id)
+ user.authenticated = False
+ self.session.commit()
+
+ user.detach_from(self.session)
+ user.authenticated = True
+ self.session.commit()
+
+ user = self.session.query(User).get(self.test_web_user_id)
+ assert_equals(user.authenticated, False)
+
+ def test_is_anonymous(self):
+ user = self.session.query(User).get(self.test_web_user_id)
+ user.authenticated = False
+ self.session.commit()
+
+ assert_equals(user.is_anonymous(), True)
diff --git a/tests/test_utils/test_one_off_functions.py
b/tests/test_utils/test_one_off_functions.py
new file mode 100644
index 0000000..8ed9e82
--- /dev/null
+++ b/tests/test_utils/test_one_off_functions.py
@@ -0,0 +1,28 @@
+import datetime
+import decimal
+from nose.tools import assert_true, assert_equals
+from unittest import TestCase
+from wikimetrics.utils import (
+ stringify, mediawiki_date,
+)
+from wikimetrics.metrics import NamespaceEdits
+
+
+class UtilsTest(TestCase):
+
+ def test_better_encoder_date(self):
+ string = stringify(date_not_date_time=datetime.date(2013, 06, 01))
+ assert_true(string.find('"date_not_date_time"') >= 0)
+
+ def test_better_encoder_decimal(self):
+ string = stringify(deci=decimal.Decimal(6.01))
+ assert_true(string.find('"deci"') >= 0)
+
+ def test_better_encoder_default(self):
+ string = stringify(normal='hello world')
+ assert_true(string.find('"normal"') >= 0)
+
+ def test_mediawiki_date(self):
+ edits = NamespaceEdits(start_date='2013-06-01')
+ mw_date = mediawiki_date(edits.start_date)
+ assert_equals(mw_date, '20130601000000')
diff --git a/wikimetrics/database.py b/wikimetrics/database.py
index 6e3b013..c57d24d 100644
--- a/wikimetrics/database.py
+++ b/wikimetrics/database.py
@@ -39,6 +39,20 @@
return (host_id, projects)
+def get_host_projects_map():
+ # TODO: these numbers are hardcoded, is that ok?
+ num_hosts = 7
+ host_projects = map(get_host_projects, range(1, num_hosts + 1))
+ project_host_map = {}
+ host_fmt = 's{0}'
+ for host_id, projects in host_projects:
+ host = host_fmt.format(host_id)
+ for project in projects:
+ project_host_map[project] = host
+
+ return project_host_map
+
+
class Database(object):
"""
Basically a collection of all database related objects and methods.
@@ -151,15 +165,8 @@
"""
cache_name = 'project_host_map.json'
if not exists(cache_name) or not usecache:
- # TODO: these numbers are hardcoded, is that ok?
- num_hosts = 7
- host_projects = map(get_host_projects, range(1, num_hosts + 1))
- project_host_map = {}
- host_fmt = 's{0}'
- for host_id, projects in host_projects:
- host = host_fmt.format(host_id)
- for project in projects:
- project_host_map[project] = host
+
+ project_host_map = get_host_projects_map()
if usecache and os.access(cache_name, os.W_OK):
try:
json.dump(project_host_map, open(cache_name, 'w'))
diff --git a/wikimetrics/metrics/namespace_edits.py
b/wikimetrics/metrics/namespace_edits.py
index a40d8c0..08a550f 100644
--- a/wikimetrics/metrics/namespace_edits.py
+++ b/wikimetrics/metrics/namespace_edits.py
@@ -70,8 +70,8 @@
.join(Page)
.filter(Page.page_namespace.in_(self.namespaces.data))
.filter(Revision.rev_user.in_(user_ids))
- .filter(Revision.rev_timestamp >= start_date)\
- .filter(Revision.rev_timestamp <= end_date)\
+ .filter(Revision.rev_timestamp >= start_date)
+ .filter(Revision.rev_timestamp <= end_date)
.group_by(Revision.rev_user)
.all()
)
diff --git a/wikimetrics/metrics/pages_created.py
b/wikimetrics/metrics/pages_created.py
index 10097cd..bcc30b0 100644
--- a/wikimetrics/metrics/pages_created.py
+++ b/wikimetrics/metrics/pages_created.py
@@ -37,7 +37,7 @@
start_date = DateField(default=thirty_days_ago)
end_date = DateField(default=today)
-
+
namespaces = CommaSeparatedIntegerListField(
None,
[Required()],
@@ -70,7 +70,7 @@
.filter(Revision.rev_timestamp <= end_date)
.all()
)
-
+
return {
user_id: {'pages_created': p.get(user_id, 0)}
for user_id in user_ids
diff --git a/wikimetrics/models/report_nodes/report.py
b/wikimetrics/models/report_nodes/report.py
index 06c95dc..ec401bb 100644
--- a/wikimetrics/models/report_nodes/report.py
+++ b/wikimetrics/models/report_nodes/report.py
@@ -16,6 +16,7 @@
'Report',
'ReportNode',
'ReportLeaf',
+ 'queue_task',
]
--
To view, visit https://gerrit.wikimedia.org/r/81153
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0e625cb126ca264c6aca8c394554daad3d8b88d4
Gerrit-PatchSet: 1
Gerrit-Project: analytics/wikimetrics
Gerrit-Branch: master
Gerrit-Owner: Milimetric <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits