gifs/api

Paddy 2014-08-27 Child:18cb30e077ba

0:08ec88016e2f Go to Latest

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.

History
1 package api
3 import (
4 "testing"
5 "time"
7 "secondbit.org/uuid"
8 )
10 var datastores = []Datastore{NewMemstore()}
12 func compareCollections(expectation, result Collection) (field string, expected, got interface{}) {
13 if !expectation.ID.Equal(result.ID) {
14 return "ID", expectation.ID, result.ID
15 }
16 if expectation.Name != result.Name {
17 return "name", expectation.Name, result.Name
18 }
19 if !expectation.Owner.Equal(result.Owner) {
20 return "owner", expectation.Owner, result.Owner
21 }
22 if !expectation.Created.Equal(result.Created) {
23 return "created", expectation.Created, result.Created
24 }
25 return "", nil, nil
26 }
28 func TestCreateCollection(t *testing.T) {
29 for pos, datastore := range datastores {
30 expectation := Collection{
31 ID: uuid.NewID(),
32 Name: "Test Collection",
33 Owner: uuid.NewID(),
34 Created: time.Now(),
35 }
36 err := datastore.CreateCollection(expectation)
37 if err != nil {
38 t.Errorf("Error creating collection in datastore %d: %s\n", pos, err)
39 }
40 result, err := datastore.GetCollectionByID(expectation.ID)
41 if err != nil {
42 t.Errorf("Error retrieving collection in datastore %d: %s\n", pos, err)
43 }
44 field, exp, got := compareCollections(expectation, result)
45 if field != "" {
46 t.Errorf("Collection did not meet expectations for datastore %d. Expected %v for %s, got %v.", pos, exp, field, got)
47 }
48 }
49 }