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/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) { 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..d8c33a3b293 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.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