| 1 | /* Copyright (c) 2011, 2026, Oracle and/or its affiliates. |
| 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, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include "sql/sql_class.h" |
| 27 | #include "unittest/gunit/fake_table.h" |
| 28 | #include "unittest/gunit/mock_field_timestamp.h" |
| 29 | #include "unittest/gunit/test_utils.h" |
| 30 | |
| 31 | namespace field_timestamp_unittests { |
| 32 | |
| 33 | using my_testing::Mock_error_handler; |
| 34 | using my_testing::Server_initializer; |
| 35 | |
| 36 | /* |
| 37 | Tests of the public interface of Field_timestamp. |
| 38 | */ |
| 39 | class FieldTimestampTest : public ::testing::Test { |
| 40 | protected: |
| 41 | void SetUp() override { initializer.SetUp(); } |
| 42 | void TearDown() override { initializer.TearDown(); } |
| 43 | |
| 44 | THD *get_thd() { return initializer.thd(); } |
| 45 | |
| 46 | Server_initializer initializer; |
| 47 | }; |
| 48 | |
| 49 | TEST_F(FieldTimestampTest, hasInsertDefaultFunction) { |
| 50 | { |
| 51 | Mock_field_timestamp field_dn(Field::DEFAULT_NOW); |
| 52 | EXPECT_TRUE(field_dn.has_insert_default_datetime_value_expression()); |
| 53 | } |
| 54 | { |
| 55 | Mock_field_timestamp field_un(Field::ON_UPDATE_NOW); |
| 56 | EXPECT_FALSE(field_un.has_insert_default_datetime_value_expression()); |
| 57 | } |
| 58 | { |
| 59 | Mock_field_timestamp field_dnun(Field::DEFAULT_NOW | Field::ON_UPDATE_NOW); |
| 60 | EXPECT_TRUE(field_dnun.has_insert_default_datetime_value_expression()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | TEST_F(FieldTimestampTest, hasUpdateDefaultFunction) { |
| 65 | { |
| 66 | Mock_field_timestamp field_dn(Field::DEFAULT_NOW); |
| 67 | EXPECT_FALSE(field_dn.has_update_default_datetime_value_expression()); |
| 68 | } |
| 69 | { |
| 70 | Mock_field_timestamp field_un(Field::ON_UPDATE_NOW); |
| 71 | EXPECT_TRUE(field_un.has_update_default_datetime_value_expression()); |
| 72 | } |
| 73 | { |
| 74 | Mock_field_timestamp field_dnun(Field::DEFAULT_NOW | Field::ON_UPDATE_NOW); |
| 75 | EXPECT_TRUE(field_dnun.has_update_default_datetime_value_expression()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | Test of DEFAULT CURRENT_TIMESTAMP functionality. Note that CURRENT_TIMESTAMP |
| 81 | should be truncated to whole seconds. |
| 82 | */ |
| 83 | TEST_F(FieldTimestampTest, EvaluateInsertDefaultFunction) { |
| 84 | const timeval now = {1, 1}; |
| 85 | get_thd()->set_time(&now); |
| 86 | |
| 87 | { |
| 88 | Mock_field_timestamp field_dn(Field::DEFAULT_NOW); |
| 89 | field_dn.evaluate_insert_default_function(); |
| 90 | EXPECT_EQ(now.tv_sec, field_dn.to_timeval().m_tv_sec); |
| 91 | EXPECT_EQ(0, field_dn.to_timeval().m_tv_usec); |
| 92 | } |
| 93 | { |
| 94 | Mock_field_timestamp field_un(Field::ON_UPDATE_NOW); |
| 95 | field_un.evaluate_insert_default_function(); |
| 96 | EXPECT_EQ(0, field_un.to_timeval().m_tv_sec); |
| 97 | EXPECT_EQ(0, field_un.to_timeval().m_tv_usec); |
| 98 | } |
| 99 | { |
| 100 | Mock_field_timestamp field_dnun(Field::DEFAULT_NOW | Field::ON_UPDATE_NOW); |
| 101 | field_dnun.evaluate_insert_default_function(); |
| 102 | EXPECT_EQ(now.tv_sec, field_dnun.to_timeval().m_tv_sec); |
| 103 | EXPECT_EQ(0, field_dnun.to_timeval().m_tv_usec); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | Test of ON UPDATE CURRENT_TIMESTAMP functionality. Note that |
| 109 | CURRENT_TIMESTAMP should be truncated to whole seconds. |
| 110 | */ |
| 111 | TEST_F(FieldTimestampTest, EvaluateUpdateDefaultFunction) { |
| 112 | const timeval now = {1, 1}; |
| 113 | get_thd()->set_time(&now); |
| 114 | |
| 115 | { |
| 116 | Mock_field_timestamp field_dn(Field::DEFAULT_NOW); |
| 117 | field_dn.evaluate_update_default_function(); |
| 118 | EXPECT_EQ(0, field_dn.to_timeval().m_tv_sec); |
| 119 | EXPECT_EQ(0, field_dn.to_timeval().m_tv_usec); |
| 120 | } |
| 121 | { |
| 122 | Mock_field_timestamp field_un(Field::ON_UPDATE_NOW); |
| 123 | field_un.evaluate_update_default_function(); |
| 124 | EXPECT_EQ(now.tv_sec, field_un.to_timeval().m_tv_sec); |
| 125 | EXPECT_EQ(0, field_un.to_timeval().m_tv_usec); |
| 126 | } |
| 127 | { |
| 128 | Mock_field_timestamp field_dnun(Field::DEFAULT_NOW | Field::ON_UPDATE_NOW); |
| 129 | field_dnun.evaluate_update_default_function(); |
| 130 | EXPECT_EQ(now.tv_sec, field_dnun.to_timeval().m_tv_sec); |
| 131 | EXPECT_EQ(0, field_dnun.to_timeval().m_tv_usec); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } // namespace field_timestamp_unittests |