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
#include <gtest/gtest.h>
24
#include <drizzled/type/decimal.h>
25
#include <drizzled/temporal.h>
26
#include <drizzled/temporal_format.h>
28
#include "temporal_generator.h"
30
using namespace drizzled;
32
class DateTimeTest: public ::testing::Test
37
uint32_t years, months, days;
38
uint32_t hours, minutes, seconds;
42
TemporalGenerator::DateTimeGen::make_valid_datetime(&datetime);
45
void assignDateTimeValues()
47
years= datetime.years();
48
months= datetime.months();
49
days= datetime.days();
50
hours= datetime.hours();
51
minutes= datetime.minutes();
52
seconds= datetime.seconds();
56
TEST_F(DateTimeTest, is_valid_onValidDateTime_shouldReturn_True)
58
result= datetime.is_valid();
62
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithYearBelowMinimum_shouldReturn_False)
64
datetime.set_years(DRIZZLE_MIN_YEARS_SQL - 1);
66
result= datetime.is_valid();
71
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithYearAboveMaximum_shouldReturn_False)
73
datetime.set_years(DRIZZLE_MAX_YEARS_SQL + 1);
75
result= datetime.is_valid();
80
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMonthSetToZero_shouldReturn_False)
82
datetime.set_months(0);
84
result= datetime.is_valid();
90
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMonthAboveMaximum_shouldReturn_False)
92
datetime.set_months(13);
94
result= datetime.is_valid();
99
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithDaySetToZero_shouldReturn_False)
101
datetime.set_days(0);
103
result= datetime.is_valid();
105
ASSERT_FALSE(result);
108
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithDayAboveDaysInMonth_shouldReturn_False)
110
datetime.set_days(32);
112
result= datetime.is_valid();
114
ASSERT_FALSE(result);
117
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithLeapDayInNonLeapYear_shouldReturn_False)
119
TemporalGenerator::TemporalGen::leap_day_in_non_leap_year(&datetime);
121
result= datetime.is_valid();
123
ASSERT_FALSE(result);
126
TEST_F(DateTimeTest, is_valid_onValidDateTimeWithLeapDayInLeapYear_shouldReturn_True)
128
TemporalGenerator::TemporalGen::leap_day_in_leap_year(&datetime);
130
result= datetime.is_valid();
135
TEST_F(DateTimeTest, is_valid_onValidMinimalTime_shouldReturn_True)
137
TemporalGenerator::TemporalGen::make_min_time(&datetime);
139
result= datetime.is_valid();
144
TEST_F(DateTimeTest, is_valid_onValidMaximalTime_shouldReturn_True)
146
TemporalGenerator::TemporalGen::make_max_time(&datetime);
148
result= datetime.is_valid();
153
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithHourAboveMaximum23_shouldReturn_False)
155
datetime.set_hours(24);
157
result= datetime.is_valid();
159
ASSERT_FALSE(result);
162
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMinutesAboveMaximum59_shouldReturn_False)
164
datetime.set_minutes(60);
166
result= datetime.is_valid();
168
ASSERT_FALSE(result);
171
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithSecondsAboveMaximum61_shouldReturn_False)
173
datetime.set_seconds(62);
175
result= datetime.is_valid();
177
ASSERT_FALSE(result);
180
TEST_F(DateTimeTest, to_string_shouldProduce_hyphenSeperatedDateElements_and_colonSeperatedTimeElements)
182
char expected[DateTime::MAX_STRING_LENGTH]= "2010-05-01 08:07:06.123456";
183
char returned[DateTime::MAX_STRING_LENGTH];
184
TemporalGenerator::DateTimeGen::make_datetime(&datetime, 2010, 5, 1, 8, 7, 6, 123456);
186
datetime.to_string(returned, DateTime::MAX_STRING_LENGTH);
188
ASSERT_STREQ(expected, returned);
191
TEST_F(DateTimeTest, to_string_nullBuffer_noMicroSeconds_shouldReturnProperLengthAnyway)
193
int length= datetime.to_string(NULL, 0);
195
ASSERT_EQ(DateTime::MAX_STRING_LENGTH - 1 - 7, length);
198
TEST_F(DateTimeTest, to_int64_t)
200
TemporalGenerator::DateTimeGen::make_datetime(&datetime, 2030, 8, 7, 14, 5, 13);
201
int64_t representation;
203
datetime.to_int64_t(&representation);
205
ASSERT_EQ(20300807140513LL, representation);
208
TEST_F(DateTimeTest, from_int64_t_no_conversion_format_YYYYMMDDHHMMSSshouldPopulateDateTimeCorrectly)
210
datetime.from_int64_t(20300807140513LL, false);
212
assignDateTimeValues();
214
EXPECT_EQ(2030, years);
215
EXPECT_EQ(8, months);
217
EXPECT_EQ(14, hours);
218
EXPECT_EQ(5, minutes);
219
EXPECT_EQ(13, seconds);
222
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearOver2000)
224
datetime.from_int64_t(20300807140513LL, true);
226
assignDateTimeValues();
228
EXPECT_EQ(2030, years);
229
EXPECT_EQ(8, months);
231
EXPECT_EQ(14, hours);
232
EXPECT_EQ(5, minutes);
233
EXPECT_EQ(13, seconds);
236
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearBelow2000)
238
datetime.from_int64_t(19900807140513LL, true);
240
assignDateTimeValues();
242
EXPECT_EQ(1990, years);
243
EXPECT_EQ(8, months);
245
EXPECT_EQ(14, hours);
246
EXPECT_EQ(5, minutes);
247
EXPECT_EQ(13, seconds);
250
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearOver2000)
252
datetime.from_int64_t(300807140513LL, true);
254
assignDateTimeValues();
256
EXPECT_EQ(2030, years);
257
EXPECT_EQ(8, months);
259
EXPECT_EQ(14, hours);
260
EXPECT_EQ(5, minutes);
261
EXPECT_EQ(13, seconds);
264
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearBelow2000)
266
datetime.from_int64_t(900807140513LL, true);
268
assignDateTimeValues();
270
EXPECT_EQ(1990, years);
271
EXPECT_EQ(8, months);
273
EXPECT_EQ(14, hours);
274
EXPECT_EQ(5, minutes);
275
EXPECT_EQ(13, seconds);
278
TEST_F(DateTimeTest, DISABLED_to_tm)
280
years= 2030, months= 8, days= 17, hours= 14, minutes= 45, seconds= 13;
281
TemporalGenerator::DateTimeGen::make_datetime(&datetime, years, months, days, hours, minutes, seconds);
284
datetime.to_tm(&filled);
286
EXPECT_EQ(2030 - 1900, filled.tm_year);
287
EXPECT_EQ(8 - 1, filled.tm_mon);
288
EXPECT_EQ(17, filled.tm_mday);
289
EXPECT_EQ(14, filled.tm_hour);
290
EXPECT_EQ(45, filled.tm_min);
291
EXPECT_EQ(13, filled.tm_sec);
293
/* TODO:these fail, shouldn't they also be set properly? */
294
EXPECT_EQ(228, filled.tm_yday);
295
EXPECT_EQ(6, filled.tm_wday);
296
EXPECT_EQ(-1, filled.tm_isdst);
299
TEST_F(DateTimeTest, to_decimal)
301
drizzled::type::Decimal to;
302
TemporalGenerator::DateTimeGen::make_datetime(&datetime, 1987, 6, 13, 5, 10, 13, 456);
304
datetime.to_decimal(&to);
306
EXPECT_EQ(19870,to.buf[0]);
307
EXPECT_EQ(613051013,to.buf[1]);
308
EXPECT_EQ(456000,to.buf[2]);
313
class DateTimeFromStringTest
318
uint32_t years, months, days;
319
uint32_t hours, minutes, seconds;
323
init_temporal_formats();
328
deinit_temporal_formats();
331
void assignDateTimeValues()
333
years= datetime.years();
334
months= datetime.months();
335
days= datetime.days();
336
hours= datetime.hours();
337
minutes= datetime.minutes();
338
seconds= datetime.seconds();
342
class DateTimeFromStringFullFormatTest: public ::testing::TestWithParam<const char*>, public DateTimeFromStringTest
349
virtual void TearDown()
355
class DateTimeFromStringNoSecondFormatTest: public ::testing::TestWithParam<const char*>, public DateTimeFromStringTest
362
virtual void TearDown()
368
class DateTimeFromStringDateOnlyTest: public ::testing::TestWithParam<const char*>, public DateTimeFromStringTest
375
virtual void TearDown()
381
TEST_P(DateTimeFromStringFullFormatTest, from_string_validString)
383
const char *valid_string= GetParam();
385
result= datetime.from_string(valid_string, strlen(valid_string));
388
assignDateTimeValues();
390
EXPECT_EQ(2010, years);
391
EXPECT_EQ(5, months);
394
EXPECT_EQ(7, minutes);
395
EXPECT_EQ(6, seconds);
397
/* TODO:for some reason this was not declared by the macro, needs clarification*/
398
testing::internal::ParamGenerator<const char*> gtest_ValidStringDateTimeFromStringFullFormatTest_EvalGenerator_();
399
INSTANTIATE_TEST_CASE_P(ValidString, DateTimeFromStringFullFormatTest,
400
::testing::Values("20100501080706",
401
"2010-05-01 08:07:06",
402
"2010/05/01T08:07:06",
409
TEST_P(DateTimeFromStringNoSecondFormatTest, from_string_validString)
411
const char *valid_string= GetParam();
413
result= datetime.from_string(valid_string, strlen(valid_string));
416
assignDateTimeValues();
418
EXPECT_EQ(2010, years);
419
EXPECT_EQ(5, months);
422
EXPECT_EQ(7, minutes);
425
/* TODO:for some reason this was not declared by the macro, needs clarification*/
426
testing::internal::ParamGenerator<const char*> gtest_ValidStringDateTimeFromStringNoSecondFormatTest_EvalGenerator_();
427
INSTANTIATE_TEST_CASE_P(ValidString, DateTimeFromStringNoSecondFormatTest,
428
::testing::Values("2010-05-01 08:07",
437
TEST_P(DateTimeFromStringDateOnlyTest, from_string_validString)
439
const char *valid_string= GetParam();
441
result= datetime.from_string(valid_string, strlen(valid_string));
444
assignDateTimeValues();
446
EXPECT_EQ(2010, years);
447
EXPECT_EQ(6, months);
451
/* TODO:for some reason this was not declared by the macro, needs clarification*/
452
testing::internal::ParamGenerator<const char*> gtest_ValidStringDateTimeFromStringDateOnlyTest_EvalGenerator_();
453
INSTANTIATE_TEST_CASE_P(ValidString, DateTimeFromStringDateOnlyTest,
454
::testing::Values("20100607", /* YYYYMMDD */
455
"06/07/2010",/* MM[-/.]DD[-/.]YYYY (US common format)*/
456
"10.06.07",/* YY[-/.]MM[-/.]DD */
457
"10/6/7",/* YY[-/.][M]M[-/.][D]D */
458
"2010-6-7"/* YYYY[-/.][M]M[-/.][D]D */));