NullTime in normalizeTime & tests

This commit is contained in:
Matthew Dillon 2014-10-31 09:38:49 -08:00
parent 58344b82a5
commit f48ee87ccd
8 changed files with 49 additions and 17 deletions

View file

@ -1,9 +1,23 @@
package datastore
import "time"
import (
"fmt"
"time"
func normalizeTime(t ...*time.Time) {
"github.com/lib/pq"
)
func normalizeTime(t ...interface{}) {
for _, v := range t {
*v = v.In(time.UTC)
switch u := v.(type) {
default:
fmt.Printf("unexpected type %T", u)
case *time.Time:
x, _ := v.(*time.Time)
*x = x.In(time.UTC)
case *pq.NullTime:
x, _ := v.(*pq.NullTime)
*x = pq.NullTime{Time: x.Time.In(time.UTC), Valid: x.Valid}
}
}
}

View file

@ -36,6 +36,7 @@ func TestGeneraStore_Get_db(t *testing.T) {
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
normalizeTime(&genus.CreatedAt, &genus.UpdatedAt, &genus.DeletedAt)
if !reflect.DeepEqual(genus, want) {
t.Errorf("got genus %+v, want %+v", genus, want)
}
@ -74,8 +75,9 @@ func TestGeneraStore_List_db(t *testing.T) {
t.Fatal(err)
}
for _, g := range want {
normalizeTime(&g.CreatedAt, &g.UpdatedAt, &g.DeletedAt)
for i := range want {
normalizeTime(&want[i].CreatedAt, &want[i].UpdatedAt, &want[i].DeletedAt)
normalizeTime(&genera[i].CreatedAt, &genera[i].UpdatedAt, &genera[i].DeletedAt)
}
if !reflect.DeepEqual(genera, want) {
t.Errorf("got genera %+v, want %+v", genera, want)

View file

@ -38,6 +38,7 @@ func TestSpeciesStore_Get_db(t *testing.T) {
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
normalizeTime(&species.CreatedAt, &species.UpdatedAt, &species.DeletedAt)
if !reflect.DeepEqual(species, want) {
t.Errorf("got species %+v, want %+v", species, want)
}
@ -77,8 +78,9 @@ func TestSpeciesStore_List_db(t *testing.T) {
t.Fatal(err)
}
for _, g := range want {
normalizeTime(&g.CreatedAt, &g.UpdatedAt, &g.DeletedAt)
for i := range want {
normalizeTime(&want[i].CreatedAt, &want[i].UpdatedAt, &want[i].DeletedAt)
normalizeTime(&species[i].CreatedAt, &species[i].UpdatedAt, &species[i].DeletedAt)
}
if !reflect.DeepEqual(species, want) {
t.Errorf("got species %+v, want %+v", species, want)

View file

@ -40,6 +40,7 @@ func TestStrainsStore_Get_db(t *testing.T) {
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
normalizeTime(&strain.CreatedAt, &strain.UpdatedAt, &strain.DeletedAt)
if !reflect.DeepEqual(strain, want) {
t.Errorf("got strain %+v, want %+v", strain, want)
@ -80,8 +81,9 @@ func TestStrainsStore_List_db(t *testing.T) {
t.Fatal(err)
}
for _, g := range want {
normalizeTime(&g.CreatedAt, &g.UpdatedAt, &g.DeletedAt)
for i := range want {
normalizeTime(&want[i].CreatedAt, &want[i].UpdatedAt, &want[i].DeletedAt)
normalizeTime(&strains[i].CreatedAt, &strains[i].UpdatedAt, &strains[i].DeletedAt)
}
if !reflect.DeepEqual(strains, want) {
t.Errorf("got strains %+v, want %+v", strains, want)

View file

@ -2,6 +2,7 @@ package models
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@ -9,6 +10,8 @@ import (
"reflect"
"testing"
"time"
"github.com/lib/pq"
)
var (
@ -88,8 +91,17 @@ func testBody(t *testing.T, r *http.Request, want string) {
}
}
func normalizeTime(t ...*time.Time) {
func normalizeTime(t ...interface{}) {
for _, v := range t {
*v = v.In(time.UTC)
switch u := v.(type) {
default:
fmt.Printf("unexpected type %T", u)
case *time.Time:
x, _ := v.(*time.Time)
*x = x.In(time.UTC)
case *pq.NullTime:
x, _ := v.(*pq.NullTime)
*x = pq.NullTime{Time: x.Time.In(time.UTC), Valid: x.Valid}
}
}
}

View file

@ -54,7 +54,7 @@ func TestGeneraService_Create(t *testing.T) {
mux.HandleFunc(urlPath(t, router.CreateGenus, nil), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "POST")
testBody(t, r, `{"id":1,"genus_name":"Test Genus","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":"0001-01-01T00:00:00Z"}`+"\n")
testBody(t, r, `{"id":1,"genus_name":"Test Genus","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
@ -124,7 +124,7 @@ func TestGeneraService_Update(t *testing.T) {
mux.HandleFunc(urlPath(t, router.UpdateGenus, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "PUT")
testBody(t, r, `{"id":1,"genus_name":"Test Genus Updated","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":"0001-01-01T00:00:00Z"}`+"\n")
testBody(t, r, `{"id":1,"genus_name":"Test Genus Updated","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)

View file

@ -55,7 +55,7 @@ func TestSpeciesService_Create(t *testing.T) {
mux.HandleFunc(urlPath(t, router.CreateSpecies, nil), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "POST")
testBody(t, r, `{"id":1,"genus_id":1,"species_name":"Test Species","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":"0001-01-01T00:00:00Z"}`+"\n")
testBody(t, r, `{"id":1,"genus_id":1,"species_name":"Test Species","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
@ -124,7 +124,7 @@ func TestSpeciesService_Update(t *testing.T) {
mux.HandleFunc(urlPath(t, router.UpdateSpecies, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "PUT")
testBody(t, r, `{"id":1,"genus_id":1,"species_name":"Test Species Updated","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":"0001-01-01T00:00:00Z"}`+"\n")
testBody(t, r, `{"id":1,"genus_id":1,"species_name":"Test Species Updated","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)

View file

@ -55,7 +55,7 @@ func TestStrainService_Create(t *testing.T) {
mux.HandleFunc(urlPath(t, router.CreateStrain, nil), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "POST")
testBody(t, r, `{"id":1,"species_id":1,"strain_name":"Test Strain","strain_type":"Test Type","etymology":"Test Etymology","accession_banks":"Test Accession","genbank_embl_ddb":"Test Genbank","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":"0001-01-01T00:00:00Z"}`+"\n")
testBody(t, r, `{"id":1,"species_id":1,"strain_name":"Test Strain","strain_type":"Test Type","etymology":"Test Etymology","accession_banks":"Test Accession","genbank_embl_ddb":"Test Genbank","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
@ -124,7 +124,7 @@ func TestStrainService_Update(t *testing.T) {
mux.HandleFunc(urlPath(t, router.UpdateStrain, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "PUT")
testBody(t, r, `{"id":1,"species_id":1,"strain_name":"Test Strain Updated","strain_type":"Test Type Updated","etymology":"Test Etymology Updated","accession_banks":"Test Accession Updated","genbank_embl_ddb":"Test Genbank Updated","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":"0001-01-01T00:00:00Z"}`+"\n")
testBody(t, r, `{"id":1,"species_id":1,"strain_name":"Test Strain Updated","strain_type":"Test Type Updated","etymology":"Test Etymology Updated","accession_banks":"Test Accession Updated","genbank_embl_ddb":"Test Genbank Updated","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)
})