~drizzle-trunk/drizzle/development

2097.2.4 by Andrew Hutchings
Convert all unit tests to boost::test
1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
*
4
*  Copyright (C) 2010 Pawel Blokus
5
*
6
*  This program is free software; you can redistribute it and/or modify
7
*  it under the terms of the GNU General Public License as published by
8
*  the Free Software Foundation; either version 2 of the License, or
9
*  (at your option) any later version.
10
*
11
*  This program is distributed in the hope that it will be useful,
12
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
*  GNU General Public License for more details.
15
*
16
*  You should have received a copy of the GNU General Public License
17
*  along with this program; if not, write to the Free Software
18
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
*/
20
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
2097.2.4 by Andrew Hutchings
Convert all unit tests to boost::test
22
23
#define BOOST_TEST_DYN_LINK
24
#include <boost/test/unit_test.hpp>
25
26
#include <drizzled/type/decimal.h>
27
#include <drizzled/temporal.h>
28
#include <drizzled/temporal_format.h>
29
30
#include "temporal_generator.h"
31
32
using namespace drizzled;
33
34
class DateTimeTest
35
{
36
  protected:
37
    DateTime datetime;
38
    bool result;
39
    uint32_t years, months, days;
40
    uint32_t hours, minutes, seconds;
41
    
42
    DateTimeTest()
43
    {
44
      TemporalGenerator::DateTimeGen::make_valid_datetime(&datetime);
45
    }
46
47
    void assignDateTimeValues()
48
    {
49
      years= datetime.years();
50
      months= datetime.months();
51
      days= datetime.days();
52
      hours= datetime.hours();
53
      minutes= datetime.minutes();
54
      seconds= datetime.seconds();
55
    }
56
};
57
58
BOOST_FIXTURE_TEST_SUITE(DateTimeTestValidation, DateTimeTest)
59
BOOST_AUTO_TEST_CASE(is_valid_onValidDateTime_shouldReturn_True)
60
{
61
  result= datetime.is_valid();
62
  BOOST_REQUIRE(result);
63
}
64
65
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithYearBelowMinimum_shouldReturn_False)
66
{
67
  datetime.set_years(DRIZZLE_MIN_YEARS_SQL - 1);
68
  
69
  result= datetime.is_valid();
70
  
71
  BOOST_REQUIRE(not result);
72
}
73
74
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithYearAboveMaximum_shouldReturn_False)
75
{
76
  datetime.set_years(DRIZZLE_MAX_YEARS_SQL + 1);
77
    
78
  result= datetime.is_valid();
79
  
80
  BOOST_REQUIRE(not result);
81
}
82
83
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithMonthSetToZero_shouldReturn_False)
84
{
85
  datetime.set_months(0);
86
  
87
  result= datetime.is_valid();
88
  
89
  BOOST_REQUIRE(not result);
90
}
91
92
93
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithMonthAboveMaximum_shouldReturn_False)
94
{
95
  datetime.set_months(13);
96
  
97
  result= datetime.is_valid();
98
  
99
  BOOST_REQUIRE(not result);
100
}
101
102
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithDaySetToZero_shouldReturn_False)
103
{
104
  datetime.set_days(0);
105
  
106
  result= datetime.is_valid();
107
  
108
  BOOST_REQUIRE(not result);
109
}
110
111
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithDayAboveDaysInMonth_shouldReturn_False)
112
{
113
  datetime.set_days(32);
114
  
115
  result= datetime.is_valid();
116
  
117
  BOOST_REQUIRE(not result);
118
}
119
120
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithLeapDayInNonLeapYear_shouldReturn_False)
121
{
122
  TemporalGenerator::TemporalGen::leap_day_in_non_leap_year(&datetime);
123
  
124
  result= datetime.is_valid();
125
  
126
  BOOST_REQUIRE(not result);
127
}
128
129
BOOST_AUTO_TEST_CASE(is_valid_onValidDateTimeWithLeapDayInLeapYear_shouldReturn_True)
130
{
131
  TemporalGenerator::TemporalGen::leap_day_in_leap_year(&datetime);
132
  
133
  result= datetime.is_valid();
134
  
135
  BOOST_REQUIRE(result);
136
}
137
138
BOOST_AUTO_TEST_CASE(is_valid_onValidMinimalTime_shouldReturn_True)
139
{
140
  TemporalGenerator::TemporalGen::make_min_time(&datetime);
141
  
142
  result= datetime.is_valid();
143
  
144
  BOOST_REQUIRE(result);
145
}
146
147
BOOST_AUTO_TEST_CASE(is_valid_onValidMaximalTime_shouldReturn_True)
148
{
149
  TemporalGenerator::TemporalGen::make_max_time(&datetime);
150
  
151
  result= datetime.is_valid();
152
  
153
  BOOST_REQUIRE(result);
154
}
155
156
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithHourAboveMaximum23_shouldReturn_False)
157
{
158
  datetime.set_hours(24);
159
  
160
  result= datetime.is_valid();
161
  
162
  BOOST_REQUIRE(not result);
163
}
164
165
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithMinutesAboveMaximum59_shouldReturn_False)
166
{
167
  datetime.set_minutes(60);
168
  
169
  result= datetime.is_valid();
170
  
171
  BOOST_REQUIRE(not result);
172
}
173
174
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithSecondsAboveMaximum61_shouldReturn_False)
175
{
176
  datetime.set_seconds(62);
177
  
178
  result= datetime.is_valid();
179
  
180
  BOOST_REQUIRE(not result);
181
}
182
183
BOOST_AUTO_TEST_CASE(to_string_shouldProduce_hyphenSeperatedDateElements_and_colonSeperatedTimeElements)
184
{
185
  char expected[DateTime::MAX_STRING_LENGTH]= "2010-05-01 08:07:06.123456";
186
  char returned[DateTime::MAX_STRING_LENGTH];
187
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, 2010, 5, 1, 8, 7, 6, 123456);
188
  
