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
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)
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)
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 ]
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)
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)
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)
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)
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)
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)
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)
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)
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)
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