ducky/devices
2015-11-29
ducky/devices/devices_test.go
Add tests for ToSlice and ToMap. Add simple unit tests that verify that ToSlice and ToMap both work as we expect. These are probably overly-simplistic, but so are the functions we're testing. Not sure about this... feel a bit conflicted. But let's try it.
1 package devices
3 import (
4 "testing"
5 "time"
7 "code.secondbit.org/uuid.hg"
8 )
10 func TestToMap(t *testing.T) {
11 devices := []Device{
12 {ID: uuid.NewID(), Name: "Test 1", Owner: uuid.NewID(), Type: TypeAndroidPhone, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
13 {ID: uuid.NewID(), Name: "Test 2", Owner: uuid.NewID(), Type: TypeAndroidTablet, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
14 {ID: uuid.NewID(), Name: "Test 3", Owner: uuid.NewID(), Type: TypeChromeExtension, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
15 }
16 expectation := map[string]Device{
17 devices[0].ID.String(): devices[0],
18 devices[1].ID.String(): devices[1],
19 devices[2].ID.String(): devices[2],
20 }
21 result := ToMap(devices)
23 if len(expectation) != len(result) {
24 t.Errorf("Expected %d devices in map, got %d\n", len(expectation), len(result))
25 }
27 for _, device := range devices {
28 ok, field, exp, res := compareDevices(expectation[device.ID.String()], result[device.ID.String()])
29 if !ok {
30 t.Errorf("Expected field %s of %s to be %v, got %v\n", field, device.Name, exp, res)
31 }
32 }
33 }
35 func TestToSlice(t *testing.T) {
36 id1, id2, id3 := uuid.NewID(), uuid.NewID(), uuid.NewID()
37 devices := map[string]Device{
38 id1.String(): {ID: id1, Name: "Test 1", Owner: uuid.NewID(), Type: TypeAndroidPhone, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
39 id2.String(): {ID: id2, Name: "Test 2", Owner: uuid.NewID(), Type: TypeAndroidTablet, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
40 id3.String(): {ID: id3, Name: "Test 3", Owner: uuid.NewID(), Type: TypeChromeExtension, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
41 }
42 expectation := []Device{devices[id1.String()], devices[id2.String()], devices[id3.String()]}
43 result := ToSlice(devices)
45 if len(expectation) != len(result) {
46 t.Errorf("Expected %d devices in slice, got %d\n", len(expectation), len(result))
47 }
49 for _, device := range expectation {
50 var found bool
51 for _, d := range result {
52 if !d.ID.Equal(device.ID) {
53 continue
54 }
55 found = true
56 ok, field, exp, res := compareDevices(device, d)
57 if !ok {
58 t.Errorf("Expected field %s of %s to be %v, got %v\n", field, d.Name, exp, res)
59 }
60 }
61 if !found {
62 t.Errorf("Expected to find %s in result, did not\n", device.Name)
63 }
64 }
65 }