Skip to content

Commit

Permalink
Simplify tests for splits in negative test data (#31)
Browse files Browse the repository at this point in the history
We propagate the split of negative samples in
[aas-core3.0-testgen 08d3ec37c] into unserializable and invalid negative
examples. This makes the generation and future maintenance of the test
code easier in this SDK as well, since we now do not have to track the
individual negative cases.

In addition, we improve the error messages if the recorded test data is
missing to make it clearer for the programmer what needs to be fixed in
that case.

[aas-core3.0-testgen 08d3ec37c]: aas-core-works/aas-core3.0-testgen@08d3ec37c
  • Loading branch information
mristin authored May 31, 2024
1 parent c493f8f commit c24c635
Show file tree
Hide file tree
Showing 6,256 changed files with 2,661 additions and 1,105 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,29 @@ def _generate_for_self_contained(cls: intermediate.ConcreteClass) -> List[Stripp
Stripped(
f"""\
func {test_name}(t *testing.T) {{
{I}for _, cause := range causesForDeserializationFailure {{
{II}pths := aastesting.FindFilesBySuffixRecursively(
{III}filepath.Join(
{IIII}aastesting.TestDataDir,
{IIII}"Json",
{IIII}"SelfContained",
{IIII}"Unexpected",
{IIII}cause,
{III}{model_type_literal},
{I}pattern := filepath.Join(
{II}aastesting.TestDataDir,
{II}"Json",
{II}"SelfContained",
{II}"Unexpected",
{II}"Unserializable",
{II}"*", // This asterisk represents the cause.
{II}{model_type_literal},
{I})
{I}causeDirs, err := filepath.Glob(pattern)
{I}if err != nil {{
{II}panic(
{III}fmt.Sprintf(
{IIII}"Failed to find cause directories matching %s: %s",
{IIII}pattern, err.Error(),
{III}),
{II})
{I}}}
{I}for _, causeDir := range causeDirs {{
{II}pths := aastesting.FindFilesBySuffixRecursively(
{III}causeDir,
{III}".json",
{II})
{II}sort.Strings(pths)
Expand Down Expand Up @@ -233,16 +246,29 @@ def _generate_for_contained_in_container(
Stripped(
f"""\
func {test_name}(t *testing.T) {{
{I}for _, cause := range causesForDeserializationFailure {{
{II}pths := aastesting.FindFilesBySuffixRecursively(
{III}filepath.Join(
{IIII}aastesting.TestDataDir,
{IIII}"Json",
{IIII}{contained_in_literal},
{IIII}"Unexpected",
{IIII}cause,
{IIII}{model_type_literal},
{I}pattern := filepath.Join(
{II}aastesting.TestDataDir,
{II}"Json",
{II}{contained_in_literal},
{II}"Unexpected",
{II}"Unserializable",
{II}"*", // This asterisk represents the cause.
{II}{model_type_literal},
{I})
{I}causeDirs, err := filepath.Glob(pattern)
{I}if err != nil {{
{II}panic(
{III}fmt.Sprintf(
{IIII}"Failed to find cause directories matching %s: %s",
{IIII}pattern, err.Error(),
{III}),
{II})
{I}}}
{I}for _, causeDir := range causeDirs {{
{II}pths := aastesting.FindFilesBySuffixRecursively(
{III}causeDir,
{III}".json",
{II})
{II}sort.Strings(pths)
Expand Down
31 changes: 22 additions & 9 deletions _dev_scripts/test_codegen/generate_verification_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,29 @@ def _generate_for_cls(
Stripped(
f"""\
func {test_name}(t *testing.T) {{
{I}for _, cause := range causesForVerificationFailure {{
{II}pths := aastesting.FindFilesBySuffixRecursively(
{III}filepath.Join(
{IIII}aastesting.TestDataDir,
{IIII}"Json",
{IIII}{contained_in_literal},
{IIII}"Unexpected",
{IIII}cause,
{IIII}{model_type_literal},
{I}pattern := filepath.Join(
{II}aastesting.TestDataDir,
{II}"Json",
{II}{contained_in_literal},
{II}"Unexpected",
{II}"Invalid",
{II}"*", // This asterisk represents the cause.
{II}{model_type_literal},
{I})
{I}causeDirs, err := filepath.Glob(pattern)
{I}if err != nil {{
{II}panic(
{III}fmt.Sprintf(
{IIII}"Failed to find cause directories matching %s: %s",
{IIII}pattern, err.Error(),
{III}),
{II})
{I}}}
{I}for _, causeDir := range causeDirs {{
{II}pths := aastesting.FindFilesBySuffixRecursively(
{III}causeDir,
{III}".json",
{II})
{II}sort.Strings(pths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,29 @@ def _generate_for(
Stripped(
f"""\
func {test_name}(t *testing.T) {{
{I}for _, cause := range causesForDeserializationFailure {{
{II}pths := aastesting.FindFilesBySuffixRecursively(
{III}filepath.Join(
{IIII}aastesting.TestDataDir,
{IIII}"Xml",
{IIII}{contained_in_literal},
{IIII}"Unexpected",
{IIII}cause,
{IIII}{xml_class_name_literal},
{I}pattern := filepath.Join(
{II}aastesting.TestDataDir,
{II}"Xml",
{II}{contained_in_literal},
{II}"Unexpected",
{II}"Unserializable",
{II}"*", // This asterisk represents the cause.
{II}{xml_class_name_literal},
{I})
{I}causeDirs, err := filepath.Glob(pattern)
{I}if err != nil {{
{II}panic(
{III}fmt.Sprintf(
{IIII}"Failed to find cause directories matching %s: %s",
{IIII}pattern, err.Error(),
{III}),
{II})
{I}}}
{I}for _, causeDir := range causeDirs {{
{II}pths := aastesting.FindFilesBySuffixRecursively(
{III}causeDir,
{III}".xml",
{II})
{II}sort.Strings(pths)
Expand Down
4 changes: 0 additions & 4 deletions aastesting/common_jsonization.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,15 +535,11 @@ func MustLoadMinimalSubmodel() (result aastypes.ISubmodel) {
"minimal.json",
)

fmt.Printf("Must read jsonable\n")
jsonable := MustReadJsonable(pth)

container, err := aasjsonization.EnvironmentFromJsonable(
jsonable,
)
fmt.Printf("container: %v\n", container)
fmt.Printf("err: %v\n", err)

if err != nil {
panic(
fmt.Sprintf(
Expand Down
8 changes: 6 additions & 2 deletions aastesting/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import (
"strings"
)

const RecordModeEnvironmentVariableName string = "AAS_CORE_3_0_GOLANG_RECORD_MODE"

// NOTE (mristin, 2023-05-24):
// It is tedious to record manually all the expected error messages. Therefore we
// include this variable to steer the automatic recording. We intentionally
// intertwine the recording code with the test code to keep them close to each other
// so that they are easier to maintain.
var rM = os.Getenv("AAS_CORE_3_0_GOLANG_RECORD_MODE")
var rM = os.Getenv(RecordModeEnvironmentVariableName)
var RecordMode = rM == "1" || strings.ToLower(rM) == "true" || strings.ToLower(rM) == "on"

const TestDataDirEnvironmentVariableName string = "AAS_CORE_3_0_GOLANG_TEST_DATA_DIR"

func getTestDataDir() string {
variable := "AAS_CORE_3_0_GOLANG_TEST_DATA_DIR"
variable := TestDataDirEnvironmentVariableName
val, ok := os.LookupEnv(variable)
if !ok {
panic(
Expand Down
2 changes: 1 addition & 1 deletion jsonization/jsonization.go
Original file line number Diff line number Diff line change
Expand Up @@ -20859,7 +20859,7 @@ func dataSpecificationIEC61360ToMap(
return
}

// Serialize ``that`` instance to a JSON-able representation.
// Serialize that instance to a JSON-able representation.
//
// Return a structure which can be readily converted to JSON,
// or an error if some value could not be converted.
Expand Down
28 changes: 18 additions & 10 deletions jsonization/test/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func assertNoDeserializationError(
"but got: %v",
source, err,
)
return
}
return
}
Expand All @@ -46,6 +47,7 @@ func assertNoSerializationError(
"but got: %v",
source, err,
)
return
}
return
}
Expand Down Expand Up @@ -77,25 +79,18 @@ func assertSerializationEqualsDeserialization(
otherText := string(otherBytes)

if thatText != otherText {
ok = false
t.Fatalf(
"The serialization of the de-serialized instance from %s does not equal "+
"the original JSON-able:\nOriginal:\n%s\nSerialized:\n%s",
source, thatText, otherText,
)
ok = false
return
}

return
}

var causesForDeserializationFailure = [...]string{
"TypeViolation",
"RequiredViolation",
"EnumViolation",
"NullViolation",
"UnexpectedAdditionalProperty",
}

// Assert that there is a de-serialization error.
//
// If [aastesting.RecordMode] is set, the de-serialization error is re-recorded
Expand Down Expand Up @@ -145,6 +140,18 @@ func assertDeserializationErrorEqualsExpectedOrRecord(
)
}
} else {
_, err := os.Stat(expectedPth)
if err != nil {
ok = false
t.Fatalf(
"Failed to stat the file %s: %s; if the file does not exist, "+
"you probably want to record the test data by "+
"setting the environment variable %s",
expectedPth, err.Error(), aastesting.RecordModeEnvironmentVariableName,
)
return
}

bb, err := os.ReadFile(expectedPth)
if err != nil {
panic(
Expand All @@ -162,12 +169,13 @@ func assertDeserializationErrorEqualsExpectedOrRecord(
expected = strings.Replace(expected, "\r", "", -1)

if expected != got {
ok = false
t.Fatalf(
"What we got differs from the expected in %s. "+
"We got:\n%s\nWe expected:\n%s",
expectedPth, got, expected,
)
ok = false
return
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ package jsonization_test
// Do NOT edit or append.

import (
"fmt"
aastesting "github.com/aas-core-works/aas-core3.0-golang/aastesting"
aasjsonization "github.com/aas-core-works/aas-core3.0-golang/jsonization"
"testing"
)

func TestHasSemanticsRoundTripOKOverDescendant(t *testing.T) {
fmt.Print("Hi!\n")
instance := aastesting.MustLoadMinimalRelationshipElement()

fmt.Printf("Instance: %v\n", instance)

jsonable, err := aasjsonization.ToJsonable(instance)
if err != nil {
t.Fatalf(
Expand Down
Loading

0 comments on commit c24c635

Please sign in to comment.