Commit 1a52772e authored by Philip Withnall's avatar Philip Withnall Committed by Philip Withnall
Browse files

mock-backend: Add a mock backed implementation

This is an implementation of GeocodeBackend which returns results from
an in-memory map of parameters to result sets, for both forward and
reverse queries. That map is set up using API exposed by the
GeocodeMockBackend — so a unit test harness for an application or
library which uses geocode-glib can create a GeocodeMockBackend, set up
some expected queries in it, then test the application or library using
the mock GeocodeBackend instance.

This allows, for example, unit tests to be written which do not require
internet access, which is useful for many build and CI machines.
Secondly, it allows unit tests to reliably test the error paths in an
application or library’s code, as the map of queries can include error
values. This allows testing an application’s handling of network
timeouts, for example, which is otherwise tricky to set up (it requires
a network namespace and a mock server).

The GeocodeMockBackend is public API. Unit tests for it will be added in
a follow-up commit.

https://bugzilla.gnome.org/show_bug.cgi?id=774631
parent a05220e5
......@@ -21,6 +21,7 @@
<xi:include href="xml/geocode-error.xml"/>
<xi:include href="xml/geocode-forward.xml"/>
<xi:include href="xml/geocode-location.xml"/>
<xi:include href="xml/geocode-mock-backend.xml"/>
<xi:include href="xml/geocode-nominatim.xml"/>
<xi:include href="xml/geocode-place.xml"/>
<xi:include href="xml/geocode-reverse.xml"/>
......
......@@ -10,6 +10,7 @@ lib_LTLIBRARIES = libgeocode-glib.la
libgeocode_glib_la_PUBLICSOURCES = \
geocode-backend.c \
geocode-mock-backend.c \
geocode-nominatim.c \
geocode-location.c \
geocode-forward.c \
......@@ -36,6 +37,7 @@ libgeocode_glib_la_LDFLAGS = \
GCGLIB_HEADER_FILES = \
geocode-glib.h \
geocode-backend.h \
geocode-mock-backend.h \
geocode-nominatim.h \
geocode-location.h \
geocode-forward.h \
......
......@@ -34,5 +34,6 @@
#include <geocode-glib/geocode-enum-types.h>
#include <geocode-glib/geocode-backend.h>
#include <geocode-glib/geocode-nominatim.h>
#include <geocode-glib/geocode-mock-backend.h>
#endif /* GEOCODE_GLIB_H */
......@@ -91,6 +91,12 @@ geocode_backend_forward_search
geocode_backend_reverse_resolve_async
geocode_backend_reverse_resolve_finish
geocode_backend_reverse_resolve
geocode_mock_backend_add_forward_result
geocode_mock_backend_add_reverse_result
geocode_mock_backend_clear
geocode_mock_backend_get_query_log
geocode_mock_backend_get_type
geocode_mock_backend_new
geocode_nominatim_get_gnome
geocode_nominatim_get_type
geocode_nominatim_new
This diff is collapsed.
/*
* Copyright (C) 2016 Collabora Ltd.
*
* The geocode-glib library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* The geocode-glib 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Authors: Philip Withnall <philip.withnall@collabora.co.uk>
*/
#ifndef GEOCODE_MOCK_BACKEND_H
#define GEOCODE_MOCK_BACKEND_H
#include <glib.h>
#include <glib-object.h>
G_BEGIN_DECLS
/**
* GeocodeMockBackend:
*
* All the fields in the #GeocodeMockBackend structure are private and should
* never be accessed directly.
*
* Since: UNRELEASED
*/
#define GEOCODE_TYPE_MOCK_BACKEND (geocode_mock_backend_get_type ())
G_DECLARE_FINAL_TYPE (GeocodeMockBackend, geocode_mock_backend,
GEOCODE, MOCK_BACKEND, GObject)
GeocodeMockBackend *geocode_mock_backend_new (void);
void geocode_mock_backend_add_forward_result (GeocodeMockBackend *self,
GHashTable *params,
GList *results,
const GError *error);
void geocode_mock_backend_add_reverse_result (GeocodeMockBackend *self,
GHashTable *params,
GList *results,
const GError *error);
void geocode_mock_backend_clear (GeocodeMockBackend *self);
/**
* GeocodeMockBackendQuery:
* @params: query parameters, in the format accepted by geocode_forward_search()
* (if @is_forward is %TRUE) or geocode_reverse_resolve() (otherwise)
* @is_forward: %TRUE if this represents a call to geocode_forward_search();
* %FALSE if it represents a call to geocode_reverse_resolve()
* @results: (nullable) (element-type GeocodePlace): results returned by the
* query, or %NULL if an error was returned
* @error: (nullable): error returned by the query, or %NULL if a result set
* was returned
*
* The details of a forward or reverse query which was performed on a
* #GeocodeMockBackend by application code. This includes the input (@params,
* @is_forward), and the output which was returned (@results or @error).
*
* Empty result sets are represented by the %GEOCODE_ERROR_NO_MATCHES error
* (for forward queries) or the %GEOCODE_ERROR_NOT_SUPPORTED error (for reverse
* queries), rather than an empty @results list.
*
* Since: UNRELEASED
*/
typedef struct {
/* Request */
GHashTable *params;
gboolean is_forward;
/* Response */
GList *results;
GError *error;
} GeocodeMockBackendQuery;
GPtrArray *geocode_mock_backend_get_query_log (GeocodeMockBackend *self);
G_END_DECLS
#endif /* GEOCODE_MOCK_BACKEND_H */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment