Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue5 test suite #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 50 additions & 55 deletions dbhandle.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,69 @@
import click
import subprocess
import pymongo
import sys

from ddsmdb.dbhandler import DBHandler
import json
import pprint

@click.command()
@click.option('--run/--no-run', default=None, help="run mongod with `--dbpath`")
@click.option('--launch/--no-launch', default=True, help="run mongod with `--dbpath`")
@click.option('--info/--no-info', default=False, help="the names of the databases")
@click.option('--create', default=None, help="create a database, specify a name")
@click.option('--delete', default=None, help="delete a database, specify a name")
@click.option('--shutdown/--no-shutdown', default=None, help='shutdown the database server using `--dbpath`')
@click.option('--create/--no-create', default=False, help="create a database")
@click.option('--delete/--no-delete', default=False, help="delete a database")
@click.option('--dbname', default=None, help="the name of the db")
@click.option('--shutdown/--no-shutdown', default=False, help='shutdown the database server using `--dbpath`')
@click.option('--dbpath', default=None, help='specify the dbpath to run or remove')
@click.option('--verbose/--no-verbose', default=False, help="dump mongod output to stdout")
@click.option('--port', default=None, type=int, help="the port number")
@click.option('--host', default=None, help="the host")
@click.option('--jsonfile', default=None, help="upload JSON data to database")
@click.option('--view/--no-view', default=False, help="dump the data in the database to screeen")

def handle(run, info, create, delete, shutdown, dbpath):
if run:
dbrun(dbpath)
def handle(launch,
info,
create,
delete,
dbname,
shutdown,
dbpath,
verbose,
port,
host,
jsonfile,
view):

client = pymongo.MongoClient('localhost', 27017)
if shutdown:
launch = False

handler = DBHandler(port=port, dbpath=dbpath, host=host, launch=launch, verbose=verbose)

if create or delete or jsonfile or view:
if not dbname:
dbname = click.prompt('enter a db name', type=str)

if create:
dbcreate(client, create)
handler.create(dbname)

if info:
dbinfo(client)
info = handler.info()
click.echo(info)

if delete:
dbdelete(client, delete)
handler.delete(dbname)

if jsonfile:
with open(jsonfile, 'r') as f:
data = json.load(f)
handler.create(dbname)
handler.set_data(dbname, data)

if view:
data = handler.get_data(dbname)
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data)

if shutdown:
dbshutdown(dbpath)
handler.shutdown()

def dbrun(dbpath):
command = ['mongod']
if dbpath:
command.append('--dbpath')
command.append(dbpath)
subprocess.Popen(command)

def dbcreate(client, dbname):
db = client[dbname]
collection = db['setup-collection']
collection.insert({'setup-data' : True})

def dbinfo(client):
click.echo('list of databases')
for name in client.database_names():
click.echo('database: {0}'.format(name))

def dbdelete(client, dbname):
if click.confirm('Do you want to delete the {0} database?'.format(dbname)):
client.drop_database(dbname)
click.echo('deleted the {0} database'.format(dbname))

def dbsetup():
#Add some test data to the database
pass

def dbshutdown(dbpath):
if "linux" in sys.platform or "darwin" in sys.platform:
command = ['mongo']
command.append('--eval')
command.append('db.getSiblingDB(\'admin\').shutdownServer()')
subprocess.call(command)
else:
click.echo('your {0} platform is not supported.'.format(sys.platform))
if click.confirm('Try the mongo shell option anyway?'):
command = ['mongo']
command.append('--eval')
command.append('db.getSiblingDB(\'admin\').shutdownServer()')
subprocess.call(command)

if __name__ == "__main__":
handle()

Expand Down
1 change: 1 addition & 0 deletions ddsmdb/data/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"a": 1}, {"b": 2}]
Loading