-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add from_hash and from_json helpers to Schemas
- Loading branch information
1 parent
366f39e
commit 79ea51e
Showing
2 changed files
with
30 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,33 @@ | ||
# typed: true | ||
|
||
class SchemaTest < Minitest::Test | ||
def test_from_struct_returns_schema | ||
expected_schema = Typed::Schema.new( | ||
def setup | ||
@schema = Typed::Schema.new( | ||
fields: [ | ||
Typed::Field.new(name: :name, type: String), | ||
Typed::Field.new(name: :age, type: Integer), | ||
Typed::Field.new(name: :job, type: Job, required: false) | ||
], | ||
target: Person | ||
) | ||
@person = Person.new(name: "Max", age: 29) | ||
end | ||
|
||
def test_from_struct_returns_schema | ||
assert_equal(@schema, Typed::Schema.from_struct(Person)) | ||
end | ||
|
||
def test_from_hash_create_struct | ||
result = @schema.from_hash({name: "Max", age: 29}) | ||
|
||
assert_success(result) | ||
assert_payload(@person, result) | ||
end | ||
|
||
def test_from_json_creates_struct | ||
result = @schema.from_json('{"name": "Max", "age": 29}') | ||
|
||
assert_equal(expected_schema, Typed::Schema.from_struct(Person)) | ||
assert_success(result) | ||
assert_payload(@person, result) | ||
end | ||
end |