From e14cbd054232c1dbaaab2b46267e005fd2d33c8e Mon Sep 17 00:00:00 2001 From: prabatuty Date: Sat, 17 Nov 2007 17:26:02 +0000 Subject: [PATCH] Adding tests for checking unique index creation --- test/dbapi/Index/Makefile | 43 +++++++++++++++++++++++++++++++ test/dbapi/Index/common.h | 56 +++++++++++++++++++++++++++++++++++++++++ test/dbapi/Index/uniqueindex1.c | 20 +++++++++++++++ test/dbapi/Index/uniqueindex2.c | 20 +++++++++++++++ test/dbapi/Index/uniqueindex3.c | 22 ++++++++++++++++ 5 files changed, 161 insertions(+) create mode 100644 test/dbapi/Index/Makefile create mode 100644 test/dbapi/Index/common.h create mode 100644 test/dbapi/Index/uniqueindex1.c create mode 100644 test/dbapi/Index/uniqueindex2.c create mode 100644 test/dbapi/Index/uniqueindex3.c diff --git a/test/dbapi/Index/Makefile b/test/dbapi/Index/Makefile new file mode 100644 index 00000000..c8178a47 --- /dev/null +++ b/test/dbapi/Index/Makefile @@ -0,0 +1,43 @@ + +OSNAME = $(shell uname -s) +PlatForm=supported + +ifeq ($(OSNAME), SunOS) +CPlus = CC +CPlusFlags = -O2 -w -mt -xarch=v8 +SYSLIBS=-ldl -lnsl -lsocket -lposix4 +else +ifeq ($(OSNAME), Linux) +CPlus = g++ +CPlusFlags = -g -w +SYSLIBS= -rdynamic -lrt -lpthread -lcrypt +else +PlatForm=notsupported +all: + echo "CSQL is not supported on $(OSNAME) platform" +endif +endif + +INCL= -I$(CSQL_INSTALL_ROOT)/include +LIBS= -L$(CSQL_INSTALL_ROOT)/lib -lcsql + +TARGETS = \ + test001 \ + test002 \ + test003 + + +ifeq ($(PlatForm), supported) +all: $(TARGETS) +endif + +test001: uniqueindex1.c + $(CPlus) $(CPlusFlags) -o $@ $< $(INCL) $(LIBS) $(SYSLIBS) +test002: uniqueindex2.c + $(CPlus) $(CPlusFlags) -o $@ $< $(INCL) $(LIBS) $(SYSLIBS) +test003: uniqueindex3.c + $(CPlus) $(CPlusFlags) -o $@ $< $(INCL) $(LIBS) $(SYSLIBS) + +clean: + rm -f $(TARGETS) + diff --git a/test/dbapi/Index/common.h b/test/dbapi/Index/common.h new file mode 100644 index 00000000..52a3d49c --- /dev/null +++ b/test/dbapi/Index/common.h @@ -0,0 +1,56 @@ +#include +int createIndex(DatabaseManager *dbMgr, bool unique) +{ + //Creating hash index on field f1 of table t1 + HashIndexInitInfo *idxInfo = new HashIndexInitInfo(); + strcpy(idxInfo->tableName, "t1"); + idxInfo->list.append("f1"); + idxInfo->indType = hashIndex; +#ifndef DEFAULT + idxInfo->isUnique = unique; +#endif + DbRetVal rv = dbMgr->createIndex("indx1", idxInfo); + if (rv != OK) { printf("Index creation failed\n"); return 1; } + printf("Index created\n"); + return 0; +} +int createTable(DatabaseManager *dbMgr) +{ + TableDef tabDef; + tabDef.addField("f1", typeInt, 0, NULL, true, true); + tabDef.addField("f2", typeInt); + tabDef.addField("f3", typeString, 20); + DbRetVal rv = dbMgr->createTable("t1", tabDef); + if (rv != OK) { printf("Table creation failed\n"); return 1; } + printf("Table created\n"); + return 0; +} +int insertTupleWithSameValue(DatabaseManager *dbMgr, Connection &conn) +{ + Table *table = dbMgr->openTable("t1"); + if (table == NULL) + { + printf("Unable to open table\n"); + return 0; + } + int id1 = 0, id2 = 5; + char name[20] = "PRAVEEN"; + table->bindFld("f1", &id1); + table->bindFld("f2", &id2); + table->bindFld("f3", name); + int icount =0; + DbRetVal rv = OK; + for (int i = 0 ; i < 10 ; i++) + { + conn.startTransaction(); + id1= 10; + rv = table->insertTuple(); + if (rv != OK) break; + icount++; + conn.commit(); + + } + printf("Total Tuples inserted is %d\n", icount); + dbMgr->closeTable(table); + return icount; +} diff --git a/test/dbapi/Index/uniqueindex1.c b/test/dbapi/Index/uniqueindex1.c new file mode 100644 index 00000000..36e9f984 --- /dev/null +++ b/test/dbapi/Index/uniqueindex1.c @@ -0,0 +1,20 @@ +#include +#include "common.h" +int main() +{ + Connection conn; + DbRetVal rv = conn.open("praba", "manager"); + if (rv != OK) return 1; + DatabaseManager *dbMgr = conn.getDatabaseManager(); + if (dbMgr == NULL) { printf("Auth failed\n"); return 2;} + + if ( createTable(dbMgr) != 0 ) { conn.close(); return 3; } + if ( createIndex(dbMgr, true) != 0 ) { dbMgr->dropTable("t1");conn.close(); return 4; + } + int inscount = insertTupleWithSameValue(dbMgr, conn); + if (inscount != 1 ) { dbMgr->dropTable("t1"); conn.close(); return 5; } + dbMgr->dropTable("t1"); + conn.close(); + return 0; + +} diff --git a/test/dbapi/Index/uniqueindex2.c b/test/dbapi/Index/uniqueindex2.c new file mode 100644 index 00000000..d9efd9c4 --- /dev/null +++ b/test/dbapi/Index/uniqueindex2.c @@ -0,0 +1,20 @@ +#include +#include "common.h" +int main() +{ + Connection conn; + DbRetVal rv = conn.open("praba", "manager"); + if (rv != OK) return 1; + DatabaseManager *dbMgr = conn.getDatabaseManager(); + if (dbMgr == NULL) { printf("Auth failed\n"); return 2;} + + if ( createTable(dbMgr) != 0 ) { conn.close(); return 3; } + if ( createIndex(dbMgr, false) != 0 ) { dbMgr->dropTable("t1");conn.close(); return 4; + } + int inscount = insertTupleWithSameValue(dbMgr, conn); + if (inscount != 10 ) { dbMgr->dropTable("t1"); conn.close(); return 5; } + dbMgr->dropTable("t1"); + conn.close(); + return 0; + +} diff --git a/test/dbapi/Index/uniqueindex3.c b/test/dbapi/Index/uniqueindex3.c new file mode 100644 index 00000000..d458a219 --- /dev/null +++ b/test/dbapi/Index/uniqueindex3.c @@ -0,0 +1,22 @@ +#include +#define DEFAULT +#include "common.h" +//test with default value for isUnique of IndexInitInfo +int main() +{ + Connection conn; + DbRetVal rv = conn.open("praba", "manager"); + if (rv != OK) return 1; + DatabaseManager *dbMgr = conn.getDatabaseManager(); + if (dbMgr == NULL) { printf("Auth failed\n"); return 2;} + + if ( createTable(dbMgr) != 0 ) { conn.close(); return 3; } + if ( createIndex(dbMgr, false) != 0 ) { dbMgr->dropTable("t1");conn.close(); return 4; + } + int inscount = insertTupleWithSameValue(dbMgr, conn); + if (inscount != 1 ) { dbMgr->dropTable("t1"); conn.close(); return 5; } + dbMgr->dropTable("t1"); + conn.close(); + return 0; + +} -- 2.11.4.GIT