189
  datetime.to_string(returned, DateTime::MAX_STRING_LENGTH);
190
  
191
  BOOST_REQUIRE_EQUAL(expected, returned);
192
}
193
194
BOOST_AUTO_TEST_CASE(to_string_nullBuffer_noMicroSeconds_shouldReturnProperLengthAnyway)
195
{
196
  int length= datetime.to_string(NULL, 0);
197
  
198
  BOOST_REQUIRE_EQUAL(DateTime::MAX_STRING_LENGTH - 1 - 7, length);  
199
}
200
201
BOOST_AUTO_TEST_CASE(to_int64_t)
202
{
203
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, 2030, 8, 7, 14, 5, 13);
204
  int64_t representation;
205
206
  datetime.to_int64_t(&representation);
207
208
  BOOST_REQUIRE_EQUAL(20300807140513LL, representation);
209
}
210
211
BOOST_AUTO_TEST_CASE(from_int64_t_no_conversion_format_YYYYMMDDHHMMSSshouldPopulateDateTimeCorrectly)
212
{
213
  datetime.from_int64_t(20300807140513LL, false);
214
  
215
  assignDateTimeValues();
216
  
217
  BOOST_REQUIRE_EQUAL(2030, years);
218
  BOOST_REQUIRE_EQUAL(8, months);
219
  BOOST_REQUIRE_EQUAL(7, days);
220
  BOOST_REQUIRE_EQUAL(14, hours);
221
  BOOST_REQUIRE_EQUAL(5, minutes);
222
  BOOST_REQUIRE_EQUAL(13, seconds);
223
}
224
225
BOOST_AUTO_TEST_CASE(from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearOver2000)
226
{
227
  datetime.from_int64_t(20300807140513LL, true);
228
  
229
  assignDateTimeValues();
230
  
231
  BOOST_REQUIRE_EQUAL(2030, years);
232
  BOOST_REQUIRE_EQUAL(8, months);
233
  BOOST_REQUIRE_EQUAL(7, days);
234
  BOOST_REQUIRE_EQUAL(14, hours);
235
  BOOST_REQUIRE_EQUAL(5, minutes);
236
  BOOST_REQUIRE_EQUAL(13, seconds);
237
}
238
239
BOOST_AUTO_TEST_CASE(from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearBelow2000)
240
{
241
  datetime.from_int64_t(19900807140513LL, true);
242
  
243
  assignDateTimeValues();
244
  
245
  BOOST_REQUIRE_EQUAL(1990, years);
246
  BOOST_REQUIRE_EQUAL(8, months);
247
  BOOST_REQUIRE_EQUAL(7, days);
248
  BOOST_REQUIRE_EQUAL(14, hours);
249
  BOOST_REQUIRE_EQUAL(5, minutes);
250
  BOOST_REQUIRE_EQUAL(13, seconds);
251
}
252
253
BOOST_AUTO_TEST_CASE(from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearOver2000)
254
{
255
  datetime.from_int64_t(300807140513LL, true);
256
  
257
  assignDateTimeValues();
258
  
259
  BOOST_REQUIRE_EQUAL(2030, years);
260
  BOOST_REQUIRE_EQUAL(8, months);
261
  BOOST_REQUIRE_EQUAL(7, days);
262
  BOOST_REQUIRE_EQUAL(14, hours);
263
  BOOST_REQUIRE_EQUAL(5, minutes);
264
  BOOST_REQUIRE_EQUAL(13, seconds);
265
}
266
267
BOOST_AUTO_TEST_CASE(from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearBelow2000)
268
{
269
  datetime.from_int64_t(900807140513LL, true);
270
  
271
  assignDateTimeValues();
272
  
273
  BOOST_REQUIRE_EQUAL(1990, years);
274
  BOOST_REQUIRE_EQUAL(8, months);
275
  BOOST_REQUIRE_EQUAL(7, days);
276
  BOOST_REQUIRE_EQUAL(14, hours);
277
  BOOST_REQUIRE_EQUAL(5, minutes);
278
  BOOST_REQUIRE_EQUAL(13, seconds);
279
}
280
281
BOOST_AUTO_TEST_CASE(to_tm)
282
{
283
  years= 2030, months= 8, days= 17, hours= 14, minutes= 45, seconds= 13;
284
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, years, months, days, hours, minutes, seconds);
285
  struct tm filled;
