[grilo-plugins] tests: lua-factory check xml-parser



commit f67dbca7e5a721695128d210ad4f7dc3f7d4d89a
Author: Victor Toso <me victortoso com>
Date:   Sat Oct 3 08:26:36 2015 +0200

    tests: lua-factory check xml-parser
    
    Basic test for lua's xml-parser. This test runs a fake lua-source which
    download two mocked contents: a xml and its equivalent in lua's table.
    
    The fake test checks if the table provided by
    grl.lua.xml.string_to_table is correct.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=755447

 tests/lua-factory/Makefile.am                      |   23 +++-
 tests/lua-factory/data/config.ini                  |    6 +
 tests/lua-factory/data/test-source-xml-parser.lua  |  116 +++++++++++++++
 .../data/xml-parser-test-simple-table.lua          |   23 +++
 tests/lua-factory/data/xml-parser-test-simple.xml  |    1 +
 tests/lua-factory/test_lua_factory_xml_parser.c    |  147 ++++++++++++++++++++
 6 files changed, 311 insertions(+), 5 deletions(-)
---
diff --git a/tests/lua-factory/Makefile.am b/tests/lua-factory/Makefile.am
index cbca250..abb0c62 100644
--- a/tests/lua-factory/Makefile.am
+++ b/tests/lua-factory/Makefile.am
@@ -10,7 +10,8 @@ include $(top_srcdir)/gtester.mk
 SUBDIRS = sources
 
 TEST_PROGS +=  \
-       test_lua_factory_grl_media
+       test_lua_factory_grl_media      \
+       test_lua_factory_xml_parser
 
 test_lua_factory_defines =     \
        -DLUA_FACTORY_PLUGIN_PATH=\""$(abs_top_srcdir)/src/lua-factory/.libs/"\"        \
@@ -31,6 +32,17 @@ test_lua_factory_grl_media_CFLAGS =  \
        @JSON_CFLAGS@                   \
        $(test_lua_factory_defines)
 
+test_lua_factory_xml_parser_SOURCES =  \
+       test_lua_factory_xml_parser.c   \
+       luafactorytestsresources.h luafactorytestsresources.c
+
+test_lua_factory_xml_parser_LDADD =    \
+       @DEPS_LIBS@
+
+test_lua_factory_xml_parser_CFLAGS =   \
+       @DEPS_CFLAGS@                   \
+       $(test_lua_factory_defines)
+
 lua_factory_tests_resources_files =    \
   data/grl-media-test-all-metadata.json
 
@@ -49,10 +61,11 @@ CLEANFILES = \
        luafactorytestsresources.h      \
        luafactorytestsresources.c
 
-dist_noinst_DATA =                             \
-       data/config.ini                         \
-       data/test-source-grl-media.lua          \
-       data/grl-media-test-all-metadata.json
+dist_noinst_DATA =                             \
+       $(lua_factory_tests_resources_files)    \
+       data/config.ini                         \
+       data/test-source-grl-media.lua          \
+       data/test-source-xml-parser.lua
 
 noinst_PROGRAMS = $(TEST_PROGS)
 
