Last active
August 29, 2015 14:04
-
-
Save takus/a2ea6f483832b64c812b to your computer and use it in GitHub Desktop.
mrjob's unit test example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mrjob.job import MRJob | |
from mrjob.step import MRStep | |
class MRCountingJob(MRJob): | |
def __init__(self, *args, **kwargs): | |
super(MRCountingJob, self).__init__(*args, **kwargs) | |
def steps(self): | |
# 3 steps so we can check behavior of counters for multiple steps | |
return [self.mr(mapper=self.mapper), | |
self.mr(mapper=self.mapper), | |
self.mr(mapper=self.mapper)] | |
def mapper(self, _, value): | |
self.increment_counter('group', 'counter_name', 1) | |
yield _, value | |
if __name__ == '__main__': | |
MRCountingJob.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import with_statement | |
try: | |
import unittest2 as unittest | |
except ImportError: | |
import unittest | |
from StringIO import StringIO | |
from mr_counting_job import MRCountingJob | |
class CounterTestCase(unittest.TestCase): | |
def test_counters(self): | |
stdin = StringIO('foo\nbar\n') | |
mr_job = MRCountingJob(['--no-conf', '-']) | |
mr_job.sandbox(stdin=stdin) | |
with mr_job.make_runner() as runner: | |
runner.run() | |
self.assertEqual(runner.counters(), | |
[{'group': {'counter_name': 2}}, | |
{'group': {'counter_name': 2}}, | |
{'group': {'counter_name': 2}}]) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment