ducky/devices

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

9:f5a9d5f8f28d Browse Files

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?

devices.go memstore.go memstore_test.go storer_test.go

     1.1 --- a/devices.go	Sun Nov 15 04:40:30 2015 -0800
     1.2 +++ b/devices.go	Fri Nov 20 01:14:58 2015 -0800
     1.3 @@ -76,10 +76,6 @@
     1.4  	DeleteDevices(ids []uuid.ID, c context.Context) error
     1.5  	CreateDevices(devices []Device, c context.Context) error
     1.6  	ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error)
     1.7 -
     1.8 -	// These are used for testing only.
     1.9 -	Factory(c context.Context) (Storer, error)
    1.10 -	Destroy(c context.Context) error
    1.11  }
    1.12  
    1.13  type ErrDeviceAlreadyExists uuid.ID
     2.1 --- a/memstore.go	Sun Nov 15 04:40:30 2015 -0800
     2.2 +++ b/memstore.go	Fri Nov 20 01:14:58 2015 -0800
     2.3 @@ -68,12 +68,3 @@
     2.4  	}
     2.5  	return devices, nil
     2.6  }
     2.7 -
     2.8 -func (m *Memstore) Factory(c context.Context) (Storer, error) {
     2.9 -	return NewMemstore(), nil
    2.10 -}
    2.11 -
    2.12 -func (m *Memstore) Destroy(c context.Context) error {
    2.13 -	m.devices = nil
    2.14 -	return nil
    2.15 -}
     3.1 --- a/memstore_test.go	Sun Nov 15 04:40:30 2015 -0800
     3.2 +++ b/memstore_test.go	Fri Nov 20 01:14:58 2015 -0800
     3.3 @@ -1,5 +1,27 @@
     3.4  package devices
     3.5  
     3.6 +import (
     3.7 +	"fmt"
     3.8 +
     3.9 +	"golang.org/x/net/context"
    3.10 +)
    3.11 +
    3.12  func init() {
    3.13 -	storers = append(storers, NewMemstore())
    3.14 +	storerFactories = append(storerFactories, MemstoreFactory{})
    3.15  }
    3.16 +
    3.17 +type MemstoreFactory struct {
    3.18 +}
    3.19 +
    3.20 +func (m MemstoreFactory) NewStorer(ctx context.Context) (Storer, error) {
    3.21 +	return NewMemstore(), nil
    3.22 +}
    3.23 +
    3.24 +func (m MemstoreFactory) TeardownStorer(storer Storer, ctx context.Context) error {
    3.25 +	memstorer, ok := storer.(*Memstore)
    3.26 +	if !ok {
    3.27 +		return fmt.Errorf("Storer was not a *Memstore, was a %T", storer)
    3.28 +	}
    3.29 +	memstorer.devices = nil
    3.30 +	return nil
    3.31 +}
     4.1 --- a/storer_test.go	Sun Nov 15 04:40:30 2015 -0800
     4.2 +++ b/storer_test.go	Fri Nov 20 01:14:58 2015 -0800
     4.3 @@ -8,7 +8,12 @@
     4.4  	"golang.org/x/net/context"
     4.5  )
     4.6  
     4.7 -var storers []Storer
     4.8 +type StorerFactory interface {
     4.9 +	NewStorer(ctx context.Context) (Storer, error)
    4.10 +	TeardownStorer(storer Storer, ctx context.Context) error
    4.11 +}
    4.12 +
    4.13 +var storerFactories []StorerFactory
    4.14  
    4.15  func compareDevices(device1, device2 Device) (ok bool, field string, expected, result interface{}) {
    4.16  	if !device1.ID.Equal(device2.ID) {
    4.17 @@ -36,10 +41,10 @@
    4.18  }
    4.19  
    4.20  func TestCreateAndGetDevices(t *testing.T) {
    4.21 -	for _, storer := range storers {
    4.22 -		storer, err := storer.Factory(context.TODO())
    4.23 +	for _, factory := range storerFactories {
    4.24 +		storer, err := factory.NewStorer(context.TODO())
    4.25  		if err != nil {
    4.26 -			t.Fatalf("Fatal error creating %T: %+v\n", storer, err)
    4.27 +			t.Fatalf("Fatal error creating Storer from %T: %+v\n", factory, err)
    4.28  		}
    4.29  
    4.30  		devices := []Device{
    4.31 @@ -72,7 +77,7 @@
    4.32  				t.Errorf("Expected %s of %s to be %v, got %v from %T\n", field, device.Name, expected, result, storer)
    4.33  			}
    4.34  		}
    4.35 -		err = storer.Destroy(context.TODO())
    4.36 +		err = factory.TeardownStorer(storer, context.TODO())
    4.37  		if err != nil {
    4.38  			t.Errorf("Error cleaning up after %T: %+v\n", storer, err)
    4.39  		}
    4.40 @@ -80,10 +85,10 @@
    4.41  }
    4.42  
    4.43  func TestGetDevicesNoErrorForMissing(t *testing.T) {
    4.44 -	for _, storer := range storers {
    4.45 -		storer, err := storer.Factory(context.TODO())
    4.46 +	for _, factory := range storerFactories {
    4.47 +		storer, err := factory.NewStorer(context.TODO())
    4.48  		if err != nil {
    4.49 -			t.Fatalf("Fatal error creatng %T: %+v\n", storer, err)
    4.50 +			t.Fatalf("Fatal error creatng Storer from %T: %+v\n", factory, err)
    4.51  		}
    4.52  
    4.53  		results, err := storer.GetDevices([]uuid.ID{uuid.NewID()}, context.TODO())
    4.54 @@ -93,7 +98,7 @@
    4.55  		if len(results) != 0 {
    4.56  			t.Errorf("Expected results to be empty, got %+v from %T instead\n", results, storer)
    4.57  		}
    4.58 -		err = storer.Destroy(context.TODO())
    4.59 +		err = factory.TeardownStorer(storer, context.TODO())
    4.60  		if err != nil {
    4.61  			t.Errorf("Error cleaning up after %T: %+v\n", storer, err)
    4.62  		}
    4.63 @@ -101,10 +106,10 @@
    4.64  }
    4.65  
    4.66  func TestCreateDevicesDuplicates(t *testing.T) {
    4.67 -	for _, storer := range storers {
    4.68 -		storer, err := storer.Factory(context.TODO())
    4.69 +	for _, factory := range storerFactories {
    4.70 +		storer, err := factory.NewStorer(context.TODO())
    4.71  		if err != nil {
    4.72 -			t.Fatalf("Fatal error creating %T: %+v\n", storer, err)
    4.73 +			t.Fatalf("Fatal error creating Storer from %T: %+v\n", factory, err)
    4.74  		}
    4.75  
    4.76  		devices := []Device{
    4.77 @@ -141,7 +146,7 @@
    4.78  			t.Errorf("Expected new inserts to not be in results, got %+v from %T\n", results, storer)
    4.79  		}
    4.80  
    4.81 -		err = storer.Destroy(context.TODO())
    4.82 +		err = factory.TeardownStorer(storer, context.TODO())
    4.83  		if err != nil {
    4.84  			t.Errorf("Error cleaning up after %T: %+v\n", storer, err)
    4.85  		}
    4.86 @@ -149,10 +154,10 @@
    4.87  }
    4.88  
    4.89  func TestCreateAndListDevicesByOwner(t *testing.T) {
    4.90 -	for _, storer := range storers {
    4.91 -		storer, err := storer.Factory(context.TODO())
    4.92 +	for _, factory := range storerFactories {
    4.93 +		storer, err := factory.NewStorer(context.TODO())
    4.94  		if err != nil {
    4.95 -			t.Fatalf("Fatal error creating %T: %+v\n", storer, err)
    4.96 +			t.Fatalf("Fatal error creating Storer from %T: %+v\n", factory, err)
    4.97  		}
    4.98  
    4.99  		owner1, owner2 := uuid.NewID(), uuid.NewID()
   4.100 @@ -204,7 +209,7 @@
   4.101  			t.Errorf("Expected %s of %s to be %v, got %v from %T\n", field, devices[1].Name, expected, result, storer)
   4.102  		}
   4.103  
   4.104 -		err = storer.Destroy(context.TODO())
   4.105 +		err = factory.TeardownStorer(storer, context.TODO())
   4.106  		if err != nil {
   4.107  			t.Errorf("Error cleaning up after %T: %+v\n", storer, err)
   4.108  		}