[grilo-plugins/wip/jfelder/lua-grl-related-keys: 6/6] tests: Add a test for lua-factory GrlRelatedKeys support



commit 3fd0ebf3c048fbf03b48745da20273528b48226c
Author: Jean Felder <jfelder src gnome org>
Date:   Fri May 1 22:26:55 2020 +0200

    tests: Add a test for lua-factory GrlRelatedKeys support

 tests/lua-factory/data/config.ini                  |  2 +
 .../data/grl-media-test-related-keys.json          |  6 ++
 tests/lua-factory/test_lua_factory_grl_media.c     | 67 ++++++++++++++++++++--
 .../test_lua_factory_tests.gresource.xml           |  1 +
 4 files changed, 70 insertions(+), 6 deletions(-)
---
diff --git a/tests/lua-factory/data/config.ini b/tests/lua-factory/data/config.ini
index 1f60ea50..2f079a9f 100644
--- a/tests/lua-factory/data/config.ini
+++ b/tests/lua-factory/data/config.ini
@@ -4,6 +4,8 @@ version=1
 # test_lua_factory_grl_media
 [http://grl.media.test/lua-factory/all-metadata.json]
 data = grl-media-test-all-metadata.json
+[http://grl.media.test/lua-factory/related-keys.json]
+data = grl-media-test-related-keys.json
 
 # test_lua_factory_xml_parser
 [http://xml.parser.test/lua-factory/simple.xml]
diff --git a/tests/lua-factory/data/grl-media-test-related-keys.json 
b/tests/lua-factory/data/grl-media-test-related-keys.json
new file mode 100644
index 00000000..6fbefe58
--- /dev/null
+++ b/tests/lua-factory/data/grl-media-test-related-keys.json
@@ -0,0 +1,6 @@
+{
+    "title": "this is a song",
+    "1": {"artist": "https://";, "mb-artist-id": "240"},
+    "2": {"artist": "file:///", "mb-artist-id": "480"},
+    "mb-release-id": "f14638e1-fb36-358c-aba2-39b084864b13"
+}
diff --git a/tests/lua-factory/test_lua_factory_grl_media.c b/tests/lua-factory/test_lua_factory_grl_media.c
index 77244418..46d8411f 100644
--- a/tests/lua-factory/test_lua_factory_grl_media.c
+++ b/tests/lua-factory/test_lua_factory_grl_media.c
@@ -76,7 +76,8 @@ test_lua_factory_shutdown (void)
 static void
 check_metadata (GrlMedia *media,
                 GrlKeyID key_id,
-                JsonReader *reader)
+                JsonReader *reader,
+                gint64 index)
 {
   GrlRegistry *registry = grl_registry_get_default ();
   GType type = grl_registry_lookup_metadata_key_type (registry, key_id);
@@ -120,8 +121,14 @@ check_metadata (GrlMedia *media,
       g_assert_null (list);
 
     } else {
-      const gchar *from_json = json_reader_get_string_value (reader);
-      const gchar *from_media = grl_data_get_string (GRL_DATA (media), key_id);
+      GrlRelatedKeys *relkeys;
+      const gchar *from_media;
+      const gchar *from_json;
+
+      from_json = json_reader_get_string_value (reader);
+      relkeys = grl_data_get_related_keys (GRL_DATA (media), key_id, index);
+      g_assert (relkeys);
+      from_media = grl_related_keys_get_string (relkeys, key_id);
       g_assert_cmpstr (from_json, ==, from_media);
     }
   break;
@@ -185,13 +192,29 @@ test_metadata_from_media (GrlMedia *media,
     for (i = 0; i < len; i++) {
       GrlKeyID key_id = GRL_METADATA_KEY_INVALID;
       const gchar *key_name;
+      gint64 key_index;
 
       json_reader_read_element (reader, i);
       key_name = json_reader_get_member_name (reader);
-      if (g_strcmp0 (key_name, "type") != 0) {
+      key_index = g_ascii_strtoll (key_name, NULL, 0);
+      if (key_index) {
+        if (json_reader_is_object (reader)) {
+          guint index_member = 0;
+          guint num_members = json_reader_count_members (reader);
+
+          for (index_member = 0; index_member < num_members; index_member++) {
+            json_reader_read_element (reader, index_member);
+            key_name = json_reader_get_member_name (reader);
+            key_id = grl_registry_lookup_metadata_key (registry, key_name);
+            g_assert (key_id != GRL_METADATA_KEY_INVALID);
+            check_metadata (media, key_id, reader, key_index-1);
+            json_reader_end_element (reader);
+          }
+        }
+      } else if (g_strcmp0 (key_name, "type") != 0) {
         key_id = grl_registry_lookup_metadata_key (registry, key_name);
         g_assert (key_id != GRL_METADATA_KEY_INVALID);
-        check_metadata (media, key_id, reader);
+        check_metadata (media, key_id, reader, 0);
       }
       json_reader_end_element (reader);
     }
@@ -268,6 +291,35 @@ test_build_media (void)
   }
 }
 
+static void
+test_related_keys (void)
+{
+  gint i;
+
+  struct {
+    gchar *uri;
+    gchar *url;
+  } media_tests[] = {
+    /* This is a basic test to check all metadata keys provided by core in the
+     * simplest way possible. Boolean, strings, numbers, arrays. */
+    { GRESOURCE_PREFIX "grl-media-test-related-keys.json",
+      TEST_GRL_MEDIA_URL "related-keys.json" }
+  };
+
+  for (i = 0; i < G_N_ELEMENTS (media_tests); i++) {
+    GFile *file;
+    gchar *input;
+    GError *error = NULL;
+
+    file = g_file_new_for_uri (media_tests[i].uri);
+    g_file_load_contents (file, NULL, &input, NULL, NULL, &error);
+    g_assert_no_error (error);
+    g_object_unref (file);
+    resolve_fake_src (input, media_tests[i].url);
+    g_free (input);
+  }
+}
+
 gint
 main (gint argc, gchar **argv)
 {
@@ -285,9 +337,12 @@ main (gint argc, gchar **argv)
 
   /* Check if metadata-keys are created with value we expect with no errors */
   g_test_add_func ("/lua-factory/lua-library/metadata-keys", test_build_media);
+
+  /* test GrlRelatedKeys */
+  g_test_add_func ("/lua-factory/lua-library/related-keys", test_related_keys);
+
   /* TODO:
    * 1-) keys with array of all provided by grl_data_add_* (binary, boxed, float,..)
-   * 2-) test for GrlRelatedKeys after: https://bugzilla.gnome.org/show_bug.cgi?id=756203
    */
 
   gint result = g_test_run ();
diff --git a/tests/lua-factory/test_lua_factory_tests.gresource.xml 
b/tests/lua-factory/test_lua_factory_tests.gresource.xml
index f936db66..76e729d3 100644
--- a/tests/lua-factory/test_lua_factory_tests.gresource.xml
+++ b/tests/lua-factory/test_lua_factory_tests.gresource.xml
@@ -2,5 +2,6 @@
 <gresources>
   <gresource prefix="/org/gnome/grilo/plugins/test/lua-factory/">
     <file compressed="false">data/grl-media-test-all-metadata.json</file>
+    <file compressed="false">data/grl-media-test-related-keys.json</file>
   </gresource>
 </gresources>


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