/*
 * Seagull: A SQLite helper library
 * Copyright (C) Seagull Developers
 *
 * Seagull is the legal property of its developers, whose names are too
 * numerous to list here. Please refer to the AUTHORS file distributed with
 * this source distribution
 *
 * This program 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; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This program 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, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <seagull.h>

#define RESOURCE_ROOT "/org/imfreedom/keep/seagull/seagull/tests/statement-resources/"

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_statement_new_from_resource_empty(void) {
	SeagullSqlite3 *db = NULL;
	SeagullStatement *stmt = NULL;
	GError *error = NULL;
	const char *sql = NULL;
	gboolean result = FALSE;

	db = seagull_db_new_in_memory(&error);
	g_assert_no_error(error);
	g_assert_nonnull(db);

	stmt = seagull_statement_new_from_resource(db, RESOURCE_ROOT "empty.sql",
	                                           &error);
	g_assert_no_error(error);
	g_assert_true(SEAGULL_IS_STATEMENT(stmt));

	sql = seagull_statement_get_sql(stmt);
	g_assert_cmpstr(sql, ==, "");

	g_assert_finalize_object(stmt);

	result = seagull_db_close(db, &error);
	g_assert_no_error(error);
	g_assert_true(result);
}

static void
test_statement_new_from_resource_basic(void) {
	SeagullSqlite3 *db = NULL;
	SeagullStatement *stmt = NULL;
	GError *error = NULL;
	const char *sql = NULL;
	gboolean result = FALSE;

	db = seagull_db_new_in_memory(&error);
	g_assert_no_error(error);
	g_assert_nonnull(db);

	stmt = seagull_statement_new_from_resource(db, RESOURCE_ROOT "basic.sql",
	                                           &error);
	g_assert_no_error(error);
	g_assert_true(SEAGULL_IS_STATEMENT(stmt));

	sql = seagull_statement_get_sql(stmt);
	g_assert_cmpstr(sql, ==, "SELECT 1;");

	g_assert_finalize_object(stmt);

	result = seagull_db_close(db, &error);
	g_assert_no_error(error);
	g_assert_true(result);
}

static void
test_statement_new_from_resource_error(void) {
	SeagullSqlite3 *db = NULL;
	SeagullStatement *stmt = NULL;
	GError *error = NULL;
	gboolean result = FALSE;

	db = seagull_db_new_in_memory(&error);
	g_assert_no_error(error);
	g_assert_nonnull(db);

	stmt = seagull_statement_new_from_resource(db, RESOURCE_ROOT "error.sql",
	                                           &error);
	g_assert_error(error,
	               SEAGULL_STATEMENT_ERROR,
	               SEAGULL_STATEMENT_ERROR_PREPARATION_FAILED);
	g_clear_error(&error);
	g_assert_null(stmt);

	result = seagull_db_close(db, &error);
	g_assert_no_error(error);
	g_assert_true(result);
}

static void
test_statement_new_from_resource_not_found(void) {
	SeagullSqlite3 *db = NULL;
	SeagullStatement *stmt = NULL;
	GError *error = NULL;
	gboolean result = FALSE;

	db = seagull_db_new_in_memory(&error);
	g_assert_no_error(error);
	g_assert_nonnull(db);

	stmt = seagull_statement_new_from_resource(db,
	                                           RESOURCE_ROOT "not-found.sql",
	                                           &error);
	g_assert_error(error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
	g_clear_error(&error);
	g_assert_null(stmt);

	result = seagull_db_close(db, &error);
	g_assert_no_error(error);
	g_assert_true(result);
}

/******************************************************************************
 * Main
 *****************************************************************************/
int
main(int argc, char *argv[]) {
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/statement/new-from-resource/empty",
	                test_statement_new_from_resource_empty);
	g_test_add_func("/statement/new-from-resource/basic",
	                test_statement_new_from_resource_basic);
	g_test_add_func("/statement/new-from-resource/error",
	                test_statement_new_from_resource_error);
	g_test_add_func("/statement/new-from-resource/not-found",
	                test_statement_new_from_resource_not_found);

	return g_test_run();
}
