Beispiel #1
0
    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):
        """Get an instance.

        :param str path:
        :param dict overrides:
        :param func builder:
        """
        if not path:
            return self.get_all_instances(overrides=overrides, builder=builder)

        remaining_path = ''
        if isinstance(path, _compat.string_types):
            path = path.split(".")
            first_level = path[0]
            remaining_path = ".".join(path[1:])

        # First try to get the fixture from the cache
        instance = self.fixture_manager.cache.get(first_level)
        if (not overrides
                and instance
                and not isinstance(instance, FixtureCollection)):
            if not remaining_path:
                return instance
            return utils.richgetter(instance, remaining_path)

        # Or just get it
        fixture = self.get(first_level)
        # Then we ask it to return an instance.
        return fixture.get_instance(path=remaining_path,
                                    overrides=overrides,
                                    builder=builder,
                                    )
    def get_instance(self, path=None, overrides=None, builder=None):
        """Get an instance.

        :param str path:
        :param dict overrides:
        :param func builder:
        """
        if not path:
            return self.get_all_instances(overrides=overrides, builder=builder)

        remaining_path = ''
        if isinstance(path, _compat.string_types):
            path = path.split(".")
            first_level = path[0]
            remaining_path = ".".join(path[1:])

        # First try to get the fixture from the cache
        instance = self.fixture_manager.cache.get(first_level)
        if (not overrides and instance
                and not isinstance(instance, FixtureCollection)):
            if not remaining_path:
                return instance
            return utils.richgetter(instance, remaining_path)

        # Or just get it
        fixture = self.get(first_level)
        # Then we ask it to return an instance.
        return fixture.get_instance(
            path=remaining_path,
            overrides=overrides,
            builder=builder,
        )
Beispiel #4
0
    def test_resolves_objects(self):
        obj = Namespace(key=Namespace(value='value'))

        result = richgetter(obj, 'key.value')

        self.assertEquals(result, 'value')
Beispiel #5
0
    def test_resolves_mixed_containers(self):
        obj = Namespace(key=dict(values=['value']))

        result = richgetter(obj, 'key.values.0')

        self.assertEquals(result, 'value')
Beispiel #6
0
    def test_resolves_sequences(self):
        obj = [['value']]

        result = richgetter(obj, '0.0')

        self.assertEquals(result, 'value')
Beispiel #7
0
    def test_resolves_mappings(self):
        obj = dict(key=dict(value='value'))

        result = richgetter(obj, 'key.value')

        self.assertEquals(result, 'value')
Beispiel #8
0
    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