Skip to content

Commit

Permalink
Merge pull request #1306 from martinholmer/cli-test-option
Browse files Browse the repository at this point in the history
Add --test option to tc CLI
  • Loading branch information
martinholmer authored Apr 21, 2017
2 parents 7c7c36d + f3d087e commit ba873e2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
78 changes: 66 additions & 12 deletions taxcalc/cli/tc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@

import sys
import argparse
import difflib
from taxcalc import TaxCalcIO


TEST_INPUT_FILENAME = 'test.csv'
TEST_TAXYEAR = 2017


def main():
"""
Contains command-line interface (CLI) to Tax-Calculator TaxCalcIO class.
"""
# parse command-line arguments:
usage_str = 'tc INPUT TAXYEAR {}{}{}'.format(
'[--reform REFORM] [--assump ASSUMP]\n',
' ',
'[--exact] [--tables] [--graphs] [--ceeu] [--dump]')
' ',
'[--exact] [--tables] [--graphs] [--ceeu] [--dump] [--test]')
parser = argparse.ArgumentParser(
prog='',
usage=usage_str,
Expand Down Expand Up @@ -84,21 +89,31 @@ def main():
'output.'),
default=False,
action="store_true")
parser.add_argument('--test',
help=('optional flag that conducts installation '
'test. No screen output implies passed test; '
'differences on screen show failed test.'),
default=False,
action="store_true")
args = parser.parse_args()
# instantiate TaxCalcIO object and do tax analysis
tcio = TaxCalcIO(input_data=args.INPUT,
tax_year=args.TAXYEAR,
reform=args.reform,
assump=args.assump)
# write test input and expected output files if --test option specified
if args.test:
write_test_input_output_files()
inputfn = TEST_INPUT_FILENAME
taxyear = TEST_TAXYEAR
else:
inputfn = args.INPUT
taxyear = args.TAXYEAR
# instantiate taxcalcio object and do tax analysis
tcio = TaxCalcIO(input_data=inputfn, tax_year=taxyear,
reform=args.reform, assump=args.assump)
if len(tcio.errmsg) > 0:
sys.stderr.write(tcio.errmsg)
sys.stderr.write('USAGE: tc --help\n')
return 1
aging = args.INPUT.endswith('puf.csv') or args.INPUT.endswith('cps.csv')
tcio.init(input_data=args.INPUT,
tax_year=args.TAXYEAR,
reform=args.reform,
assump=args.assump,
aging = inputfn.endswith('puf.csv') or inputfn.endswith('cps.csv')
tcio.init(input_data=inputfn, tax_year=taxyear,
reform=args.reform, assump=args.assump,
growdiff_response=None,
aging_input_data=aging,
exact_calculations=args.exact)
Expand All @@ -111,10 +126,49 @@ def main():
output_graphs=args.graphs,
output_ceeu=args.ceeu,
output_dump=args.dump)
# compare test output with expected test output if --test option specified
if args.test:
compare_test_output_files()
# return no-error exit code
return 0
# end of main function code


EXPECTED_TEST_OUTPUT_FILENAME = 'test-{}-out.csv'.format(str(TEST_TAXYEAR)[2:])
ACTUAL_TEST_OUTPUT_FILENAME = 'test-{}-#-#.csv'.format(str(TEST_TAXYEAR)[2:])


def write_test_input_output_files():
"""
Write test input and expected output files.
"""
input_data = (
'RECID,MARS,XTOT,EIC,e00200,e00200p,e00200s,p23250,e18400,e19800\n'
'1, 2, 3, 1, 40000, 40000, 0, 0, 3000, 4000\n'
'2, 2, 3, 1,200000, 200000, 0, 0, 15000, 20000\n'
)
with open(TEST_INPUT_FILENAME, 'w') as ifile:
ifile.write(input_data)
expected_output_data = (
'RECID,YEAR,WEIGHT,INCTAX,LSTAX,PAYTAX\n'
'1,2017,0.00,682.99,0.00,6120.00\n'
'2,2017,0.00,29690.00,0.00,21572.80\n'
)
with open(EXPECTED_TEST_OUTPUT_FILENAME, 'w') as ofile:
ofile.write(expected_output_data)


def compare_test_output_files():
"""
Compare expected and actual test output files.
"""
explines = open(EXPECTED_TEST_OUTPUT_FILENAME, 'U').readlines()
actlines = open(ACTUAL_TEST_OUTPUT_FILENAME, 'U').readlines()
diff = difflib.unified_diff(explines, actlines,
fromfile=EXPECTED_TEST_OUTPUT_FILENAME,
tofile=ACTUAL_TEST_OUTPUT_FILENAME, n=0)
sys.stdout.writelines(diff)


if __name__ == '__main__':
sys.exit(main())
4 changes: 2 additions & 2 deletions taxcalc/records_variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,12 @@
},
"p22250": {
"type": "float",
"desc": "Sch D: Net short-term gain/loss",
"desc": "Sch D: Net short-term capital gains/losses",
"form": {"2013-2016": "1040 Sch D, line 7"}
},
"p23250": {
"type": "float",
"desc": "Sch D: Net long-term gain/loss",
"desc": "Sch D: Net long-term capital gains/losses",
"form": {"2013-2016": "1040 Sch D, line 15"}
},
"p25470": {
Expand Down

0 comments on commit ba873e2

Please sign in to comment.