1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Pawel Blokus
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.
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.
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
23
#define BOOST_TEST_DYN_LINK
24
#include <boost/test/unit_test.hpp>
26
#include <drizzled/type/decimal.h>
27
#include <drizzled/temporal.h>
28
#include <drizzled/temporal_format.h>
30
#include "temporal_generator.h"
32
using namespace drizzled;
39
uint32_t years, months, days;
40
uint32_t hours, minutes, seconds;
44
TemporalGenerator::DateTimeGen::make_valid_datetime(&datetime);
47
void assignDateTimeValues()
49
years= datetime.years();
50
months= datetime.months();
51
days= datetime.days();
52
hours= datetime.hours();
53
minutes= datetime.minutes();
54
seconds= datetime.seconds();
58
BOOST_FIXTURE_TEST_SUITE(DateTimeTestValidation, DateTimeTest)
59
BOOST_AUTO_TEST_CASE(is_valid_onValidDateTime_shouldReturn_True)
61
result= datetime.is_valid();
62
BOOST_REQUIRE(result);
65
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithYearBelowMinimum_shouldReturn_False)
67
datetime.set_years(DRIZZLE_MIN_YEARS_SQL - 1);
69
result= datetime.is_valid();
71
BOOST_REQUIRE(not result);
74
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithYearAboveMaximum_shouldReturn_False)
76
datetime.set_years(DRIZZLE_MAX_YEARS_SQL + 1);
78
result= datetime.is_valid();
80
BOOST_REQUIRE(not result);
83
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithMonthSetToZero_shouldReturn_False)
85
datetime.set_months(0);
87
result= datetime.is_valid();
89
BOOST_REQUIRE(not result);
93
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithMonthAboveMaximum_shouldReturn_False)
95
datetime.set_months(13);
97
result= datetime.is_valid();
99
BOOST_REQUIRE(not result);
102
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithDaySetToZero_shouldReturn_False)
104
datetime.set_days(0);
106
result= datetime.is_valid();
108
BOOST_REQUIRE(not result);
111
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithDayAboveDaysInMonth_shouldReturn_False)
113
datetime.set_days(32);
115
result= datetime.is_valid();
117
BOOST_REQUIRE(not result);
120
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithLeapDayInNonLeapYear_shouldReturn_False)
122
TemporalGenerator::TemporalGen::leap_day_in_non_leap_year(&datetime);
124
result= datetime.is_valid();
126
BOOST_REQUIRE(not result);
129
BOOST_AUTO_TEST_CASE(is_valid_onValidDateTimeWithLeapDayInLeapYear_shouldReturn_True)
131
TemporalGenerator::TemporalGen::leap_day_in_leap_year(&datetime);
133
result= datetime.is_valid();
135
BOOST_REQUIRE(result);
138
BOOST_AUTO_TEST_CASE(is_valid_onValidMinimalTime_shouldReturn_True)
140
TemporalGenerator::TemporalGen::make_min_time(&datetime);
142
result= datetime.is_valid();
144
BOOST_REQUIRE(result);
147
BOOST_AUTO_TEST_CASE(is_valid_onValidMaximalTime_shouldReturn_True)
149
TemporalGenerator::TemporalGen::make_max_time(&datetime);
151
result= datetime.is_valid();
153
BOOST_REQUIRE(result);
156
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithHourAboveMaximum23_shouldReturn_False)
158
datetime.set_hours(24);
160
result= datetime.is_valid();
162
BOOST_REQUIRE(not result);
165
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithMinutesAboveMaximum59_shouldReturn_False)
167
datetime.set_minutes(60);
169
result= datetime.is_valid();
171
BOOST_REQUIRE(not result);
174
BOOST_AUTO_TEST_CASE(is_valid_onInvalidDateTimeWithSecondsAboveMaximum61_shouldReturn_False)
176
datetime.set_seconds(62);
178
result= datetime.is_valid();
180
BOOST_REQUIRE(not result);
183
BOOST_AUTO_TEST_CASE(to_string_shouldProduce_hyphenSeperatedDateElements_and_colonSeperatedTimeElements)
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);
189
datetime.to_string(returned, DateTime::MAX_STRING_LENGTH);
191
BOOST_REQUIRE_EQUAL(expected, returned);
194
BOOST_AUTO_TEST_CASE(to_string_nullBuffer_noMicroSeconds_shouldReturnProperLengthAnyway)
196
int length= datetime.to_string(NULL, 0);
198
BOOST_REQUIRE_EQUAL(DateTime::MAX_STRING_LENGTH - 1 - 7, length);
201
BOOST_AUTO_TEST_CASE(to_int64_t)
203
TemporalGenerator::DateTimeGen::make_datetime(&datetime, 2030, 8, 7, 14, 5, 13);
204
int64_t representation;
206
datetime.to_int64_t(&representation);
208
BOOST_REQUIRE_EQUAL(20300807140513LL, representation);
211
BOOST_AUTO_TEST_CASE(from_int64_t_no_conversion_format_YYYYMMDDHHMMSSshouldPopulateDateTimeCorrectly)
213
datetime.from_int64_t(20300807140513LL, false);
215
assignDateTimeValues();
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);
225
BOOST_AUTO_TEST_CASE(from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearOver2000)
227
datetime.from_int64_t(20300807140513LL, true);
229
assignDateTimeValues();
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);
239
BOOST_AUTO_TEST_CASE(from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearBelow2000)
241
datetime.from_int64_t(19900807140513LL, true);
243
assignDateTimeValues();
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);
253
BOOST_AUTO_TEST_CASE(from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearOver2000)
255
datetime.from_int64_t(300807140513LL, true);
257
assignDateTimeValues();
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);
267
BOOST_AUTO_TEST_CASE(from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearBelow2000)
269
datetime.from_int64_t(900807140513LL, true);
271
assignDateTimeValues();
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);
281
BOOST_AUTO_TEST_CASE(to_tm)
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);
287
datetime.to_tm(&filled);
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);
296
/* TODO:these fail, shouldn't they also be set properly? */
297
BOOST_WARN_EQUAL(228, filled.tm_yday);
298
BOOST_WARN_EQUAL(6, filled.tm_wday);
299
BOOST_WARN_EQUAL(-1, filled.tm_isdst);
302
BOOST_AUTO_TEST_CASE(to_decimal)
304
drizzled::type::Decimal to;
305
TemporalGenerator::DateTimeGen::make_datetime(&datetime, 1987, 6, 13, 5, 10, 13, 456);
307
datetime.to_decimal(&to);
309
BOOST_REQUIRE_EQUAL(19870,to.buf[0]);
310
BOOST_REQUIRE_EQUAL(613051013,to.buf[1]);
311
BOOST_REQUIRE_EQUAL(456000,to.buf[2]);
313
BOOST_AUTO_TEST_SUITE_END()
316
class DateTimeFromStringTest
319
static const char* allStrings[];
322
uint32_t years, months, days;
323
uint32_t hours, minutes, seconds;
325
DateTimeFromStringTest()
327
init_temporal_formats();
330
virtual ~DateTimeFromStringTest()
332
deinit_temporal_formats();
335
virtual int stringCount() { return 0; }
336
virtual const char** strings() { return NULL; }
338
void assignDateTimeValues()
340
years= datetime.years();
341
months= datetime.months();
342
days= datetime.days();
343
hours= datetime.hours();
344
minutes= datetime.minutes();
345
seconds= datetime.seconds();
349
const char* DateTimeFromStringTest::allStrings[]= {"NULL"};
352
class DateTimeFromStringFullFormatTest : public DateTimeFromStringTest
355
static const char* allStrings[];
357
const char** strings()
368
const char* DateTimeFromStringFullFormatTest::allStrings[]= {"20100501080706",
369
"2010-05-01 08:07:06",
370
"2010/05/01T08:07:06",
376
class DateTimeFromStringNoSecondFormatTest : public DateTimeFromStringTest
379
static const char* allStrings[];
381
const char** strings()
392
const char* DateTimeFromStringNoSecondFormatTest::allStrings[]= {"2010-05-01 08:07",
399
class DateTimeFromStringDateOnlyTest: public DateTimeFromStringTest
402
static const char* allStrings[];
404
const char** strings()
415
const char* DateTimeFromStringDateOnlyTest::allStrings[]= {"20100607", /* YYYYMMDD */
416
"06/07/2010",/* MM[-/.]DD[-/.]YYYY (US common format)*/
417
"10.06.07",/* YY[-/.]MM[-/.]DD */
418
"10/6/7",/* YY[-/.][M]M[-/.][D]D */
419
"2010-6-7"/* YYYY[-/.][M]M[-/.][D]D */};
421
BOOST_AUTO_TEST_SUITE(DateTimeFromStringTestSuite)
422
BOOST_FIXTURE_TEST_CASE(from_string_validStringFull, DateTimeFromStringFullFormatTest)
424
for (int it= 0; it < stringCount(); it++)
426
const char *valid_string= strings()[it];
428
result= datetime.from_string(valid_string, strlen(valid_string));
429
BOOST_REQUIRE(result);
431
assignDateTimeValues();
433
BOOST_REQUIRE_EQUAL(2010, years);
434
BOOST_REQUIRE_EQUAL(5, months);
435
BOOST_REQUIRE_EQUAL(1, days);
436
BOOST_REQUIRE_EQUAL(8, hours);
437
BOOST_REQUIRE_EQUAL(7, minutes);
438
BOOST_REQUIRE_EQUAL(6, seconds);
442
BOOST_FIXTURE_TEST_CASE(from_string_validStringNoSecond, DateTimeFromStringNoSecondFormatTest)
444
for (int it= 0; it < stringCount(); it++)
446
const char *valid_string= strings()[it];
448
result= datetime.from_string(valid_string, strlen(valid_string));
449
BOOST_REQUIRE(result);
451
assignDateTimeValues();
453
BOOST_REQUIRE_EQUAL(2010, years);
454
BOOST_REQUIRE_EQUAL(5, months);
455
BOOST_REQUIRE_EQUAL(1, days);
456
BOOST_REQUIRE_EQUAL(8, hours);
457
BOOST_REQUIRE_EQUAL(7, minutes);
461
BOOST_FIXTURE_TEST_CASE(from_string_validStringDateOnly, DateTimeFromStringDateOnlyTest)
463
for (int it= 0; it < stringCount(); it++)
465
const char *valid_string= strings()[it];
467
result= datetime.from_string(valid_string, strlen(valid_string));
468
BOOST_REQUIRE(result);
470
assignDateTimeValues();
472
BOOST_REQUIRE_EQUAL(2010, years);
473
BOOST_REQUIRE_EQUAL(6, months);
474
BOOST_REQUIRE_EQUAL(7, days);
478
BOOST_AUTO_TEST_SUITE_END()