FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_libfossdb.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
12 /* includes for files that will be tested */
13 #include <libfossdb.h>
14 
15 /* library includes */
16 #include <string.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 
20 /* cunit includes */
21 #include <CUnit/CUnit.h>
22 
23 #ifndef COMMIT_HASH
24 #define COMMIT_HASH "COMMIT_HASH Unknown"
25 #endif
26 
27 extern char* dbConf;
28 
38 {
39  PGconn* pgConn;
40  int nonexistant_table;
41  int existing_table;
42  char* DBConfFile = dbConf;
43  char* ErrorBuf;
44 
45  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
46 
47  CU_ASSERT_PTR_NOT_NULL(pgConn);
48 
49  nonexistant_table = fo_tableExists(pgConn, "nonexistanttable");
50  CU_ASSERT_FALSE(nonexistant_table);
51 
52  PGresult* result = PQexec(pgConn, "CREATE table exists()");
53  CU_ASSERT_PTR_NOT_NULL_FATAL(result);
54  CU_ASSERT_FALSE_FATAL(fo_checkPQcommand(pgConn, result, "create", __FILE__, __LINE__));
55 
56  existing_table = fo_tableExists(pgConn, "exists");
57  CU_ASSERT_TRUE(existing_table);
58 
59  PQfinish(pgConn);
60  return;
61 }
62 
63 
64 /* ************************************************************************** */
65 /* *** cunit test info ****************************************************** */
66 /* ************************************************************************** */
67 CU_TestInfo libfossdb_testcases[] =
68  {
69  {"fo_tableExists()", test_fo_tableExists},
70  CU_TEST_INFO_NULL
71  };
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:29
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
Definition: libfossdb.c:232
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
Definition: libfossdb.c:204
char * DBConfFile
DB conf file location.
Definition: testRun.c:21
void test_fo_tableExists()
fo_tableExists() tests