17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include "mock_field_datetime.h"
21 #include "mock_field_timestamp.h"
22 #include "mock_field_timestampf.h"
23 #include "test_utils.h"
25 #include "sql_class.h"
26 #include "rpl_handler.h"
29 namespace item_func_now_local_unittest {
34 const int CURRENT_TIMESTAMP_WHOLE_SECONDS= 123456;
35 const int CURRENT_TIMESTAMP_FRACTIONAL_SECONDS= 654321;
48 CURRENT_TIMESTAMP_WHOLE_SECONDS, CURRENT_TIMESTAMP_FRACTIONAL_SECONDS
50 get_thd()->set_time(&now);
53 virtual void TearDown() { initializer.TearDown(); }
55 THD *get_thd() {
return initializer.thd(); }
70 item->fix_length_and_dec();
74 EXPECT_EQ(get_thd()->query_start_timeval().tv_sec, f.to_timeval().tv_sec);
76 EXPECT_EQ(0, f.to_timeval().tv_usec);
84 TEST_F(ItemFuncNowLocalTest, storeInTimestamp)
89 EXPECT_EQ(get_thd()->query_start_timeval().tv_sec, f.to_timeval().tv_sec);
91 EXPECT_EQ(0, f.to_timeval().tv_usec);
92 EXPECT_TRUE(f.store_timestamp_called);
95 int powers_of_10[DATETIME_MAX_DECIMALS + 1] =
96 { 1, 10, 100, 1000, 10000, 100000, 1000000 };
101 int truncate(
int n,
int scale)
103 EXPECT_TRUE(scale >= 0);
104 EXPECT_TRUE(scale <= DATETIME_MAX_DECIMALS);
105 return (n / powers_of_10[DATETIME_MAX_DECIMALS - scale]) *
106 powers_of_10[DATETIME_MAX_DECIMALS - scale];
116 TEST_F(ItemFuncNowLocalTest, storeInTimestampf)
118 for(ulong scale= 0; scale <= DATETIME_MAX_DECIMALS; ++scale)
124 EXPECT_EQ(get_thd()->query_start_timeval().tv_sec, f.to_timeval().tv_sec);
126 EXPECT_EQ(truncate(CURRENT_TIMESTAMP_FRACTIONAL_SECONDS, scale),
127 f.to_timeval().tv_usec);
128 EXPECT_TRUE(f.store_timestamp_internal_called);
137 TEST_F(ItemFuncNowLocalTest, storeInDatetime)
142 timeval now= { 1313677243, 1234 };
146 thd->variables.time_zone->gmt_sec_to_TIME(&now_time, thd->start_time);
148 f.get_time(&stored_time);
150 EXPECT_EQ(now_time.year, stored_time.year);
151 EXPECT_EQ(now_time.month, stored_time.month);
152 EXPECT_EQ(now_time.day, stored_time.day);
153 EXPECT_EQ(now_time.hour, stored_time.hour);
154 EXPECT_EQ(now_time.minute, stored_time.minute);
155 EXPECT_EQ(now_time.second, stored_time.second);
158 EXPECT_EQ(now_time.neg, stored_time.neg);
159 EXPECT_EQ(now_time.time_type, stored_time.time_type);