17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include "field_temporal_utils.h"
21 #include "test_utils.h"
22 #include "fake_table.h"
26 namespace field_datetime_unittests {
34 virtual void SetUp() { initializer.SetUp(); }
35 virtual void TearDown() { initializer.TearDown(); }
37 THD *thd() {
return initializer.thd(); }
48 uchar buffer[PACK_LENGTH];
55 memset(buffer, 0, PACK_LENGTH);
71 void make_writable() { bitmap_set_bit(table->write_set, field_index); }
77 char buff[MAX_FIELD_WIDTH];
78 String str(buff,
sizeof(buff), &my_charset_bin);
84 field_dt.make_writable();
85 thd()->count_cuted_fields= CHECK_FIELD_WARN;
89 test_store_string(&field_dt, STRING_WITH_LEN(
"2001-01-01 00:00:01"),
90 "2001-01-01 00:00:01", 0, TYPE_OK);
94 test_store_string(&field_dt, STRING_WITH_LEN(
"0000-00-00 00:00:00"),
95 "0000-00-00 00:00:00", 0, TYPE_OK);
99 test_store_string(&field_dt, STRING_WITH_LEN(
"0001-00-00 00:00:00"),
100 "0001-00-00 00:00:00", 0, TYPE_OK);
105 TEST_F(FieldDatetimeTest, StoreIllegalStringValues)
110 field_dt.make_writable();
111 thd()->count_cuted_fields= CHECK_FIELD_WARN;
116 test_store_string(&field_dt, STRING_WITH_LEN(
"99999-01-01 00:00:01"),
117 "0000-00-00 00:00:00",
118 WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE);
124 test_store_string(&field_dt, STRING_WITH_LEN(
"2001-13-01 00:00:01"),
125 "0000-00-00 00:00:00",
126 WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE);
132 test_store_string(&field_dt, STRING_WITH_LEN(
"2001-01-32 00:00:01"),
133 "0000-00-00 00:00:00",
134 WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE);
140 test_store_string(&field_dt, STRING_WITH_LEN(
"2001-01-01 72:00:01"),
141 "0000-00-00 00:00:00",
142 WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE);
148 test_store_string(&field_dt, STRING_WITH_LEN(
"2001-01-01 00:72:01"),
149 "0000-00-00 00:00:00",
150 WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE);
156 test_store_string(&field_dt, STRING_WITH_LEN(
"2001-01-01 00:00:72"),
157 "0000-00-00 00:00:00",
158 WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE);
164 test_store_string(&field_dt, STRING_WITH_LEN(
"foo"),
"0000-00-00 00:00:00",
165 WARN_DATA_TRUNCATED, TYPE_ERR_BAD_VALUE);
171 static const int no_modes= 4;
172 static const sql_mode_t strict_modes[no_modes]=
175 MODE_STRICT_TRANS_TABLES,
176 MODE_STRICT_ALL_TABLES,
177 MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES
188 TEST_F(FieldDatetimeTest, StoreZeroDateSqlModeNoZeroRestrictions)
193 field_dt.make_writable();
194 thd()->count_cuted_fields= CHECK_FIELD_WARN;
196 for (
int i= 0;
i < no_modes;
i++)
199 store_zero_in_sql_mode(&field_dt,
200 STRING_WITH_LEN(
"0000-00-00 00:00:00"),
201 "0000-00-00 00:00:00",
202 TYPE_OK, strict_modes[
i], 0);
205 for (
int i= 0;
i < no_modes;
i++)
208 store_zero_in_sql_mode(&field_dt,
209 STRING_WITH_LEN(
"0000-01-01 00:00:00"),
210 "0000-01-01 00:00:00",
211 TYPE_OK, strict_modes[
i], 0);
214 for (
int i= 0;
i < no_modes;
i++)
217 store_zero_in_sql_mode(&field_dt,
218 STRING_WITH_LEN(
"2001-00-01 00:00:00"),
219 "2001-00-01 00:00:00",
220 TYPE_OK, strict_modes[
i], 0);
223 for (
int i= 0;
i < no_modes;
i++)
226 store_zero_in_sql_mode(&field_dt,
227 STRING_WITH_LEN(
"2001-01-00 00:00:00"),
228 "2001-01-00 00:00:00",
229 TYPE_OK, strict_modes[
i], 0);
234 static const type_conversion_status nozero_expected_status[]=
236 TYPE_NOTE_TIME_TRUNCATED,
249 TEST_F(FieldDatetimeTest, StoreZeroDateSqlModeNoZeroDate)
254 field_dt.make_writable();
255 thd()->count_cuted_fields= CHECK_FIELD_WARN;
258 for (
int i= 0;
i < no_modes;
i++)
261 store_zero_in_sql_mode(&field_dt,
262 STRING_WITH_LEN(
"0000-00-00 00:00:00"),
263 "0000-00-00 00:00:00",
264 nozero_expected_status[
i],
265 MODE_NO_ZERO_DATE | strict_modes[i],
266 ER_WARN_DATA_OUT_OF_RANGE);
270 for (
int i= 0;
i < no_modes;
i++)
273 store_zero_in_sql_mode(&field_dt,
274 STRING_WITH_LEN(
"0000-01-01 00:00:00"),
275 "0000-01-01 00:00:00",
277 MODE_NO_ZERO_DATE | strict_modes[
i],
281 for (
int i= 0;
i < no_modes;
i++)
284 store_zero_in_sql_mode(&field_dt,
285 STRING_WITH_LEN(
"2001-00-01 00:00:00"),
286 "2001-00-01 00:00:00",
288 MODE_NO_ZERO_DATE | strict_modes[
i],
292 for (
int i= 0;
i < no_modes;
i++)
295 store_zero_in_sql_mode(&field_dt,
296 STRING_WITH_LEN(
"2001-01-00 00:00:00"),
297 "2001-01-00 00:00:00",
299 MODE_NO_ZERO_DATE | strict_modes[
i],
310 TEST_F(FieldDatetimeTest, StoreZeroDateSqlModeNoZeroInDate)
315 field_dt.make_writable();
316 thd()->count_cuted_fields= CHECK_FIELD_WARN;
319 for (
int i= 0;
i < no_modes;
i++)
322 store_zero_in_sql_mode(&field_dt,
323 STRING_WITH_LEN(
"0000-00-00 00:00:00"),
324 "0000-00-00 00:00:00",
326 MODE_NO_ZERO_IN_DATE | strict_modes[
i],
331 for (
int i= 0;
i < no_modes;
i++)
334 store_zero_in_sql_mode(&field_dt,
335 STRING_WITH_LEN(
"0000-01-01 00:00:00"),
336 "0000-01-01 00:00:00",
338 MODE_NO_ZERO_IN_DATE | strict_modes[
i],
343 for (
int i= 0;
i < no_modes;
i++)
346 store_zero_in_sql_mode(&field_dt,
347 STRING_WITH_LEN(
"2001-00-01 00:00:00"),
348 "0000-00-00 00:00:00",
349 nozero_expected_status[
i],
350 MODE_NO_ZERO_IN_DATE | strict_modes[i],
351 ER_WARN_DATA_OUT_OF_RANGE);
355 for (
int i= 0;
i < no_modes;
i++)
358 store_zero_in_sql_mode(&field_dt,
359 STRING_WITH_LEN(
"2001-01-00 00:00:00"),
360 "0000-00-00 00:00:00",
361 nozero_expected_status[
i],
362 MODE_NO_ZERO_IN_DATE | strict_modes[i],
363 ER_WARN_DATA_OUT_OF_RANGE);