FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_database.c
1 /*
2  Author: Daniele Fognini, Andreas Wuerl
3  SPDX-FileCopyrightText: © 2013-2017 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include <CUnit/CUnit.h>
9 #include <libfocunit.h>
10 
11 #include "database.h"
12 
13 extern fo_dbManager* dbManager;
14 
15 void test_queryAllLicenses() {
16  PGresult* licenses = queryAllLicenses(dbManager);
17 
18  CU_ASSERT_PTR_NOT_NULL_FATAL(licenses);
19 
20  FO_ASSERT_EQUAL_FATAL(PQntuples(licenses), 2);
21 
22  PQclear(licenses);
23 }
24 
25 void test_getTextFromId() {
26  char* lic1Text = getLicenseTextForLicenseRefId(dbManager, 1);
27  CU_ASSERT_STRING_EQUAL(lic1Text, "gnu general public license version 3,");
28  g_free(lic1Text);
29 }
30 
31 void test_getTextFromBadId() {
32  printf("test: expecting a warning: \n--\n");
33  char* notExistingText = getLicenseTextForLicenseRefId(dbManager, LONG_MAX);
34  printf("\n--\n");
35  CU_ASSERT_STRING_EQUAL(notExistingText, "");
36  g_free(notExistingText);
37 }
38 
39 #define doOrReturnError(fmt, ...) do {\
40  PGresult* copy = fo_dbManager_Exec_printf(dbManager, fmt, #__VA_ARGS__); \
41  if (!copy) {\
42  return 1; \
43  } else {\
44  PQclear(copy);\
45  }\
46 } while(0)
47 
48 int database_setUpFunc() {
49  if (!dbManager) {
50  return 1;
51  }
52 
53  if (!fo_dbManager_tableExists(dbManager, "license_ref")) {
54  doOrReturnError("CREATE TABLE license_ref(rf_pk int, rf_shortname text, rf_text text, rf_active bool, rf_detector_type int)",);
55  }
56 
57  doOrReturnError("INSERT INTO license_ref(rf_pk, rf_shortname, rf_text, rf_active ,rf_detector_type) "
58  "VALUES (1, 'GPL-3.0', 'gnu general public license version 3,', true, 1)",);
59  doOrReturnError("INSERT INTO license_ref(rf_pk, rf_shortname, rf_text, rf_active ,rf_detector_type) "
60  "VALUES (2, 'GPL-2.0', 'gnu general public license, version 2', true, 1)",);
61 
62  return 0;
63 }
64 
65 int database_tearDownFunc() {
66  if (!dbManager) {
67  return 1;
68  }
69 
70  doOrReturnError("DROP TABLE license_ref",);
71 
72  return 0;
73 }
74 
75 CU_TestInfo database_testcases[] = {
76  {"Testing get lla licenses:", test_queryAllLicenses},
77  {"Testing get text from id:", test_getTextFromId},
78  {"Testing get text from bad id:", test_getTextFromBadId},
79  CU_TEST_INFO_NULL
80 };
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16