Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
Add tests for relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
charlax committed Jan 21, 2014
1 parent ad7a331 commit 3f7e7ee
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 40 deletions.
12 changes: 9 additions & 3 deletions charlatan/fixture.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import importlib
import operator

from charlatan.file_format import RelationshipToken
from charlatan import _compat
Expand Down Expand Up @@ -169,7 +170,7 @@ def get_instance(self, path=None, include_relationships=True, fields=None):
setattr(instance, attr, value)

if path:
return getattr(instance, path)
return operator.attrgetter(path)(instance)
else:
return instance

Expand Down Expand Up @@ -199,7 +200,11 @@ def get_class(self):
model=self.model_name.lower())
klass = self.model_name

return get_class(module, klass)
try:
return get_class(module, klass)
except ImportError:
# Then try to import from yourlib:Toaster
return get_class(root_models_package, klass)

@staticmethod
def extract_rel_name(name):
Expand All @@ -210,7 +215,8 @@ def extract_rel_name(name):

# TODO: we support only one level for now
if "." in name:
rel_name, attr = name.split(".")
path = name.split(".")
rel_name, attr = path[0], path[1]
return rel_name, attr

def extract_relationships(self):
Expand Down
4 changes: 2 additions & 2 deletions charlatan/fixture_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def get(self, path):
if not path[0] in self.fixtures:
raise KeyError("No such fixtures: '%s'" % path[0])

return self.fixtures[path[0]], "".join(path[1:])
return self.fixtures[path[0]], ".".join(path[1:])


class ListFixtureCollection(FixtureCollection):
Expand All @@ -128,4 +128,4 @@ def get(self, path):
:param str path:
"""
path = path.split(".")
return self.fixtures[int(path[0])], "".join(path[1:])
return self.fixtures[int(path[0])], ".".join(path[1:])
9 changes: 0 additions & 9 deletions charlatan/tests/data/relationships.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,3 @@ relationship_alone:
fields:
name: "red"
model: charlatan.tests.fixtures.models:Color

toaster:
fields:
color: red
slots: 3
model: charlatan.tests.fixtures.simple_models:Toaster

toaster_colors:
fields: [!rel toaster.color]
2 changes: 1 addition & 1 deletion charlatan/tests/fixtures/simple_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Toaster(object):

def __init__(self, color, slots, content=None):
def __init__(self, color, slots=2, content=None):
self.color = color
self.slots = slots
self.content = content
Expand Down
15 changes: 0 additions & 15 deletions charlatan/tests/test_relationships.py

This file was deleted.

32 changes: 32 additions & 0 deletions docs/examples/relationships.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
toaster:
model: Toaster
fields:
color: red

user:
model: User
fields:
# You can link to another fixture
toasters:
- !rel toaster

toaster_colors:
# You can also link to a specific attribute
fields:
color: !rel toaster.color

toaster_colors_list:
fields: ['red']

# Let's define a collection
toasters:
model: Toaster
objects:
red:
color: red

toaster_from_collection:
inherit_from: toaster
fields:
# You can link a specific attribute of a collection's item.
color: !rel toasters.red.color
39 changes: 29 additions & 10 deletions docs/file-format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,41 @@ Linking to other objects

Example:

.. code-block:: yaml
user:
fields:
name: Michel Audiard
favorite_toaster: !rel red_toaster
toaster_id: !rel toaster.id
model: User
.. literalinclude:: examples/relationships.yaml
:language: yaml
:lines: 1-16

To link to another object defined in the configuration file, use ``!rel``. You
can link to another objet (e.g. ``!rel red_toaster``) or to another object's
attribute (e.g. ``!rel toaster.id``).
can link to another objet (e.g. ``!rel toaster``) or to another object's
attribute (e.g. ``!rel toaster.color``).

.. doctest::

>>> manager = FixturesManager()
>>> manager.load("docs/examples/relationships.yaml",
... models_package="charlatan.tests.fixtures.simple_models")
>>> manager.get_fixture("user").toasters
[<Toaster 'red'>]
>>> manager.get_fixture("toaster_colors")
{'color': 'red'}

You can also link to specific attributes of collection's item (see
:ref:`collection` for more information about collections).

.. literalinclude:: examples/relationships.yaml
:language: yaml
:lines: 18-

.. doctest::

>>> manager.get_fixture("toaster_from_collection")
<Toaster 'red'>

.. versionadded:: 0.2.0
It is now possible to link to another object' attribute.

.. _collection:

Collections of Fixtures
-----------------------

Expand Down

0 comments on commit 3f7e7ee

Please sign in to comment.