~drizzle-trunk/drizzle/development

1377.8.20 by Paweł Blokus
moved shared generators to Temporal
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
21
#include "config.h"
22
23
#include <gtest/gtest.h>
1377.8.27 by Paweł Blokus
tests for to_decimal methods
24
#include <drizzled/decimal.h>
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
25
#include <drizzled/temporal.h>
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
26
#include <drizzled/temporal_format.h>
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
27
1377.9.1 by Paweł Blokus
updated includes to last file rename
28
#include "temporal_generator.h"
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
29
30
using namespace drizzled;
31
32
class DateTimeTest: public ::testing::Test
33
{
34
  protected:
35
    DateTime datetime;
36
    bool result;
1377.8.26 by Paweł Blokus
created or modified all to_int**_t and from_int**_t function tests
37
    uint32_t years, months, days;
38
    uint32_t hours, minutes, seconds;
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
39
    
40
    virtual void SetUp()
41
    {
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
42
      TemporalGenerator::DateTimeGen::make_valid_datetime(&datetime);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
43
    }
1377.8.26 by Paweł Blokus
created or modified all to_int**_t and from_int**_t function tests
44
45
    void assignDateTimeValues()
46
    {
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
47
      years= datetime.years();
48
      months= datetime.months();
49
      days= datetime.days();
50
      hours= datetime.hours();
51
      minutes= datetime.minutes();
52
      seconds= datetime.seconds();
1377.8.26 by Paweł Blokus
created or modified all to_int**_t and from_int**_t function tests
53
    }
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
54
};
55
56
TEST_F(DateTimeTest, is_valid_onValidDateTime_shouldReturn_True)
57
{
58
  result= datetime.is_valid();
59
  ASSERT_TRUE(result);
60
}
61
62
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithYearBelowMinimum_shouldReturn_False)
63
{
64
  datetime.set_years(DRIZZLE_MIN_YEARS_SQL - 1);
65
  
66
  result= datetime.is_valid();
67
  
68
  ASSERT_FALSE(result);
69
}
70
71
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithYearAboveMaximum_shouldReturn_False)
72
{
73
  datetime.set_years(DRIZZLE_MAX_YEARS_SQL + 1);
74
    
75
  result= datetime.is_valid();
76
  
77
  ASSERT_FALSE(result);
78
}
79
80
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMonthSetToZero_shouldReturn_False)
81
{
82
  datetime.set_months(0);
83
  
84
  result= datetime.is_valid();
85
  
86
  ASSERT_FALSE(result);
87
}
88
89
90
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMonthAboveMaximum_shouldReturn_False)
91
{
92
  datetime.set_months(13);
93
  
94
  result= datetime.is_valid();
95
  
96
  ASSERT_FALSE(result);
97
}
98
99
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithDaySetToZero_shouldReturn_False)
100
{
101
  datetime.set_days(0);
102
  
103
  result= datetime.is_valid();
104
  
105
  ASSERT_FALSE(result);
106
}
107
108
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithDayAboveDaysInMonth_shouldReturn_False)
109
{
110
  datetime.set_days(32);
111
  
112
  result= datetime.is_valid();
113
  
114
  ASSERT_FALSE(result);
115
}
116
117
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithLeapDayInNonLeapYear_shouldReturn_False)
118
{
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
119
  TemporalGenerator::TemporalGen::leap_day_in_non_leap_year(&datetime);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
120
  
121
  result= datetime.is_valid();
122
  
123
  ASSERT_FALSE(result);
124
}
125
126
TEST_F(DateTimeTest, is_valid_onValidDateTimeWithLeapDayInLeapYear_shouldReturn_True)
127
{
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
128
  TemporalGenerator::TemporalGen::leap_day_in_leap_year(&datetime);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
129
  
130
  result= datetime.is_valid();
131
  
132
  ASSERT_TRUE(result);
133
}
134
135
TEST_F(DateTimeTest, is_valid_onValidMinimalTime_shouldReturn_True)
136
{
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
137
  TemporalGenerator::TemporalGen::make_min_time(&datetime);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
138
  
139
  result= datetime.is_valid();
140
  
141
  ASSERT_TRUE(result);
142
}
143
144
TEST_F(DateTimeTest, is_valid_onValidMaximalTime_shouldReturn_True)
145
{
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
146
  TemporalGenerator::TemporalGen::make_max_time(&datetime);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
147
  
148
  result= datetime.is_valid();
149
  
150
  ASSERT_TRUE(result);
151
}
152
153
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithHourAboveMaximum23_shouldReturn_False)
154
{
155
  datetime.set_hours(24);
156
  
157
  result= datetime.is_valid();
158
  
159
  ASSERT_FALSE(result);
160
}
161
162
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMinutesAboveMaximum59_shouldReturn_False)
163
{
164
  datetime.set_minutes(60);
165
  
166
  result= datetime.is_valid();
167
  
168
  ASSERT_FALSE(result);
169
}
170
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
171
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithSecondsAboveMaximum61_shouldReturn_False)
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
172
{
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
173
  datetime.set_seconds(62);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
174
  
175
  result= datetime.is_valid();
176
  
177
  ASSERT_FALSE(result);
178
}
179
180
TEST_F(DateTimeTest, to_string_shouldProduce_hyphenSeperatedDateElements_and_colonSeperatedTimeElements)
181
{
1377.8.34 by Paweł Blokus
take microsedonds into account where they are used in datetime and time
182
  char expected[DateTime::MAX_STRING_LENGTH]= "2010-05-01 08:07:06.123456";
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
183
  char returned[DateTime::MAX_STRING_LENGTH];
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
184
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, 2010, 5, 1, 8, 7, 6, 123456);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
185
  
186
  datetime.to_string(returned, DateTime::MAX_STRING_LENGTH);
187
  
1377.8.34 by Paweł Blokus
take microsedonds into account where they are used in datetime and time
188
  ASSERT_STREQ(expected, returned);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
189
}
190
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
191
TEST_F(DateTimeTest, to_string_nullBuffer_noMicroSeconds_shouldReturnProperLengthAnyway)
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
192
{
193
  int length= datetime.to_string(NULL, 0);
194
  
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
195
  ASSERT_EQ(DateTime::MAX_STRING_LENGTH - 1 - 7, length);  
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
196
}
197
1377.8.26 by Paweł Blokus
created or modified all to_int**_t and from_int**_t function tests
198
TEST_F(DateTimeTest, to_int64_t)
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
199
{
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
200
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, 2030, 8, 7, 14, 5, 13);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
201
  int64_t representation;
1377.8.26 by Paweł Blokus
created or modified all to_int**_t and from_int**_t function tests
202
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
203
  datetime.to_int64_t(&representation);
1377.8.26 by Paweł Blokus
created or modified all to_int**_t and from_int**_t function tests
204
205
  ASSERT_EQ(20300807140513LL, representation);
206
}
207
208
TEST_F(DateTimeTest, from_int64_t_no_conversion_format_YYYYMMDDHHMMSSshouldPopulateDateTimeCorrectly)
209
{
210
  datetime.from_int64_t(20300807140513LL, false);
211
  
212
  assignDateTimeValues();
213
  
214
  EXPECT_EQ(2030, years);
215
  EXPECT_EQ(8, months);
216
  EXPECT_EQ(7, days);
217
  EXPECT_EQ(14, hours);
218
  EXPECT_EQ(5, minutes);
219
  EXPECT_EQ(13, seconds);
220
}
221
222
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearOver2000)
223
{
224
  datetime.from_int64_t(20300807140513LL, true);
225
  
226
  assignDateTimeValues();
227
  
228
  EXPECT_EQ(2030, years);
229
  EXPECT_EQ(8, months);
230
  EXPECT_EQ(7, days);
231
  EXPECT_EQ(14, hours);
232
  EXPECT_EQ(5, minutes);
233
  EXPECT_EQ(13, seconds);
234
}
235
236
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearBelow2000)
237
{
238
  datetime.from_int64_t(19900807140513LL, true);
239
  
240
  assignDateTimeValues();
241
  
242
  EXPECT_EQ(1990, years);
243
  EXPECT_EQ(8, months);
244
  EXPECT_EQ(7, days);
245
  EXPECT_EQ(14, hours);
246
  EXPECT_EQ(5, minutes);
247
  EXPECT_EQ(13, seconds);
248
}
249
250
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearOver2000)
251
{
252
  datetime.from_int64_t(300807140513LL, true);
253
  
254
  assignDateTimeValues();
255
  
256
  EXPECT_EQ(2030, years);
257
  EXPECT_EQ(8, months);
258
  EXPECT_EQ(7, days);
259
  EXPECT_EQ(14, hours);
260
  EXPECT_EQ(5, minutes);
261
  EXPECT_EQ(13, seconds);
262
}
263
264
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearBelow2000)
265
{
266
  datetime.from_int64_t(900807140513LL, true);
267
  
268
  assignDateTimeValues();
269
  
270
  EXPECT_EQ(1990, years);
271
  EXPECT_EQ(8, months);
272
  EXPECT_EQ(7, days);
273
  EXPECT_EQ(14, hours);
274
  EXPECT_EQ(5, minutes);
275
  EXPECT_EQ(13, seconds);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
276
}
277
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
278
TEST_F(DateTimeTest, DISABLED_to_tm)
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
279
{
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
280
  years= 2030, months= 8, days= 17, hours= 14, minutes= 45, seconds= 13;
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
281
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, years, months, days, hours, minutes, seconds);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
282
  struct tm filled;
