-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add round trip tests of Property Assignment methods (#1725)
- Loading branch information
1 parent
9d1f49f
commit 452439c
Showing
22 changed files
with
2,686 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
hack/generator/pkg/codegen/pipeline/property_assignment_test_cases.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package pipeline | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"golang.org/x/net/context" | ||
kerrors "k8s.io/apimachinery/pkg/util/errors" | ||
|
||
"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" | ||
"github.com/Azure/azure-service-operator/hack/generator/pkg/functions" | ||
"github.com/Azure/azure-service-operator/hack/generator/pkg/testcases" | ||
) | ||
|
||
// InjectPropertyAssignmentTestsID is the unique identifier for this stage | ||
const InjectPropertyAssignmentTestsID = "injectPropertyAssignmentTestCases" | ||
|
||
func InjectPropertyAssignmentTests(idFactory astmodel.IdentifierFactory) Stage { | ||
|
||
stage := MakeStage( | ||
InjectPropertyAssignmentTestsID, | ||
"Add test cases to verify PropertyAssignment functions", | ||
func(ctx context.Context, state *State) (*State, error) { | ||
factory := makePropertyAssignmentTestCaseFactory(idFactory) | ||
modifiedTypes := make(astmodel.Types) | ||
var errs []error | ||
for _, d := range state.Types() { | ||
if factory.NeedsTest(d) { | ||
updated, err := factory.AddTestTo(d) | ||
if err != nil { | ||
errs = append(errs, err) | ||
} else { | ||
modifiedTypes[updated.Name()] = updated | ||
} | ||
} | ||
} | ||
|
||
if len(errs) > 0 { | ||
return nil, kerrors.NewAggregate(errs) | ||
} | ||
|
||
return state.WithTypes(state.Types().OverlayWith(modifiedTypes)), nil | ||
}) | ||
|
||
stage.RequiresPrerequisiteStages( | ||
InjectPropertyAssignmentFunctionsStageID, // Need PropertyAssignmentFunctions to test | ||
InjectJsonSerializationTestsID, // We reuse the generators from the JSON tests | ||
) | ||
|
||
return stage | ||
} | ||
|
||
type propertyAssignmentTestCaseFactory struct { | ||
injector *astmodel.TestCaseInjector | ||
idFactory astmodel.IdentifierFactory | ||
} | ||
|
||
func makePropertyAssignmentTestCaseFactory(idFactory astmodel.IdentifierFactory) propertyAssignmentTestCaseFactory { | ||
return propertyAssignmentTestCaseFactory{ | ||
injector: astmodel.NewTestCaseInjector(), | ||
idFactory: idFactory, | ||
} | ||
} | ||
|
||
func (s *propertyAssignmentTestCaseFactory) NeedsTest(def astmodel.TypeDefinition) bool { | ||
container, ok := astmodel.AsFunctionContainer(def.Type()) | ||
if !ok { | ||
return false | ||
} | ||
|
||
for _, fn := range container.Functions() { | ||
if _, ok := fn.(*functions.PropertyAssignmentFunction); ok { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (s *propertyAssignmentTestCaseFactory) AddTestTo(def astmodel.TypeDefinition) (astmodel.TypeDefinition, error) { | ||
container, ok := astmodel.AsFunctionContainer(def.Type()) | ||
if !ok { | ||
return astmodel.TypeDefinition{}, errors.Errorf("expected %s to be a function container", def.Name()) | ||
} | ||
|
||
testCase := testcases.NewPropertyAssignmentTestCase(def.Name(), container, s.idFactory) | ||
return s.injector.Inject(def, testCase) | ||
} |
60 changes: 60 additions & 0 deletions
60
hack/generator/pkg/codegen/pipeline/property_assignment_test_cases_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package pipeline | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" | ||
"github.com/Azure/azure-service-operator/hack/generator/pkg/test" | ||
) | ||
|
||
func TestInjectPropertyAssignmentTests(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
|
||
idFactory := astmodel.NewIdentifierFactory() | ||
// Test Resource V1 | ||
|
||
specV1 := test.CreateSpec( | ||
test.Pkg2020, | ||
"Person", | ||
test.FullNameProperty, | ||
test.FamilyNameProperty, | ||
test.KnownAsProperty) | ||
statusV1 := test.CreateStatus(test.Pkg2020, "Person") | ||
resourceV1 := test.CreateResource(test.Pkg2020, "Person", specV1, statusV1) | ||
|
||
// Test Resource V2 | ||
|
||
specV2 := test.CreateSpec( | ||
test.Pkg2021, | ||
"Person", | ||
test.FullNameProperty, | ||
test.FamilyNameProperty, | ||
test.KnownAsProperty, | ||
test.ResidentialAddress2021, | ||
test.PostalAddress2021) | ||
statusV2 := test.CreateStatus(test.Pkg2021, "Person") | ||
resourceV2 := test.CreateResource(test.Pkg2021, "Person", specV2, statusV2) | ||
|
||
types := make(astmodel.Types) | ||
types.AddAll(resourceV1, specV1, statusV1, resourceV2, specV2, statusV2, test.Address2021) | ||
|
||
state := NewState().WithTypes(types) | ||
|
||
finalState, err := RunTestPipeline( | ||
state, | ||
CreateConversionGraph(), | ||
CreateStorageTypes(), | ||
InjectPropertyAssignmentFunctions(idFactory), | ||
InjectJsonSerializationTests(idFactory), | ||
InjectPropertyAssignmentTests(idFactory)) | ||
g.Expect(err).To(Succeed()) | ||
|
||
test.AssertPackagesGenerateExpectedCode(t, finalState.Types()) | ||
} |
Oops, something went wrong.