github.com/lkingland/gridd@v0.0.0-20230313082622-f3ae21fe9d22/client_test.go (about) 1 // Licensed under the Apache License, Version 2.0. See LICENSE file. 2 3 // +build !integration 4 5 package gridd_test 6 7 import ( 8 "context" 9 "testing" 10 11 "github.com/lkingland/gridd" 12 "github.com/lkingland/gridd/mock" 13 ) 14 15 // TestList ensures the list base case: no errors and an empty list, with the 16 // underlying provider's List method invoked. 17 func TestList(t *testing.T) { 18 var ( 19 p = mock.NewProvider() 20 c = gridd.New(p) 21 ) 22 names, err := c.List(context.Background()) 23 if err != nil { 24 t.Fatal(err) 25 } 26 if len(names) != 0 { 27 t.Fatalf("unexpected list item received. Expected 0, got %v", names) 28 } 29 if !p.ListInvoked { 30 t.Fatal("Provider not invoked") 31 } 32 } 33 34 // TestCreate ensures the Create base case: no errors and a call to Create 35 // invokes the underlying provider's Create with no errors. 36 func TestCreate(t *testing.T) { 37 var ( 38 p = mock.NewProvider() 39 c = gridd.New(p) 40 f = gridd.Function{} 41 ) 42 if err := c.Create(context.Background(), f); err != nil { 43 t.Fatal(err) 44 } 45 if !p.CreateInvoked { 46 t.Fatal("Provider not invoked") 47 } 48 } 49 50 // TestCreateDefaultsLanguage ensures that the default language of Gridd is 51 // provided to the underlying Provider, intended to override its default. 52 func TestCreateDefaultsLanguage(t *testing.T) { 53 var ( 54 p = mock.NewProvider() 55 c = gridd.New(p) 56 f = gridd.Function{} 57 ) 58 p.CreateFn = func(ctx context.Context, x gridd.Function) error { 59 if x.Language != gridd.DefaultLanguage { 60 t.Fatalf("Default language not applied. Expected '%v' got '%v'", 61 gridd.DefaultLanguage, x.Language) 62 } 63 return nil 64 } 65 if err := c.Create(context.Background(), f); err != nil { 66 t.Fatal(f) 67 } 68 if !p.CreateInvoked { 69 t.Fatal("Provider not invoked") 70 } 71 } 72 73 // TestDelete ensures the Update base case: no errors and a call to Delete 74 // invokes the underlying provider's implementation with no errors. 75 func TestUpdate(t *testing.T) { 76 var ( 77 p = mock.NewProvider() 78 c = gridd.New(p) 79 ) 80 if err := c.Delete(context.Background(), "myfunc"); err != nil { 81 t.Fatal(err) 82 } 83 if !p.DeleteInvoked { 84 t.Fatal("Provider not invoked") 85 } 86 } 87 88 // TODO TestUpdateEmpty