ducky/devices

Paddy 2015-11-12 Child:8d40c0e5f4d9

2:426d7e3c4ecd Go to Latest

ducky/devices/storer_test.go

Start testing our Storers. Set up a file to start testing our Storer implementations, and write a sample test for the happy path of GetDevices--e.g., we expect no errors. This also required us to setup a helper to compare two Device instances and see if they're equal or not, and if not, how they differ. In the future, we'll keep adding test methods to test more logical paths for the interface contract, and to test more of the methods for the interface.

History
paddy@2 1 package devices
paddy@2 2
paddy@2 3 import (
paddy@2 4 "testing"
paddy@2 5 "time"
paddy@2 6
paddy@2 7 "code.secondbit.org/uuid.hg"
paddy@2 8 "golang.org/x/net/context"
paddy@2 9 )
paddy@2 10
paddy@2 11 var storers []Storer
paddy@2 12
paddy@2 13 func compareDevices(device1, device2 Device) (ok bool, field string, expected, result interface{}) {
paddy@2 14 if !device1.ID.Equal(device2.ID) {
paddy@2 15 return false, "ID", device1.ID, device2.ID
paddy@2 16 }
paddy@2 17 if device1.Name != device2.Name {
paddy@2 18 return false, "Name", device1.Name, device2.Name
paddy@2 19 }
paddy@2 20 if !device1.Owner.Equal(device2.Owner) {
paddy@2 21 return false, "Owner", device1.Owner, device2.Owner
paddy@2 22 }
paddy@2 23 if device1.Type != device2.Type {
paddy@2 24 return false, "Type", device1.Type, device2.Type
paddy@2 25 }
paddy@2 26 if !device1.Created.Equal(device2.Created) {
paddy@2 27 return false, "Created", device1.Created, device2.Created
paddy@2 28 }
paddy@2 29 if !device1.LastSeen.Equal(device2.LastSeen) {
paddy@2 30 return false, "LastSeen", device1.LastSeen, device2.LastSeen
paddy@2 31 }
paddy@2 32 if device1.PushToken != device2.PushToken {
paddy@2 33 return false, "PushToken", device1.PushToken, device2.PushToken
paddy@2 34 }
paddy@2 35 return true, "", nil, nil
paddy@2 36 }
paddy@2 37
paddy@2 38 func TestGetDevicesHappyPath(t *testing.T) {
paddy@2 39 for _, storer := range storers {
paddy@2 40 storer, err := storer.Factory(context.TODO())
paddy@2 41 if err != nil {
paddy@2 42 t.Fatalf("Fatal error creating %T: %+v\n", storer, err)
paddy@2 43 }
paddy@2 44
paddy@2 45 devices := []Device{
paddy@2 46 {ID: uuid.NewID(), Name: "Test 1", Owner: uuid.NewID(), Type: TypeAndroidPhone, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
paddy@2 47 {ID: uuid.NewID(), Name: "Test 2", Owner: uuid.NewID(), Type: TypeAndroidTablet, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
paddy@2 48 {ID: uuid.NewID(), Name: "Test 3", Owner: uuid.NewID(), Type: TypeChromeExtension, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
paddy@2 49 }
paddy@2 50 ids := make([]uuid.ID, 0, len(devices))
paddy@2 51 for _, device := range devices {
paddy@2 52 ids = append(ids, device.ID)
paddy@2 53 }
paddy@2 54
paddy@2 55 results, err := storer.GetDevices(ids, context.TODO())
paddy@2 56 if err != nil {
paddy@2 57 t.Errorf("Unexpected error retrieving devices from %T: %+v\n", storer, err)
paddy@2 58 }
paddy@2 59 for _, device := range devices {
paddy@2 60 d, returned := results[device.ID.String()]
paddy@2 61 if !returned {
paddy@2 62 t.Errorf("Expected device %s to be in results from %T, but wasn't present\n", device.Name, storer)
paddy@2 63 }
paddy@2 64 ok, field, expected, result := compareDevices(device, d)
paddy@2 65 if !ok {
paddy@2 66 t.Errorf("Expected %s of %s to be %v, got %v from %T\n", field, device.Name, expected, result, storer)
paddy@2 67 }
paddy@2 68 }
paddy@2 69 err = storer.Destroy(context.TODO())
paddy@2 70 if err != nil {
paddy@2 71 t.Errorf("Error cleaning up after %T: %+v\n", storer, err)
paddy@2 72 }
paddy@2 73 }
paddy@2 74 }