Skip to content

Commit

Permalink
Merge 4e11919 into cdfd693
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSkriblovsky authored Oct 13, 2016
2 parents cdfd693 + 4e11919 commit eebda2f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def setUp(self):

@defer.inlineCallbacks
def tearDown(self):
yield self.named_conn.drop_database("db")
yield self.named_conn.disconnect()
yield self.unnamed_conn.disconnect()

Expand Down Expand Up @@ -75,3 +76,38 @@ def patch_deadline(_):
d_insert = self.named_conn.db.coll.find_one(
{'x': 42}, deadline=time()+2)
yield self.assertFailure(d_insert, TimeExceeded)


class TestDropDatabase(unittest.TestCase):

@defer.inlineCallbacks
def setUp(self):
self.conn = connection.ConnectionPool()
self.db = self.conn.db
self.coll = self.db.coll
yield self.coll.insert({'x': 42})
yield self.assert_coll_count(1)

@defer.inlineCallbacks
def tearDown(self):
yield self.conn.drop_database(self.db)
yield self.conn.disconnect()

@defer.inlineCallbacks
def assert_coll_count(self, n):
self.assertEqual((yield self.coll.count()), n)

@defer.inlineCallbacks
def test_by_name(self):
yield self.conn.drop_database("db")
yield self.assert_coll_count(0)

def test_invalid_args(self):
self.assertRaises(TypeError, self.conn.drop_database, 42)
self.assertRaises(TypeError, self.conn.drop_database, self.conn)
self.assertRaises(TypeError, self.conn.drop_database, self.coll)

@defer.inlineCallbacks
def test_by_object(self):
yield self.conn.drop_database(self.db)
yield self.assert_coll_count(0)
11 changes: 11 additions & 0 deletions txmongo/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ def get_default_database(self):
else:
return None

def drop_database(self, name_or_database):
if isinstance(name_or_database, (bytes, StringType)):
db = self[name_or_database]
elif isinstance(name_or_database, Database):
db = name_or_database
else:
raise TypeError("argument to drop_database() should be database name "
"or database object")

return db.command("dropDatabase")

def disconnect(self):
for factory in self.__pool:
factory.stopTrying()
Expand Down

0 comments on commit eebda2f

Please sign in to comment.