1377.8.2
by Paweł Blokus
created files for temporal tests |
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 |
||
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
23 |
#include <boost/shared_ptr.hpp> |
24 |
||
25 |
#include <cstdlib> |
|
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> |
|
1623
by Monty Taylor
Fixed a non-initialized issue on OSX. |
32 |
#include <drizzled/drizzled.h> |
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
33 |
|
1377.9.1
by Paweł Blokus
updated includes to last file rename |
34 |
#include "temporal_generator.h" |
1377.8.32
by Paweł Blokus
setters to TemporalInterval |
35 |
|
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
36 |
using namespace drizzled; |
1377.8.31
by Paweł Blokus
little fix in include.am |
37 |
|
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
38 |
class ItemStub : public Item |
39 |
{
|
|
40 |
ItemStub(Session *fake_session) : Item(fake_session, this) |
|
41 |
{
|
|
1377.8.35
by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test |
42 |
string_to_return= NULL; |
43 |
int_to_return= 0; |
|
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
44 |
}
|
45 |
||
46 |
public: |
|
47 |
String *string_to_return; |
|
48 |
int int_to_return; |
|
49 |
||
50 |
static boost::shared_ptr<ItemStub> get_item_stub(Session *fake_session) |
|
51 |
{
|
|
52 |
boost::shared_ptr<ItemStub> result(new ItemStub(fake_session)); |
|
53 |
return result; |
|
54 |
}
|
|
55 |
||
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) |
|
60 |
{
|
|
61 |
(void) str; |
|
62 |
return string_to_return; |
|
63 |
};
|
|
2030.1.4
by Brian Aker
Change my_decimal to Decimal |
64 |
virtual type::Decimal *val_decimal(type::Decimal *decimal_buffer) |
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
65 |
{
|
66 |
(void) decimal_buffer; |
|
67 |
return NULL; |
|
68 |
};
|
|
69 |
};
|
|
70 |
||
71 |
class TemporalIntervalTest : public ::testing::Test |
|
72 |
{
|
|
73 |
protected: |
|
74 |
String string_to_return; |
|
75 |
String buffer; |
|
76 |
bool result; |
|
77 |
boost::shared_ptr<ItemStub> item; |
|
78 |
TemporalInterval interval; |
|
79 |
Session *fake_session; |
|
80 |
||
81 |
plugin::Client *fake_client; |
|
82 |
||
83 |
virtual void SetUp() |
|
84 |
{
|
|
1623
by Monty Taylor
Fixed a non-initialized issue on OSX. |
85 |
init_thread_environment(); |
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
86 |
fake_client= plugin::Listen::getNullClient(); |
1377.8.35
by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test |
87 |
fake_session= new Session(fake_client); |
1377.8.31
by Paweł Blokus
little fix in include.am |
88 |
fake_session->thread_stack= (char*) &fake_session; |
89 |
fake_session->initGlobals(); |
|
1377.8.35
by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test |
90 |
item= ItemStub::get_item_stub(fake_session); |
91 |
||
92 |
||
93 |
/*28 for full date and time format with sign*/
|
|
94 |
string_to_return.alloc(28); |
|
95 |
buffer.alloc(28); |
|
96 |
item->string_to_return= &string_to_return; |
|
97 |
item->int_to_return= 0; |
|
98 |
item->null_value= false; |
|
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
99 |
}
|
100 |
};
|
|
101 |
||
102 |
TEST_F(TemporalIntervalTest, initFromItem_intervalWeek) |
|
103 |
{
|
|
1377.8.35
by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test |
104 |
char string[]= "30"; |
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
105 |
item->string_to_return->set_ascii(string, strlen(string)); |
1377.8.35
by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test |
106 |
item->int_to_return= 30; |
1377.8.29
by Paweł Blokus
tests for init/deinit_temporal_formats |
107 |
|
108 |
interval.initFromItem(item.get(), INTERVAL_WEEK, &buffer); |
|
109 |
||
110 |
ASSERT_EQ(210, interval.get_day()); |
|
111 |
}
|
|
1377.8.31
by Paweł Blokus
little fix in include.am |
112 |
|
113 |
TEST_F(TemporalIntervalTest, initFromItem_intervalDayMicrosecond) |
|
114 |
{
|
|
1377.8.35
by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test |
115 |
char string[]= "7 12:45:19.777"; |
1377.8.31
by Paweł Blokus
little fix in include.am |
116 |
item->string_to_return->set_ascii(string, strlen(string)); |
117 |
||
118 |
interval.initFromItem(item.get(), INTERVAL_DAY_MICROSECOND, &buffer); |
|
119 |
||
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()); |
|
125 |
}
|
|
126 |
||
1377.8.32
by Paweł Blokus
setters to TemporalInterval |
127 |
TEST_F(TemporalIntervalTest, initFromItem_intervalDayMicrosecond_tooFewArguments_shouldOmitHighEndItems) |
1377.8.31
by Paweł Blokus
little fix in include.am |
128 |
{
|
1377.8.35
by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test |
129 |
char string[]= "45:19.777"; |
1377.8.31
by Paweł Blokus
little fix in include.am |
130 |
item->string_to_return->set_ascii(string, strlen(string)); |
131 |
||
132 |
interval.initFromItem(item.get(), INTERVAL_DAY_MICROSECOND, &buffer); |
|
133 |
||
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()); |
|
139 |
}
|
|
140 |
||
1377.8.32
by Paweł Blokus
setters to TemporalInterval |
141 |
|
142 |
TEST(TemporalIntervalAddDateTest, addDate_positiveDayMicrosecond) |
|
143 |
{
|
|
2030.1.5
by Brian Aker
Update for moving DRIZZLE_TIME to type::Time |
144 |
type::Time drizzle_time= {1990, 3, 25, 15, 5, 16, 876543, false, DRIZZLE_TIMESTAMP_DATETIME}; |
1377.8.36
by Paweł Blokus
changed Generator to TemporalGenerator |
145 |
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval( |
1377.8.32
by Paweł Blokus
setters to TemporalInterval |
146 |
0, 0, 6, 13, 54, 3, 435675, false); |
147 |
||
148 |
interval->addDate(&drizzle_time, INTERVAL_DAY_MICROSECOND); |
|
149 |
||
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); |
|
157 |
}
|
|
158 |
||
159 |
TEST(TemporalIntervalAddDateTest, addDate_negativeDayMicrosecond) |
|
160 |
{
|
|
2030.1.5
by Brian Aker
Update for moving DRIZZLE_TIME to type::Time |
161 |
type::Time drizzle_time= {1990, 4, 1, 4, 59, 20, 312218, false, DRIZZLE_TIMESTAMP_DATETIME}; |
1377.8.36
by Paweł Blokus
changed Generator to TemporalGenerator |
162 |
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval( |
1377.8.32
by Paweł Blokus
setters to TemporalInterval |
163 |
0, 0, 6, 13, 54, 3, 435675, true); |
164 |
||
165 |
interval->addDate(&drizzle_time, INTERVAL_DAY_MICROSECOND); |
|
166 |
||
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); |
|
174 |
}
|
|
175 |
||
176 |
TEST(TemporalIntervalAddDateTest, addDate_positiveDayMicrosecond_shouldCountLeapDayToo) |
|
177 |
{
|
|
2030.1.5
by Brian Aker
Update for moving DRIZZLE_TIME to type::Time |
178 |
type::Time drizzle_time= {2004, 2, 25, 15, 5, 16, 876543, false, DRIZZLE_TIMESTAMP_DATETIME}; |
1377.8.36
by Paweł Blokus
changed Generator to TemporalGenerator |
179 |
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval( |
1377.8.32
by Paweł Blokus
setters to TemporalInterval |
180 |
0, 0, 6, 13, 54, 3, 435675, false); |
181 |
||
182 |
interval->addDate(&drizzle_time, INTERVAL_DAY_MICROSECOND); |
|
183 |
||
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); |
|
191 |
}
|
|
192 |
||
193 |
TEST(TemporalIntervalAddDateTest, addDate_negativeWeek) |
|
194 |
{
|
|
2030.1.5
by Brian Aker
Update for moving DRIZZLE_TIME to type::Time |
195 |
type::Time drizzle_time= {1998, 1, 25, 0, 0, 0, 0, false, DRIZZLE_TIMESTAMP_DATE}; |
1377.8.36
by Paweł Blokus
changed Generator to TemporalGenerator |
196 |
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval( |
1377.8.32
by Paweł Blokus
setters to TemporalInterval |
197 |
0, 0, 28, 0, 0, 0, 0, true); |
198 |
||
199 |
interval->addDate(&drizzle_time, INTERVAL_WEEK); |
|
200 |
||
201 |
EXPECT_EQ(1997, drizzle_time.year); |
|
202 |
EXPECT_EQ(12, drizzle_time.month); |
|
203 |
EXPECT_EQ(28, drizzle_time.day); |
|
204 |
}
|
|
205 |
||
206 |
TEST(TemporalIntervalAddDateTest, addDate_addPositiveYearToLeapDay) |
|
207 |
{
|
|
2030.1.5
by Brian Aker
Update for moving DRIZZLE_TIME to type::Time |
208 |
type::Time drizzle_time= {2004, 2, 29, 0, 0, 0, 0, false, DRIZZLE_TIMESTAMP_DATE}; |
1377.8.36
by Paweł Blokus
changed Generator to TemporalGenerator |
209 |
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval( |
1377.8.32
by Paweł Blokus
setters to TemporalInterval |
210 |
5, 0, 0, 0, 0, 0, 0, false); |
211 |
||
212 |
interval->addDate(&drizzle_time, INTERVAL_YEAR); |
|
213 |
||
214 |
EXPECT_EQ(2009, drizzle_time.year); |
|
215 |
EXPECT_EQ(2, drizzle_time.month); |
|
216 |
EXPECT_EQ(28, drizzle_time.day); |
|
217 |
}
|
|
218 |
||
219 |
TEST(TemporalIntervalAddDateTest, addDate_addOneMonthToLastDayInMonth_shouldChangeToProperLastDay) |
|
220 |
{
|
|
2030.1.5
by Brian Aker
Update for moving DRIZZLE_TIME to type::Time |
221 |
type::Time drizzle_time= {2004, 7, 31, 0, 0, 0, 0, false, DRIZZLE_TIMESTAMP_DATE}; |
1377.8.36
by Paweł Blokus
changed Generator to TemporalGenerator |
222 |
TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval( |
1377.8.32
by Paweł Blokus
setters to TemporalInterval |
223 |
0, 2, 0, 0, 0, 0, 0, false); |
224 |
||
225 |
interval->addDate(&drizzle_time, INTERVAL_MONTH); |
|
226 |
||
227 |
EXPECT_EQ(2004, drizzle_time.year); |
|
228 |
EXPECT_EQ(9, drizzle_time.month); |
|
229 |
EXPECT_EQ(30, drizzle_time.day); |
|
230 |
}
|