[geocode-glib] Add testcase for our & freegeoip JSON formats
- From: Zeeshan Ali Khattak <zeeshanak src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [geocode-glib] Add testcase for our & freegeoip JSON formats
- Date: Wed, 3 Apr 2013 17:24:37 +0000 (UTC)
commit d2216cdc0bcd4f06111138958a44eb2b54e5c7fd
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date: Wed Mar 27 03:23:10 2013 +0200
Add testcase for our & freegeoip JSON formats
https://bugzilla.gnome.org/show_bug.cgi?id=696525
geocode-glib/geocode-ip-server/Makefile.am | 13 ++-
geocode-glib/geocode-ip-server/test-geoipformat.c | 142 +++++++++++++++++++++
2 files changed, 153 insertions(+), 2 deletions(-)
---
diff --git a/geocode-glib/geocode-ip-server/Makefile.am b/geocode-glib/geocode-ip-server/Makefile.am
index 628511a..83bb015 100644
--- a/geocode-glib/geocode-ip-server/Makefile.am
+++ b/geocode-glib/geocode-ip-server/Makefile.am
@@ -2,17 +2,26 @@ include $(top_srcdir)/Makefile.decl
BUILT_GIRSOURCES =
-AM_CFLAGS = -I$(top_srcdir) $(GEOCODE_SERVER_CFLAGS) $(COMMON_CFLAGS) $(WARN_CFLAGS) $(DISABLE_DEPRECATED)
-DGEOIP_DATABASE_PATH=\""$(GEOIP_DATABASE_PATH)"\"
+AM_CFLAGS = \
+ -I$(top_srcdir) \
+ $(GEOCODE_SERVER_CFLAGS) \
+ $(COMMON_CFLAGS) \
+ $(WARN_CFLAGS) \
+ $(DISABLE_DEPRECATED) \
+ -DGEOIP_DATABASE_PATH=\""$(GEOIP_DATABASE_PATH)"\" \
+ -DTEST_SRCDIR=\""$(top_srcdir)/geocode-glib/data/"\" \
+ -DBUILDDIR=\""$(builddir)/"\"
bin_PROGRAMS = geoip-update
-TEST_PROGS += geoip-lookup
+TEST_PROGS += geoip-lookup test-geoipformat
noinst_PROGRAMS = $(TEST_PROGS)
geoip_lookup_SOURCES = geoip-lookup.c geoip-server.h
geoip_lookup_LDADD = $(GEOCODE_SERVER_LIBS)
geoip_update_LDADD = $(GEOCODE_DBUPDATE_LIBS)
+test_geoipformat_LDADD = $(GEOCODE_LIBS)
MAINTAINERCLEANFILES = Makefile.in
diff --git a/geocode-glib/geocode-ip-server/test-geoipformat.c
b/geocode-glib/geocode-ip-server/test-geoipformat.c
new file mode 100644
index 0000000..faae705
--- /dev/null
+++ b/geocode-glib/geocode-ip-server/test-geoipformat.c
@@ -0,0 +1,142 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+
+#include <glib.h>
+#include <string.h>
+#include <json-glib/json-glib.h>
+
+static void
+test_response_data (gconstpointer data)
+{
+ JsonParser *parser;
+ JsonNode *node;
+ JsonObject *object;
+ double latitude, longitude;
+
+ parser = json_parser_new ();
+
+ g_assert (json_parser_load_from_data (parser, (const char *) data, -1, NULL));
+
+ node = json_parser_get_root (parser);
+ g_assert (JSON_NODE_HOLDS_OBJECT (node));
+ object = json_node_get_object (node);
+ g_assert (object != NULL);
+
+ g_assert (json_object_has_member (object, "ip"));
+ g_assert (strcmp (json_object_get_string_member (object, "ip"), "213.243.180.91") == 0);
+
+ g_assert (json_object_has_member (object, "latitude"));
+ latitude = json_object_get_double_member (object, "latitude");
+ g_assert (latitude > 60.1755 && latitude <= 60.1756);
+ g_assert (json_object_has_member (object, "longitude"));
+ longitude = json_object_get_double_member (object, "longitude");
+ g_assert (longitude >= 24.9342 && longitude < 24.9343);
+
+ g_assert (json_object_has_member (object, "city"));
+ g_assert (strcmp (json_object_get_string_member (object, "city"), "Helsinki") == 0);
+ g_assert (json_object_has_member (object, "region_name"));
+ g_assert (strcmp (json_object_get_string_member (object, "region_name"), "Southern Finland") == 0);
+ g_assert (json_object_has_member (object, "country_name"));
+ g_assert (strcmp (json_object_get_string_member (object, "country_name"), "Finland") == 0);
+
+ if (json_object_has_member (object, "accuracy"))
+ g_assert (strcmp (json_object_get_string_member (object, "accuracy"), "city") == 0);
+ if (json_object_has_member (object, "timezone"))
+ g_assert (strcmp (json_object_get_string_member (object, "timezone"), "Europe/Helsinki") == 0);
+
+ g_object_unref (parser);
+}
+
+static char *
+get_freegeoip_response (void)
+{
+ GFile *file;
+ char *contents = NULL;
+ char *path;
+ GError *error = NULL;
+
+ path = g_build_filename(TEST_SRCDIR, "freegeoip-results.json", NULL);
+ g_assert (path != NULL);
+ file = g_file_new_for_path(path);
+ if (!g_file_load_contents (file, NULL, &contents, NULL, NULL, &error)) {
+ g_warning ("Failed to load file '%s': %s", path, error->message);
+ g_error_free (error);
+ g_assert_not_reached ();
+ }
+
+ g_free (path);
+
+ return contents;
+}
+
+static char *
+get_our_server_response (void)
+{
+ char *response;
+ char *p;
+ char *standard_output;
+ int exit_status;
+ char **environ;
+ char *argv[] = { NULL, NULL };
+ GError *error = NULL;
+
+ environ = g_get_environ ();
+ environ = g_environ_setenv (environ, "QUERY_STRING", "ip=213.243.180.91", TRUE);
+
+ argv[0] = g_build_filename (BUILDDIR, "geoip-lookup", NULL);
+ g_assert (argv[0] != NULL);
+
+ if (!g_spawn_sync (BUILDDIR,
+ argv,
+ environ,
+ G_SPAWN_STDERR_TO_DEV_NULL,
+ NULL,
+ NULL,
+ &standard_output,
+ NULL,
+ &exit_status,
+ &error)) {
+ g_warning ("Failed to execute '%s': %s", argv[0], error->message);
+ g_error_free (error);
+ g_assert_not_reached ();
+ }
+
+ g_strfreev (environ);
+ g_free (argv[0]);
+
+ /* Get rid of headers */
+ p = strstr (standard_output, "\n\n");
+ g_assert (p != NULL);
+ p += 2;
+
+ response = g_strdup (p);
+ g_free (standard_output);
+
+ return response;
+}
+
+int
+main (int argc, char **argv)
+{
+ char *our_response, *freegeoip_response;
+ int ret;
+
+ g_type_init ();
+ g_test_init (&argc, &argv, NULL);
+ g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=");
+
+ our_response = get_our_server_response ();
+ g_test_add_data_func ("/geoip/geocode-glib-server-format",
+ our_response,
+ test_response_data);
+
+ freegeoip_response = get_freegeoip_response ();
+ g_test_add_data_func ("/geoip/geocode-freegeoip-format",
+ freegeoip_response,
+ test_response_data);
+
+ ret = g_test_run ();
+ g_free (freegeoip_response);
+ g_free (our_response);
+
+ return ret;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]