|
| 1 | +package sqlmock |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestColumn(t *testing.T) { |
| 10 | + now, _ := time.Parse(time.RFC3339, "2020-06-20T22:08:41Z") |
| 11 | + column1 := NewColumn("test").OfType("VARCHAR", "").Nullable(true).WithLength(100) |
| 12 | + column2 := NewColumn("number").OfType("DECIMAL", float64(0.0)).Nullable(false).WithPrecisionAndScale(10, 4) |
| 13 | + column3 := NewColumn("when").OfType("TIMESTAMP", now) |
| 14 | + |
| 15 | + if column1.ScanType().Kind() != reflect.String { |
| 16 | + t.Errorf("string scanType mismatch: %v", column1.ScanType()) |
| 17 | + } |
| 18 | + if column2.ScanType().Kind() != reflect.Float64 { |
| 19 | + t.Errorf("float scanType mismatch: %v", column2.ScanType()) |
| 20 | + } |
| 21 | + if column3.ScanType() != reflect.TypeOf(time.Time{}) { |
| 22 | + t.Errorf("time scanType mismatch: %v", column3.ScanType()) |
| 23 | + } |
| 24 | + |
| 25 | + nullable, ok := column1.IsNullable() |
| 26 | + if !nullable || !ok { |
| 27 | + t.Errorf("'test' column should be nullable") |
| 28 | + } |
| 29 | + nullable, ok = column2.IsNullable() |
| 30 | + if nullable || !ok { |
| 31 | + t.Errorf("'number' column should not be nullable") |
| 32 | + } |
| 33 | + nullable, ok = column3.IsNullable() |
| 34 | + if ok { |
| 35 | + t.Errorf("'when' column nullability should be unknown") |
| 36 | + } |
| 37 | + |
| 38 | + length, ok := column1.Length() |
| 39 | + if length != 100 || !ok { |
| 40 | + t.Errorf("'test' column wrong length") |
| 41 | + } |
| 42 | + length, ok = column2.Length() |
| 43 | + if ok { |
| 44 | + t.Errorf("'number' column is not of variable length type") |
| 45 | + } |
| 46 | + length, ok = column3.Length() |
| 47 | + if ok { |
| 48 | + t.Errorf("'when' column is not of variable length type") |
| 49 | + } |
| 50 | + |
| 51 | + _, _, ok = column1.PrecisionScale() |
| 52 | + if ok { |
| 53 | + t.Errorf("'test' column not applicable") |
| 54 | + } |
| 55 | + precision, scale, ok := column2.PrecisionScale() |
| 56 | + if precision != 10 || scale != 4 || !ok { |
| 57 | + t.Errorf("'number' column not applicable") |
| 58 | + } |
| 59 | + _, _, ok = column3.PrecisionScale() |
| 60 | + if ok { |
| 61 | + t.Errorf("'when' column not applicable") |
| 62 | + } |
| 63 | +} |
0 commit comments