Skip to content

Commit

Permalink
test: Prove that typed array serialization works
Browse files Browse the repository at this point in the history
  • Loading branch information
maxveldink committed Mar 14, 2024
1 parent 795ddd9 commit ca4b3cb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions test/support/structs/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_relative "city"

class Country < T::Struct
include ActsAsComparable

const :name, String
const :cities, T::Array[City]
end
Expand Down
30 changes: 29 additions & 1 deletion test/typed/hash_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ def test_with_boolean_it_can_serialize
assert_payload({name: "New York", capital: false}, result)
end

def test_with_array_it_can_serialize
result = Typed::HashSerializer.new(schema: Typed::Schema.from_struct(Country)).serialize(US_COUNTRY)

assert_success(result)
assert_payload({name: "US", cities: [NEW_YORK_CITY, DC_CITY]}, result)
end

def test_with_array_it_can_deep_serialize
result = Typed::HashSerializer.new(schema: Typed::Schema.from_struct(Country), should_serialize_values: true).serialize(US_COUNTRY)

assert_success(result)
assert_payload({name: "US", cities: [{name: "New York", capital: false}, {name: "DC", capital: true}]}, result)
end

def test_when_struct_given_is_not_of_target_type_returns_failure
result = @serializer.serialize(Job.new(title: "Testing", salary: 90_00))

Expand Down Expand Up @@ -67,8 +81,22 @@ def test_with_boolean_it_can_deserialize
assert_payload(NEW_YORK_CITY, result)
end

def test_with_array_it_can_deserialize
result = Typed::HashSerializer.new(schema: Typed::Schema.from_struct(Country)).deserialize({name: "US", cities: [NEW_YORK_CITY, DC_CITY]})

assert_success(result)
assert_payload(US_COUNTRY, result)
end

def test_with_array_it_can_deep_deserialize
result = Typed::HashSerializer.new(schema: Typed::Schema.from_struct(Country)).deserialize({name: "US", cities: [{name: "New York", capital: false}, {name: "DC", capital: true}]})

assert_success(result)
assert_payload(US_COUNTRY, result)
end

def test_it_can_deserialize_with_nested_object
result = @serializer.deserialize({name: "Alex", age: 31, ruby_rank: RubyRank::Brilliant, job: {title: "Software Developer", salary: 1_000_000_00}})
result = @serializer.deserialize({name: "Alex", age: 31, ruby_rank: "pretty", job: {title: "Software Developer", salary: 1_000_000_00}})

assert_success(result)
assert_payload(ALEX_PERSON, result)
Expand Down
14 changes: 14 additions & 0 deletions test/typed/json_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def test_with_boolean_it_can_serialize
assert_payload('{"name":"New York","capital":false}', result)
end

def test_with_array_it_can_serialize
result = Typed::JSONSerializer.new(schema: Typed::Schema.from_struct(Country)).serialize(US_COUNTRY)

assert_success(result)
assert_payload('{"name":"US","cities":[{"name":"New York","capital":false},{"name":"DC","capital":true}]}', result)
end

# Deserialize Tests

def test_it_can_simple_deserialize
Expand All @@ -46,6 +53,13 @@ def test_with_boolean_it_can_deserialize
assert_payload(NEW_YORK_CITY, result)
end

def test_with_array_it_can_deep_deserialize
result = Typed::JSONSerializer.new(schema: Typed::Schema.from_struct(Country)).deserialize('{"name":"US","cities":[{"name":"New York","capital":false},{"name":"DC","capital":true}]}')

assert_success(result)
assert_payload(US_COUNTRY, result)
end

def test_it_can_deserialize_with_nested_object
result = @serializer.deserialize('{"name":"Alex","age":31,"ruby_rank":"pretty","job":{"title":"Software Developer","salary":100000000}}')

Expand Down

0 comments on commit ca4b3cb

Please sign in to comment.