FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testDBLoadMime.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 
18 /* cunit includes */
19 #include <CUnit/CUnit.h>
20 #include "finder.h"
21 #include <string.h>
22 
28 extern void DBLoadMime();
29 extern char *DBConfFile;
30 
35 {
36  char *ErrorBuf;
37 
38  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
39  if (!pgConn)
40  {
41  LOG_FATAL("Unable to connect to database");
42  exit(-1);
43  }
44  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
45  DBMime = NULL;
46 
47  return 0;
48 }
53 {
54  if (pgConn) PQfinish(pgConn);
55  DBMime = NULL;
56  return 0;
57 }
58 
59 /* test functions */
60 
68 {
69  char SQL[MAXCMD] = {0};
70  PGresult *result = NULL;
71  char mimetype_name[] = "application/octet-stream";
72  /* delete the record mimetype_name is application/octet-stream in mimetype */
73  memset(SQL, '\0', MAXCMD);
74  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
75  result = PQexec(pgConn, SQL);
76  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
77  {
78  PQfinish(pgConn);
79  exit(-1);
80  }
81  PQclear(result);
82  memset(SQL, '\0', MAXCMD);
83  snprintf(SQL, MAXCMD, "INSERT INTO mimetype (mimetype_name) VALUES ('%s');", mimetype_name);
84  result = PQexec(pgConn, SQL);
85  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
86  {
87  PQfinish(pgConn);
88  exit(-1);
89  }
90  PQclear(result);
91  MaxDBMime = 0;
92  /* exectue the tested function */
93  DBLoadMime();
94  /* select the record mimetype_name is application/octet-stream */
95  memset(SQL, '\0', MAXCMD);
96  snprintf(SQL, MAXCMD, "SELECT mimetype_name from mimetype where mimetype_name = ('%s');", mimetype_name);
97  result = PQexec(pgConn, SQL);
98  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
99  {
100  PQfinish(pgConn);
101  exit(-1);
102  }
103  int count = PQntuples(result);
104  PQclear(result);
105 
106  CU_ASSERT_EQUAL(MaxDBMime, count);
107  /* delete the record mimetype_name is application/octet-stream in mimetype */
108  memset(SQL, '\0', MAXCMD);
109  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
110  result = PQexec(pgConn, SQL);
111  /* reset the evn, that is clear all data in mimetype */
112  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
113  {
114  PQfinish(pgConn);
115  exit(-1);
116  }
117  MaxDBMime = 0;
118  PQclear(result);
119 }
120 
124 CU_TestInfo testcases_DBLoadMime[] =
125 {
126 #if 0
127 #endif
128 {"DBLoadMime:InsertOctet", testDBLoadMime},
129  CU_TEST_INFO_NULL
130 };
131 
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:181
PGresult * DBMime
contents of mimetype table
Definition: finder.c:27
int DBLoadMimeInit()
initialize DB
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
void DBLoadMime()
Populate the DBMime table.
Definition: finder.c:69
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
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:90
CU_TestInfo testcases_DBLoadMime[]
testcases for function DBLoadGold
int MaxDBMime
how many rows in DBMime
Definition: finder.c:28
void testDBLoadMime()
for function DBLoadMime()
magic_t MagicCookie
for Magic
Definition: finder.c:34
int DBLoadMimeClean()
clean the env
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:40