FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_database.c
1 /*
2 Author: Daniele Fognini, Andreas Wuerl
3 Copyright (C) 2013-2017, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 #include <CUnit/CUnit.h>
19 #include <libfocunit.h>
20 
21 #include "database.h"
22 
23 extern fo_dbManager* dbManager;
24 
25 void test_queryAllLicenses() {
26  PGresult* licenses = queryAllLicenses(dbManager);
27 
28  CU_ASSERT_PTR_NOT_NULL_FATAL(licenses);
29 
30  FO_ASSERT_EQUAL_FATAL(PQntuples(licenses), 2);
31 
32  PQclear(licenses);
33 }
34 
35 void test_getTextFromId() {
36  char* lic1Text = getLicenseTextForLicenseRefId(dbManager, 1);
37  CU_ASSERT_STRING_EQUAL(lic1Text, "gnu general public license version 3,");
38  g_free(lic1Text);
39 }
40 
41 void test_getTextFromBadId() {
42  printf("test: expecting a warning: \n--\n");
43  char* notExistingText = getLicenseTextForLicenseRefId(dbManager, LONG_MAX);
44  printf("\n--\n");
45  CU_ASSERT_STRING_EQUAL(notExistingText, "");
46  g_free(notExistingText);
47 }
48 
49 #define doOrReturnError(fmt, ...) do {\
50  PGresult* copy = fo_dbManager_Exec_printf(dbManager, fmt, #__VA_ARGS__); \
51  if (!copy) {\
52  return 1; \
53  } else {\
54  PQclear(copy);\
55  }\
56 } while(0)
57 
58 int database_setUpFunc() {
59  if (!dbManager) {
60  return 1;
61  }
62 
63  if (!fo_dbManager_tableExists(dbManager, "license_ref")) {
64  doOrReturnError("CREATE TABLE license_ref(rf_pk int, rf_shortname text, rf_text text, rf_active bool, rf_detector_type int)",);
65  }
66 
67  doOrReturnError("INSERT INTO license_ref(rf_pk, rf_shortname, rf_text, rf_active ,rf_detector_type) "
68  "VALUES (1, 'GPL-3.0', 'gnu general public license version 3,', true, 1)",);
69  doOrReturnError("INSERT INTO license_ref(rf_pk, rf_shortname, rf_text, rf_active ,rf_detector_type) "
70  "VALUES (2, 'GPL-2.0', 'gnu general public license, version 2', true, 1)",);
71 
72  return 0;
73 }
74 
75 int database_tearDownFunc() {
76  if (!dbManager) {
77  return 1;
78  }
79 
80  doOrReturnError("DROP TABLE license_ref",);
81 
82  return 0;
83 }
84 
85 CU_TestInfo database_testcases[] = {
86  {"Testing get lla licenses:", test_queryAllLicenses},
87  {"Testing get text from id:", test_getTextFromId},
88  {"Testing get text from bad id:", test_getTextFromBadId},
89  CU_TEST_INFO_NULL
90 };
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28