283
  
284
  datetime.to_tm(&filled);
285
  
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
286
  EXPECT_EQ(2030 - 1900, filled.tm_year);
287
  EXPECT_EQ(8 - 1, filled.tm_mon);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
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);
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
292
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
293
  /* TODO:these fail, shouldn't they also be set properly? */
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
294
  EXPECT_EQ(228, filled.tm_yday);
295
  EXPECT_EQ(6, filled.tm_wday);
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
296
  EXPECT_EQ(-1, filled.tm_isdst);
1377.8.20 by Paweł Blokus
moved shared generators to Temporal
297
}
1377.8.27 by Paweł Blokus
tests for to_decimal methods
298
299
TEST_F(DateTimeTest, to_decimal)
300
{
301
  drizzled::my_decimal to;
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
302
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, 1987, 6, 13, 5, 10, 13, 456);
1377.8.27 by Paweł Blokus
tests for to_decimal methods
303
304
  datetime.to_decimal(&to);
305
  
1377.8.34 by Paweł Blokus
take microsedonds into account where they are used in datetime and time
306
  EXPECT_EQ(19870,to.buf[0]);
307
  EXPECT_EQ(613051013,to.buf[1]);
308
  EXPECT_EQ(456000,to.buf[2]);
1377.8.27 by Paweł Blokus
tests for to_decimal methods
309
}
310
311
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
312
313
class DateTimeFromStringTest
1377.8.27 by Paweł Blokus
tests for to_decimal methods
314
{
315
  protected:
316
    DateTime datetime;
317
    bool result;
318
    uint32_t years, months, days;
319
    uint32_t hours, minutes, seconds;
320
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
321
    void init()
1377.8.27 by Paweł Blokus
tests for to_decimal methods
322
    {
323
      init_temporal_formats();
324
    }
325
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
326
    void deinit()
1377.8.27 by Paweł Blokus
tests for to_decimal methods
327
    {
328
      deinit_temporal_formats();
329
    }
330
331
    void assignDateTimeValues()
332
    {
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
333
      years= datetime.years();
334
      months= datetime.months();
335
      days= datetime.days();
336
      hours= datetime.hours();
337
      minutes= datetime.minutes();
338
      seconds= datetime.seconds();
1377.8.27 by Paweł Blokus
tests for to_decimal methods
339
    }
340
};
341
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
342
class DateTimeFromStringFullFormatTest: public ::testing::TestWithParam<const char*>, public DateTimeFromStringTest
343
{
344
  virtual void SetUp()
345
  {
346
    init();
347
  }
348
  
349
  virtual void TearDown()
350
  {
351
    deinit();
352
  }  
353
};
354
355
class DateTimeFromStringNoSecondFormatTest: public ::testing::TestWithParam<const char*>, public DateTimeFromStringTest
356
{
357
  virtual void SetUp()
358
  {
359
    init();
360
  }
361
  
362
  virtual void TearDown()
363
  {
364
    deinit();
365
  }
366
};
367
368
class DateTimeFromStringDateOnlyTest: public ::testing::TestWithParam<const char*>, public DateTimeFromStringTest
369
{
370
  virtual void SetUp()
371
  {
372
    init();
373
  }
374
  
375
  virtual void TearDown()
376
  {
377
    deinit();
378
  }
379
};
380
381
TEST_P(DateTimeFromStringFullFormatTest, from_string_validString)
1377.8.27 by Paweł Blokus
tests for to_decimal methods
382
{
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
383
  const char *valid_string= GetParam();
1377.8.27 by Paweł Blokus
tests for to_decimal methods
384
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
385
  result= datetime.from_string(valid_string, strlen(valid_string));
1377.8.27 by Paweł Blokus
tests for to_decimal methods
386
  ASSERT_TRUE(result);
387
388
  assignDateTimeValues();
389
390
  EXPECT_EQ(2010, years);
391
  EXPECT_EQ(5, months);
392
  EXPECT_EQ(1, days);
393
  EXPECT_EQ(8, hours);
394
  EXPECT_EQ(7, minutes);
395
  EXPECT_EQ(6, seconds);
396
}
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
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,
1377.8.27 by Paweł Blokus
tests for to_decimal methods
400
                        ::testing::Values("20100501080706",
401
                                          "2010-05-01 08:07:06",
402
                                          "2010/05/01T08:07:06",
403
                                          "2010.5.1 08:07:06",
404
                                          "10-05-01 08:07:06",
405
                                          "10/5/1 08:07:06",
406
                                          "10.5.1 08:07:06"));
