コード例 #1
0
    def get(self, pk):
        """
        Return an activity based on its primary key.

        For details see the corresponding method in ``hamster_lib.storage``.
        """
        result = self._interface.Get(pk)
        return helpers.dbus_to_hamster_activity(result)
コード例 #2
0
 def test_get_all(self, activity_manager, stored_activity_batch_factory):
     """Make sure we get all stored categories."""
     activities = stored_activity_batch_factory(5)
     result = activity_manager.GetAll(-2)
     result = [helpers.dbus_to_hamster_activity(each) for each in result]
     assert len(result) == 5
     for activity in activities:
         assert activity in result
コード例 #3
0
 def test_save_new(self, activity_manager, activity):
     """Make sure instance is saved and returned."""
     dbus_activity = helpers.hamster_to_dbus_activity(activity)
     result = activity_manager.Save(dbus_activity)
     result = helpers.dbus_to_hamster_activity(result)
     assert result.pk
     assert result.name == activity.name
     # We create a new instance, hence result will have a pk where the
     # original does not.
     assert activity.as_tuple(include_pk=False) == result.as_tuple(
         include_pk=False)
コード例 #4
0
    def save(self, activity):
        """
        Save an Activivty.

        For details see the corresponding method in ``hamster_lib.storage``.
        """
        if not isinstance(activity, lib_objects.Activity):
            message = _(
                "You need to pass a ``hamster_lib.objects.Activity`` instance")
            raise TypeError(message)

        dbus_activity = helpers.hamster_to_dbus_activity(activity)
        result = self._interface.Save(dbus_activity)
        return helpers.dbus_to_hamster_activity(result)
コード例 #5
0
    def get_by_composite(self, name, category):
        """
        Lookup for unique 'name/category.name'-composite key.

        For details see the corresponding method in ``hamster_lib.storage``.
        """
        if not (isinstance(category, lib_objects.Category) or
                (category is None)):
            message = _(
                "You need to pass a hamster_lib.objects.Category instance or None"
            )
            raise TypeError(message)

        dbus_category = helpers.hamster_to_dbus_category(category)
        name = text_type(name)
        result = self._interface.GetByComposite(name, dbus_category)
        return helpers.dbus_to_hamster_activity(result)
コード例 #6
0
ファイル: objects.py プロジェクト: landonb/hamster-dbus
    def Save(self, activity_tuple):
        """
        Save an activity.

        Args:
            activity_tuple (tuple): Tuple with activity values.

        Returns:
            tuple: Tuple representing the saved activity.
        """
        activity = helpers.dbus_to_hamster_activity(activity_tuple)
        result = self._controller.activities.save(activity)

        self._main_object.ActivityChanged()
        self._main_object.CategoryChanged()
        # This is because during activity creation/updating a new category may
        # be created as well.
        return helpers.hamster_to_dbus_activity(result)
コード例 #7
0
    def get_or_create(self, activity):
        """
        Convenience method to either get an activity matching the specs or create a new one.

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

        Args:
            activity (hamster_lib.Activity): The activity we want.

        Returns:
            hamster_lib.Activity: The retrieved or created activity

        Raises:
            TypeError: If ``activity`` is not a ``lib_objects.Activity`` instance.
        """
        if not isinstance(activity, lib_objects.Activity):
            message = _(
                "You need to pass a ``hamster_lib.objects.Activity`` instance")
            raise TypeError(message)

        dbus_activity = helpers.hamster_to_dbus_activity(activity)
        result = self._interface.GetOrCreate(dbus_activity)
        return helpers.dbus_to_hamster_activity(result)
コード例 #8
0
    def get_all(self, category=False, search_term=''):
        """
        Return all matching activities.

        For details see the corresponding method in ``hamster_lib.storage``.
        """
        if isinstance(category, lib_objects.Category):
            category = category.pk
        elif category is None:
            category = -1
        elif category is False:
            category = -2
        else:
            message = _(
                "'category' needs to be either a 'hamster_lib.objects.Category' instance,"
                " 'False' or 'None'.")
            raise TypeError(message)

        search_term = text_type(search_term)

        result = self._interface.GetAll(category, search_term)
        return [
            helpers.dbus_to_hamster_activity(activity) for activity in result
        ]
コード例 #9
0
def test_dbus_to_hamster_activity(activity_tuple, expectation):
    """Make sure that de-serialization works as intended."""
    result = helpers.dbus_to_hamster_activity(activity_tuple)
    assert result == expectation
    if expectation:
        assert isinstance(result, Activity)
コード例 #10
0
ファイル: conftest.py プロジェクト: landonb/hamster-dbus
 def factory(**kwargs):
     activity = activity_factory.build(**kwargs)
     result = activity_manager.Save(
         helpers.hamster_to_dbus_activity(activity))
     return helpers.dbus_to_hamster_activity(result)
コード例 #11
0
 def test_get(self, activity_manager, stored_activity):
     """Make sure instance is returned."""
     result = activity_manager.Get(stored_activity.pk)
     result = helpers.dbus_to_hamster_activity(result)
     assert result == stored_activity