From 2176a9b9834c5b9faee7bf59ed8909c93528250e Mon Sep 17 00:00:00 2001 From: Bevan Arps Date: Tue, 17 Aug 2021 14:00:23 +1200 Subject: [PATCH 1/3] Use FormatedReporter for output --- hack/generator/pkg/astmodel/std_references.go | 1 + .../pkg/testcases/json_serialization_test_case.go | 14 +++++++++++--- .../person_test.golden | 3 ++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/hack/generator/pkg/astmodel/std_references.go b/hack/generator/pkg/astmodel/std_references.go index 62dec04b6fa..8c5093dc9f8 100644 --- a/hack/generator/pkg/astmodel/std_references.go +++ b/hack/generator/pkg/astmodel/std_references.go @@ -14,6 +14,7 @@ var ( ErrorsReference = MakeExternalPackageReference("errors") FmtReference = MakeExternalPackageReference("fmt") JsonReference = MakeExternalPackageReference("encoding/json") + OSReference = MakeExternalPackageReference("os") ReflectReference = MakeExternalPackageReference("reflect") TestingReference = MakeExternalPackageReference("testing") diff --git a/hack/generator/pkg/testcases/json_serialization_test_case.go b/hack/generator/pkg/testcases/json_serialization_test_case.go index e1aa94ad072..c657d60c76b 100644 --- a/hack/generator/pkg/testcases/json_serialization_test_case.go +++ b/hack/generator/pkg/testcases/json_serialization_test_case.go @@ -120,7 +120,8 @@ func (o *JSONSerializationTestCase) RequiredImports() *astmodel.PackageImportSet result := astmodel.NewPackageImportSet() // Standard Go Packages - result.AddImportsOfReferences(astmodel.JsonReference, astmodel.ReflectReference, astmodel.TestingReference) + result.AddImportsOfReferences( + astmodel.JsonReference, astmodel.OSReference, astmodel.ReflectReference, astmodel.TestingReference) // Cmp result.AddImportsOfReferences(astmodel.CmpReference, astmodel.CmpOptsReference) @@ -167,6 +168,7 @@ func (o *JSONSerializationTestCase) createTestRunner(codegenContext *astmodel.Co ) gopterPackage := codegenContext.MustGetImportedPackageName(astmodel.GopterReference) + osPackage := codegenContext.MustGetImportedPackageName(astmodel.OSReference) propPackage := codegenContext.MustGetImportedPackageName(astmodel.GopterPropReference) testingPackage := codegenContext.MustGetImportedPackageName(astmodel.TestingReference) @@ -208,8 +210,14 @@ func (o *JSONSerializationTestCase) createTestRunner(codegenContext *astmodel.Co testName, propForAll) - // properties.TestingRun(t) - runTests := astbuilder.InvokeQualifiedFunc(propertiesLocal, testingRunMethod, t) + // properties.TestingRun(t, gopter.NewFormatedReporter(true, 160, os.Stdout)) + createReporter := astbuilder.CallQualifiedFunc( + gopterPackage, + "NewFormatedReporter", + dst.NewIdent("true"), + astbuilder.IntLiteral(240), + astbuilder.Selector(dst.NewIdent(osPackage), "Stdout")) + runTests := astbuilder.InvokeQualifiedFunc(propertiesLocal, testingRunMethod, t, createReporter) // Define our function fn := astbuilder.NewTestFuncDetails( diff --git a/hack/generator/pkg/testcases/testdata/TestJSONSerializationTestCase_AsFunc/person_test.golden b/hack/generator/pkg/testcases/testdata/TestJSONSerializationTestCase_AsFunc/person_test.golden index d4ad32239ba..91fff11a76c 100644 --- a/hack/generator/pkg/testcases/testdata/TestJSONSerializationTestCase_AsFunc/person_test.golden +++ b/hack/generator/pkg/testcases/testdata/TestJSONSerializationTestCase_AsFunc/person_test.golden @@ -12,6 +12,7 @@ import ( "github.com/leanovate/gopter" "github.com/leanovate/gopter/gen" "github.com/leanovate/gopter/prop" + "os" "reflect" "testing" ) @@ -23,7 +24,7 @@ func Test_Person_Spec_WhenSerializedToJson_DeserializesAsEqual(t *testing.T) { properties.Property( "Round trip of Person_Spec via JSON returns original", prop.ForAll(RunJSONSerializationTestForPersonSpec, PersonSpecGenerator())) - properties.TestingRun(t) + properties.TestingRun(t, gopter.NewFormattedReporter(true, 240, os.Stdout)) } // RunJSONSerializationTestForPersonSpec runs a test to see if a specific instance of Person_Spec round trips to JSON and back losslessly From eb8c3232fa0513163ef3d5a6e3a0647ea298065c Mon Sep 17 00:00:00 2001 From: Bevan Arps Date: Tue, 17 Aug 2021 14:00:41 +1200 Subject: [PATCH 2/3] Suppress problem ARM types --- .../pipeline/json_serialization_test_cases.go | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/hack/generator/pkg/codegen/pipeline/json_serialization_test_cases.go b/hack/generator/pkg/codegen/pipeline/json_serialization_test_cases.go index e495ea40c2b..432fd936015 100644 --- a/hack/generator/pkg/codegen/pipeline/json_serialization_test_cases.go +++ b/hack/generator/pkg/codegen/pipeline/json_serialization_test_cases.go @@ -48,22 +48,47 @@ func InjectJsonSerializationTests(idFactory astmodel.IdentifierFactory) Stage { } type objectSerializationTestCaseFactory struct { - injector *astmodel.TestCaseInjector - idFactory astmodel.IdentifierFactory + injector *astmodel.TestCaseInjector + idFactory astmodel.IdentifierFactory + suppressions []string } func makeObjectSerializationTestCaseFactory(idFactory astmodel.IdentifierFactory) objectSerializationTestCaseFactory { result := objectSerializationTestCaseFactory{ injector: astmodel.NewTestCaseInjector(), idFactory: idFactory, + suppressions: []string{ + "DatabaseAccounts_SpecARM", + "DatabaseAccountCreateUpdatePropertiesARM", + "BackupPolicyARM", + }, } return result } +// NeedsTest returns true if we should generate a testcase for the specified definition func (s *objectSerializationTestCaseFactory) NeedsTest(def astmodel.TypeDefinition) bool { _, ok := astmodel.AsPropertyContainer(def.Type()) - return ok + if !ok { + // Can only generate tests for property containers + return false + } + + // Check for types that we need to suppress - these are ARM types that don't currently round trip because they're + // OneOf implementations that are only used in one direction. + // + // See https://github.com/Azure/azure-service-operator/issues/1721 for more information + // + result := true + for _, s := range s.suppressions { + if def.Name().Name() == s { + result = false + break + } + } + + return result } func (s *objectSerializationTestCaseFactory) AddTestTo(def astmodel.TypeDefinition) (astmodel.TypeDefinition, error) { From 6e7f7c9d8c8e721e751b167ec34d2615e06c0478 Mon Sep 17 00:00:00 2001 From: Bevan Arps Date: Tue, 17 Aug 2021 15:05:27 +1200 Subject: [PATCH 3/3] Update golden test --- .../TestJSONSerializationTestCase_AsFunc/person_test.golden | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/generator/pkg/testcases/testdata/TestJSONSerializationTestCase_AsFunc/person_test.golden b/hack/generator/pkg/testcases/testdata/TestJSONSerializationTestCase_AsFunc/person_test.golden index 91fff11a76c..d8c33a3b293 100644 --- a/hack/generator/pkg/testcases/testdata/TestJSONSerializationTestCase_AsFunc/person_test.golden +++ b/hack/generator/pkg/testcases/testdata/TestJSONSerializationTestCase_AsFunc/person_test.golden @@ -24,7 +24,7 @@ func Test_Person_Spec_WhenSerializedToJson_DeserializesAsEqual(t *testing.T) { properties.Property( "Round trip of Person_Spec via JSON returns original", prop.ForAll(RunJSONSerializationTestForPersonSpec, PersonSpecGenerator())) - properties.TestingRun(t, gopter.NewFormattedReporter(true, 240, os.Stdout)) + properties.TestingRun(t, gopter.NewFormatedReporter(true, 240, os.Stdout)) } // RunJSONSerializationTestForPersonSpec runs a test to see if a specific instance of Person_Spec round trips to JSON and back losslessly