286
  
287
  datetime.to_tm(&filled);
288
  
289
  BOOST_REQUIRE_EQUAL(2030 - 1900, filled.tm_year);
290
  BOOST_REQUIRE_EQUAL(8 - 1, filled.tm_mon);
291
  BOOST_REQUIRE_EQUAL(17, filled.tm_mday);
292
  BOOST_REQUIRE_EQUAL(14, filled.tm_hour);
293
  BOOST_REQUIRE_EQUAL(45, filled.tm_min);
294
  BOOST_REQUIRE_EQUAL(13, filled.tm_sec);
295
296
}
297
298
BOOST_AUTO_TEST_CASE(to_decimal)
299
{
300
  drizzled::type::Decimal to;
301
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, 1987, 6, 13, 5, 10, 13, 456);
302
303
  datetime.to_decimal(&to);
304
  
305
  BOOST_REQUIRE_EQUAL(19870,to.buf[0]);
306
  BOOST_REQUIRE_EQUAL(613051013,to.buf[1]);
307
  BOOST_REQUIRE_EQUAL(456000,to.buf[2]);
308
}
309
BOOST_AUTO_TEST_SUITE_END()
310
311
312
class DateTimeFromStringTest
313
{
314
  protected:
315
    static const char* allStrings[];
316
    DateTime datetime;
317
    bool result;
318
    uint32_t years, months, days;
319
    uint32_t hours, minutes, seconds;
320
321
    DateTimeFromStringTest()
322
    {
323
      init_temporal_formats();
324
    }
325
326
    virtual ~DateTimeFromStringTest()
327
    {
328
      deinit_temporal_formats();
329
    }
330
331
    virtual int stringCount() { return 0; }
332
    virtual const char** strings() { return NULL; }
333
334
    void assignDateTimeValues()
335
    {
336
      years= datetime.years();
337
      months= datetime.months();
338
      days= datetime.days();
339
      hours= datetime.hours();
340
      minutes= datetime.minutes();
341
      seconds= datetime.seconds();
342
    }
343
};
344
345
const char* DateTimeFromStringTest::allStrings[]= {"NULL"};
346
347
348
class DateTimeFromStringFullFormatTest : public DateTimeFromStringTest
349
{
350
 protected:
351
  static const char* allStrings[];
352
353
  const char** strings()
354
  {
355
    return allStrings;
356
  }
357
  
358
  int stringCount()
359
  {
360
    return 7;
361
  }  
362
};
363
364
const char* DateTimeFromStringFullFormatTest::allStrings[]= {"20100501080706",
365
            "2010-05-01 08:07:06",
366
            "2010/05/01T08:07:06",
367
            "2010.5.1 08:07:06",
368
            "10-05-01 08:07:06",
369
            "10/5/1 08:07:06",
370
            "10.5.1 08:07:06"};
