OpenDNSSEC-enforcer  1.4.6
test_dd_string.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 /*+
28  * Filename: test_dd_string.c - Test dd_string
29  *
30  * Description:
31  * This is a short test module to check the functions in the code that
32  * constructs a DELETE statement.
33  *
34  * The test program makes use of the CUnit framework, as described in
35  * http://cunit.sourceforge.net
36 -*/
37 
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <time.h>
42 
43 #include "CUnit/Basic.h"
44 
45 #include "ksm/database_statement.h"
46 #include "test_routines.h"
47 
48 
49 
50 /*+
51  * TestDdsBasic - Test Basic Dds Routines
52  *
53  * Description:
54  * Constructs a database DELETE statement and checks the string so
55  * constructed.
56 -*/
57 
58 static void TestDdsBasic(void)
59 {
60  char* sql = NULL;
61 
62  sql = DdsInit("TEST");
63  DdsEnd(&sql);
64 
65  CU_ASSERT_STRING_EQUAL(sql, "DELETE FROM TEST");
66  DdsFree(sql);
67 
68  return;
69 }
70 
71 /*+
72  * TestDdsConditionInt - Test Conditional
73  *
74  * Description:
75  * Checks that the deletion can be constrained by a WHERE clause comparing
76  * fields to integers.
77 -*/
78 
79 static void TestDdsConditionInt(void)
80 {
81  char* sql = NULL;
82  int clause = 0;
83 
84  sql = DdsInit("TEST");
85  DdsConditionInt(&sql, "ALPHA", DQS_COMPARE_LT, 1, clause++);
86  DdsConditionInt(&sql, "BETA", DQS_COMPARE_LE, 2, clause++);
87  DdsConditionInt(&sql, "GAMMA", DQS_COMPARE_EQ, 3, clause++);
88  DdsConditionInt(&sql, "DELTA", DQS_COMPARE_NE, 4, clause++);
89  DdsConditionInt(&sql, "EPSILON", DQS_COMPARE_GE, 5, clause++);
90  DdsConditionInt(&sql, "ZETA", DQS_COMPARE_GT, 6, clause++);
91  DdsEnd(&sql);
92 
93  CU_ASSERT_STRING_EQUAL(sql,
94  "DELETE FROM TEST WHERE ALPHA < 1 AND BETA <= 2 AND GAMMA = 3 "
95  "AND DELTA != 4 AND EPSILON >= 5 AND ZETA > 6");
96  DdsFree(sql);
97 
98  return;
99 }
100 
101 /*+
102  * TestDdsConditionString - Test Conditional
103  *
104  * Description:
105  * Checks that the deletion can be constrained by a WHERE clause comparing
106  * fields to strings.
107 -*/
108 
109 static void TestDdsConditionString(void)
110 {
111  char* sql = NULL;
112  int clause = 0;
113  static const char* TEST =
114  "DELETE FROM TEST WHERE ALPHA < 'PETER' AND BETA <= 'PIPER' "
115  "AND GAMMA = 'PICKED' AND DELTA != 'A' AND EPSILON >= 'PECK' "
116  "AND ZETA > 'OF'";
117 
118  sql = DdsInit("TEST");
119  DdsConditionString(&sql, "ALPHA", DQS_COMPARE_LT, "PETER", clause++);
120  DdsConditionString(&sql, "BETA", DQS_COMPARE_LE, "PIPER", clause++);
121  DdsConditionString(&sql, "GAMMA", DQS_COMPARE_EQ, "PICKED", clause++);
122  DdsConditionString(&sql, "DELTA", DQS_COMPARE_NE, "A", clause++);
123  DdsConditionString(&sql, "EPSILON", DQS_COMPARE_GE, "PECK", clause++);
124  DdsConditionString(&sql, "ZETA", DQS_COMPARE_GT, "OF", clause++);
125  DdsEnd(&sql);
126 
127  CU_ASSERT_STRING_EQUAL(sql, TEST);
128  DdsFree(sql);
129 
130  return;
131 }
132 
133 /*+
134  * TestDdsConditionKeyword - Test Conditional
135  *
136  * Description:
137  * Checks that the deletion can be constrained by a WHERE clause comprising
138  * an IN clause.
139 -*/
140 
141 
142 static void TestDdsConditionKeyword(void)
143 {
144  char* sql = NULL;
145  int clause = 0;
146  static const char* TEST =
147  "DELETE FROM TEST WHERE ALPHA IN (1, 2, 3) "
148  "AND BETA IN (\"ALEPH\", \"BETH\")";
149 
150  sql = DdsInit("TEST");
151  DdsConditionKeyword(&sql, "ALPHA", DQS_COMPARE_IN, "(1, 2, 3)", clause++);
152  DdsConditionKeyword(&sql, "BETA", DQS_COMPARE_IN, "(\"ALEPH\", \"BETH\")",
153  clause++);
154  DdsEnd(&sql);
155 
156  CU_ASSERT_STRING_EQUAL(sql, TEST);
157  DdsFree(sql);
158 
159  return;
160 }
161 
162 
163 /*+
164  * TestDds - Create Test Suite
165  *
166  * Description:
167  * Adds the test suite to the CUnit test registry and adds all the tests
168  * to it.
169  *
170  * Arguments:
171  * None.
172  *
173  * Returns:
174  * int
175  * Return status. 0 => Success.
176  */
177 
178 int TestDds(void); /* Declaration */
179 int TestDds(void)
180 {
181  struct test_testdef tests[] = {
182  {"TestDdsBasic", TestDdsBasic},
183  {"TestDdsConditionInt", TestDdsConditionInt},
184  {"TestDdsConditionString", TestDdsConditionString},
185  {"TestDdsConditionKeyword", TestDdsConditionKeyword},
186  {NULL, NULL}
187  };
188 
189  return TcuCreateSuite("Dds", NULL, NULL, tests);
190 }
int TcuCreateSuite(const char *title, int(*init)(), int(*teardown)(), struct test_testdef *tests)
void DdsConditionKeyword(char **query, const char *field, DQS_COMPARISON compare, const char *value, int index)
Definition: dd_string.c:102
void DdsFree(char *query)
Definition: dd_string.c:115
void DdsConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int index)
Definition: dd_string.c:88
void DdsConditionString(char **query, const char *field, DQS_COMPARISON compare, const char *value, int index)
Definition: dd_string.c:95
char * DdsInit(const char *table)
Definition: dd_string.c:60
void DdsEnd(char **query)
Definition: dd_string.c:109
int TestDds(void)