|
| 1 | +package mssql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// TestDatetimeAccuracy validates that DATETIME type values |
| 11 | +// are properly created from time.Time giving the same rounding |
| 12 | +// logic as SQL Server itself implements. |
| 13 | +// |
| 14 | +// Datetime is rounded to increments of .000, .003, or .007 seconds (accuracy is 1/300 of a second) |
| 15 | +// (see: https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql?view=sql-server-ver16). |
| 16 | +// |
| 17 | +// This test creates 3 schematically identical tables filled in 3 different ways: |
| 18 | +// |
| 19 | +// - datetime_test_insert_time_as_str (filled via regular INSERT with time as str params) |
| 20 | +// - datetime_test_insert_time_as_time (filled via regular INSERT with time as go time.Time params) |
| 21 | +// - datetime_test_insert_bulk (filled via Bulk Copy) |
| 22 | +// |
| 23 | +// After creating these 3 tables the test will compare the data read from all of the tables |
| 24 | +// expecting the data to be identical. |
| 25 | +func TestDatetimeAccuracy(t *testing.T) { |
| 26 | + ctx := context.Background() |
| 27 | + conn, logger := open(t) |
| 28 | + t.Cleanup(func() { |
| 29 | + conn.Close() |
| 30 | + logger.StopLogging() |
| 31 | + }) |
| 32 | + |
| 33 | + createTable := func(tableName string) { |
| 34 | + _, err := conn.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", tableName)) |
| 35 | + if err != nil { |
| 36 | + t.Fatal("Failed to drop table: ", err) |
| 37 | + } |
| 38 | + _, err = conn.Exec(fmt.Sprintf(`CREATE TABLE %s ( |
| 39 | + id INT NOT NULL PRIMARY KEY, |
| 40 | + dt DATETIME |
| 41 | + )`, tableName)) |
| 42 | + if err != nil { |
| 43 | + t.Fatal("Failed to create table: ", err) |
| 44 | + } |
| 45 | + t.Cleanup(func() { _, _ = conn.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", tableName)) }) |
| 46 | + } |
| 47 | + |
| 48 | + fillTable := func(tableName string, dts []any) { |
| 49 | + for i, dt := range dts { |
| 50 | + _, err := conn.Exec(fmt.Sprintf("INSERT INTO %s (id, dt) VALUES (@p1, @p2)", tableName), i, dt) |
| 51 | + if err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fillTableBulkCopy := func(tableName string, dts []any) { |
| 58 | + conn, err := conn.Conn(context.Background()) |
| 59 | + if err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + defer func() { _ = conn.Close() }() |
| 63 | + stmt, err := conn.PrepareContext(ctx, CopyIn(tableName, BulkOptions{}, "id", "dt")) |
| 64 | + if err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + for i, dt := range dts { |
| 68 | + if _, err = stmt.Exec(i, dt); err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + } |
| 72 | + if _, err = stmt.Exec(); err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + readTable := func(tableName string) (res []time.Time) { |
| 78 | + rows, err := conn.QueryContext(ctx, fmt.Sprintf("SELECT dt FROM %s ORDER BY id", tableName)) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + defer func() { _ = rows.Close() }() |
| 83 | + for rows.Next() { |
| 84 | + var dt time.Time |
| 85 | + if err = rows.Scan(&dt); err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + res = append(res, dt) |
| 89 | + } |
| 90 | + if rows.Err() != nil { |
| 91 | + t.Fatal(rows.Err()) |
| 92 | + } |
| 93 | + return res |
| 94 | + } |
| 95 | + |
| 96 | + // generate data to be inserted into the tables: |
| 97 | + // times with fraction of a second from .000 to .999. |
| 98 | + var dtsTime []any |
| 99 | + var dtsStrs []any |
| 100 | + for i := 0; i < 1000; i++ { |
| 101 | + ns := int(time.Duration(i) * (time.Second / 1000) * time.Nanosecond) |
| 102 | + dt := time.Date(2025, 4, 11, 10, 30, 42, ns, time.UTC) |
| 103 | + str := dt.Format("2006-01-02T15:04:05.999Z") |
| 104 | + dtsTime = append(dtsTime, dt) |
| 105 | + dtsStrs = append(dtsStrs, str) |
| 106 | + } |
| 107 | + |
| 108 | + createTable("datetime_test_insert_time_as_str") |
| 109 | + fillTable("datetime_test_insert_time_as_str", dtsStrs) |
| 110 | + |
| 111 | + createTable("datetime_test_insert_time_as_time") |
| 112 | + fillTable("datetime_test_insert_time_as_time", dtsTime) |
| 113 | + |
| 114 | + createTable("datetime_test_insert_bulk") |
| 115 | + fillTableBulkCopy("datetime_test_insert_bulk", dtsTime) |
| 116 | + |
| 117 | + as := readTable("datetime_test_insert_time_as_str") |
| 118 | + bs := readTable("datetime_test_insert_time_as_time") |
| 119 | + cs := readTable("datetime_test_insert_bulk") |
| 120 | + |
| 121 | + if len(dtsTime) != len(as) || len(dtsTime) != len(bs) || len(dtsTime) != len(cs) { |
| 122 | + t.Fatalf("Not all data inserted into tables: want = %d, got = %d %d %d", len(dtsTime), len(as), len(bs), len(cs)) |
| 123 | + } |
| 124 | + |
| 125 | + for i := 0; i < len(dtsTime); i++ { |
| 126 | + if !as[i].Equal(bs[i]) || !as[i].Equal(cs[i]) { |
| 127 | + t.Fatalf(`Rows not equal at #%d: |
| 128 | + | %-36s | %-36s | %-36s | |
| 129 | + | %36s | %36s | %36s |`, |
| 130 | + i, |
| 131 | + "datetime_test_insert_time_as_str", |
| 132 | + "datetime_test_insert_time_as_time", |
| 133 | + "datetime_test_insert_bulk", |
| 134 | + as[i].Format(time.RFC3339Nano), |
| 135 | + bs[i].Format(time.RFC3339Nano), |
| 136 | + cs[i].Format(time.RFC3339Nano), |
| 137 | + ) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestThreeHundredthsToNanosConverter(t *testing.T) { |
| 143 | + for want := 0; want < 300; want++ { |
| 144 | + ns := threeHundredthsOfASecondToNanos(want) |
| 145 | + got := nanosToThreeHundredthsOfASecond(ns) |
| 146 | + if want != got { |
| 147 | + t.Fatalf("want = %d, got = %d", want, got) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments