MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
field_timestamp-t.cc
1 /* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include "fake_table.h"
21 #include "mock_field_timestamp.h"
22 #include "test_utils.h"
23 
24 namespace field_timestamp_unittests {
25 
26 
29 
30 
31 /*
32  Tests of the public interface of Field_timestamp.
33 */
34 class FieldTimestampTest : public ::testing::Test
35 {
36 protected:
37  virtual void SetUp() { initializer.SetUp(); }
38  virtual void TearDown() { initializer.TearDown(); }
39 
40  THD *get_thd() { return initializer.thd(); }
41 
42  Server_initializer initializer;
43 };
44 
45 
46 TEST_F(FieldTimestampTest, hasInsertDefaultFunction)
47 {
48  {
49  Mock_field_timestamp field_dn(Field::TIMESTAMP_DN_FIELD);
50  EXPECT_TRUE(field_dn.has_insert_default_function());
51  }
52  {
53  Mock_field_timestamp field_un(Field::TIMESTAMP_UN_FIELD);
54  EXPECT_FALSE(field_un.has_insert_default_function());
55  }
56  {
57  Mock_field_timestamp field_dnun(Field::TIMESTAMP_DNUN_FIELD);
58  EXPECT_TRUE(field_dnun.has_insert_default_function());
59  }
60 }
61 
62 
63 TEST_F(FieldTimestampTest, hasUpdateDefaultFunction)
64 {
65  {
66  Mock_field_timestamp field_dn(Field::TIMESTAMP_DN_FIELD);
67  EXPECT_FALSE(field_dn.has_update_default_function());
68  }
69  {
70  Mock_field_timestamp field_un(Field::TIMESTAMP_UN_FIELD);
71  EXPECT_TRUE(field_un.has_update_default_function());
72  }
73  {
74  Mock_field_timestamp field_dnun(Field::TIMESTAMP_DNUN_FIELD);
75  EXPECT_TRUE(field_dnun.has_update_default_function());
76  }
77 }
78 
79 
80 /*
81  Test of DEFAULT CURRENT_TIMESTAMP functionality. Note that CURRENT_TIMESTAMP
82  should be truncated to whole seconds.
83 */
84 TEST_F(FieldTimestampTest, EvaluateInsertDefaultFunction)
85 {
86  const timeval now= { 1, 1 };
87  get_thd()->set_time(&now);
88 
89  {
90  Mock_field_timestamp field_dn(Field::TIMESTAMP_DN_FIELD);
91  field_dn.evaluate_insert_default_function();
92  EXPECT_EQ(now.tv_sec, field_dn.to_timeval().tv_sec);
93  EXPECT_EQ(0, field_dn.to_timeval().tv_usec);
94  }
95  {
96  Mock_field_timestamp field_un(Field::TIMESTAMP_UN_FIELD);
97  field_un.evaluate_insert_default_function();
98  EXPECT_EQ(0, field_un.to_timeval().tv_sec);
99  EXPECT_EQ(0, field_un.to_timeval().tv_usec);
100  }
101  {
102  Mock_field_timestamp field_dnun(Field::TIMESTAMP_DNUN_FIELD);
103  field_dnun.evaluate_insert_default_function();
104  EXPECT_EQ(now.tv_sec, field_dnun.to_timeval().tv_sec);
105  EXPECT_EQ(0, field_dnun.to_timeval().tv_usec);
106  }
107 }
108 
109 
110 /*
111  Test of ON UPDATE CURRENT_TIMESTAMP functionality. Note that
112  CURRENT_TIMESTAMP should be truncated to whole seconds.
113 */
114 TEST_F(FieldTimestampTest, EvaluateUpdateDefaultFunction)
115 {
116  const timeval now= { 1, 1 };
117  get_thd()->set_time(&now);
118 
119  {
120  Mock_field_timestamp field_dn(Field::TIMESTAMP_DN_FIELD);
121  field_dn.evaluate_update_default_function();
122  EXPECT_EQ(0, field_dn.to_timeval().tv_sec);
123  EXPECT_EQ(0, field_dn.to_timeval().tv_usec);
124  }
125  {
126  Mock_field_timestamp field_un(Field::TIMESTAMP_UN_FIELD);
127  field_un.evaluate_update_default_function();
128  EXPECT_EQ(now.tv_sec, field_un.to_timeval().tv_sec);
129  EXPECT_EQ(0, field_un.to_timeval().tv_usec);
130  }
131  {
132  Mock_field_timestamp field_dnun(Field::TIMESTAMP_DNUN_FIELD);
133  field_dnun.evaluate_update_default_function();
134  EXPECT_EQ(now.tv_sec, field_dnun.to_timeval().tv_sec);
135  EXPECT_EQ(0, field_dnun.to_timeval().tv_usec);
136  }
137 }
138 
139 }