|
- import nilmdb
- from nilmdb.printf import *
- import nilmdb.cmdline
-
- from nose.tools import *
- from nose.tools import assert_raises
- import json
- import itertools
- import os
- import shutil
- import sys
- import threading
- import urllib2
- from urllib2 import urlopen, HTTPError
- import Queue
- import StringIO
- import shlex
-
- from test_helpers import *
-
- testdb = "tests/cmdline-testdb"
-
- class TestCmdline(object):
-
- def setUp(self):
- # Clear out DB
- recursive_unlink(testdb)
-
- # Start web app on a custom port
- self.db = nilmdb.NilmDB(testdb, sync=False)
- self.server = nilmdb.Server(self.db, host = "127.0.0.1",
- port = 12380, stoppable = False)
- self.server.start(blocking = False)
-
- def tearDown(self):
- # Close web app
- self.server.stop()
- self.db.close()
-
- def run(self, arg_string, input_string = None):
- """Run a cmdline client with the specified argument string,
- passing the given input. Returns a tuple with the output and
- exit code"""
- class stdio_wrapper:
- def __init__(self, stdin, stdout, stderr):
- self.io = (stdin, stdout, stderr)
- def __enter__(self):
- self.saved = ( sys.stdin, sys.stdout, sys.stderr )
- ( sys.stdin, sys.stdout, sys.stderr ) = self.io
- def __exit__(self, type, value, traceback):
- ( sys.stdin, sys.stdout, sys.stderr ) = self.saved
- infile = StringIO.StringIO(input_string)
- outfile = StringIO.StringIO()
- with stdio_wrapper(infile, outfile, outfile) as s:
- try:
- nilmdb.cmdline.run(args = shlex.split(arg_string))
- sys.exit(0)
- except SystemExit as e:
- exitcode = e.code
- output = outfile.getvalue()
- return (output, exitcode)
-
- def test_cmdline_basic(self):
- (output, exitcode) = self.run("--help")
- assert("Usage:" in output)
- eq_(exitcode, 0)
-
- (output, exitcode) = self.run("--nosuchoption")
- ne_(exitcode, 0)
-
- (output, exitcode) = self.run("--url http://localhost:12380/")
- eq_(exitcode, 0)
|