Add a test for the "sqlite3" engine
This commit is contained in:
parent
799a5298e6
commit
957cc15b74
2
tests/.gitignore
vendored
2
tests/.gitignore
vendored
|
@ -1,2 +1,4 @@
|
|||
/pylint.log
|
||||
/sqlite3
|
||||
/sqlite3.db
|
||||
/tests.log
|
||||
|
|
110
tests/database.sh
Executable file
110
tests/database.sh
Executable file
|
@ -0,0 +1,110 @@
|
|||
#!/bin/sh
|
||||
#$Id$
|
||||
#Copyright (c) 2016 Pierre Pronchery <khorben@defora.org>
|
||||
#This program is free software: you can redistribute it and/or modify
|
||||
#it under the terms of the GNU General Public License as published by
|
||||
#the Free Software Foundation, version 3 of the License.
|
||||
#
|
||||
#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 General Public License for more details.
|
||||
#
|
||||
#You should have received a copy of the GNU General Public License
|
||||
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
|
||||
#variables
|
||||
DEBUG="_debug"
|
||||
DEVNULL="/dev/null"
|
||||
PROGNAME="database.sh"
|
||||
#executables
|
||||
RM="rm -f"
|
||||
SQLITE2="sqlite"
|
||||
SQLITE3="sqlite3"
|
||||
|
||||
|
||||
#functions
|
||||
#debug
|
||||
_debug()
|
||||
{
|
||||
echo "$@" 1>&2
|
||||
"$@"
|
||||
ret=$?
|
||||
#ignore errors when the command is not available
|
||||
[ $ret -eq 127 ] && return 0
|
||||
return $ret
|
||||
}
|
||||
|
||||
|
||||
#error
|
||||
_error()
|
||||
{
|
||||
echo "$PROGNAME: $@" 1>&2
|
||||
return 2
|
||||
}
|
||||
|
||||
|
||||
#usage
|
||||
_usage()
|
||||
{
|
||||
echo "Usage: $PROGNAME [-P prefix] target" 1>&2
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#main
|
||||
clean=0
|
||||
while getopts "cP:" name; do
|
||||
case "$name" in
|
||||
c)
|
||||
clean=1
|
||||
;;
|
||||
P)
|
||||
#XXX ignored for compatibility
|
||||
;;
|
||||
?)
|
||||
_usage
|
||||
exit $?
|
||||
;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND - 1))
|
||||
if [ $# -ne 1 ]; then
|
||||
_usage
|
||||
exit $?
|
||||
fi
|
||||
target="$1"
|
||||
database="${target#$OBJDIR}"
|
||||
|
||||
[ "$clean" -ne 0 ] && exit 0
|
||||
|
||||
case "$database" in
|
||||
sqlite2.db)
|
||||
$RM -- "$target" || exit 2
|
||||
#XXX run twice to ensure there are no false positives
|
||||
echo .read "sqlite2.sql" | $DEBUG $SQLITE2 "$target" > "$DEVNULL" 2>&1
|
||||
echo .read "sqlite2.sql" | $DEBUG $SQLITE2 "$target"
|
||||
res=$?
|
||||
if [ $res -ne 0 ]; then
|
||||
_error "$target: Error $res"
|
||||
exit $?
|
||||
fi
|
||||
;;
|
||||
sqlite3.db)
|
||||
$RM -- "$target" || exit 2
|
||||
#XXX run twice to ensure there are no false positives
|
||||
echo .read "sqlite3.sql" | $DEBUG $SQLITE3 "$target" > "$DEVNULL" 2>&1
|
||||
echo .read "sqlite3.sql" | $DEBUG $SQLITE3 "$target"
|
||||
res=$?
|
||||
if [ $res -ne 0 ]; then
|
||||
_error "$target: Error $res"
|
||||
exit $?
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
_error "$database: Unknown database target"
|
||||
exit 2
|
||||
;;
|
||||
esac
|
|
@ -1,5 +1,20 @@
|
|||
targets=pylint.log,tests.log
|
||||
dist=Makefile,pylint.sh,python.sh,tests.sh
|
||||
targets=pylint.log,sqlite3,sqlite3.db,tests.log
|
||||
cppflags_force=-I ../include
|
||||
cflags_force=`pkg-config --cflags libSystem`
|
||||
cflags=-W -Wall -g -O2 -pedantic -fPIE -D_FORTIFY_SOURCE=2 -fstack-protector-all
|
||||
ldflags_force=`pkg-config --libs libSystem` -L $(OBJDIR)../src -lDatabase
|
||||
ldflags=-pie
|
||||
dist=Makefile,database.sh,pylint.sh,python.sh,sqlite3.sql,tests.sh
|
||||
|
||||
[sqlite3]
|
||||
type=binary
|
||||
sources=sqlite3.c
|
||||
depends=$(OBJDIR)sqlite3.db
|
||||
|
||||
[sqlite3.db]
|
||||
type=script
|
||||
script=./database.sh
|
||||
depends=sqlite3.sql
|
||||
|
||||
[pylint.log]
|
||||
type=script
|
||||
|
@ -9,4 +24,4 @@ depends=pylint.sh
|
|||
[tests.log]
|
||||
type=script
|
||||
script=./tests.sh
|
||||
depends=python.sh,tests.sh
|
||||
depends=python.sh,$(OBJDIR)sqlite3$(SOEXT),tests.sh
|
||||
|
|
95
tests/sqlite3.c
Normal file
95
tests/sqlite3.c
Normal file
|
@ -0,0 +1,95 @@
|
|||
/* $Id$ */
|
||||
/* Copyright (c) 2016 Pierre Pronchery <khorben@defora.org> */
|
||||
/* This file is part of DeforaOS Database libDatabase */
|
||||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <System.h>
|
||||
#include "Database.h"
|
||||
|
||||
#ifndef PROGNAME
|
||||
# define PROGNAME "sqlite3"
|
||||
#endif
|
||||
|
||||
|
||||
/* sqlite3 */
|
||||
/* private */
|
||||
/* prototypes */
|
||||
static int _sqlite3(void);
|
||||
|
||||
static int _usage(void);
|
||||
|
||||
|
||||
/* functions */
|
||||
/* sqlite3 */
|
||||
static int _sqlite3(void)
|
||||
{
|
||||
Config * config;
|
||||
Database * db;
|
||||
String * filename;
|
||||
char const * p;
|
||||
|
||||
if((config = config_new()) == NULL)
|
||||
{
|
||||
error_print(PROGNAME);
|
||||
return -1;
|
||||
}
|
||||
if((p = getenv("OBJDIR")) != NULL)
|
||||
filename = string_new_append(p, "/sqlite3.db", NULL);
|
||||
else
|
||||
filename = string_new("sqlite3.db");
|
||||
if(filename == NULL
|
||||
|| config_set(config, NULL, "filename", filename) != 0)
|
||||
{
|
||||
error_print(PROGNAME);
|
||||
string_delete(filename);
|
||||
config_delete(config);
|
||||
return -1;
|
||||
}
|
||||
string_delete(filename);
|
||||
db = database_new("sqlite3", config, NULL);
|
||||
config_delete(config);
|
||||
if(db == NULL)
|
||||
return 2;
|
||||
if(database_query(db, "SELECT * FROM \"table\"", NULL, NULL) != 0)
|
||||
{
|
||||
error_print(PROGNAME);
|
||||
database_delete(db);
|
||||
return 2;
|
||||
}
|
||||
database_delete(db);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* usage */
|
||||
static int _usage(void)
|
||||
{
|
||||
fputs("Usage: " PROGNAME "\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* public */
|
||||
/* functions */
|
||||
/* main */
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
if(argc != 1)
|
||||
return _usage();
|
||||
return (_sqlite3() == 0) ? 0 : 2;
|
||||
}
|
21
tests/sqlite3.sql
Normal file
21
tests/sqlite3.sql
Normal file
|
@ -0,0 +1,21 @@
|
|||
-- $Id$
|
||||
-- Copyright (c) 2016 Pierre Pronchery <khorben@defora.org>
|
||||
-- This file is part of DeforaOS Database libDatabase
|
||||
-- This program is free software: you can redistribute it and/or modify
|
||||
-- it under the terms of the GNU General Public License as published by
|
||||
-- the Free Software Foundation, version 3 of the License.
|
||||
--
|
||||
-- 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 General Public License for more details.
|
||||
--
|
||||
-- You should have received a copy of the GNU General Public License
|
||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
DROP TABLE "table";
|
||||
|
||||
CREATE TABLE "table" (
|
||||
table_id INTEGER PRIMARY KEY
|
||||
);
|
|
@ -100,6 +100,7 @@ target="$1"
|
|||
$DATE > "$target"
|
||||
FAILED=
|
||||
echo "Performing tests:" 1>&2
|
||||
_test "sqlite3"
|
||||
echo "Expected failures:" 1>&2
|
||||
_fail "python.sh"
|
||||
if [ -n "$FAILED" ]; then
|
||||
|
|
Loading…
Reference in New Issue
Block a user