def test_newtool_overwriting_no(tmpdir): """ Run 'pytool newtool testtool tt' while testtool.py does exist. Verify that we are prompted about overwriting. Answer 'no' and verify that the original file is not overwritten. """ pytest.debug_func() tname = toyfile(tmpdir, "testtool.py", content=["original testtool.py"]) tlink = toyfile(tmpdir, "testtool", content=["original testtool"]) with U.Chdir(tmpdir.strpath): cmd = pexpect.which("pytool") if cmd is None: pytest.skip("pytool not found") S = pexpect.spawn("{} newtool {} tt".format(cmd, tlink)) which = S.expect([r'Are you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('no') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail("should have asked about overwriting {}".format(tname)) for fname in [tlink, tname]: exp = "original %s" % fname.basename got = fname.read() assert got == exp
def test_execution(): """ Does pstrack run successfully? """ pytest.debug_func() result = pexpect.run('pstrack') assert 'Traceback' not in result
def test_offset_list(tz, exp): """ Check the behavior of function offset_list() """ pytest.debug_func() result = nldt.offset_list(tz) assert result == exp
def test_hexdump(): """ Routine hexdump() reads one file and hexdumps it to another. """ pytest.debug_func() exp = "\n".join([ " 54 77 61 73 20 62 72 69 6c 6c 69 67 20 61 6e 64 " "Twas bri llig and", " 20 74 68 65 20 73 6c 69 74 68 65 20 74 6f 76 65 " " the sli the tove", " 73 0a 44 69 64 20 67 79 72 65 20 61 6e 64 20 67 " "s.Did gy re and g", " 69 6d 62 6c 65 20 72 6f 75 6e 64 20 74 68 65 20 " "imble ro und the ", " 77 61 62 65 0a 41 6c 6c 20 6d 69 6d 73 79 20 77 " "wabe.All mimsy w", " 65 72 65 20 74 68 65 20 62 6f 72 6f 67 72 6f 76 " "ere the borogrov", " 65 73 0a 41 6e 64 20 74 68 65 20 6d 6f 6d 65 20 " "es.And t he mome ", " 72 61 74 68 73 20 6f 75 74 67 72 61 62 65 0a " "raths ou tgrabe. ", "" ]) q = sio("\n".join(["Twas brillig and the slithe toves", "Did gyre and gimble round the wabe", "All mimsy were the borogroves", "And the mome raths outgrabe\n"])) z = sio() hd.hexdump(q, z) result = z.getvalue() z.close() q.close() assert exp == result
def test_hd_help(): """ Verify that 'hd --help' does the right thing """ pytest.debug_func() result = pexpect.run("hd --help") assert "Hexdump stdin or a file" in result
def test_docopt(fx_usage): """ Check that pstrack.__doc__ contains what docopt requires """ pytest.debug_func() for strval in fx_usage: assert any([strval in line for line in pstrack.__doc__.split("\n")])
def test_mega(fx_mchk): """ Test 'mag' for megabits (1000^2) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "98765432", "98.77 Mb" fx_mchk.result = mag.main([fx_mchk.inp], True)
def test_yotta(fx_mchk): """ Test 'mag' for terabits (1000^8) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "75843541873239090087685432", "75.84 Yb" fx_mchk.result = mag.main([fx_mchk.inp], True)
def test_bmega(fx_mchk): """ Test 'mag' for mebibits (1024^2) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "98765432", "94.19 Mib" fx_mchk.result = mag.main(["-b", fx_mchk.inp], True)
def test_giga(fx_mchk): """ Test 'mag' for megabits (1000^3) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "12398765432", "12.40 Gb" fx_mchk.result = mag.main([fx_mchk.inp], True)
def test_bgiga(fx_mchk): """ Test 'mag' for gibibits (1024^3) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "12398765432", "11.55 Gib" fx_mchk.result = mag.main(["-b", fx_mchk.inp], True)
def test_tera(fx_mchk): """ Test 'mag' for terabits (1000^4) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "12390008765432", "12.39 Tb" fx_mchk.result = mag.main([fx_mchk.inp], True)
def test_week_find_day(text, exp): """ Coverage for week.find_day() """ pytest.debug_func() w = nldt.week() assert w.find_day(text) == exp
def test_newpy_overwriting_no(tmpdir): """ Run 'pytool newpy xyzzy' when xyzzy already exists. Verify that confirmation is requested. Answer 'no' and verify that the existing file is not overwritten. """ pytest.debug_func() xyzzy = toyfile(tmpdir, "xyzzy", content="original xyzzy\n") xyzzy_py = toyfile(tmpdir, "xyzzy.py", content="original xyzzy.py\n") with U.Chdir(tmpdir.strpath): cmd = pexpect.which("pytool") S = pexpect.spawn("{} newpy xyzzy".format(cmd)) which = S.expect([r'you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('no') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail('should have asked about overwriting xyzzy') expected = ['original xyzzy'] got = U.contents(xyzzy.strpath) assert got == expected expected = ['original xyzzy.py'] got = U.contents(xyzzy_py.strpath) assert got == expected
def test_newtool_overwriting_yes(tmpdir): """ Run 'pytool newtool testtool tt' while testtool.py exists. Verify that we are prompted about overwriting. Answer 'yes' and verify that the original file is overwritten. """ pytest.debug_func() tname = toyfile(tmpdir, "testtool.py", content=["original testtool.py"]) tlink = toyfile(tmpdir, "testtool", content=["original testtool"]) with U.Chdir(tmpdir.strpath): cmd = pexpect.which("pytool") if cmd is None: pytest.skip("pytool not found") S = pexpect.spawn("{} newtool {} tt".format(cmd, tlink)) which = S.expect([r'Are you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('yes') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail("should have asked about overwriting {}".format(tname)) exp = tname.strpath actual = tlink.readlink() assert actual == exp expected = expected_testtool_py() got = tname.read().split("\n") assert got == expected
def test_newpy_overwriting_yes(tmpdir): """ Run 'pytool newpy xyzzy' when xyzzy, xyzzy.py already exist. Verify that confirmation is requested. Answer 'yes' and verify that the existing file IS overwritten. """ pytest.debug_func() xyzzy = toyfile(tmpdir, "xyzzy", content="original xyzzy") xyzzy_py = toyfile(tmpdir, "xyzzy.py", content="original xyzzy.py") with U.Chdir(tmpdir.strpath): cmd = pexpect.which('pytool') S = pexpect.spawn('%s newpy %s' % (cmd, xyzzy.strpath)) which = S.expect([r'Are you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('yes') S.expect(pexpect.EOF) elif which == 1: print S.before + S.after pytest.fail('unexpected exception') else: pytest.fail('should have asked about overwriting xyzzy') exp = os.path.abspath(xyzzy_py.strpath) got = os.readlink(xyzzy.strpath) assert got == exp expected = expected_xyzzy_py() got = U.contents(xyzzy_py.strpath) assert got == expected
def test_bpeta(fx_mchk): """ Test 'mag' for tibibits (1024^5) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "71233986700065432", "63.27 Pib" fx_mchk.result = mag.main(["-b", fx_mchk.inp], True)
def test_newtool(tmpdir): """ Run 'pytool newtool testtool' while testtool.py does not exist. Verify that testtool.py is created and has the right contents. """ pytest.debug_func() toolname = toyfile(tmpdir, "testtool.py") toollink = toyfile(tmpdir, "testtool") with U.Chdir(tmpdir.strpath): cmd = pexpect.which("pytool") if cmd is None: pytest.skip("pytool not found") S = pexpect.spawn("{} newtool {} tt".format(cmd, toollink.strpath)) which = S.expect([r'Are you sure\? >', 'Error:', pexpect.EOF]) if which == 0: S.sendline('no') S.expect(pexpect.EOF) pytest.fail('should not have asked about overwriting') elif which == 1: print S.before + S.after pytest.fail('unexpected exception') assert toolname.strpath == toollink.readlink() expected = expected_testtool_py() got = toolname.read().split("\n") assert got == expected
def test_bbit(fx_mchk): """ Test 'mag' for bits (1024^0) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "999", "999.00 b" fx_mchk.result = mag.main([fx_mchk.inp], True)
def test_tu_magnitude(inp, exp): """ Test magnitude() method of class time_units """ pytest.debug_func() tu = nldt.time_units() assert tu.magnitude(inp) == exp
def test_week_match_weekdays(inp, exp): """ Verify that the weekday matching regex works """ pytest.debug_func() w = nldt.week() assert re.findall(w.match_weekdays(), inp, re.I) == exp
def test_btera(fx_mchk): """ Test 'mag' for tibibits (1024^4) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "12390008765432", "11.27 Tib" fx_mchk.result = mag.main(["-b", fx_mchk.inp], True)
def test_gtx_hooks_create_all_already(tmpdir): """ Ask to create all links, some already exist """ pytest.debug_func() hooklist = ['first', 'second', 'third'] bn_d = gtxtest_setup_hldirs(tmpdir.strpath, hooks=hooklist, mklinks=['second']) assertLink(bn_d['second']['hook'], bn_d['second']['link']) hookdir = bn_d['hookdir'] linkdir = bn_d['linkdir'] S = pexpect.spawn("gtx hooks -C -H %s -l %s" % (hookdir, linkdir)) while True: which = S.expect(["does not appear to have a link", "already has link", pexpect.EOF, pexpect.TIMEOUT]) if 0 == which: S.expect(r"Shall I add one\? >") S.sendline("y") elif 1 == which: pass else: break assert(" <-- " in S.before) for hook in hooklist: assertLink(bn_d[hook]['hook'], bn_d[hook]['link'])
def test_bexa(fx_mchk): """ Test 'mag' for tibibits (1024^6) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "87271233986700065432", "75.70 Eib" fx_mchk.result = mag.main(["-b", fx_mchk.inp], True)
def test_gtx_hooks_create_name_already(tmpdir): """ Create by name, some already there """ pytest.debug_func() hooklist = ['one', 'two', 'three'] hl_d = gtxtest_setup_hldirs(tmpdir.strpath, hooks=hooklist, mklinks=['two']) hookdir = hl_d['hookdir'] linkdir = hl_d['linkdir'] assertLink(hl_d['two']['hook'], hl_d['two']['link']) S = pexpect.spawn("gtx hooks -C -H %s -l %s three" % (hookdir, linkdir)) while True: which = S.expect(["does not appear to have a link", "already has link", pexpect.EOF, pexpect.TIMEOUT]) if 0 == which: S.expect(r"Shall I add one\? >") S.sendline("y") elif 1 == which: pass else: break S.expect(pexpect.EOF) assertNoLink(hl_d['one']['hook'], hl_d['one']['link']) assertLink(hl_d['two']['hook'], hl_d['two']['link']) assertLink(hl_d['three']['hook'], hl_d['three']['link'])
def test_bzetta(fx_mchk): """ Test 'mag' for tibibits (1024^7) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "23487271233986700065432", "19.89 Zib" fx_mchk.result = mag.main(["-b", fx_mchk.inp], True)
def test_pydoc(): """ Verify that public items show up in pydoc output while private items do not """ pytest.debug_func() present = ['__call__', '__eq__', '__init__', '__repr__', '__str__', 'dst', 'epoch', 'localtime', 'class duration', 'class Indexable', 'class moment', 'class month', 'class week', 'class Parser', 'class prepositions', 'class time_units', 'class Stub', 'class InitError', ] absent = ['_DAY', '_end_of_day', '_end_of_month', '_end_of_week', '_guess_format', '_MONTHS', '_MONTH_LEN', '_nl_match', '_parse_return', '_WEEK', '_week_ago', '_WEEKDAYS', 'month_index', 'month_names', 'weekday_index', 'weekday_names', 'parse' ] docker = pydoc.TextDoc() result = re.sub("\x08.", "", docker.document(nldt)) for item in present: assert item in result for item in absent: pattern = r"\W" + item + r"\W" assert not re.search(pattern, result)
def test_mcal_title(): """ Verify that title puts out what we expect """ pytest.debug_func() result = mcal.title(1999, 2) assert result == ' 1999.02 '
def test_peta(fx_mchk): """ Test 'mag' for terabits (1000^5) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "17239090087685432", "17.24 Pb" fx_mchk.result = mag.main([fx_mchk.inp], True)
def test_tu_find_unit(inp, exp): """ Test find_unit() method of class time_units """ pytest.debug_func() tu = nldt.time_units() assert tu.find_unit(inp) == exp
def test_month_isleap(year, exp): """ month.isleap() returns True when year is a leap year, otherwise False """ pytest.debug_func() m = nldt.month() assert m.isleap(year) == exp
def test_exa(fx_mchk): """ Test 'mag' for terabits (1000^6) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "41873239090087685432", "41.87 Eb" fx_mchk.result = mag.main([fx_mchk.inp], True)
def test_kilo(fx_mchk): """ Test 'mag' for bits (1000^0) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "98765", "98.77 Kb" fx_mchk.result = mag.main([fx_mchk.inp], True)
def test_bkilo(fx_mchk): """ Test 'mag' for kibibits (1024^1) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "98765", "96.45 Kib" fx_mchk.result = mag.main(["-b", fx_mchk.inp], True)
def test_byotta(fx_mchk): """ Test 'mag' for tibibits (1024^8) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "39423487271233986700065432", "32.61 Yib" fx_mchk.result = mag.main(["-b", fx_mchk.inp], True)
def test_gtx_hooks_create_all_nothing(tmpdir): """ Create all links, nothing in place """ pytest.debug_func() hooklist = ['one', 'two', 'three'] hl_d = gtxtest_setup_hldirs(tmpdir.strpath, hooks=hooklist) hookdir = hl_d['hookdir'] linkdir = hl_d['linkdir'] S = pexpect.spawn("gtx hooks -C -H %s -l %s" % (hookdir, linkdir)) while True: which = S.expect(["does not appear to have a link", "already has link", pexpect.EOF, pexpect.TIMEOUT]) if 0 == which: S.expect(r"Shall I add one\? >") S.sendline("y") elif 1 == which: pytest.fail("'already has link' was not expected") else: break S.expect(pexpect.EOF) for hook in hooklist: hpath = os.path.join(hl_d['hookdir'], hook) lpath = os.path.join(hl_d['linkdir'], hook) assert(os.path.exists(hpath)) assert(os.path.exists(lpath))
def test_zetta(fx_mchk): """ Test 'mag' for terabits (1000^7) """ pytest.debug_func() fx_mchk.inp, fx_mchk.exp = "43541873239090087685432", "43.54 Zb" fx_mchk.result = mag.main([fx_mchk.inp], True)
def test_flake(): """ Scan code for good formatting """ pytest.debug_func() flake_cmd = "flake8 {}".format(" ".join(pyfiles())) result = tbx.run(flake_cmd) assert result == ''
def test_week_index(inp, exp): """ Verify that weekday names are mapped to the correct numbers """ pytest.debug_func() w = nldt.week() for inps in inp: assert w.index(inps) == exp
def test_tu_unit_list(): """ Verify time_units().unit_list() """ pytest.debug_func() tu = nldt.time_units() exp = ["second", "minute", "hour", "day", "week", "month", "year"] assert list(tu.unit_list()) == exp
def test_week_fullname(inpl, exp): """ Verify mapping index and abbreviations to full names """ pytest.debug_func() w = nldt.week() for inp in inpl: assert w.fullname(inp) == exp
def test_tu_constructor(): """ Test class time_units constructor """ pytest.debug_func() tu = nldt.time_units() for unit in ['second', 'minute', 'hour', 'day', 'week', 'month', 'year']: assert unit in tu._units
def test_month_under_days(): """ """ pytest.debug_func() mobj = nldt.month() exp = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] for dnum in range(1, 13): assert mobj._days(dnum) == exp[dnum-1]
def test_week_backdiff(inps, inpe, exp): """ week.backdiff() returns the number of days between two week days, jumping backward. The returned value ranges between 1 and 7 """ pytest.debug_func() w = nldt.week() assert w.backdiff(inps, inpe) == exp
def test_from_now_except(): """ Cover the ValueError exception in parse_ago """ pytest.debug_func() prs = nldt.Parser() with pytest.raises(ValueError) as err: prs("no number no unit from now") assert txt["err-nounit"] in str(err)
def test_tz_daylight(): """ Return 1 if the timezone has a DST definition, else 0 """ pytest.debug_func() lz = nldt.timezone('US/Central') assert lz.daylight() == 1 hz = nldt.timezone('US/Hawaii') assert hz.daylight() == 0
def test_version(): """ Verify that nldt.version() returns a valid version string. test_deployable() late in the test sequence will verify the correctness of the version returned. This test just makes sure the version string is reasonable (i.e., matches the regexp). """ pytest.debug_func() assert re.match(r"\d+\.\d+\.\d+", nldt.version())
def test_debug(): """ Cover the line where we fire up the debugger """ pytest.debug_func() proc = pexpect.spawn('nldt -d today') proc.expect('(Pdb)') proc.sendline('c') proc.expect(pexpect.EOF)
def test_indexable_abc(): """ Indexable is an abstract base class that should not be instantiated directly. """ pytest.debug_func() with pytest.raises(TypeError) as err: _ = nldt.Indexable() assert isinstance(_, nldt.Indexable) assert txt['ABC-noinst'] in str(err)
def test_caller_name(): """ Test function caller_name() """ pytest.debug_func() def foobar(): assert nldt.caller_name() == 'test_caller_name' foobar()
def test_month_names(): """ nldt.month_names() returns the list of month names in order """ pytest.debug_func() m = nldt.month() result = m.names() exp = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'] assert result == exp
def test_week_day_list(): """ Verify nldt.week.day_list() """ pytest.debug_func() w = nldt.week() dl = w.day_list() for wname in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']: assert wname.lower() in dl
def test_month_short_names(): """ month.short_names() is supposed to return a list of three letter lowercase month name abbreviations """ pytest.debug_func() m = nldt.month() for item in m.short_names(): assert len(item) == 3 assert item.lower() == item
def test_tz_tzname(): """ Method tzname() returns a tuple containing the abbreviated timezone names. Method zone() returns the name used to initialize the object. """ pytest.debug_func() inp = 'US/Mountain' lz = nldt.timezone(inp) assert lz.tzname() == ('MST', 'MDT') assert lz.zone() == inp
def test_Stub(): """ The Stub class raises an exception reporting the current function that needs attention """ pytest.debug_func() with pytest.raises(nldt.Stub) as err: raise nldt.Stub("extra text") msg = "test_Stub() is a stub -- please complete it. (extra text)" assert msg in str(err)
def test_parse_now(): """ Test for nldt.Parser() object parsing 'now' """ pytest.debug_func() prs = nldt.Parser() q = M() then = M(time.time() - 30) assert prs('now') == q assert prs('now', q) == q assert prs('now', then) == then
def test_month_days_curyear(): """ Verify that month.days() for february this year does the right thing """ pytest.debug_func() mobj = nldt.month() now = nldt.moment() curyear = int(now('%Y')) exp = 29 if mobj.isleap(curyear) else 28 # payload assert mobj.days(2) == exp
def test_week_constructor(): """ nldt.week() constructor should return an object with dict of months """ pytest.debug_func() w = nldt.week() assert hasattr(w, '_dict') for midx in range(0, 7): assert midx in w._dict for wname in ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']: assert wname in w._dict
def test_dst(when, zone, exp): """ Testing dst function """ pytest.debug_func() if isinstance(exp, Exception): with pytest.raises(type(exp)) as err: assert nldt.dst(when, zone) assert str(exp) in str(err) else: assert nldt.dst(when, zone) == exp
def test_tz_context_explicit(zname, std, soff, dst, doff): """ Verify that 'with nldt.tz_context(FOOBAR)' creates a context with FOOBAR as the local timezone. """ pytest.debug_func() with nldt.tz_context(zname): assert time.timezone == soff assert time.altzone == doff assert time.daylight == (soff != doff) assert time.tzname == (std, dst)