FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_DBInsert.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2012 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 #include "run_tests.h"
18 #include "../agent/externs.h"
24 static PGresult *result = NULL;
25 static long upload_pk = -1;
26 static long pfile_pk = -1;
27 extern char *DBConfFile;
28 
33 {
34  char *ErrorBuf;
35  char *upload_filename = "test_1.orig.tar.gz";
36  int upload_mode = 104;
37  char *upload_origin = "test_1.orig.tar.gz";
38  char *tmp;
39 
40  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
41  if (!pgConn)
42  {
43  LOG_FATAL("Unable to connect to database");
44  exit(-1);
45  }
46 
47  /* insert upload info */
48  memset(SQL,'\0',MAXSQL);
49  snprintf(SQL,MAXSQL,"INSERT INTO upload (upload_filename,upload_mode,upload_origin) VALUES ('%s', %d, '%s');",
50  upload_filename, upload_mode, upload_origin);
51  result = PQexec(pgConn, SQL);
52  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
53  {
54  printf("Insert upload information ERROR!\n");
55  return (-1);
56  }
57  PQclear(result);
58 
59  /* select the upload pk */
60  memset(SQL,'\0',MAXSQL);
61  snprintf(SQL,MAXSQL,"SELECT upload_pk FROM upload WHERE upload_filename = '%s';",
62  upload_filename);
63  result = PQexec(pgConn, SQL); /* SELECT */
64  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__)) return(-1);
65 
66  tmp = PQgetvalue(result,0,0);
67  if(tmp)
68  {
69  Upload_Pk = tmp;
70  upload_pk = atol(tmp);
71  }
72  PQclear(result);
73  return 0;
74 }
75 
80 {
81  memset(SQL,'\0',MAXSQL);
82  snprintf(SQL,MAXSQL,"BEGIN;");
83  result = PQexec(pgConn, SQL);
84  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
85  {
86  printf("Remove pfile database information ERROR!\n");
87  return (-1);
88  }
89  PQclear(result);
90 
91  /* delete uploadtree info */
92  memset(SQL,'\0',MAXSQL);
93  snprintf(SQL,MAXSQL,"DELETE FROM uploadtree WHERE upload_fk = %ld;", upload_pk);
94  result = PQexec(pgConn, SQL);
95  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
96  {
97  printf("Remove pfile database information ERROR!\n");
98  return (-1);
99  }
100  PQclear(result);
101 
102  /* delete upload info */
103  memset(SQL,'\0',MAXSQL);
104  snprintf(SQL,MAXSQL,"DELETE FROM upload WHERE upload_pk = %ld;", upload_pk);
105  result = PQexec(pgConn, SQL);
106  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
107  {
108  printf("Remove pfile database information ERROR!\n");
109  return (-1);
110  }
111  PQclear(result);
112 
113  /* delete pfile info */
114  memset(SQL,'\0',MAXSQL);
115  snprintf(SQL,MAXSQL,"DELETE FROM pfile WHERE pfile_pk = %ld;", pfile_pk);
116  result = PQexec(pgConn, SQL);
117  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
118  {
119  printf("Remove pfile database information ERROR!\n");
120  return (-1);
121  }
122  PQclear(result);
123 
124 
125  memset(SQL,'\0',MAXSQL);
126  snprintf(SQL,MAXSQL,"COMMIT;");
127  result = PQexec(pgConn, SQL);
128  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
129  {
130  printf("Perpare pfile information ERROR!\n");
131  return (-1);
132  }
133  PQclear(result);
134 
135  if (pgConn) PQfinish(pgConn);
136  return 0;
137 }
138 
146 {
147  ContainerInfo *CI = NULL;
148  struct stat Stat = {0};
149  ParentInfo PI = {0, 1287725739, 1287725739, 0, 0};
150  ContainerInfo CITest = {"../testdata/test_1.orig.tar.gz", "./test-result/",
151  "test_1.orig.tar.gz", "test_1.orig.tar.gz.dir", 1, 1, 0, 0, Stat, PI, 0, 0, 0, 0, 0, 0};
152  CI = &CITest;
153  char *Fuid = "383A1791BA72A77F80698A90F22C1B7B04C59BEF.720B5CECCC4700FC90D628FCB45490E3.1aa248f65785e15aa9da4fa3701741d85653584544ab4003ef45e232a761a2f1.1312";
154  int result = DBInsertPfile(CI, Fuid);
155  CU_ASSERT_EQUAL(result, 1);
156 }
157 
165 {
166  ContainerInfo *CI = NULL;
167  struct stat Stat = {0};
168  ParentInfo PI = {0, 1287725739, 1287725739, 0, 0};
169  ContainerInfo CITest = {"../testdata/test_1.orig.tar.gz", "./test-result/",
170  "test_1.orig.tar.gz", "test_1.orig.tar.gz.dir", 1, 1, 0, 0, Stat, PI, 0, 0, 0, 0, 0, 0};
171  CI = &CITest;
172  int result = DBInsertUploadTree(CI, 1);
173  CU_ASSERT_EQUAL(result, 0);
174 }
175 
176 /* ************************************************************************** */
177 /* **** cunit test cases **************************************************** */
178 /* ************************************************************************** */
179 
180 CU_TestInfo DBInsertPfile_testcases[] =
181 {
182  {"DBInsertPfile:", testDBInsertPfile},
183  CU_TEST_INFO_NULL
184 };
186 {
187  {"DBInsertUploadTree:", testDBInsertUploadTree},
188  CU_TEST_INFO_NULL
189 };
190 
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
Structure for storing information about a particular file.
Definition: ununpack.h:120
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
CU_TestInfo DBInsertUploadTree_testcases[]
DB insertion test cases (uploadtree)
int DBInsertClean()
clean the database
Definition: test_DBInsert.c:79
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
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
void testDBInsertUploadTree()
test DBInsertUploadTree function
char * Upload_Pk
Upload pk in DB.
CU_TestInfo DBInsertPfile_testcases[]
DB insertion test cases (pfile)
int DBInsertInit()
initialize
Definition: test_DBInsert.c:32
void testDBInsertPfile()
test DBInsertPfile function
int DBInsertUploadTree(ContainerInfo *CI, int Mask)
Insert an UploadTree record.
Definition: utils.c:1296
const char * upload_pk
Definition: sqlstatements.h:93
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:40
int DBInsertPfile(ContainerInfo *CI, char *Fuid)
Insert a Pfile record. Sets the pfile_pk in CI.
Definition: utils.c:1124