[tracker: 1/3] tracker-resource: add datetime helper




commit cc5f3fd9648768eb08cc8e2bdc6694296aec96c9
Author: nis130 <nishitlimbani130 gmail com>
Date:   Sun Mar 21 10:31:19 2021 +0530

    tracker-resource: add datetime helper
    
    Add tracker_resource_add_datetime, tracker_resource_set_datetime,
    tracker_resource_get_first_datetime and also includ tests for
    the above mentioned functions in tracker-resource-test.c

 src/libtracker-sparql/tracker-resource.c        | 33 +++++++++++++++++++++++++
 src/libtracker-sparql/tracker-resource.h        |  6 +++++
 tests/libtracker-sparql/tracker-resource-test.c | 15 +++++++++--
 3 files changed, 52 insertions(+), 2 deletions(-)
---
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
index 225b0ee62..2b22884b6 100644
--- a/src/libtracker-sparql/tracker-resource.c
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -443,6 +443,17 @@ SET_PROPERTY_FOR_GTYPE (tracker_resource_set_string, const char *, G_TYPE_STRING
  */
 SET_PROPERTY_FOR_GTYPE (tracker_resource_set_uri, const char *, TRACKER_TYPE_URI, g_value_set_string, 
validate_pointer);
 
+/**
+ * tracker_resource_set_datetime:
+ * @self: the #TrackerResource
+ * @property_uri: a string identifying the property to modify
+ * @value: the property object
+ *
+ * Sets a single-valued GDateTime as a #TrackerResource
+ * Since: 3.2
+ */
+SET_PROPERTY_FOR_GTYPE (tracker_resource_set_datetime, GDateTime *, G_TYPE_DATE_TIME, g_value_set_boxed, 
validate_pointer);
+
 /**
  * tracker_resource_add_gvalue:
  * @self: the #TrackerResource
@@ -646,6 +657,16 @@ ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_string, const char *, G_TYPE_STRING
  */
 ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_uri, const char *, TRACKER_TYPE_URI, g_value_set_string, 
validate_pointer);
 
+/**
+ * tracker_resource_add_datetime:
+ * @self: the #TrackerResource
+ * @property_uri: a string identifying the property to modify
+ * @value: the property object
+ *
+ * Adds GDateTime object to the multi-valued property.
+ * Since: 3.2
+ */
+ADD_PROPERTY_FOR_GTYPE (tracker_resource_add_datetime, GDateTime *, G_TYPE_DATE_TIME, g_value_set_boxed, 
validate_pointer);
 
 /**
  * tracker_resource_get_values:
@@ -800,6 +821,18 @@ GET_PROPERTY_FOR_GTYPE (tracker_resource_get_first_string, const char *, G_TYPE_
  */
 GET_PROPERTY_FOR_GTYPE (tracker_resource_get_first_uri, const char *, TRACKER_TYPE_URI, g_value_get_string, 
NULL);
 
