def extract_relationships(self): """Return all dependencies. :rtype generator: Yields ``(depends_on, attr_name)``. """ # TODO: make this DRYer since it's mostly copied from # _process_relationships for dep in self.depend_on: yield dep, None for name, value in safe_iteritems(self.fields): # One to one relationship if isinstance(value, RelationshipToken): yield self.extract_rel_name(value) # One to many relationship elif isinstance(value, (tuple, list)): for i, nested_value in enumerate(value): if isinstance(nested_value, RelationshipToken): yield self.extract_rel_name(nested_value)
def get_instance(self, path=None, overrides=None, builder=None): """Instantiate the fixture using the model and return the instance. :param str path: remaining path to return :param dict overrides: overriding fields :param func builder: function that is used to get the fixture .. deprecated:: 0.4.0 ``fields`` argument renamed ``overrides``. .. versionadded:: 0.4.0 ``builder`` argument added. .. deprecated:: 0.3.7 ``include_relationships`` argument removed. """ self.inherit_from_parent() # Does the modification in place. if self.database_id: object_class = self.get_class() # No need to create a new object, just get it from the db instance = (self.fixture_manager.session.query(object_class).get( self.database_id)) else: # We need to do a copy since we're modifying them. params = copy.deepcopy(self.fields) if overrides: params.update(overrides) for key, value in safe_iteritems(params): if callable(value): params[key] = value() # Get the class object_class = self.get_class() # Does not return anything, does the modification in place (in # fields). self._process_relationships(params) if object_class: instance = builder(self.fixture_manager, object_class, params) else: # Return the fields as is. This allows to enter dicts # and lists directly. instance = params # Do any extra assignment for attr, value in self.post_creation.items(): if isinstance(value, RelationshipToken): value = self.get_relationship(value) setattr(instance, attr, value) if path: return richgetter(instance, path) else: return instance
def get_instance(self, path=None, overrides=None, builder=None): """Instantiate the fixture using the model and return the instance. :param str path: remaining path to return :param dict overrides: overriding fields :param func builder: function that is used to get the fixture .. deprecated:: 0.4.0 ``fields`` argument renamed ``overrides``. .. versionadded:: 0.4.0 ``builder`` argument added. .. deprecated:: 0.3.7 ``include_relationships`` argument removed. """ self.inherit_from_parent() # Does the modification in place. if self.database_id: object_class = self.get_class() # No need to create a new object, just get it from the db instance = ( self.fixture_manager.session.query(object_class) .get(self.database_id) ) else: # We need to do a copy since we're modifying them. params = copy.deepcopy(self.fields) if overrides: params.update(overrides) for key, value in safe_iteritems(params): if callable(value): params[key] = value() # Get the class object_class = self.get_class() # Does not return anything, does the modification in place (in # fields). self._process_relationships(params) if object_class: instance = builder(self.fixture_manager, object_class, params) else: # Return the fields as is. This allows to enter dicts # and lists directly. instance = params # Do any extra assignment for attr, value in self.post_creation.items(): if isinstance(value, RelationshipToken): value = self.get_relationship(value) setattr(instance, attr, value) if path: return richgetter(instance, path) else: return instance