371
372
class DateTimeFromStringNoSecondFormatTest : public DateTimeFromStringTest
373
{
374
 protected:
375
  static const char* allStrings[];
376
377
  const char** strings()
378
  {
379
    return allStrings;
380
  }
381
  
382
  int stringCount()
383
  {
384
    return 6;
385
  }
386
};
387
388
const char* DateTimeFromStringNoSecondFormatTest::allStrings[]= {"2010-05-01 08:07",
389
            "2010/05/01 08:07",
390
            "2010.5.1 08:07",
391
            "10-05-01 08:07",
392
            "10/5/1 08:07",
393
            "10.5.1 08:07"};
394
395
class DateTimeFromStringDateOnlyTest: public DateTimeFromStringTest
396
{
397
 protected:
398
  static const char* allStrings[];
399
400
  const char** strings()
401
  {
402
    return allStrings;
403
  }
404
  
405
  int stringCount()
406
  {
407
    return 5;
408
  }
409
};
410
411
const char* DateTimeFromStringDateOnlyTest::allStrings[]= {"20100607", /* YYYYMMDD */
412
            "06/07/2010",/* MM[-/.]DD[-/.]YYYY (US common format)*/
413
            "10.06.07",/* YY[-/.]MM[-/.]DD */
414
            "10/6/7",/* YY[-/.][M]M[-/.][D]D */
415
            "2010-6-7"/* YYYY[-/.][M]M[-/.][D]D */};
416
417
BOOST_AUTO_TEST_SUITE(DateTimeFromStringTestSuite)
418
BOOST_FIXTURE_TEST_CASE(from_string_validStringFull, DateTimeFromStringFullFormatTest)
419
{
420
  for (int it= 0; it < stringCount(); it++)
421
  {
422
    const char *valid_string= strings()[it];
423
424
    result= datetime.from_string(valid_string, strlen(valid_string));
425
    BOOST_REQUIRE(result);
426
427
    assignDateTimeValues();
428
429
    BOOST_REQUIRE_EQUAL(2010, years);
430
    BOOST_REQUIRE_EQUAL(5, months);
431
    BOOST_REQUIRE_EQUAL(1, days);
432
    BOOST_REQUIRE_EQUAL(8, hours);
433
    BOOST_REQUIRE_EQUAL(7, minutes);
434
    BOOST_REQUIRE_EQUAL(6, seconds);
435
  }
436
}
437
                                          
438
BOOST_FIXTURE_TEST_CASE(from_string_validStringNoSecond, DateTimeFromStringNoSecondFormatTest)
439
{
440
  for (int it= 0; it < stringCount(); it++)
441
  {
442
    const char *valid_string= strings()[it];
443
444
    result= datetime.from_string(valid_string, strlen(valid_string));
445
    BOOST_REQUIRE(result);
446
447
    assignDateTimeValues();
448
449
    BOOST_REQUIRE_EQUAL(2010, years);
450
    BOOST_REQUIRE_EQUAL(5, months);
451
    BOOST_REQUIRE_EQUAL(1, days);
452
    BOOST_REQUIRE_EQUAL(8, hours);
453
    BOOST_REQUIRE_EQUAL(7, minutes);
454
  }
455
}
456
457
BOOST_FIXTURE_TEST_CASE(from_string_validStringDateOnly, DateTimeFromStringDateOnlyTest)
458
{
459
  for (int it= 0; it < stringCount(); it++)
460
  {
461
    const char *valid_string= strings()[it];
462
463
    result= datetime.from_string(valid_string, strlen(valid_string));
464
    BOOST_REQUIRE(result);
465
466
    assignDateTimeValues();
467
468
    BOOST_REQUIRE_EQUAL(2010, years);
469
    BOOST_REQUIRE_EQUAL(6, months);
470
    BOOST_REQUIRE_EQUAL(7, days);
471
  }
472
}
473
474
BOOST_AUTO_TEST_SUITE_END()