test/twin/hash_test.rb in disposable-0.4.3 vs test/twin/hash_test.rb in disposable-0.4.4
- old
+ new
@@ -31,13 +31,13 @@
model = Model.new(1, {})
model.inspect.must_equal "#<struct HashTest::Model id=1, content={}>"
song = Song.new(model)
song.id.must_equal 1
- song.content.title.must_equal nil
- song.content.band.name.must_equal nil
- song.content.band.label.location.must_equal nil
+ song.content.title.must_be_nil
+ song.content.band.name.must_be_nil
+ song.content.band.label.location.must_be_nil
song.content.releases.must_equal []
# model's hash hasn't changed.
model.inspect.must_equal "#<struct HashTest::Model id=1, content={}>"
end
@@ -46,13 +46,13 @@
model = Model.new(1)
model.inspect.must_equal "#<struct HashTest::Model id=1, content=nil>"
song = Song.new(model)
song.id.must_equal 1
- song.content.title.must_equal nil
- song.content.band.name.must_equal nil
- song.content.band.label.location.must_equal nil
+ song.content.title.must_be_nil
+ song.content.band.name.must_be_nil
+ song.content.band.label.location.must_be_nil
# model's hash hasn't changed.
model.inspect.must_equal "#<struct HashTest::Model id=1, content=nil>"
end
@@ -135,11 +135,11 @@
require "disposable/twin/coercion"
class Coercing < Disposable::Twin
include Property::Hash
feature Coercion
- property :id, type: Types::Coercible::Int
+ property :id, type: const_get("Types::Coercible::#{DRY_TYPES_INT_CONSTANT}")
property :content, field: :hash do
property :title
property :band do
property :name, type: Types::Coercible::String
end
@@ -159,11 +159,11 @@
class Unnesting < Disposable::Twin
feature Sync
include Property::Hash
property :id
- content=property :content, field: :hash do
+ property :content, field: :hash do
property :title
property :band do
property :name
property :label do
@@ -196,13 +196,60 @@
song.title = "Notorious"
song.title.must_equal "Notorious"
song.content.title.must_equal "Notorious"
# singular nested accessors
- song.band.name.must_equal nil
- song.content.band.name.must_equal nil
+ song.band.name.must_be_nil
+ song.content.band.name.must_be_nil
song.band.name = "Duran Duran"
song.band.name.must_equal "Duran Duran"
+ end
+ end
+
+ describe 'collection schema' do
+ AlbumModel = Struct.new(:id, :songs)
+ class Album < Disposable::Twin
+ feature Sync
+ feature Property::Hash
+
+ property :id
+ collection :songs, field: :hash do
+ property :title
+ property :band do
+ property :name
+
+ property :label do
+ property :location
+ end
+ end
+
+ collection :featured_artists do
+ property :name
+ end
+ end
+ end
+
+ it "exposes reader and writer" do
+ model = AlbumModel.new(1, [
+ { title: "Sherry", band: { name: 'The Four Seasons', label: { location: 'US' } }, featured_artists: [{ name: 'Frankie Valli' }, { name: 'The Variatones' }] },
+ { title: "Walk Like a Man", band: { name: 'The Four Seasons', label: { location: 'US' } }, featured_artists: [{ name: 'Frankie Valli' }] }
+ ])
+ contract = Album.new(model)
+
+ song1 = contract.songs[0]
+
+ song1.title.must_equal "Sherry"
+ song1.band.name.must_equal 'The Four Seasons'
+ song1.band.label.location.must_equal 'US'
+ song1.featured_artists[0].name.must_equal 'Frankie Valli'
+ song1.featured_artists[1].name.must_equal 'The Variatones'
+
+ song2 = contract.songs[1]
+
+ song2.title.must_equal "Walk Like a Man"
+ song2.band.name.must_equal 'The Four Seasons'
+ song2.band.label.location.must_equal 'US'
+ song2.featured_artists[0].name.must_equal 'Frankie Valli'
end
end
end
# fixme: make sure default hash is different for every invocation, and not created at compile time.