Skip to content

Commit

Permalink
Make it possible to guess Thingy.client from Thingy.database
Browse files Browse the repository at this point in the history
As of now:
- Thingy.client can be guessed from Thingy.database
- Thingy.database can be guessed from Thingy._client or Thingy._collection
- Thingy.collection can be guessed from Thingy.database
  • Loading branch information
ramnes committed Apr 26, 2018
1 parent 130cd08 commit 284e2b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
18 changes: 14 additions & 4 deletions mongo_thingy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ def collection_name(cls):

@classproperty
def client(cls):
return cls._client
return cls.get_client()

@classmethod
def _get_client(cls, database):
return database.client

@classmethod
def _get_database(cls, collection, name):
if collection:
return collection.database
if cls.client and name:
return cls.client[name]
raise AttributeError("Undefined client.")
if cls._client and name:
return cls._client[name]
raise AttributeError("Undefined database.")

@classmethod
def _get_table(cls, database, table_name):
Expand All @@ -58,6 +62,12 @@ def _get_database_name(cls, database):
def _get_table_name(cls, table):
return table.name

@classmethod
def get_client(cls):
if cls._client:
return cls._client
return cls._get_client(cls.database)

@classmethod
def get_collection(cls):
return cls.get_table()
Expand Down
28 changes: 25 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def test_thingy_database(TestThingy, database):
assert TestThingy.database == database


def test_thingy_client(TestThingy, client):
assert TestThingy.client == client


def test_thingy_collection(TestThingy, collection):
assert TestThingy.collection == collection

Expand Down Expand Up @@ -54,13 +58,27 @@ class FooBar(Thingy):
assert FooBar.collection_name == collection.name


def test_thingy_database_from_client(client):
class FooBar(Thingy):
_client = client

assert FooBar.database == client.foo


def test_thingy_database_from_collection(collection):
class Foo(Thingy):
_collection = collection

assert Foo.database == collection.database


def test_thingy_client_from_database(database):
class Foo(Thingy):
_database = database

assert Foo.client == database.client


def test_thingy_collection_from_database(database):
class Foo(Thingy):
_database = database
Expand Down Expand Up @@ -106,22 +124,26 @@ def test_thingy_count(TestThingy, collection):
@pytest.mark.parametrize("connect", [connect, Thingy.connect])
@pytest.mark.parametrize("disconnect", [disconnect, Thingy.disconnect])
def test_thingy_connect_disconnect(connect, disconnect):
assert Thingy.client is None
with pytest.raises(AttributeError):
Thingy.client

connect()
assert isinstance(Thingy.client, MongoClient)
assert Thingy._database is None

disconnect()
assert Thingy.client is None
assert Thingy._client is None

connect("mongodb://hostname/database")
assert isinstance(Thingy.client, MongoClient)
assert Thingy.database
assert Thingy.database.name == "database"
disconnect()

assert Thingy.client is None
assert Thingy._client is None
with pytest.raises(AttributeError):
Thingy.client

assert Thingy._database is None
with pytest.raises(AttributeError):
Thingy.database
Expand Down

0 comments on commit 284e2b3

Please sign in to comment.