From 82a1cfedf3fce9e0965c0e5022e8d05c98dc33ba Mon Sep 17 00:00:00 2001 From: ericwenn Date: Fri, 20 Aug 2021 23:51:06 +0200 Subject: [PATCH] fix: more specific names for test suite structs --- internal/plugin/name.go | 9 +++++ internal/plugin/resource.go | 12 +++--- internal/plugin/service.go | 6 +-- .../freight/v1/testing/freight_service.go | 40 +++++++++---------- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/internal/plugin/name.go b/internal/plugin/name.go index 04fabd0..8c3a4de 100644 --- a/internal/plugin/name.go +++ b/internal/plugin/name.go @@ -3,8 +3,17 @@ package plugin import ( "go.einride.tech/aip/reflect/aipreflect" "google.golang.org/genproto/googleapis/api/annotations" + "google.golang.org/protobuf/reflect/protoreflect" ) func resourceType(r *annotations.ResourceDescriptor) string { return aipreflect.ResourceType(r.GetType()).Type() } + +func serviceTestSuiteName(service protoreflect.ServiceDescriptor) string { + return string(service.Name()) + "TestSuite" +} + +func resourceTestSuiteConfigName(resource *annotations.ResourceDescriptor) string { + return resourceType(resource) + "TestSuiteConfig" +} diff --git a/internal/plugin/resource.go b/internal/plugin/resource.go index bfefd5e..bbc4f03 100644 --- a/internal/plugin/resource.go +++ b/internal/plugin/resource.go @@ -39,7 +39,7 @@ func (r *resourceGenerator) generateFixture(f *protogen.GeneratedFile) { GoImportPath: r.service.Methods[0].Input.GoIdent.GoImportPath, }) - f.P("type ", resourceType(r.resource), " struct {") + f.P("type ", resourceTestSuiteConfigName(r.resource), " struct {") f.P("ctx ", context) f.P("service ", service) f.P("currParent int") @@ -88,7 +88,7 @@ func (r *resourceGenerator) generateTestMethod(f *protogen.GeneratedFile) { GoImportPath: "testing", }) - f.P("func (fx *", resourceType(r.resource), ") test(t *", testingT, ") {") + f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") test(t *", testingT, ") {") scope := suite.Scope{ Service: r.service, Resource: r.resource, @@ -117,7 +117,7 @@ func (r *resourceGenerator) generateTestCases(f *protogen.GeneratedFile) error { if !s.Enabled(scope) { continue } - f.P("func (fx *", resourceType(r.resource), ") test", s.Name, "(t *", testingT, ") {") + f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") test", s.Name, "(t *", testingT, ") {") for _, t := range s.Tests { if err := r.generateTestCase(f, t, scope); err != nil { return err @@ -173,7 +173,7 @@ func (r *resourceGenerator) generateSkip(f *protogen.GeneratedFile) { GoName: "Contains", GoImportPath: "strings", }) - f.P("func (fx *", resourceType(r.resource), ") maybeSkip(t *", testingT, ") {") + f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") maybeSkip(t *", testingT, ") {") f.P("for _, skip := range fx.Skip {") f.P("if ", stringsContains, "(t.Name(), skip) {") f.P("t.Skip(\"skipped because of .Skip\")") @@ -191,7 +191,7 @@ func (r *resourceGenerator) generateParentMethods(f *protogen.GeneratedFile) { GoName: "T", GoImportPath: "testing", }) - f.P("func (fx *", resourceType(r.resource), ") nextParent(t *", testingT, ", pristine bool) string {") + f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") nextParent(t *", testingT, ", pristine bool) string {") f.P("if pristine {") f.P("fx.currParent++") f.P("}") @@ -201,7 +201,7 @@ func (r *resourceGenerator) generateParentMethods(f *protogen.GeneratedFile) { f.P("return fx.Parents[fx.currParent]") f.P("}") f.P() - f.P("func (fx *", resourceType(r.resource), ") peekNextParent(t *", testingT, ") string {") + f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") peekNextParent(t *", testingT, ") string {") f.P("next := fx.currParent + 1") f.P("if next >= len(fx.Parents) {") f.P("t.Fatal(\"need at least\", next +1, \"parents\")") diff --git a/internal/plugin/service.go b/internal/plugin/service.go index b9dff8e..71971cd 100644 --- a/internal/plugin/service.go +++ b/internal/plugin/service.go @@ -41,7 +41,7 @@ func (s *serviceGenerator) generateFixture(f *protogen.GeneratedFile) { GoImportPath: s.service.Methods[0].Input.GoIdent.GoImportPath, }) - f.P("type ", s.service.GoName, " struct {") + f.P("type ", serviceTestSuiteName(s.service.Desc), " struct {") f.P("T *", testingT) f.P("// Server to test.") f.P("Server ", service) @@ -60,9 +60,9 @@ func (s *serviceGenerator) generateTestMethods(f *protogen.GeneratedFile) { GoName: "T", GoImportPath: "testing", }) - serviceFx := s.service.GoName + serviceFx := serviceTestSuiteName(s.service.Desc) for _, resource := range s.resources { - resourceFx := resourceType(resource) + resourceFx := resourceTestSuiteConfigName(resource) f.P("func (fx ", serviceFx, ") Test", resourceFx, "(ctx ", context, ", options ", resourceFx, ") {") f.P("fx.T.Run(", strconv.Quote(resourceType(resource)), ", func(t *", testingT, ") {") f.P("options.ctx = ctx") diff --git a/proto/gen/einride/example/freight/v1/testing/freight_service.go b/proto/gen/einride/example/freight/v1/testing/freight_service.go index ac83e75..47da656 100644 --- a/proto/gen/einride/example/freight/v1/testing/freight_service.go +++ b/proto/gen/einride/example/freight/v1/testing/freight_service.go @@ -17,13 +17,13 @@ import ( time "time" ) -type FreightService struct { +type FreightServiceTestSuite struct { T *testing.T // Server to test. Server v1.FreightServiceServer } -func (fx FreightService) TestShipper(ctx context.Context, options Shipper) { +func (fx FreightServiceTestSuite) TestShipperTestSuiteConfig(ctx context.Context, options ShipperTestSuiteConfig) { fx.T.Run("Shipper", func(t *testing.T) { options.ctx = ctx options.service = fx.Server @@ -31,7 +31,7 @@ func (fx FreightService) TestShipper(ctx context.Context, options Shipper) { }) } -func (fx FreightService) TestSite(ctx context.Context, options Site) { +func (fx FreightServiceTestSuite) TestSiteTestSuiteConfig(ctx context.Context, options SiteTestSuiteConfig) { fx.T.Run("Site", func(t *testing.T) { options.ctx = ctx options.service = fx.Server @@ -39,7 +39,7 @@ func (fx FreightService) TestSite(ctx context.Context, options Site) { }) } -type Shipper struct { +type ShipperTestSuiteConfig struct { ctx context.Context service v1.FreightServiceServer currParent int @@ -57,14 +57,14 @@ type Shipper struct { Skip []string } -func (fx *Shipper) test(t *testing.T) { +func (fx *ShipperTestSuiteConfig) test(t *testing.T) { t.Run("Create", fx.testCreate) t.Run("Get", fx.testGet) t.Run("Update", fx.testUpdate) t.Run("List", fx.testList) } -func (fx *Shipper) testCreate(t *testing.T) { +func (fx *ShipperTestSuiteConfig) testCreate(t *testing.T) { // Field create_time should be populated when the resource is created. t.Run("create time", func(t *testing.T) { @@ -173,7 +173,7 @@ func (fx *Shipper) testCreate(t *testing.T) { } -func (fx *Shipper) testGet(t *testing.T) { +func (fx *ShipperTestSuiteConfig) testGet(t *testing.T) { // Method should fail with InvalidArgument if no name is provided. t.Run("missing name", func(t *testing.T) { fx.maybeSkip(t) @@ -221,7 +221,7 @@ func (fx *Shipper) testGet(t *testing.T) { } -func (fx *Shipper) testUpdate(t *testing.T) { +func (fx *ShipperTestSuiteConfig) testUpdate(t *testing.T) { // Method should fail with InvalidArgument if no name is provided. t.Run("missing name", func(t *testing.T) { fx.maybeSkip(t) @@ -343,7 +343,7 @@ func (fx *Shipper) testUpdate(t *testing.T) { } -func (fx *Shipper) testList(t *testing.T) { +func (fx *ShipperTestSuiteConfig) testList(t *testing.T) { // Method should fail with InvalidArgument is provided page token is not valid. t.Run("invalid page token", func(t *testing.T) { @@ -365,7 +365,7 @@ func (fx *Shipper) testList(t *testing.T) { } -func (fx *Shipper) maybeSkip(t *testing.T) { +func (fx *ShipperTestSuiteConfig) maybeSkip(t *testing.T) { for _, skip := range fx.Skip { if strings.Contains(t.Name(), skip) { t.Skip("skipped because of .Skip") @@ -373,7 +373,7 @@ func (fx *Shipper) maybeSkip(t *testing.T) { } } -type Site struct { +type SiteTestSuiteConfig struct { ctx context.Context service v1.FreightServiceServer currParent int @@ -396,7 +396,7 @@ type Site struct { Skip []string } -func (fx *Site) test(t *testing.T) { +func (fx *SiteTestSuiteConfig) test(t *testing.T) { t.Run("Create", fx.testCreate) t.Run("Get", fx.testGet) t.Run("BatchGet", fx.testBatchGet) @@ -404,7 +404,7 @@ func (fx *Site) test(t *testing.T) { t.Run("List", fx.testList) } -func (fx *Site) testCreate(t *testing.T) { +func (fx *SiteTestSuiteConfig) testCreate(t *testing.T) { // Method should fail with InvalidArgument if no parent is provided. t.Run("missing parent", func(t *testing.T) { fx.maybeSkip(t) @@ -498,7 +498,7 @@ func (fx *Site) testCreate(t *testing.T) { } -func (fx *Site) testGet(t *testing.T) { +func (fx *SiteTestSuiteConfig) testGet(t *testing.T) { // Method should fail with InvalidArgument if no name is provided. t.Run("missing name", func(t *testing.T) { fx.maybeSkip(t) @@ -550,7 +550,7 @@ func (fx *Site) testGet(t *testing.T) { } -func (fx *Site) testBatchGet(t *testing.T) { +func (fx *SiteTestSuiteConfig) testBatchGet(t *testing.T) { // Method should fail with InvalidArgument if provided parent is invalid. t.Run("invalid parent", func(t *testing.T) { fx.maybeSkip(t) @@ -699,7 +699,7 @@ func (fx *Site) testBatchGet(t *testing.T) { } -func (fx *Site) testUpdate(t *testing.T) { +func (fx *SiteTestSuiteConfig) testUpdate(t *testing.T) { // Method should fail with InvalidArgument if no name is provided. t.Run("missing name", func(t *testing.T) { fx.maybeSkip(t) @@ -806,7 +806,7 @@ func (fx *Site) testUpdate(t *testing.T) { } -func (fx *Site) testList(t *testing.T) { +func (fx *SiteTestSuiteConfig) testList(t *testing.T) { // Method should fail with InvalidArgument if provided parent is invalid. t.Run("invalid parent", func(t *testing.T) { fx.maybeSkip(t) @@ -950,7 +950,7 @@ func (fx *Site) testList(t *testing.T) { } -func (fx *Site) nextParent(t *testing.T, pristine bool) string { +func (fx *SiteTestSuiteConfig) nextParent(t *testing.T, pristine bool) string { if pristine { fx.currParent++ } @@ -960,7 +960,7 @@ func (fx *Site) nextParent(t *testing.T, pristine bool) string { return fx.Parents[fx.currParent] } -func (fx *Site) peekNextParent(t *testing.T) string { +func (fx *SiteTestSuiteConfig) peekNextParent(t *testing.T) string { next := fx.currParent + 1 if next >= len(fx.Parents) { t.Fatal("need at least", next+1, "parents") @@ -968,7 +968,7 @@ func (fx *Site) peekNextParent(t *testing.T) string { return fx.Parents[next] } -func (fx *Site) maybeSkip(t *testing.T) { +func (fx *SiteTestSuiteConfig) maybeSkip(t *testing.T) { for _, skip := range fx.Skip { if strings.Contains(t.Name(), skip) { t.Skip("skipped because of .Skip")