FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testDBFindMime.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 char *DBConfFile;
29 
34 {
35  char *ErrorBuf;
36 
37  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
38  if (!pgConn)
39  {
40  LOG_FATAL("Unable to connect to database");
41  exit(-1);
42  }
43  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
44  DBMime = NULL;
45 
46  return 0;
47 }
52 {
53  if (pgConn) PQfinish(pgConn);
54  DBMime = NULL;
55  return 0;
56 }
57 
58 /* test functions */
59 
70 {
71  char SQL[MAXCMD] = {0};
72  PGresult *result = NULL;
73  char mimetype_name[] = "application/octet-stream";
74  /* delete the record mimetype_name is application/octet-stream in mimetype */
75  memset(SQL, '\0', MAXCMD);
76  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
77  result = PQexec(pgConn, SQL);
78  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
79  {
80  PQfinish(pgConn);
81  exit(-1);
82  }
83  PQclear(result);
84  /* insert one record */
85  memset(SQL, '\0', MAXCMD);
86  snprintf(SQL, MAXCMD, "INSERT INTO mimetype (mimetype_name) VALUES ('%s');", mimetype_name);
87  result = PQexec(pgConn, SQL);
88  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
89  {
90  PQfinish(pgConn);
91  exit(-1);
92  }
93  PQclear(result);
94  /* exectue the tested function */
95  /* 1. the Mimetype is already in table mimetype */
96  int ret = DBFindMime(mimetype_name);
97  /* select the record mimetype_name is application/octet-stream */
98  memset(SQL, '\0', MAXCMD);
99  snprintf(SQL, MAXCMD, "SELECT mimetype_name from mimetype where mimetype_name = ('%s');", mimetype_name);
100  result = PQexec(pgConn, SQL);
101  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
102  {
103  PQfinish(pgConn);
104  exit(-1);
105  }
106  int mimetype_id = atoi(PQgetvalue(result, 0, 0));
107  PQclear(result);
108 
109  CU_ASSERT_NOT_EQUAL(ret, mimetype_id);
110 
111  /* delete the record mimetype_name is application/octet-stream in mimetype */
112  memset(SQL, '\0', MAXCMD);
113  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
114  result = PQexec(pgConn, SQL);
115  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
116  {
117  PQfinish(pgConn);
118  exit(-1);
119  }
120  PQclear(result);
121 
122  DBMime = NULL;
123  /* 2. the Mimetype is not in table mimetype */
124  /* select the record mimetype_name is application/octet-stream */
125  ret = DBFindMime(mimetype_name);
126  memset(SQL, '\0', MAXCMD);
127  snprintf(SQL, MAXCMD, "SELECT mimetype_name from mimetype where mimetype_name = ('%s');", mimetype_name);
128  result = PQexec(pgConn, SQL);
129  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
130  {
131  PQfinish(pgConn);
132  exit(-1);
133  }
134  mimetype_id = 0;
135  mimetype_id = atoi(PQgetvalue(result, 0, 0));
136  PQclear(result);
137 
138  CU_ASSERT_NOT_EQUAL(ret, mimetype_id);
139  /* delete the record mimetype_name is application/octet-stream in mimetype */
140  memset(SQL, '\0', MAXCMD);
141  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
142  result = PQexec(pgConn, SQL);
143  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
144  {
145  PQfinish(pgConn);
146  exit(-1);
147  }
148  PQclear(result);
149 }
150 
154 CU_TestInfo testcases_DBFindMime[] =
155 {
156 #if 0
157 #endif
158 {"DBFindMime:ExistAndNot", testDBFindMime},
159  CU_TEST_INFO_NULL
160 };
161 
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
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
void testDBFindMime()
for function DBFindMime()
int DBFindMime(char *Mimetype)
Find a mime type in the DBMime table.
Definition: finder.c:92
int DBFindMimeClean()
clean the env
int DBFindMimeInit()
initialize
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
magic_t MagicCookie
for Magic
Definition: finder.c:34
CU_TestInfo testcases_DBFindMime[]
testcases for function DBFindMime
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:40