Ejemplo n.º 1
0
func TestMustParseNullTime(t *testing.T) {
	panicked := false
	defer func() {
		if r := recover(); r != nil {
			panicked = true
		}
		if !panicked {
			t.Error("MustParseNullTime: didn't panic")
		}
	}()

	for given, expected := range parseTable {
		time := toki.MustParseNullTime(given)
		result := time.String()
		if result != expected {
			t.Errorf("MustParseNullTime: %s → expected %s, got: %s", given, expected, result)
		}
	}

	time := toki.MustParseNullTime("")
	if time.Valid {
		t.Error("MustParseNullTime: empty string isn't null")
	}

	toki.MustParseNullTime("invalid input")
}
Ejemplo n.º 2
0
func TestNullValue(t *testing.T) {
	time := toki.MustParseNullTime("12:34")
	v, err := time.Value()
	if string(v.([]byte)) != "12:34" {
		t.Errorf("Value: %v ≠ 12:34", v)
	}
	if err != nil {
		t.Errorf("Value: error: %v", err)
	}

	nullTime := toki.MustParseNullTime("")
	v, err = nullTime.Value()
	if v != nil {
		t.Errorf("Value: %v ≠ 12:34", v)
	}
	if err != nil {
		t.Errorf("Value: error: %v", err)
	}
}
Ejemplo n.º 3
0
func TestNullEquals(t *testing.T) {
	same1 := toki.MustParseNullTime("12:34")
	same2 := toki.MustParseNullTime("12:34")
	if !same1.Equals(same2) {
		t.Error(same1.String(), "≠", same2.String())
	}

	diff := toki.MustParseNullTime("12:35")
	if same1.Equals(diff) {
		t.Error(same1.String(), "=", diff.String())
	}

	blankNull := toki.MustParseNullTime("")
	nullWithTime := toki.MustParseNullTime("12:34")
	nullWithTime.Valid = false

	if !blankNull.Equals(nullWithTime) {
		t.Error(blankNull.String(), "≠", nullWithTime.String())
	}

	if same1.Equals(nullWithTime) {
		t.Error(blankNull.String(), "=", nullWithTime.String())
	}
}
Ejemplo n.º 4
0
func TestNullScan(t *testing.T) {
	expected := toki.MustParseNullTime("12:34")
	byteTime := toki.NullTime{}
	err := byteTime.Scan([]byte("12:34:00"))
	if err != nil {
		t.Errorf("Scan: error: %v", err)
	}
	if byteTime != expected {
		t.Errorf("Scan: %#v ≠ %#v", byteTime, expected)
	}

	nullTime := toki.NullTime{}
	err = nullTime.Scan(nil)
	if err != nil {
		t.Errorf("Scan: error: %v", err)
	}
	if nullTime.Valid {
		t.Errorf("Scan: not null, got %s", nullTime.String())
	}

	emptyTime := toki.NullTime{}
	err = emptyTime.Scan("")
	if err != nil {
		t.Errorf("Scan: error: %v", err)
	}
	if emptyTime.Valid {
		t.Errorf("Scan: not null, got %s", emptyTime.String())
	}

	emptyByteTime := toki.NullTime{}
	err = emptyByteTime.Scan([]byte{})
	if err != nil {
		t.Errorf("Scan: error: %v", err)
	}
	if emptyByteTime.Valid {
		t.Errorf("Scan: not null, got %s", emptyByteTime.String())
	}
}
Ejemplo n.º 5
0
func TestMarshalText(t *testing.T) {
	time := toki.MustParseNullTime("")
	if len(time.String()) != 0 {
		t.Errorf("MarshalText: unexpected value %s", time.String())
	}
}