-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.c
245 lines (193 loc) · 6.51 KB
/
db_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* database interface test program for smatool
Copyright Tony Brown 2011
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#define _XOPEN_SOURCE
#include "db_interface.h"
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include "minunit.h"
#define DATE_STR_LENGTH 25
int tests_run = 0;
char *server = NULL;
char *user = NULL;
char *password = NULL;
char *database = NULL;
int do_install = 0;
const char *tst_sunrise="2011-02-22 06:35:40";
const char *tst_sunset="2011-02-22 19:27:28";
const char *tst_date="2011-02-22 15:01:39";
const char *tst_format="%Y-%m-%d %H:%M:%S";
const int power1 = 3456; //kW
const int energy1 = 13003; // scaled * 1000 kWh; 13.003kWh
const int power2 = 3000; //kW
const int energy2 = 13600; // scaled * 1000 kWh; 13.6kWh
static char * test_db_install_tables() {
mu_assert_equal_int(1, db_install_tables() );
return 0;
}
static char * test_db_get_schema() {
mu_assert_equal_int(2, db_get_schema());
return 0;
}
static char * test_db_update_almanac() {
struct tm date;
strptime( tst_date,tst_format,&date);
mu_assert_equal_int(1, db_update_almanac(&date, tst_sunrise, tst_sunset ));
return 0;
}
static char * test_db_fetch_almanac() {
char sunrise_get[DATE_STR_LENGTH];
char sunset_get[DATE_STR_LENGTH];
struct tm date;
strptime( tst_date,tst_format,&date);
date.tm_hour = 0;
mu_assert_equal_int(1, db_fetch_almanac( &date, sunrise_get, sunset_get ) );
mu_assert_equal_string(tst_sunrise, sunrise_get );
mu_assert_equal_string(tst_sunset, sunset_get );
return 0;
}
static char * test_db_fetch_almanac_not_found() {
char sunrise_get[DATE_STR_LENGTH];
char sunset_get[DATE_STR_LENGTH];
sunrise_get[0] = '\0';
sunset_get[0] = '\0';
struct tm date;
strptime( tst_date,tst_format,&date);
date.tm_year++;
mu_assert_equal_int(0, db_fetch_almanac( &date, sunrise_get, sunset_get ) );
mu_assert_equal_string("", sunrise_get );
mu_assert_equal_string("", sunset_get );
return 0;
}
static char * test_db_set_interval_value() {
struct tm date;
strptime( tst_date,tst_format,&date);
//later time first
date.tm_sec = 0;
date.tm_min = 15;
mu_assert_equal_int( 1, db_set_interval_value( &date, "inv", 1234567890, power2, energy2 ));
date.tm_min = 10;
mu_assert_equal_int( 1, db_set_interval_value( &date, "inv", 1234567890, power1, energy1 ));
return 0;
}
static char * test_db_get_last_recorded_interval_datetime() {
struct tm date;
strptime( tst_date,tst_format,&date);
struct tm last_date = db_get_last_recorded_interval_datetime( &date );
date.tm_min = 15;
date.tm_sec = 0;
char exp[DATE_STR_LENGTH];
char res[DATE_STR_LENGTH];
strftime(exp,DATE_STR_LENGTH, tst_format ,&date);
strftime(res,DATE_STR_LENGTH, tst_format ,&last_date);
mu_assert_equal_string( res, exp );
return 0;
}
static char * test_db_get_last_recorded_interval_datetime_not_found() {
struct tm date;
strptime( tst_date,tst_format,&date);
date.tm_year++;
struct tm last_date = db_get_last_recorded_interval_datetime( &date );
//date should be 1970-01-01 if no record found
mu_assert_equal_int( 70, last_date.tm_year );
mu_assert_equal_int( 0, last_date.tm_mon );
mu_assert_equal_int( 1, last_date.tm_mday );
return 0;
}
static char * test_db_get_unposted_data()
{
struct tm date;
strptime( tst_date,tst_format,&date);
row_handle *row = db_get_unposted_data( &date );
mu_assert("No rows found", row != NULL );
date.tm_sec = 0;
date.tm_min = 10;
char exp[DATE_STR_LENGTH];
strftime(exp,DATE_STR_LENGTH, tst_format ,&date);
char *got = db_row_string_data( row, 0 );
mu_assert_equal_string( exp, got );
sprintf(exp,"%d.%03d", energy1 / 1000 , energy1 % 1000 );
got = db_row_string_data( row, 1 );
mu_assert_equal_string( exp, got );
//next row
mu_assert_equal_int(1, db_row_next( row ) );
date.tm_min = 15;
strftime(exp,DATE_STR_LENGTH, tst_format ,&date);
got = db_row_string_data( row, 0 );
mu_assert_equal_string( exp, got );
sprintf(exp,"%d.%03d", energy2 / 1000 , energy2 % 1000 );
got = db_row_string_data( row, 1 );
//code to ensure fetched number has 3 decimal places
char *dotPos = strchr(got,'.');
if( dotPos != NULL )
{
strcat(got,"000");
*(dotPos+4)='\0';
}
//end ensure trailing zeros
mu_assert_equal_string( exp, got );
db_row_handle_free( row );
return 0;
}
static char * test_db_set_data_posted(){
struct tm from_date, to_date;
strptime( tst_date,tst_format,&from_date);
from_date.tm_sec = 0;
from_date.tm_min = 10;
to_date = from_date;
to_date.tm_min = 15;
int result = db_set_data_posted( &from_date, &to_date );
mu_assert_equal_int(1, result );
//assert there should be no rows
row_handle *row = db_get_unposted_data( &from_date );
mu_assert("should be no unposted data", row == NULL );
return 0;
}
static char * all_tests() {
if( do_install ) mu_run_test(test_db_install_tables);
mu_run_test(test_db_get_schema);
mu_run_test(test_db_update_almanac);
mu_run_test(test_db_fetch_almanac);
mu_run_test(test_db_fetch_almanac_not_found);
mu_run_test(test_db_set_interval_value);
mu_run_test(test_db_get_last_recorded_interval_datetime);
mu_run_test(test_db_get_last_recorded_interval_datetime_not_found);
mu_run_test(test_db_get_unposted_data);
mu_run_test(test_db_set_data_posted);
return 0;
}
int main(int argc, char **argv) {
if( argc < 5 )
{
puts("db_test server user password database [do_install]");
puts("For sqlite3, \"database\" is filename of data file and all other params are ignored");
return 1;
}
server = argv[1];
user = argv[2];
password = argv[3];
database = argv[4];
if( argv[5] ) do_install = 1;
db_init(server, user, password, database);
char *result = all_tests();
if (result != 0) {
printf("Test %s failed. %s.\n", t_name, result);
}
else {
printf("ALL TESTS PASSED\n");
}
printf("Tests run: %d\n", tests_run);
db_close();
return result != 0;
}