-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from cwacek/fix/229
feature: Add support for the const keyword.
- Loading branch information
Showing
5 changed files
with
96 additions
and
5 deletions.
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
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,51 @@ | ||
import pytest | ||
|
||
import python_jsonschema_objects as pjo | ||
|
||
|
||
def test_const_properties(): | ||
schema = { | ||
"title": "Example", | ||
"type": "object", | ||
"properties": { | ||
"url": { | ||
"type": "string", | ||
"default": "https://example.com/your-username/my-project", | ||
}, | ||
"type": {"type": "string", "const": "git"}, | ||
}, | ||
} | ||
|
||
ob = pjo.ObjectBuilder(schema) | ||
ns1 = ob.build_classes() | ||
ex = ns1.Example() | ||
ex.url = "can be anything" | ||
|
||
# we expect the value to be set already for const values | ||
assert ex.type == "git" | ||
with pytest.raises(pjo.ValidationError): | ||
# Trying to set the value to something else should throw validation errors | ||
ex.type = "mercurial" | ||
|
||
# setting the value to the const value is a no-op, but permitted | ||
ex.type = "git" | ||
|
||
|
||
def test_const_bare_type(): | ||
schema = { | ||
"title": "Example", | ||
"type": "string", | ||
"const": "I stand alone", | ||
} | ||
|
||
ob = pjo.ObjectBuilder(schema) | ||
ns1 = ob.build_classes() | ||
ex = ns1.Example("I stand alone") | ||
# we expect the value to be set already for const values | ||
assert ex == "I stand alone" | ||
with pytest.raises(pjo.ValidationError): | ||
# Trying to set the value to something else should throw validation errors | ||
ex = ns1.Example("mercurial") | ||
|
||
# setting the value to the const value is a no-op, but permitted | ||
ex = ns1.Example("I stand alone") |
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