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