ducky/devices

Paddy 2015-11-20 Parent:34130c700842 Child:74dbc04879a7

9:f5a9d5f8f28d Go to Latest

ducky/devices/storer_test.go

Separate out StorerFactory into its own interface. We had previously included a Factory and Destroy method in the Storer interface, but it makes more sense to separate those out into a StorerFactory interface. This lets us avoid the impression that we're using the same instance of a Storer for every test, separates the test methods away from the methods that are actually used in prod, and hides them from Godoc. What's not to like?

History
     1.1 --- a/storer_test.go	Sun Nov 15 04:40:30 2015 -0800
     1.2 +++ b/storer_test.go	Fri Nov 20 01:14:58 2015 -0800
     1.3 @@ -8,7 +8,12 @@
     1.4  	"golang.org/x/net/context"
     1.5  )
     1.6  
     1.7 -var storers []Storer
     1.8 +type StorerFactory interface {
     1.9 +	NewStorer(ctx context.Context) (Storer, error)
    1.10 +	TeardownStorer(storer Storer, ctx context.Context) error
    1.11 +}
    1.12 +
    1.13 +var storerFactories []StorerFactory
    1.14  
    1.15  func compareDevices(device1, device2 Device) (ok bool, field string, expected, result interface{}) {
    1.16  	if !device1.ID.Equal(device2.ID) {
    1.17 @@ -36,10 +41,10 @@
    1.18  }
    1.19  
    1.20  func TestCreateAndGetDevices(t *testing.T) {
    1.21 -	for _, storer := range storers {
    1.22 -		storer, err := storer.Factory(context.TODO())
    1.23 +	for _, factory := range storerFactories {
    1.24 +		storer, err := factory.NewStorer(context.TODO())
    1.25  		if err != nil {
    1.26 -			t.Fatalf("Fatal error creating %T: %+v\n", storer, err)
    1.27 +			t.Fatalf("Fatal error creating Storer from %T: %+v\n", factory, err)
    1.28  		}
    1.29  
    1.30  		devices := []Device{
    1.31 @@ -72,7 +77,7 @@
    1.32  				t.Errorf("Expected %s of %s to be %v, got %v from %T\n", field, device.Name, expected, result, storer)
    1.33  			}
    1.34  		}
    1.35 -		err = storer.Destroy(context.TODO())
    1.36 +		err = factory.TeardownStorer(storer, context.TODO())
    1.37  		if err != nil {
    1.38  			t.Errorf("Error cleaning up after %T: %+v\n", storer, err)
    1.39  		}
    1.40 @@ -80,10 +85,10 @@
    1.41  }
    1.42  
    1.43  func TestGetDevicesNoErrorForMissing(t *testing.T) {
    1.44 -	for _, storer := range storers {
    1.45 -		storer, err := storer.Factory(context.TODO())
    1.46 +	for _, factory := range storerFactories {
    1.47 +		storer, err := factory.NewStorer(context.TODO())
    1.48  		if err != nil {
    1.49 -			t.Fatalf("Fatal error creatng %T: %+v\n", storer, err)
    1.50 +			t.Fatalf("Fatal error creatng Storer from %T: %+v\n", factory, err)
    1.51  		}
    1.52  
    1.53  		results, err := storer.GetDevices([]uuid.ID{uuid.NewID()}, context.TODO())
    1.54 @@ -93,7 +98,7 @@
    1.55  		if len(results) != 0 {
    1.56  			t.Errorf("Expected results to be empty, got %+v from %T instead\n", results, storer)
    1.57  		}
    1.58 -		err = storer.Destroy(context.TODO())
    1.59 +		err = factory.TeardownStorer(storer, context.TODO())
    1.60  		if err != nil {
    1.61  			t.Errorf("Error cleaning up after %T: %+v\n", storer, err)
    1.62  		}
    1.63 @@ -101,10 +106,10 @@
    1.64  }
    1.65  
    1.66  func TestCreateDevicesDuplicates(t *testing.T) {
    1.67 -	for _, storer := range storers {
    1.68 -		storer, err := storer.Factory(context.TODO())
    1.69 +	for _, factory := range storerFactories {
    1.70 +		storer, err := factory.NewStorer(context.TODO())
    1.71  		if err != nil {
    1.72 -			t.Fatalf("Fatal error creating %T: %+v\n", storer, err)
    1.73 +			t.Fatalf("Fatal error creating Storer from %T: %+v\n", factory, err)
    1.74  		}
    1.75  
    1.76  		devices := []Device{
    1.77 @@ -141,7 +146,7 @@
    1.78  			t.Errorf("Expected new inserts to not be in results, got %+v from %T\n", results, storer)
    1.79  		}
    1.80  
    1.81 -		err = storer.Destroy(context.TODO())
    1.82 +		err = factory.TeardownStorer(storer, context.TODO())
    1.83  		if err != nil {
    1.84  			t.Errorf("Error cleaning up after %T: %+v\n", storer, err)
    1.85  		}
    1.86 @@ -149,10 +154,10 @@
    1.87  }
    1.88  
    1.89  func TestCreateAndListDevicesByOwner(t *testing.T) {
    1.90 -	for _, storer := range storers {
    1.91 -		storer, err := storer.Factory(context.TODO())
    1.92 +	for _, factory := range storerFactories {
    1.93 +		storer, err := factory.NewStorer(context.TODO())
    1.94  		if err != nil {
    1.95 -			t.Fatalf("Fatal error creating %T: %+v\n", storer, err)
    1.96 +			t.Fatalf("Fatal error creating Storer from %T: %+v\n", factory, err)
    1.97  		}
    1.98  
    1.99  		owner1, owner2 := uuid.NewID(), uuid.NewID()
   1.100 @@ -204,7 +209,7 @@
   1.101  			t.Errorf("Expected %s of %s to be %v, got %v from %T\n", field, devices[1].Name, expected, result, storer)
   1.102  		}
   1.103  
   1.104 -		err = storer.Destroy(context.TODO())
   1.105 +		err = factory.TeardownStorer(storer, context.TODO())
   1.106  		if err != nil {
   1.107  			t.Errorf("Error cleaning up after %T: %+v\n", storer, err)
   1.108  		}