gifs/api
2014-08-27
Child:18cb30e077ba
gifs/api/datastore_test.go
Spike out functionality and tests. Create our interfaces around storing data and retrieving it. Create an in-memory implementation of our interfaces, for testing and rapid dev purposes. Begin sketching out what our unit tests look like. Create our Google Cloud Storage datastore implementation. Sketch out an idea for a usage collection process to keep track of which users are actually using stuff.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/datastore_test.go Wed Aug 27 22:34:02 2014 -0400 1.3 @@ -0,0 +1,49 @@ 1.4 +package api 1.5 + 1.6 +import ( 1.7 + "testing" 1.8 + "time" 1.9 + 1.10 + "secondbit.org/uuid" 1.11 +) 1.12 + 1.13 +var datastores = []Datastore{NewMemstore()} 1.14 + 1.15 +func compareCollections(expectation, result Collection) (field string, expected, got interface{}) { 1.16 + if !expectation.ID.Equal(result.ID) { 1.17 + return "ID", expectation.ID, result.ID 1.18 + } 1.19 + if expectation.Name != result.Name { 1.20 + return "name", expectation.Name, result.Name 1.21 + } 1.22 + if !expectation.Owner.Equal(result.Owner) { 1.23 + return "owner", expectation.Owner, result.Owner 1.24 + } 1.25 + if !expectation.Created.Equal(result.Created) { 1.26 + return "created", expectation.Created, result.Created 1.27 + } 1.28 + return "", nil, nil 1.29 +} 1.30 + 1.31 +func TestCreateCollection(t *testing.T) { 1.32 + for pos, datastore := range datastores { 1.33 + expectation := Collection{ 1.34 + ID: uuid.NewID(), 1.35 + Name: "Test Collection", 1.36 + Owner: uuid.NewID(), 1.37 + Created: time.Now(), 1.38 + } 1.39 + err := datastore.CreateCollection(expectation) 1.40 + if err != nil { 1.41 + t.Errorf("Error creating collection in datastore %d: %s\n", pos, err) 1.42 + } 1.43 + result, err := datastore.GetCollectionByID(expectation.ID) 1.44 + if err != nil { 1.45 + t.Errorf("Error retrieving collection in datastore %d: %s\n", pos, err) 1.46 + } 1.47 + field, exp, got := compareCollections(expectation, result) 1.48 + if field != "" { 1.49 + t.Errorf("Collection did not meet expectations for datastore %d. Expected %v for %s, got %v.", pos, exp, field, got) 1.50 + } 1.51 + } 1.52 +}