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 <boost/shared_ptr.hpp>
26
#include <gtest/gtest.h>
27
#include <drizzled/item.h>
28
#include <drizzled/session.h>
29
#include <drizzled/sql_string.h>
30
#include <drizzled/temporal_interval.h>
31
#include <drizzled/plugin/listen.h>
32
#include <drizzled/drizzled.h>
34
#include "temporal_generator.h"
36
using namespace drizzled;
38
class ItemStub : public Item
40
ItemStub(Session *fake_session) : Item(fake_session, this)
42
string_to_return= NULL;
47
String *string_to_return;
50
static boost::shared_ptr<ItemStub> get_item_stub(Session *fake_session)
52
boost::shared_ptr<ItemStub> result(new ItemStub(fake_session));
56
virtual enum Type type() const { return FIELD_ITEM; };
57
virtual double val_real() { return 0; };
58
virtual int64_t val_int() { return int_to_return; };
59
virtual String *val_str(String *str)
62
return string_to_return;
64
virtual type::Decimal *val_decimal(type::Decimal *decimal_buffer)
66
(void) decimal_buffer;
71
class TemporalIntervalTest : public ::testing::Test
74
String string_to_return;
77
boost::shared_ptr<ItemStub> item;
78
TemporalInterval interval;
79
Session *fake_session;
81
plugin::Client *fake_client;
85
init_thread_environment();
86
fake_client= plugin::Listen::getNullClient();
87
fake_session= new Session(fake_client);
88
fake_session->thread_stack= (char*) &fake_session;
89
fake_session->initGlobals();
90
item= ItemStub::get_item_stub(fake_session);
93
/*28 for full date and time format with sign*/
94
string_to_return.alloc(28);
96
item->string_to_return= &string_to_return;
97
item->int_to_return= 0;
98
item->null_value= false;
102
TEST_F(TemporalIntervalTest, initFromItem_intervalWeek)
105
item->string_to_return->set_ascii(string, strlen(string));
106
item->int_to_return= 30;
108
interval.initFromItem(item.get(), INTERVAL_WEEK, &buffer);
110
ASSERT_EQ(210, interval.get_day());
113
TEST_F(TemporalIntervalTest, initFromItem_intervalDayMicrosecond)
115
char string[]= "7 12:45:19.777";
116
item->string_to_return->set_ascii(string, strlen(string));
118
interval.initFromItem(item.get(), INTERVAL_DAY_MICROSECOND, &buffer);
120
EXPECT_EQ(7, interval.get_day());
121
EXPECT_EQ(12, interval.get_hour());
122
EXPECT_EQ(45, interval.get_minute());
123
EXPECT_EQ(19, interval.get_second());
124
EXPECT_EQ(777000, interval.get_second_part());
127
TEST_F(TemporalIntervalTest, initFromItem_intervalDayMicrosecond_tooFewArguments_shouldOmitHighEndItems)
129
char string[]= "45:19.777";
130
item->string_to_return->set_ascii(string, strlen(string));
132
interval.initFromItem(item.get(), INTERVAL_DAY_MICROSECOND, &buffer);
134
EXPECT_EQ(0, interval.get_day());
135
EXPECT_EQ(0, interval.get_hour());
136
EXPECT_EQ(45, interval.get_minute());
137
EXPECT_EQ(19, interval.get_second());
138
EXPECT_EQ(777000, interval.get_second_part());
142
TEST(TemporalIntervalAddDateTest, addDate_positiveDayMicrosecond)
144
type::Time drizzle_time= {1990, 3, 25, 15, 5, 16, 876543, false, DRIZZLE_TIMESTAMP_DATETIME};
145
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
146
0, 0, 6, 13, 54, 3, 435675, false);
148
interval->addDate(&drizzle_time, INTERVAL_DAY_MICROSECOND);
150
EXPECT_EQ(1990, drizzle_time.year);
151
EXPECT_EQ(4, drizzle_time.month);
152
EXPECT_EQ(1, drizzle_time.day);
153
EXPECT_EQ(4, drizzle_time.hour);
154
EXPECT_EQ(59, drizzle_time.minute);
155
EXPECT_EQ(20, drizzle_time.second);
156
EXPECT_EQ(312218, drizzle_time.second_part);
159
TEST(TemporalIntervalAddDateTest, addDate_negativeDayMicrosecond)
161
type::Time drizzle_time= {1990, 4, 1, 4, 59, 20, 312218, false, DRIZZLE_TIMESTAMP_DATETIME};
162
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
163
0, 0, 6, 13, 54, 3, 435675, true);
165
interval->addDate(&drizzle_time, INTERVAL_DAY_MICROSECOND);
167
EXPECT_EQ(1990, drizzle_time.year);
168
EXPECT_EQ(3, drizzle_time.month);
169
EXPECT_EQ(25, drizzle_time.day);
170
EXPECT_EQ(15, drizzle_time.hour);
171
EXPECT_EQ(5, drizzle_time.minute);
172
EXPECT_EQ(16, drizzle_time.second);
173
EXPECT_EQ(876543, drizzle_time.second_part);
176
TEST(TemporalIntervalAddDateTest, addDate_positiveDayMicrosecond_shouldCountLeapDayToo)
178
type::Time drizzle_time= {2004, 2, 25, 15, 5, 16, 876543, false, DRIZZLE_TIMESTAMP_DATETIME};
179
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
180
0, 0, 6, 13, 54, 3, 435675, false);
182
interval->addDate(&drizzle_time, INTERVAL_DAY_MICROSECOND);
184
EXPECT_EQ(2004, drizzle_time.year);
185
EXPECT_EQ(3, drizzle_time.month);
186
EXPECT_EQ(3, drizzle_time.day);
187
EXPECT_EQ(4, drizzle_time.hour);
188
EXPECT_EQ(59, drizzle_time.minute);
189
EXPECT_EQ(20, drizzle_time.second);
190
EXPECT_EQ(312218, drizzle_time.second_part);
193
TEST(TemporalIntervalAddDateTest, addDate_negativeWeek)
195
type::Time drizzle_time= {1998, 1, 25, 0, 0, 0, 0, false, DRIZZLE_TIMESTAMP_DATE};
196
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
197
0, 0, 28, 0, 0, 0, 0, true);
199
interval->addDate(&drizzle_time, INTERVAL_WEEK);
201
EXPECT_EQ(1997, drizzle_time.year);
202
EXPECT_EQ(12, drizzle_time.month);
203
EXPECT_EQ(28, drizzle_time.day);
206
TEST(TemporalIntervalAddDateTest, addDate_addPositiveYearToLeapDay)
208
type::Time drizzle_time= {2004, 2, 29, 0, 0, 0, 0, false, DRIZZLE_TIMESTAMP_DATE};
209
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
210
5, 0, 0, 0, 0, 0, 0, false);
212
interval->addDate(&drizzle_time, INTERVAL_YEAR);
214
EXPECT_EQ(2009, drizzle_time.year);
215
EXPECT_EQ(2, drizzle_time.month);
216
EXPECT_EQ(28, drizzle_time.day);
219
TEST(TemporalIntervalAddDateTest, addDate_addOneMonthToLastDayInMonth_shouldChangeToProperLastDay)
221
type::Time drizzle_time= {2004, 7, 31, 0, 0, 0, 0, false, DRIZZLE_TIMESTAMP_DATE};
222
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
223
0, 2, 0, 0, 0, 0, 0, false);
225
interval->addDate(&drizzle_time, INTERVAL_MONTH);
227
EXPECT_EQ(2004, drizzle_time.year);
228
EXPECT_EQ(9, drizzle_time.month);
229
EXPECT_EQ(30, drizzle_time.day);