Beispiel #1
0
 def test_get_all(self, category_manager, stored_category_batch_factory):
     """Make sure we get all stored categories."""
     categories = stored_category_batch_factory(5)
     result = category_manager.GetAll()
     result = [helpers.dbus_to_hamster_category(each) for each in result]
     assert len(result) == 5
     for category in categories:
         assert category in result
Beispiel #2
0
 def test_get_or_create_get(self, category_manager, stored_category):
     """If name exists make sure it is fetched and no new one created."""
     assert len(category_manager.GetAll()) == 1
     dbus_category = helpers.hamster_to_dbus_category(stored_category)
     result = category_manager.GetOrCreate(dbus_category)
     result = helpers.dbus_to_hamster_category(result)
     assert len(category_manager.GetAll()) == 1
     assert stored_category.as_tuple(include_pk=False) == result.as_tuple(
         include_pk=False)
Beispiel #3
0
 def test_get_or_create_created(self, category_manager, category):
     """Make sure an instance is created and returned."""
     assert not category_manager.GetAll()
     dbus_category = helpers.hamster_to_dbus_category(category)
     result = category_manager.GetOrCreate(dbus_category)
     result = helpers.dbus_to_hamster_category(result)
     assert result.pk
     assert category.as_tuple(include_pk=False) == result.as_tuple(
         include_pk=False)
Beispiel #4
0
    def get_all(self):
        """
        Return a list of all categories.

        Returns:
            list: List of ``Categories``, ordered by ``lower(name)``.
        """
        result = self._interface.GetAll()
        return [
            helpers.dbus_to_hamster_category(category) for category in result
        ]
Beispiel #5
0
    def test_save_new(self, category_manager, category,
                      category_name_parametrized):
        """Make sure an instance is created and returned."""

        category.name = category_name_parametrized
        dbus_category = helpers.hamster_to_dbus_category(category)
        result = category_manager.Save(dbus_category)
        result = helpers.dbus_to_hamster_category(result)
        assert result.pk
        assert category.as_tuple(include_pk=False) == result.as_tuple(
            include_pk=False)
Beispiel #6
0
    def get_by_name(self, name):
        """
        Look up a category by its name.

        Args:
            name (str): Unique name of the ``Category`` to we want to fetch.

        Returns:
            hamster_lib.Category: ``Category`` with given name.
        """
        result = self._interface.GetByName(name)
        return helpers.dbus_to_hamster_category(result)
Beispiel #7
0
    def get(self, pk):
        """
        Get an ``Category`` by its primary key.

        Args:
            pk (int): Primary key of the ``Category`` to be fetched.

        Returns:
            hamster_lib.Category: ``Category`` with given primary key.
        """
        result = self._interface.Get(int(pk))
        return helpers.dbus_to_hamster_category(result)
Beispiel #8
0
    def Save(self, category_tuple):  # NOQA
        """
        Save category.

        Args:
            category_tuple: hamster_lib.Category tuple.

        Returns:
            tuple: ``(category.pk, category.name)`` tuple.
        """
        category = helpers.dbus_to_hamster_category(category_tuple)
        category = self._controller.store.categories.save(category)

        self._main_object.CategoryChanged()
        return helpers.hamster_to_dbus_category(category)
Beispiel #9
0
    def GetOrCreate(self, category_tuple):  # NOQA
        """
        For details please refer to ``hamster_lib.storage``.

        Args:
            category (helpers.DBusCategory or None): The category 'dbus encoded'.

        Returns:
            helpers.DBusCategory or None: The retrieved or created category.
                Either way, the returned Category will contain all data from
                the backend, including its primary key.
        """
        category = helpers.dbus_to_hamster_category(category_tuple)
        category = self._controller.store.categories.get_or_create(category)

        self._main_object.CategoryChanged()
        return helpers.hamster_to_dbus_category(category)
Beispiel #10
0
    def save(self, category):
        """
        Save a Category.

        Args:
            category (hamster_lib.Category): Category instance to be saved.

        Returns:
            hamster_lib.Category: Saved Category

        Raises:
            TypeError: If the ``category`` parameter is not a valid ``Category`` instance.
        """
        if not isinstance(category, lib_objects.Category):
            message = _("You need to pass a hamster category")
            raise TypeError(message)

        dbus_category = helpers.hamster_to_dbus_category(category)
        result = self._interface.Save(dbus_category)
        return helpers.dbus_to_hamster_category(result)
Beispiel #11
0
    def get_or_create(self, category):
        """
        Check if we already got a category with that name, if not create one.

        For details see the corresponding method in ``hamster_lib.storage``.

        Args:
            category (hamster_lib.Category or None): The categories.

        Returns:
            hamster_lib.Category or None: The retrieved or created category. Either way,
                the returned Category will contain all data from the backend, including
                its primary key.

        Raises:
            TypeError: If ``category`` is not a ``lib_objects.Category`` instance.
        """
        if not isinstance(category, lib_objects.Category):
            message = _("You need to pass a hamster category")
            raise TypeError(message)

        dbus_category = helpers.hamster_to_dbus_category(category)
        result = self._interface.GetOrCreate(dbus_category)
        return helpers.dbus_to_hamster_category(result)
Beispiel #12
0
def test_dbus_to_hamster_category(category_tuple, expectation):
    """Make sure that de-serialization works as intended."""
    result = helpers.dbus_to_hamster_category(category_tuple)
    assert result == expectation
    if expectation:
        assert isinstance(result, Category)
Beispiel #13
0
 def factory(**kwargs):
     category = category_factory.build(**kwargs)
     result = category_manager.Save(
         helpers.hamster_to_dbus_category(category))
     return helpers.dbus_to_hamster_category(result)
Beispiel #14
0
 def test_get_by_name(self, category_manager, stored_category):
     """Make sure a matching category is returned."""
     result = category_manager.GetByName(stored_category.name)
     result = helpers.dbus_to_hamster_category(result)
     assert result.pk == stored_category.pk
     assert result.name == stored_category.name