Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing assert.Equal except one field

I'm writing a test for reading/writing a struct in a DB and one of its fields is a timestamp which is auto-calculated in the DB. So when I write the struct its timestamp is 0 but when I read it from the DB the timestamp has an actual value.

I want to compare the two values but to ignore the auto-calculated field. Is it possible?

like image 826
Dina Avatar asked Jul 13 '17 13:07

Dina


1 Answers

Set the other "except" field prior to testing:

now := time.Now()
expected := SomeStruct{
    ID:       123,
    Name:     "Test",
    Timestamp: now,
    ...
}
result, _ := db.Select(....)
result.Timeestamp = now
if !reflect.DeepEqual(result, expected) {
   ...
}
like image 163
Flimzy Avatar answered Oct 31 '22 18:10

Flimzy