diff --git a/tests/lua-factory/data/config.ini b/tests/lua-factory/data/config.ini
index 369c8ec..c093592 100644
--- a/tests/lua-factory/data/config.ini
+++ b/tests/lua-factory/data/config.ini
@@ -5,3 +5,9 @@ version=1
 [http://grl.media.test/lua-factory/all-metadata.json]
 data = grl-media-test-all-metadata.json
 
+# test_lua_factory_xml_parser
+[http://xml.parser.test/lua-factory/simple.xml]
+data = xml-parser-test-simple.xml
+[http://xml.parser.test/lua-factory/simple-table.lua]
+data = xml-parser-test-simple-table.lua
+
diff --git a/tests/lua-factory/data/test-source-xml-parser.lua 
b/tests/lua-factory/data/test-source-xml-parser.lua
new file mode 100644
index 0000000..58a90c6
--- /dev/null
+++ b/tests/lua-factory/data/test-source-xml-parser.lua
@@ -0,0 +1,116 @@
+--[[
+ * Copyright (C) 2015 Victor Toso.
+ *
+ * Contact: Victor Toso <me victortoso com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+--]]
+
+---------------------------
+-- Source initialization --
+---------------------------
+
+source = {
+  id = "test-source-xml-parser",
+  name = "Fake Source for XML Parser",
+  description = "a source to test XML parser function",
+  supported_keys = { "title" },
+  supported_media = "all",
+  resolve_keys = {
+    ["type"] = "all",
+    required = { "url" } ,
+  },
+  tags = { 'test', 'net:plaintext' },
+}
+
+---------------------------------
+-- Handlers of Grilo functions --
+---------------------------------
+
+function grl_source_resolve()
+  -- This source expects an url which will be fetched and converted
+  -- to a table using grl.lua.xml.string_to_table().
+  local req = grl.get_media_keys()
+  if not req or not req.url or #req.url ~= 2 then
+    grl.warning("resolve was called without metadata-key url")
+    grl.callback()
+    return
+  end
+
+  grl.fetch(req.url, "fetch_url_cb")
+end
+
+-- feeds[1] is the xml to test
+-- feeds[2] is a lua table with this xml, to compare
+function fetch_url_cb(feeds)
+  if not feeds or #feeds ~= 2 then
+    grl.warning("failed to load xml")
+    grl.callback()
+    return
+  end
+
+  local xml = grl.lua.xml.string_to_table(feeds[1])
+  local ref = load(feeds[2])()
+  if not xml or not ref then
+    grl.warning ("xml parser failed")
+    grl.callback()
+    return
+  end
+
+  if not test_table_contains(xml, ref) or
+     not test_table_contains(ref, xml) then
+    grl.warning("xml parser failed, results are not the same\n" ..
+                "reference table of test:\n" .. grl.lua.inspect(ref) .. "\n" ..
+                "table from xml parser:\n" .. grl.lua.inspect(xml))
+    grl.callback()
+    return
+  end
+
+  local media = { id = "success" }
+  grl.callback(media, 0)
+end
+
+function test_table_contains(t, e)
+  if type(t) ~= "table" or
+     type(e) ~= "table" then
+     return false
+  end
+
+  -- This is xml: keys are always strings
+  for key, value in pairs(t) do
+    if not e[key] then
+      grl.debug ("table does not have key: " .. key)
+      return false
+    end
+
+    if type(value) == "string" then
+      if t[key] ~= e[key] then
+        grl.debug ("values differ '" .. t[key] .. "' and '" .. e[key] .. "'")
+        return false
+      end
+    elseif type(value) == "table" then
+      if not test_table_contains(t[key], e[key]) or
+         not test_table_contains(e[key], t[key]) then
+         return false
+      end
+    else
+      grl.warning("test not handling type: " .. type(value))
+      return false
+    end
+  end
+  return true
+end
diff --git a/tests/lua-factory/data/xml-parser-test-simple-table.lua 
b/tests/lua-factory/data/xml-parser-test-simple-table.lua
new file mode 100644
index 0000000..b4b6c0e
--- /dev/null
+++ b/tests/lua-factory/data/xml-parser-test-simple-table.lua
@@ -0,0 +1,23 @@
+return {
+  document = {
+    article = {
+      [1] = {
+        p = {
+          xml = "This is the first paragraph."
+        },
+        h2 = {
+          xml = "Title with opt style",
+          class = "opt"
+        }
+      },
+      [2] = {
+        p = {
+          xml = "Some  text.",
+          b = {
+            xml = "important"
+          }
+        },
+      }
+    }
+  }
+}
diff --git a/tests/lua-factory/data/xml-parser-test-simple.xml 
b/tests/lua-factory/data/xml-parser-test-simple.xml
new file mode 100644
index 0000000..2f0e57a
--- /dev/null
+++ b/tests/lua-factory/data/xml-parser-test-simple.xml
@@ -0,0 +1 @@
+<document><article><p>This is the first paragraph.</p><h2 class='opt'>Title with opt 
style</h2></article><article><p>Some <b>important</b> text.</p></article></document>
diff --git a/tests/lua-factory/test_lua_factory_xml_parser.c b/tests/lua-factory/test_lua_factory_xml_parser.c
new file mode 100644
index 0000000..1af55ef
--- /dev/null
+++ b/tests/lua-factory/test_lua_factory_xml_parser.c
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2015. All rights reserved.
+ *
+ * Author: Victor Toso <me victortoso com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include <grilo.h>
+#include <string.h>
+
+#define TEST_URL_XML_PARSER "http://xml.parser.test/lua-factory/";
+
+#define LUA_FACTORY_ID "grl-lua-factory"
+#define FAKE_SOURCE_ID "test-source-xml-parser"
+
+static void
+test_lua_factory_setup (GrlConfig *config)
+{
+  GrlRegistry *registry;
+  GError *error = NULL;
+
+  registry = grl_registry_get_default ();
+
+  if (config != NULL) {
+    grl_registry_add_config (registry, config, &error);
+    g_assert_no_error (error);
+  }
+
+  grl_registry_load_plugin_by_id (registry, LUA_FACTORY_ID, &error);
+  g_assert_no_error (error);
+}
+
+static GrlSource*
+test_lua_factory_get_source (gchar *source_id,
+                             GrlSupportedOps source_ops)
+{
+  GrlRegistry *registry = grl_registry_get_default ();
+  GrlSource *source = grl_registry_lookup_source (registry, source_id);
+  g_assert_nonnull (source);
+  g_assert (grl_source_supported_operations (source) & source_ops);
+  return source;
+}
+
+static void
+test_lua_factory_shutdown (void)
+{
+  GrlRegistry *registry;
+  GError *error = NULL;
+
+  registry = grl_registry_get_default ();
+  grl_registry_unload_plugin (registry, LUA_FACTORY_ID, &error);
+  g_assert_no_error (error);
+}
+
+static GrlMedia *
+get_media (GrlSource *source,
+           GrlMedia *media)
+{
+  GrlOperationOptions *options;
+  GList *keys;
+
+  /* Keys can't be NULL, by default we ask for title */
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_TITLE, NULL);
+
+  options = grl_operation_options_new (NULL);
+  grl_operation_options_set_resolution_flags (options, GRL_RESOLVE_NORMAL);
+
+  grl_source_resolve_sync (source, media, keys, options, NULL);
+
+  g_list_free (keys);
+  g_object_unref (options);
+  return media;
+}
+
+static void
+resolve_fake_src (const gchar *xml_url,
+                  const gchar *lua_url)
+{
+  GrlSource *source = NULL;
+  GrlMedia *media = NULL;
+
+  source = test_lua_factory_get_source (FAKE_SOURCE_ID, GRL_OP_RESOLVE);
+  g_assert_nonnull (source);
+
+  media = grl_media_new ();
+  grl_data_add_string (GRL_DATA (media), GRL_METADATA_KEY_URL, xml_url);
+  grl_data_add_string (GRL_DATA (media), GRL_METADATA_KEY_URL, lua_url);
+  media = get_media (source, media);
+  g_assert_nonnull (media);
+  g_object_unref (media);
+}
+
+static void
+test_xml_parser (void)
+{
+  guint i;
+
+  struct {
+    gchar *xml_url;
+    gchar *lua_url;
+  } xml_tests[] = {
+    { TEST_URL_XML_PARSER "simple.xml",
+      TEST_URL_XML_PARSER "simple-table.lua" }
+  };
+
+  for (i = 0; i < G_N_ELEMENTS (xml_tests); i++) {
+    resolve_fake_src (xml_tests[i].xml_url, xml_tests[i].lua_url);
+  }
+}
+
+gint
+main (gint argc, gchar **argv)
+{
+  g_setenv ("GRL_PLUGIN_PATH", LUA_FACTORY_PLUGIN_PATH, TRUE);
+  g_setenv ("GRL_PLUGIN_LIST", LUA_FACTORY_ID, TRUE);
+  g_setenv ("GRL_NET_MOCKED", LUA_FACTORY_DATA_PATH "config.ini", TRUE);
+  g_setenv ("GRL_LUA_SOURCES_PATH", LUA_FACTORY_DATA_PATH, TRUE);
+
+  grl_init (&argc, &argv);
+  g_test_init (&argc, &argv, NULL);
+
+  test_lua_factory_setup (NULL);
+
+  g_test_add_func ("/lua-factory/lua-library/xml-parser", test_xml_parser);
+
+  gint result = g_test_run ();
+
+  test_lua_factory_shutdown ();
+  grl_deinit ();
+
+  return result;
+}


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