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.
| paddy@13 | 1 package devices |
| paddy@13 | 2 |
| paddy@13 | 3 import ( |
| paddy@13 | 4 "testing" |
| paddy@13 | 5 "time" |
| paddy@13 | 6 |
| paddy@13 | 7 "code.secondbit.org/uuid.hg" |
| paddy@13 | 8 ) |
| paddy@13 | 9 |
| paddy@13 | 10 func TestToMap(t *testing.T) { |
| paddy@13 | 11 devices := []Device{ |
| paddy@13 | 12 {ID: uuid.NewID(), Name: "Test 1", Owner: uuid.NewID(), Type: TypeAndroidPhone, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"}, |
| paddy@13 | 13 {ID: uuid.NewID(), Name: "Test 2", Owner: uuid.NewID(), Type: TypeAndroidTablet, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"}, |
| paddy@13 | 14 {ID: uuid.NewID(), Name: "Test 3", Owner: uuid.NewID(), Type: TypeChromeExtension, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"}, |
| paddy@13 | 15 } |
| paddy@13 | 16 expectation := map[string]Device{ |
| paddy@13 | 17 devices[0].ID.String(): devices[0], |
| paddy@13 | 18 devices[1].ID.String(): devices[1], |
| paddy@13 | 19 devices[2].ID.String(): devices[2], |
| paddy@13 | 20 } |
| paddy@13 | 21 result := ToMap(devices) |
| paddy@13 | 22 |
| paddy@13 | 23 if len(expectation) != len(result) { |
| paddy@13 | 24 t.Errorf("Expected %d devices in map, got %d\n", len(expectation), len(result)) |
| paddy@13 | 25 } |
| paddy@13 | 26 |
| paddy@13 | 27 for _, device := range devices { |
| paddy@13 | 28 ok, field, exp, res := compareDevices(expectation[device.ID.String()], result[device.ID.String()]) |
| paddy@13 | 29 if !ok { |
| paddy@13 | 30 t.Errorf("Expected field %s of %s to be %v, got %v\n", field, device.Name, exp, res) |
| paddy@13 | 31 } |
| paddy@13 | 32 } |
| paddy@13 | 33 } |
| paddy@13 | 34 |
| paddy@13 | 35 func TestToSlice(t *testing.T) { |
| paddy@13 | 36 id1, id2, id3 := uuid.NewID(), uuid.NewID(), uuid.NewID() |
| paddy@13 | 37 devices := map[string]Device{ |
| paddy@13 | 38 id1.String(): {ID: id1, Name: "Test 1", Owner: uuid.NewID(), Type: TypeAndroidPhone, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"}, |
| paddy@13 | 39 id2.String(): {ID: id2, Name: "Test 2", Owner: uuid.NewID(), Type: TypeAndroidTablet, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"}, |
| paddy@13 | 40 id3.String(): {ID: id3, Name: "Test 3", Owner: uuid.NewID(), Type: TypeChromeExtension, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"}, |
| paddy@13 | 41 } |
| paddy@13 | 42 expectation := []Device{devices[id1.String()], devices[id2.String()], devices[id3.String()]} |
| paddy@13 | 43 result := ToSlice(devices) |
| paddy@13 | 44 |
| paddy@13 | 45 if len(expectation) != len(result) { |
| paddy@13 | 46 t.Errorf("Expected %d devices in slice, got %d\n", len(expectation), len(result)) |
| paddy@13 | 47 } |
| paddy@13 | 48 |
| paddy@13 | 49 for _, device := range expectation { |
| paddy@13 | 50 var found bool |
| paddy@13 | 51 for _, d := range result { |
| paddy@13 | 52 if !d.ID.Equal(device.ID) { |
| paddy@13 | 53 continue |
| paddy@13 | 54 } |
| paddy@13 | 55 found = true |
| paddy@13 | 56 ok, field, exp, res := compareDevices(device, d) |
| paddy@13 | 57 if !ok { |
| paddy@13 | 58 t.Errorf("Expected field %s of %s to be %v, got %v\n", field, d.Name, exp, res) |
| paddy@13 | 59 } |
| paddy@13 | 60 } |
| paddy@13 | 61 if !found { |
| paddy@13 | 62 t.Errorf("Expected to find %s in result, did not\n", device.Name) |
| paddy@13 | 63 } |
| paddy@13 | 64 } |
| paddy@13 | 65 } |