-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
pixar-oss
merged 5 commits into
PixarAnimationStudios:dev
from
roggiezhang-nv:add_bind_for_usdvalidation_error
Aug 24, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
102a3e2
Register UsdValidationErrorType as TfEnum
roggiezhang-nv fc17b6e
Add python binding for UsdValidatorMetadata
roggiezhang-nv 2ef05c1
Use getter/setter to access keywords and schemeTypes
roggiezhang-nv 06be835
Make property bindings of UsdValidatorMetadata immutable
roggiezhang-nv a048ccd
Add python binding for UsdValidationError
roggiezhang-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix them.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Thanks.