Skip to content

Commit

Permalink
Added command line arguments tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evildoor committed Nov 16, 2018
1 parent 8f06ec3 commit 4c1c44a
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
Empty file.
137 changes: 137 additions & 0 deletions Utils/Dataflow/pyDKB/dataflow/stage/tests/test_ProcessorStage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/env python

"""
Tests for pyDKB.dataflow.stage.ProcessorStage.
Usage: 'python -m unittest discover' from ..
(directory with pyDKB.dataflow.stage code).
"""


import os
import sys
import unittest


# Relative import inside of pyDKB prevents the use of simple 'import pyDKB'.
try:
base_dir = os.path.dirname(__file__) # Directory with this file
dkb_dir = os.path.join(base_dir, os.pardir) # stage directory
dkb_dir = os.path.join(dkb_dir, os.pardir) # dataflow directory
dkb_dir = os.path.join(dkb_dir, os.pardir) # pyDKB's directory
dkb_dir = os.path.join(dkb_dir, os.pardir) # pyDKB's parent directory
sys.path.append(dkb_dir)
import pyDKB
except Exception, err:
sys.stderr.write("(ERROR) Failed to import pyDKB library: %s\n" % err)
sys.exit(1)


class ProcessorStageArgsTestCase(unittest.TestCase):
default_args = {
'mode': 'f',
'config': None,
'eom': '\n',
'eop': '',
'source': 'f',
'dest': 'f',
'input_dir': os.curdir,
'output_dir': 'out',
'hdfs': False,
'input_files': []
}

def setUp(self):
self.stage = pyDKB.dataflow.stage.ProcessorStage()

def tearDown(self):
self.stage = None

def check_args(self, args):
for a in args:
self.assertEqual(getattr(self.stage.ARGS, a), args[a])

def test_default(self):
self.stage.parse_args('')
self.check_args(self.default_args)

def test_hdfs(self):
self.stage.parse_args(['--hdfs'])
args = dict(self.default_args)
args['hdfs'] = True

This comment has been minimized.

Copy link
@mgolosova

mgolosova Nov 16, 2018

Collaborator

Also:

args['source'] = 'h'
args['dest'] = 'h'

?
Maybe it is not how things work now, yet it is how it should be. According to The Plan ;)

This comment has been minimized.

Copy link
@Evildoor

Evildoor Nov 19, 2018

Author Contributor

Yes, see below.

self.check_args(args)

def test_eom(self):
self.stage.parse_args(['-e', '\t'])
args = dict(self.default_args)
args['eom'] = '\t'

This comment has been minimized.

Copy link
@mgolosova

mgolosova Feb 13, 2019

Collaborator

It could make sense to use r'\t' in parse_args() -- as values are passed from command line, so it is most likely that they are passed as some escape sequence, not tab or any other special symbol itself. Yet they are supposed to be interpreted ("decoded") in parse_args(), so should be checked against ordinary '\t'.

And maybe there should be two tests (with raw and interpreted strings passed); I don`t know what can go wrong, but it doesn`t mean it can`t :)

self.check_args(args)

def test_eop(self):
self.stage.parse_args(['-E', '\t'])
args = dict(self.default_args)
args['eop'] = '\t'
self.check_args(args)

def test_input_dir(self):
self.stage.parse_args(['--input-dir', 'something'])
args = dict(self.default_args)
args['input_dir'] = 'something'
self.check_args(args)

def test_i(self):
self.stage.parse_args(['-i', 'something'])
args = dict(self.default_args)
args['input_dir'] = 'something'
self.check_args(args)

def test_output_dir(self):
self.stage.parse_args(['--output-dir', 'something'])
args = dict(self.default_args)
args['output_dir'] = 'something'
self.check_args(args)

def test_o(self):
self.stage.parse_args(['-o', 'something'])
args = dict(self.default_args)
args['output_dir'] = 'something'
self.check_args(args)


args_to_add = {
'source': ['f', 's', 'h'],
'dest': ['f', 's', 'h'],
'mode': ['f', 's', 'm'],
}


def add_arg(arg, val, short=False):
def f(self):
if short:
self.stage.parse_args(['-' + arg[0], val])
else:
self.stage.parse_args(['--' + arg, val])
args = dict(self.default_args)
args[arg] = val
self.check_args(args)

This comment has been minimized.

Copy link
@mgolosova

mgolosova Nov 16, 2018

Collaborator

This is fine for ‘source’ and ‘dest’, but “mode” is a combination of parameters. Here you check that command line is properly parsed and --mode s gives args['mode'] == 's', but not the logic described in help message:

 -m MODE, --mode MODE  processing mode: (f)ile, (s)tream or (m)ap-reduce.
                        Processing mode is a shortcut for a combination of four
                        parameters: "-s SRC -d DEST -e EOM -E EOP", where:
                        
                         mode || -s | -d | -e | -E
                        ===========================
                          s   ||  s |  s | \n | \0
                        ---------------------------
                          f   ||  f |  f | \n | ''
                        ---------------------------
                          m   ||  s |  s | \n | ''
                        

This comment has been minimized.

Copy link
@Evildoor

Evildoor Nov 19, 2018

Author Contributor

Correct, I just wanted to check something before proceeding to this arguments-affecting-each-other part.

if short:
setattr(ProcessorStageArgsTestCase, 'test_%s_%s' % (arg[0], val), f)
else:
setattr(ProcessorStageArgsTestCase, 'test_%s_%s' % (arg, val), f)


for a in args_to_add:
for v in args_to_add[a]:
add_arg(a, v)
add_arg(a, v, True)


test_cases = (
ProcessorStageArgsTestCase,
)


def load_tests(loader, tests, pattern):
suite = unittest.TestSuite()
for case in test_cases:
suite.addTest(loader.loadTestsFromTestCase(case))
return suite

0 comments on commit 4c1c44a

Please sign in to comment.