+/**
+ * tracker_resource_get_first_datetime:
+ * @self: A #TrackerResource
+ * @property_uri: a string identifying the property to look up
+ *
+ * Returns the first resource object previously assigned to a property.
+ *
+ * Returns: the first GDateTime object
+ * Since: 3.2
+ */
+GET_PROPERTY_FOR_GTYPE (tracker_resource_get_first_datetime, GDateTime *, G_TYPE_DATE_TIME, 
g_value_get_boxed, NULL);
+
 /**
  * tracker_resource_get_identifier:
  * @self: A #TrackerResource
diff --git a/src/libtracker-sparql/tracker-resource.h b/src/libtracker-sparql/tracker-resource.h
index f975c757c..129685eb0 100644
--- a/src/libtracker-sparql/tracker-resource.h
+++ b/src/libtracker-sparql/tracker-resource.h
@@ -51,6 +51,8 @@ TRACKER_AVAILABLE_IN_ALL
 void tracker_resource_set_string (TrackerResource *self, const char *property_uri, const char *value);
 TRACKER_AVAILABLE_IN_ALL
 void tracker_resource_set_uri (TrackerResource *self, const char *property_uri, const char *value);
+TRACKER_AVAILABLE_IN_3_2
+void tracker_resource_set_datetime (TrackerResource *self, const char *property_uri, GDateTime *value);
 
 TRACKER_AVAILABLE_IN_ALL
 void tracker_resource_add_gvalue (TrackerResource *self, const char *property_uri, const GValue *value);
@@ -70,6 +72,8 @@ TRACKER_AVAILABLE_IN_ALL
 void tracker_resource_add_string (TrackerResource *self, const char *property_uri, const char *value);
 TRACKER_AVAILABLE_IN_ALL
 void tracker_resource_add_uri (TrackerResource *self, const char *property_uri, const char *value);
+TRACKER_AVAILABLE_IN_3_2
+void tracker_resource_add_datetime (TrackerResource *self, const char *property_uri, GDateTime *value);
 
 TRACKER_AVAILABLE_IN_ALL
 GList *tracker_resource_get_values (TrackerResource *self, const char *property_uri);
@@ -88,6 +92,8 @@ TRACKER_AVAILABLE_IN_ALL
 const char *tracker_resource_get_first_string (TrackerResource *self, const char *property_uri);
 TRACKER_AVAILABLE_IN_ALL
 const char *tracker_resource_get_first_uri (TrackerResource *self, const char *property_uri);
+TRACKER_AVAILABLE_IN_3_2
+GDateTime *tracker_resource_get_first_datetime (TrackerResource *self, const char *property_uri);
 
 TRACKER_AVAILABLE_IN_ALL
 const char *tracker_resource_get_identifier (TrackerResource *self);
diff --git a/tests/libtracker-sparql/tracker-resource-test.c b/tests/libtracker-sparql/tracker-resource-test.c
index 068b1c2fc..eb22fa927 100644
--- a/tests/libtracker-sparql/tracker-resource-test.c
+++ b/tests/libtracker-sparql/tracker-resource-test.c
@@ -37,6 +37,7 @@ test_resource_get_empty (void)
        g_assert_true (tracker_resource_get_first_int64 (resource, "http://example.com/0";) == 0);
        g_assert_true (tracker_resource_get_first_string (resource, "http://example.com/0";) == NULL);
        g_assert_true (tracker_resource_get_first_uri (resource, "http://example.com/0";) == NULL);
+       g_assert_true (tracker_resource_get_first_datetime (resource, "http://example.com/0";) == NULL);
 
        g_object_unref (resource);
 }
@@ -45,21 +46,26 @@ static void
 test_resource_get_set_simple (void)
 {
        TrackerResource *resource;
+       GDateTime *date_time;
 
        resource = tracker_resource_new ("http://example.com/resource";);
+       date_time = g_date_time_new_now_utc ();
 
        tracker_resource_set_double (resource, "http://example.com/1";, 0.6);
        tracker_resource_set_int (resource, "http://example.com/2";, 60);
        tracker_resource_set_int64 (resource, "http://example.com/3";, 123456789);
        tracker_resource_set_string (resource, "http://example.com/4";, "Hello");
        tracker_resource_set_uri (resource, "http://example.com/5";, "http://example.com/";);
+       tracker_resource_set_datetime (resource, "http://example.com/6";, date_time);
 
        g_assert_true (tracker_resource_get_first_double (resource, "http://example.com/1";) == 0.6);
        g_assert_cmpint (tracker_resource_get_first_int (resource, "http://example.com/2";), ==, 60);
        g_assert_true (tracker_resource_get_first_int64 (resource, "http://example.com/3";) == 123456789);
        g_assert_cmpstr (tracker_resource_get_first_string (resource, "http://example.com/4";), ==, "Hello");
        g_assert_cmpstr (tracker_resource_get_first_uri (resource, "http://example.com/5";), ==, 
"http://example.com/";);
+       g_assert_true (g_date_time_compare (tracker_resource_get_first_datetime (resource, 
"http://example.com/6";), date_time) == 0);
 
+       g_date_time_unref (date_time);
        g_object_unref (resource);
 }
 
@@ -101,28 +107,33 @@ test_resource_get_set_many (void)
        TrackerResource *resource;
        GValue value = G_VALUE_INIT;
        GList *list;
+       GDateTime *date_time;
 
        resource = tracker_resource_new ("http://example.com/resource";);
+       date_time = g_date_time_new_now_utc ();
 
        /* All the add_* functions except for add_gvalue are generated using the
         * same macro, so we only need to test one or two of them here.
         */
        tracker_resource_add_int (resource, "http://example.com/0";, 60);
        tracker_resource_add_string (resource, "http://example.com/0";, "Hello");
+       tracker_resource_add_datetime (resource, "http://example.com/0";, date_time);
 
        init_gvalue_with_random_type (&value);
        tracker_resource_add_gvalue (resource, "http://example.com/0";, &value);
        g_value_unset (&value);
 
        list = tracker_resource_get_values (resource, "http://example.com/0";);
-       g_assert_cmpint (g_list_length (list), ==, 3);
+       g_assert_cmpint (g_list_length (list), ==, 4);
 
        g_assert_cmpint (g_value_get_int (list->data), ==, 60);
        g_assert_cmpstr (g_value_get_string (list->next->data), ==, "Hello");
-       g_assert_true (G_VALUE_HOLDS (list->next->next->data, RANDOM_GVALUE_TYPE));
+       g_assert_cmpint (g_date_time_compare (g_value_get_boxed (list->next->next->data), date_time), ==, 0);
+       g_assert_true (G_VALUE_HOLDS (list->next->next->next->data, RANDOM_GVALUE_TYPE));
 
        g_list_free_full (list, (GDestroyNotify) g_value_unset);
 
+       g_date_time_unref (date_time);
        g_object_unref (resource);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]