def add_entry(app, title, body): ''' Provide a single entry in the database. ''' now = datetime.datetime.utcnow() expected = (title, body, now) with closing(connect_db(settings)) as db: world.run_query(db, INSERT_ENTRY, expected, False) db.commit() return expected
def teardown(total): with app.test_request_context('/'): con = get_database_connection() cur = con.cursor() cur.execute("DELETE FROM entries") # and here as well con.commit() with closing(connect_db()) as db: db.cursor().execute("DROP TABLE entries") db.commit()
def req_context(db, request): """mock a request with a database attached""" settings = db req = testing.DummyRequest() with closing(connect_db(settings)) as db: req.db = db req.exception = None yield req clear_entries(settings)
def req_context(db, request): '''Mock a request with a database attached.''' settings = db req = testing.DummyRequest() with closing(connect_db(settings)) as db: req.db = db req.exception = None yield req # After the test has run, clear out the entries clear_entries(settings)
def content_gen(db, request): """Create a single entry to test.""" settings = db input = ('some title', 'some text', datetime.datetime.utcnow()) with closing(connect_db(settings)) as db: db.cursor().execute(INSERT_ENTRY, input) db.commit() def cleanup(): clear_entries(settings) request.addfinalizer(cleanup) return input
def entry(db, request): """provide a single entry in the database""" settings = db now = datetime.datetime.utcnow() expected = ('Test Title', 'Test Text', now) with closing(connect_db(settings)) as db: run_query(db, INSERT_ENTRY, expected, False) db.commit() def cleanup(): clear_entries(settings) request.addfinalizer(cleanup) return expected
def clear_db(): # This ensures the connection is closed later. # Context library is all for this kind of context stuff. with contextlib.closing(connect_db()) as db: # Testing is not supposed to be used with a deployed database, # apparently. That's where TEST_DSN's specification comes in: # This will all be done in the test_learning_journal db. # ... # NOTE: This database must be created manually on the CLI. # Done with: # createdb test_learning_journal db.cursor().execute("DROP TABLE entries") db.commit()
def setup_db(): """set up test database""" # set up test database settings = {'db': TEST_DSN} with closing(connect_db(settings)) as db: db.cursor().execute(DB_SCHEMA) db.commit() world.settings = settings # setup test app os.environ['DATABASE_URL'] = TEST_DSN app = main() world.app = TestApp(app) login_data = {'username': '******', 'password': '******'} world.app.post('/login', params=login_data) # add initial entry world.make_entry('Test Title', 'Test Text')
def clear_db(): with closing(connect_db()) as db: db.cursor().execute("DROP TABLE entries") db.commit()
def clear_db(results): with closing(connect_db()) as db: db.cursor().execute("DROP TABLE entries") db.commit()
def clear_entries(settings): with closing(connect_db(settings)) as db: db.cursor().execute("DELETE FROM entries") db.commit()
def init_db(settings): with closing(connect_db(settings)) as db: db.cursor().execute(DB_SCHEMA) db.commit()
def clear_db(scenario): ''' Clear the test database. ''' with closing(connect_db(settings)) as db: db.cursor().execute("DROP TABLE entries") db.commit()
def init_db(scenario): '''Initialize a test database for the tests.''' with closing(connect_db(settings)) as db: db.cursor().execute(world.DB_SCHEMA) db.commit()
def teardown_db(total): """tear down a database""" with closing(connect_db(world.settings)) as db: db.cursor().execute("DROP TABLE entries") db.commit()