407
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
408
                                          
409
TEST_P(DateTimeFromStringNoSecondFormatTest, from_string_validString)
1377.8.27 by Paweł Blokus
tests for to_decimal methods
410
{
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
411
  const char *valid_string= GetParam();
1377.8.27 by Paweł Blokus
tests for to_decimal methods
412
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
413
  result= datetime.from_string(valid_string, strlen(valid_string));
1377.8.27 by Paweł Blokus
tests for to_decimal methods
414
  ASSERT_TRUE(result);
415
416
  assignDateTimeValues();
417
418
  EXPECT_EQ(2010, years);
419
  EXPECT_EQ(5, months);
420
  EXPECT_EQ(1, days);
421
  EXPECT_EQ(8, hours);
422
  EXPECT_EQ(7, minutes);
423
}
424
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
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,
1377.8.27 by Paweł Blokus
tests for to_decimal methods
428
                        ::testing::Values("2010-05-01 08:07",
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
429
                                          "2010/05/01 08:07",
1377.8.27 by Paweł Blokus
tests for to_decimal methods
430
                                          "2010.5.1 08:07",
431
                                          "10-05-01 08:07",
432
                                          "10/5/1 08:07",
433
                                          "10.5.1 08:07"));
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
434
435
436
437
TEST_P(DateTimeFromStringDateOnlyTest, from_string_validString)
438
{
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
439
  const char *valid_string= GetParam();
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
440
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
441
  result= datetime.from_string(valid_string, strlen(valid_string));
1377.8.33 by Paweł Blokus
fix/hack? which makes parameterized tests compile
442
  ASSERT_TRUE(result);
443
444
  assignDateTimeValues();
445
446
  EXPECT_EQ(2010, years);
447
  EXPECT_EQ(6, months);
448
  EXPECT_EQ(7, days);
449
}
450
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 */));
459
460