FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_libfossdb.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
17 
23 /* includes for files that will be tested */
24 #include <libfossdb.h>
25 
26 /* library includes */
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 
31 /* cunit includes */
32 #include <CUnit/CUnit.h>
33 
34 #ifndef COMMIT_HASH
35 #define COMMIT_HASH "COMMIT_HASH Unknown"
36 #endif
37 
38 extern char* dbConf;
39 
49 {
50  PGconn* pgConn;
51  int nonexistant_table;
52  int existing_table;
53  char* DBConfFile = dbConf;
54  char* ErrorBuf;
55 
56  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
57 
58  CU_ASSERT_PTR_NOT_NULL(pgConn);
59 
60  nonexistant_table = fo_tableExists(pgConn, "nonexistanttable");
61  CU_ASSERT_FALSE(nonexistant_table);
62 
63  PGresult* result = PQexec(pgConn, "CREATE table exists()");
64  CU_ASSERT_PTR_NOT_NULL_FATAL(result);
65  CU_ASSERT_FALSE_FATAL(fo_checkPQcommand(pgConn, result, "create", __FILE__, __LINE__));
66 
67  existing_table = fo_tableExists(pgConn, "exists");
68  CU_ASSERT_TRUE(existing_table);
69 
70  PQfinish(pgConn);
71  return;
72 }
73 
74 
75 /* ************************************************************************** */
76 /* *** cunit test info ****************************************************** */
77 /* ************************************************************************** */
78 CU_TestInfo libfossdb_testcases[] =
79  {
80  {"fo_tableExists()", test_fo_tableExists},
81  CU_TEST_INFO_NULL
82  };
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
void test_fo_tableExists()
fo_tableExists() tests
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
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:215
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is &#39;fossology&#39;.
Definition: libfossdb.c:243
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:40