ducky/devices

Paddy 2015-11-29

13:e3ced527d4ab Go to Latest

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.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/devices_test.go	Sun Nov 29 19:46:20 2015 -0800
     1.3 @@ -0,0 +1,65 @@
     1.4 +package devices
     1.5 +
     1.6 +import (
     1.7 +	"testing"
     1.8 +	"time"
     1.9 +
    1.10 +	"code.secondbit.org/uuid.hg"
    1.11 +)
    1.12 +
    1.13 +func TestToMap(t *testing.T) {
    1.14 +	devices := []Device{
    1.15 +		{ID: uuid.NewID(), Name: "Test 1", Owner: uuid.NewID(), Type: TypeAndroidPhone, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
    1.16 +		{ID: uuid.NewID(), Name: "Test 2", Owner: uuid.NewID(), Type: TypeAndroidTablet, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
    1.17 +		{ID: uuid.NewID(), Name: "Test 3", Owner: uuid.NewID(), Type: TypeChromeExtension, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
    1.18 +	}
    1.19 +	expectation := map[string]Device{
    1.20 +		devices[0].ID.String(): devices[0],
    1.21 +		devices[1].ID.String(): devices[1],
    1.22 +		devices[2].ID.String(): devices[2],
    1.23 +	}
    1.24 +	result := ToMap(devices)
    1.25 +
    1.26 +	if len(expectation) != len(result) {
    1.27 +		t.Errorf("Expected %d devices in map, got %d\n", len(expectation), len(result))
    1.28 +	}
    1.29 +
    1.30 +	for _, device := range devices {
    1.31 +		ok, field, exp, res := compareDevices(expectation[device.ID.String()], result[device.ID.String()])
    1.32 +		if !ok {
    1.33 +			t.Errorf("Expected field %s of %s to be %v, got %v\n", field, device.Name, exp, res)
    1.34 +		}
    1.35 +	}
    1.36 +}
    1.37 +
    1.38 +func TestToSlice(t *testing.T) {
    1.39 +	id1, id2, id3 := uuid.NewID(), uuid.NewID(), uuid.NewID()
    1.40 +	devices := map[string]Device{
    1.41 +		id1.String(): {ID: id1, Name: "Test 1", Owner: uuid.NewID(), Type: TypeAndroidPhone, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
    1.42 +		id2.String(): {ID: id2, Name: "Test 2", Owner: uuid.NewID(), Type: TypeAndroidTablet, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
    1.43 +		id3.String(): {ID: id3, Name: "Test 3", Owner: uuid.NewID(), Type: TypeChromeExtension, Created: time.Now(), LastSeen: time.Now(), PushToken: "test token"},
    1.44 +	}
    1.45 +	expectation := []Device{devices[id1.String()], devices[id2.String()], devices[id3.String()]}
    1.46 +	result := ToSlice(devices)
    1.47 +
    1.48 +	if len(expectation) != len(result) {
    1.49 +		t.Errorf("Expected %d devices in slice, got %d\n", len(expectation), len(result))
    1.50 +	}
    1.51 +
    1.52 +	for _, device := range expectation {
    1.53 +		var found bool
    1.54 +		for _, d := range result {
    1.55 +			if !d.ID.Equal(device.ID) {
    1.56 +				continue
    1.57 +			}
    1.58 +			found = true
    1.59 +			ok, field, exp, res := compareDevices(device, d)
    1.60 +			if !ok {
    1.61 +				t.Errorf("Expected field %s of %s to be %v, got %v\n", field, d.Name, exp, res)
    1.62 +			}
    1.63 +		}
    1.64 +		if !found {
    1.65 +			t.Errorf("Expected to find %s in result, did not\n", device.Name)
    1.66 +		}
    1.67 +	}
    1.68 +}