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

Add python binding for UsdValidationError #3236

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
16 changes: 16 additions & 0 deletions pxr/usd/usd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ pxr_library(usd
wrapTyped.cpp
wrapUsdFileFormat.cpp
wrapUtils.cpp
wrapValidationError.cpp
wrapValidator.cpp
wrapVariantSets.cpp
wrapVersion.cpp
wrapZipFile.cpp
Expand Down Expand Up @@ -272,6 +274,8 @@ pxr_test_scripts(
testenv/testUsdTimeSamples.py
testenv/testUsdTimeValueAuthoring.py
testenv/testUsdUsdzFileFormat.py
testenv/testUsdValidationError.py
testenv/testUsdValidatorMetadata.py
testenv/testUsdValueClips.py
testenv/testUsdVariantEditing.py
testenv/testUsdVariantFallbacks.py
Expand Down Expand Up @@ -1332,3 +1336,15 @@ pxr_register_test(testUsdOpaqueAttributes
COMMAND "${CMAKE_INSTALL_PREFIX}/tests/testUsdOpaqueAttributes"
EXPECTED_RETURN_CODE 0
)

pxr_register_test(testUsdValidatorMetadata
PYTHON
COMMAND "${CMAKE_INSTALL_PREFIX}/tests/testUsdValidatorMetadata"
EXPECTED_RETURN_CODE 0
)

pxr_register_test(testUsdValidationError
PYTHON
COMMAND "${CMAKE_INSTALL_PREFIX}/tests/testUsdValidationError"
EXPECTED_RETURN_CODE 0
)
2 changes: 2 additions & 0 deletions pxr/usd/usd/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ TF_WRAP_MODULE
TF_WRAP(UsdSpecializes);
TF_WRAP(UsdPrimRange);
TF_WRAP(UsdVariantSets);
TF_WRAP(UsdValidationError);
TF_WRAP(UsdValidator);

// SchemaBase, APISchemaBase and subclasses.
TF_WRAP(UsdSchemaBase);
Expand Down
210 changes: 210 additions & 0 deletions pxr/usd/usd/testenv/testUsdValidationError.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#!/pxrpythonsubst
#
# Copyright 2024 Pixar
#
# Licensed under the terms set forth in the LICENSE.txt file available at
# https://openusd.org/license.

import unittest

from pxr import Plug, Sdf, Usd


class TestUsdValidationError(unittest.TestCase):
def test_create_default_error_site(self):
Copy link
Contributor

@tallytalwar tallytalwar Aug 21, 2024

Choose a reason for hiding this comment

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

Since I have started syncing this one internally, I am addressing these but as a fyi the naming convention we seem to use here is test_CreateDefaultErrorSite, and _VerifyErrorSiteWithLayer, etc. Refer few examples here: https://github.com/PixarAnimationStudios/OpenUSD/blob/release/pxr/usd/usd/testenv/testUsdStage.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll fix them.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @roggiezhang-nv. I have already updated these internally for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Thanks.

errorSite = Usd.ValidationErrorSite()
self.assertFalse(errorSite.IsValid())
self.assertFalse(errorSite.IsValidSpecInLayer())
self.assertFalse(errorSite.IsPrim())
self.assertFalse(errorSite.IsProperty())
self.assertFalse(errorSite.GetPropertySpec())
self.assertFalse(errorSite.GetPrimSpec())
self.assertFalse(errorSite.GetProperty())
self.assertFalse(errorSite.GetPrim())
self.assertFalse(errorSite.GetLayer())
self.assertFalse(errorSite.GetStage())

def _verify_error_site_with_layer(self, errorSite: Usd.ValidationErrorSite, layer: Sdf.Layer, objectPath: Sdf.Path):
self.assertTrue(errorSite.IsValid())
self.assertTrue(errorSite.IsValidSpecInLayer())
self.assertFalse(errorSite.IsPrim())
self.assertFalse(errorSite.IsProperty())
expected_property_spec = layer.GetPropertyAtPath(objectPath) if objectPath.IsPropertyPath() else None
self.assertEqual(errorSite.GetPropertySpec(), expected_property_spec)
expected_prim_spec = layer.GetPrimAtPath(objectPath) if objectPath.IsPrimPath() else None
self.assertEqual(errorSite.GetPrimSpec(), expected_prim_spec)
self.assertFalse(errorSite.GetProperty())
self.assertFalse(errorSite.GetPrim())
self.assertEqual(errorSite.GetLayer(), layer)
self.assertFalse(errorSite.GetStage())

def test_create_error_site_with_layer_and_prim_spec(self):
stage = Usd.Stage.CreateInMemory()
test_prim_path = Sdf.Path("/test")
stage.DefinePrim(test_prim_path, "Xform")
errorSite = Usd.ValidationErrorSite(stage.GetRootLayer(), test_prim_path)
self._verify_error_site_with_layer(errorSite, stage.GetRootLayer(), test_prim_path)

def test_create_error_site_with_layer_and_property_spec(self):
stage = Usd.Stage.CreateInMemory()
test_prim_path = Sdf.Path("/test")
test_prim = stage.DefinePrim(test_prim_path, "Xform")
test_attr = test_prim.CreateAttribute("attr", Sdf.ValueTypeNames.Int)
test_attr_path = test_attr.GetPath()
errorSite = Usd.ValidationErrorSite(stage.GetRootLayer(), test_attr_path)
self._verify_error_site_with_layer(errorSite, stage.GetRootLayer(), test_attr_path)

def _verify_error_site_with_stage(self, errorSite: Usd.ValidationErrorSite, stage: Usd.Stage, objectPath: Sdf.Path):
self.assertTrue(errorSite.IsValid())
self.assertFalse(errorSite.IsValidSpecInLayer())
self.assertEqual(errorSite.IsPrim(), objectPath.IsPrimPath())
self.assertEqual(errorSite.IsProperty(), objectPath.IsPropertyPath())
self.assertFalse(errorSite.GetPropertySpec())
self.assertFalse(errorSite.GetPrimSpec())
expected_property = stage.GetPropertyAtPath(objectPath) if objectPath.IsPropertyPath() else Usd.Property()
self.assertEqual(errorSite.GetProperty(), expected_property)
expected_prim = stage.GetPrimAtPath(objectPath) if objectPath.IsPrimPath() else Usd.Prim()
self.assertEqual(errorSite.GetPrim(), expected_prim)
self.assertFalse(errorSite.GetLayer())
self.assertEqual(errorSite.GetStage(), stage)

def _verify_error_site_with_stage_and_layer(self, errorSite: Usd.ValidationErrorSite, stage: Usd.Stage, layer: Sdf.Layer, objectPath: Sdf.Path):
self.assertTrue(errorSite.IsValid())
self.assertTrue(errorSite.IsValidSpecInLayer())
self.assertEqual(errorSite.IsPrim(), objectPath.IsPrimPath())
self.assertEqual(errorSite.IsProperty(), objectPath.IsPropertyPath())

expected_property_spec = layer.GetPropertyAtPath(objectPath) if objectPath.IsPropertyPath() else None
self.assertEqual(expected_property_spec, errorSite.GetPropertySpec())
expected_prim_spec = layer.GetPrimAtPath(objectPath) if objectPath.IsPrimPath() else None
self.assertEqual(expected_prim_spec, errorSite.GetPrimSpec())
expected_property = stage.GetPropertyAtPath(objectPath) if objectPath.IsPropertyPath() else Usd.Property()
self.assertEqual(expected_property, errorSite.GetProperty())
expected_prim = stage.GetPrimAtPath(objectPath) if objectPath.IsPrimPath() else Usd.Prim()
self.assertEqual(expected_prim, errorSite.GetPrim())

self.assertEqual(errorSite.GetLayer(), layer)
self.assertEqual(errorSite.GetStage(), stage)

def test_create_error_site_with_stage_and_prim(self):
stage = Usd.Stage.CreateInMemory()
test_prim_path = Sdf.Path("/test")
stage.DefinePrim(test_prim_path, "Xform")
errorSite = Usd.ValidationErrorSite(stage, test_prim_path)
self._verify_error_site_with_stage(errorSite, stage, test_prim_path)

# With layer also
errorSite = Usd.ValidationErrorSite(stage, test_prim_path, stage.GetRootLayer())
self._verify_error_site_with_stage_and_layer(errorSite, stage, stage.GetRootLayer(), test_prim_path)

def test_create_error_site_with_stage_and_property(self):
stage = Usd.Stage.CreateInMemory()
test_prim_path = Sdf.Path("/test")
test_prim = stage.DefinePrim(test_prim_path, "Xform")
test_attr = test_prim.CreateAttribute("attr", Sdf.ValueTypeNames.Int)
test_attr_path = test_attr.GetPath()
errorSite = Usd.ValidationErrorSite(stage, test_attr_path)
self._verify_error_site_with_stage(errorSite, stage, test_attr_path)

# With layer also
errorSite = Usd.ValidationErrorSite(stage, test_attr_path, stage.GetRootLayer())
self._verify_error_site_with_stage_and_layer(errorSite, stage, stage.GetRootLayer(), test_attr_path)

def test_create_error_site_with_invalid_args(self):
stage = Usd.Stage.CreateInMemory()
test_prim_path = Sdf.Path("/test")
stage.DefinePrim(test_prim_path, "Xform")
errors = {
"Wrong Stage Type": {
"stage": "wrong stage",
"layer": stage.GetRootLayer(),
"objectPath": test_prim_path
},
"Wrong Layer Type": {
"stage": stage,
"layer": "wrong layer",
"objectPath": test_prim_path
},
"Wrong Path Type": {
"stage": stage,
"layer": stage.GetRootLayer(),
"objectPath": 123
},
}

for error_category, args in errors.items():
with self.subTest(errorType=error_category):
with self.assertRaises(Exception):
Usd.ValidationErrorSite(**args)

def _verify_validation_error(self, error, errorType=Usd.ValidationErrorType.None_, errorSites=[], errorMessage=""):
self.assertEqual(error.GetType(), errorType)
self.assertEqual(error.GetSites(), errorSites)
self.assertEqual(error.GetMessage(), errorMessage)
if errorType != Usd.ValidationErrorType.None_:
self.assertTrue(error.GetErrorAsString())
self.assertFalse(error.HasNoError())
else:
self.assertFalse(error.GetErrorAsString())
self.assertTrue(error.HasNoError())

def test_create_default_validation_error(self):
validation_error = Usd.ValidationError()
self._verify_validation_error(validation_error)

def test_create_validation_error_with_keyword_args(self):
errors = [
{
"errorType": Usd.ValidationErrorType.None_,
"errorSites": [],
"errorMessage": ""
},
{
"errorType": Usd.ValidationErrorType.Error,
"errorSites": [Usd.ValidationErrorSite()],
"errorMessage": "This is an error."
},
{
"errorType": Usd.ValidationErrorType.Warn,
"errorSites": [Usd.ValidationErrorSite()],
"errorMessage": "This is a warning."
},
{
"errorType": Usd.ValidationErrorType.Info,
"errorSites": [Usd.ValidationErrorSite(), Usd.ValidationErrorSite()],
"errorMessage": "This is an info."
},
]

for error in errors:
with self.subTest(errorType=error["errorType"]):
validation_error = Usd.ValidationError(**error)
self._verify_validation_error(validation_error, **error)

def test_create_validation_error_with_invalid_args(self):
errors = {
"Wrong Error Type": {
"errorType": "wrong_type",
"errorSites": [],
"errorMessage": ""
},
"Wrong Sites Type": {
"errorType": Usd.ValidationErrorType.None_,
"errorSites": "wong_type",
"errorMessage": ""
},
"Wrong Message Type": {
"errorType": Usd.ValidationErrorType.None_,
"errorSites": [],
"errorMessage": 123
},
}

for error_category, error in errors.items():
with self.subTest(errorType=error_category):
with self.assertRaises(Exception):
Usd.ValidationError(**error)


if __name__ == "__main__":
unittest.main()
126 changes: 126 additions & 0 deletions pxr/usd/usd/testenv/testUsdValidatorMetadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/pxrpythonsubst
#
# Copyright 2024 Pixar
#
# Licensed under the terms set forth in the LICENSE.txt file available at
# https://openusd.org/license.

import unittest

from pxr import Plug, Sdf, Usd


class TestUsdValidatorMetadata(unittest.TestCase):
def _verify_metadata(
self,
metadata: Usd.ValidatorMetadata,
name="",
doc="",
keywords=[],
schemaTypes=[],
plugin=None,
isSuite=False
):
self.assertEqual(metadata.name, name)
self.assertEqual(metadata.doc, doc)
self.assertEqual(metadata.keywords, keywords)
self.assertEqual(metadata.schemaTypes, schemaTypes)
self.assertEqual(metadata.plugin, plugin)
self.assertEqual(metadata.isSuite, isSuite)

def test_create_default_metadata(self):
metadata = Usd.ValidatorMetadata()
self._verify_metadata(metadata)

def test_create_metadata_with_valid_keyword_args(self):
all_plugins = Plug.Registry().GetAllPlugins()
expected_plugin = all_plugins[0] if all_plugins else None
valid_metadatas = [
{
"name": "empty_validator"
},
{
"name": "validator1",
"doc": "This is a test validator.",
"keywords": ["validator1", "test"],
"schemaTypes": ["SomePrimType"],
"plugin": None,
"isSuite": False
},
{
"name": "validator2",
"doc": "This is another test validator.",
"keywords": ["validator2", "test"],
"schemaTypes": ["NewPrimType"],
"plugin": expected_plugin,
"isSuite": False
}
]

for args in valid_metadatas:
with self.subTest(name=args["name"]):
metadata = Usd.ValidatorMetadata(**args)
self._verify_metadata(metadata, **args)

def test_create_metadata_with_invalid_keyword_args(self):
invalid_metadatas = {
"Wrong Name Type": {
"name": 123
},
"Wrong Doc Type": {
"doc": 123
},
"Wrong Keywords Type": {
"keywords": 123
},
"Wrong Schema Types": {
"schemaTypes": 123
},
"Wrong Plugin Type": {
"plugin": 123
},
"Wrong IsSuite Type": {
"isSuite": "wrong type"
}
}

for error_category, args in invalid_metadatas.items():
with self.subTest(error_type=error_category):
with self.assertRaises(Exception):
Usd.ValidatorMetadata(**args)

def test_metadata_name_immutable(self):
metadata = Usd.ValidatorMetadata()
with self.assertRaises(Exception):
metadata.name = "test"

def test_metadata_doc_immutable(self):
metadata = Usd.ValidatorMetadata()
with self.assertRaises(Exception):
metadata.doc = "doc"

def test_metadata_keywords_immutable(self):
metadata = Usd.ValidatorMetadata()
with self.assertRaises(Exception):
metadata.keywords = ["keywords"]

def test_metadata_schemaTypes_immutable(self):
metadata = Usd.ValidatorMetadata()
with self.assertRaises(Exception):
metadata.schemaTypes = "PrimType1"

def test_metadata_plugin_immutable(self):
all_plugins = Plug.Registry().GetAllPlugins()
expected_plugin = all_plugins[0] if all_plugins else None
metadata = Usd.ValidatorMetadata()
with self.assertRaises(Exception):
metadata.plugin = expected_plugin

def test_metadata_is_suite_immutable(self):
metadata = Usd.ValidatorMetadata()
with self.assertRaises(Exception):
metadata.isSuite = True


if __name__ == "__main__":
unittest.main()
Loading