Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress classes causing test failures #1724

Merged
merged 4 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hack/generator/pkg/astmodel/std_references.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
ErrorsReference = MakeExternalPackageReference("errors")
FmtReference = MakeExternalPackageReference("fmt")
JsonReference = MakeExternalPackageReference("encoding/json")
OSReference = MakeExternalPackageReference("os")
ReflectReference = MakeExternalPackageReference("reflect")
TestingReference = MakeExternalPackageReference("testing")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might make sense to put this comment by the specific classes suppressions, and not here in the generic suppression handling?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case there are other reasons for suppression that aren't due to OneOf

// 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) {
Expand Down
14 changes: 11 additions & 3 deletions hack/generator/pkg/testcases/json_serialization_test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/leanovate/gopter"
"github.com/leanovate/gopter/gen"
"github.com/leanovate/gopter/prop"
"os"
"reflect"
"testing"
)
Expand All @@ -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
Expand Down