From c81972e66e274dd7846412369082df404ce459ff Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Sun, 6 Jan 2013 19:25:07 -0500 Subject: [PATCH] Minor testsuite and commandline fixes. Now supports "list /foo/bar" in addition to the older "list --path /foo/bar" --- nilmdb/cmdline/cmdline.py | 4 ++ nilmdb/cmdline/list.py | 21 ++++++++-- runtests.py | 2 +- setup.cfg | 1 + tests/test.order | 5 +++ tests/test_cmdline.py | 82 ++++++++++++++++++++++++++++++++------- 6 files changed, 98 insertions(+), 17 deletions(-) diff --git a/nilmdb/cmdline/cmdline.py b/nilmdb/cmdline/cmdline.py index c1721b1..e500953 100644 --- a/nilmdb/cmdline/cmdline.py +++ b/nilmdb/cmdline/cmdline.py @@ -131,6 +131,10 @@ class Cmdline(object): self.parser_setup() self.args = self.parser.parse_args(self.argv) + # Run preconnect handler if there is one + if "preconnect" in self.args: + self.args.preconnect(self) + self.client = nilmdb.Client(self.args.url) # Make a test connection to make sure things work diff --git a/nilmdb/cmdline/list.py b/nilmdb/cmdline/list.py index 7294ecd..00a820a 100644 --- a/nilmdb/cmdline/list.py +++ b/nilmdb/cmdline/list.py @@ -3,6 +3,7 @@ from nilmdb.utils.printf import * import nilmdb.client import fnmatch +import argparse from argparse import ArgumentDefaultsHelpFormatter as def_form def setup(self, sub): @@ -13,13 +14,16 @@ def setup(self, sub): optionally filtering by layout or path. Wildcards are accepted. """) - cmd.set_defaults(handler = cmd_list) + cmd.set_defaults(preconnect = cmd_preconnect, + handler = cmd_list) group = cmd.add_argument_group("Stream filtering") + group.add_argument("-p", "--path", metavar="PATH", default="*", + help="Match only this path (-p can be omitted)") + group.add_argument("path_positional", default="*", + nargs="?", help=argparse.SUPPRESS) group.add_argument("-l", "--layout", default="*", help="Match only this stream layout") - group.add_argument("-p", "--path", default="*", - help="Match only this path") group = cmd.add_argument_group("Interval details") group.add_argument("-d", "--detail", action="store_true", @@ -31,6 +35,17 @@ def setup(self, sub): metavar="TIME", type=self.arg_time, help="Ending timestamp (free-form)") +def cmd_preconnect(self): + # A hidden "path_positional" argument lets the user leave off the + # "-p" when specifying the path. Handle it here. + got_opt = self.args.path != "*" + got_pos = self.args.path_positional != "*" + if got_pos: + if got_opt: + self.parser.error("too many paths specified") + else: + self.args.path = self.args.path_positional + def cmd_list(self): """List available streams""" streams = self.client.stream_list() diff --git a/runtests.py b/runtests.py index ba856ba..41fc9b5 100755 --- a/runtests.py +++ b/runtests.py @@ -26,7 +26,7 @@ class JimOrderPlugin(nose.plugins.Plugin): if order and os.path.exists(order): files = [] for line in open(order): - line = line.strip() + line = line.split('#')[0].strip() if not line: continue fn = os.path.join(addr.filename, line.strip()) diff --git a/setup.cfg b/setup.cfg index 7b72e12..c365558 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,6 +12,7 @@ cover-erase= #debug-log=nose.log stop= verbosity=2 +tests=tests #tests=tests/test_bulkdata.py #tests=tests/test_mustclose.py #tests=tests/test_lrucache.py diff --git a/tests/test.order b/tests/test.order index 7c646a4..d727b6e 100644 --- a/tests/test.order +++ b/tests/test.order @@ -1,3 +1,8 @@ +test_cmdline.py +########## + +test_client.py + test_printf.py test_lrucache.py test_mustclose.py diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 51d7f9f..41c0ea3 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -4,6 +4,7 @@ import nilmdb from nilmdb.utils.printf import * import nilmdb.cmdline +import unittest from nose.tools import * from nose.tools import assert_raises import itertools @@ -133,7 +134,7 @@ class TestCmdline(object): def dump(self): printf("-----dump start-----\n%s-----dump end-----\n", self.captured) - def test_cmdline_01_basic(self): + def test_01_basic(self): # help self.ok("--help") @@ -179,14 +180,14 @@ class TestCmdline(object): self.fail("extract --start 2000-01-01 --start 2001-01-02") self.contain("duplicated argument") - def test_cmdline_02_info(self): + def test_02_info(self): self.ok("info") self.contain("Server URL: http://localhost:12380/") self.contain("Server version: " + test_server.version) self.contain("Server database path") self.contain("Server database size") - def test_cmdline_03_createlist(self): + def test_03_createlist(self): # Basic stream tests, like those in test_client. # No streams @@ -230,10 +231,17 @@ class TestCmdline(object): "/newton/raw RawData\n" "/newton/zzz/rawnotch RawNotchedData\n") - # Match just one type or one path + # Match just one type or one path. Also check + # that --path is optional self.ok("list --path /newton/raw") self.match("/newton/raw RawData\n") + self.ok("list /newton/raw") + self.match("/newton/raw RawData\n") + + self.fail("list -p /newton/raw /newton/raw") + self.contain("too many paths") + self.ok("list --layout RawData") self.match("/newton/raw RawData\n") @@ -245,10 +253,14 @@ class TestCmdline(object): self.ok("list --path *zzz* --layout Raw*") self.match("/newton/zzz/rawnotch RawNotchedData\n") + self.ok("list *zzz* --layout Raw*") + self.match("/newton/zzz/rawnotch RawNotchedData\n") + self.ok("list --path *zzz* --layout Prep*") self.match("") - def test_cmdline_04_metadata(self): + + def test_04_metadata(self): # Set / get metadata self.fail("metadata") self.fail("metadata --get") @@ -305,7 +317,7 @@ class TestCmdline(object): self.fail("metadata /newton/nosuchpath") self.contain("No stream at path /newton/nosuchpath") - def test_cmdline_05_parsetime(self): + def test_05_parsetime(self): os.environ['TZ'] = "America/New_York" cmd = nilmdb.cmdline.Cmdline(None) test = datetime_tz.datetime_tz.now() @@ -320,7 +332,7 @@ class TestCmdline(object): eq_(cmd.parse_time("snapshot-20120405-140000.raw.gz"), test) eq_(cmd.parse_time("prep-20120405T1400"), test) - def test_cmdline_06_insert(self): + def test_06_insert(self): self.ok("insert --help") self.fail("insert /foo/bar baz qwer") @@ -380,7 +392,7 @@ class TestCmdline(object): # bad start time self.fail("insert --rate 120 --start 'whatever' /newton/prep /dev/null") - def test_cmdline_07_detail(self): + def test_07_detail(self): # Just count the number of lines, it's probably fine self.ok("list --detail") lines_(self.captured, 8) @@ -414,7 +426,7 @@ class TestCmdline(object): self.ok("list --detail") lines_(self.captured, 8) - def test_cmdline_08_extract(self): + def test_08_extract(self): # nonexistent stream self.fail("extract /no/such/foo --start 2000-01-01 --end 2020-01-01") self.contain("Error getting stream info") @@ -469,7 +481,7 @@ class TestCmdline(object): self.ok("extract -c /newton/prep --start 2000-01-01 --end 2020-01-01") self.match("43200\n") - def test_cmdline_09_truncated(self): + def test_09_truncated(self): # Test truncated responses by overriding the nilmdb max_results server_stop() server_start(max_results = 2) @@ -478,7 +490,51 @@ class TestCmdline(object): server_stop() server_start() - def test_cmdline_10_destroy(self): + @unittest.skip("not ready") + def test_10_remove(self): + # Removing data + + # Try nonexistent stream + self.fail("remove /no/such/foo --start 2000-01-01 --end 2020-01-01") + self.contain("Error getting stream info") + + # empty ranges return success + self.ok("remove /newton/prep " + + "--start '23 Mar 2012 10:00:30' " + + "--end '23 Mar 2012 10:00:30'") + self.match("") + self.ok("remove /newton/prep " + + "--start '23 Mar 2012 10:00:30.000001' " + + "--end '23 Mar 2012 10:00:30.000001'") + self.match("") + self.ok("remove /newton/prep " + + "--start '23 Mar 2022 10:00:30' " + + "--end '23 Mar 2022 10:00:30'") + self.match("") + + # Verbose + self.ok("remove -v /newton/prep " + + "--start '23 Mar 2012 10:00:30' " + + "--end '23 Mar 2012 10:00:30'") + self.match("0\n") + self.ok("remove --verbose /newton/prep " + + "--start '23 Mar 2012 10:00:30' " + + "--end '23 Mar 2012 10:00:30'") + self.match("0\n") + + # Remove various chunks of prep data and make sure + # they're gone + self.ok("remove -v /newton/prep " + + "--start '23 Mar 2012 10:00:30' " + + "--end '23 Mar 2012 10:00:40'") + self.match("1200\n") + self.ok("extract -c /newton/prep --start 2000-01-01 --end 2020-01-01") + self.match("42000\n") + + # See the missing chunk in list output + #self.ok("list --detail ") + + def test_11_destroy(self): # Delete records self.ok("destroy --help") @@ -529,7 +585,7 @@ class TestCmdline(object): self.ok("list --detail --path " + path) self.contain("(no intervals)") - def test_cmdline_11_unicode(self): + def test_12_unicode(self): # Unicode paths. self.ok("destroy /newton/asdf/qwer") self.ok("destroy /newton/prep") @@ -549,7 +605,7 @@ class TestCmdline(object): self.ok(u"destroy /düsseldorf/raw") - def test_cmdline_12_files(self): + def test_13_files(self): # Test BulkData's ability to split into multiple files, # by forcing the file size to be really small. server_stop()