test/twin/composition_test.rb in disposable-0.0.9 vs test/twin/composition_test.rb in disposable-0.1.0
- old
+ new
@@ -1,45 +1,76 @@
require 'test_helper'
-require 'disposable/twin/composition'
-
+# Disposable::Twin::Composition.
class TwinCompositionTest < MiniTest::Spec
- class Request < Disposable::Twin::Composition
- property :song_title, :on => :song, :from => :title
- property :song_id, :on => :song, :from => :id
+ class Request < Disposable::Twin
+ include Sync
+ include Save
+ include Composition
- property :name, :on => :requester
- property :id, :on => :requester
+ property :song_title, on: :song, from: :title
+ property :song_id, on: :song, from: :id
- # map ...
-
- # def id
- # make map(name, options)
- # option :played?
+ property :name, on: :requester
+ property :id, on: :requester
+ property :captcha, readable: false, writeable: false, on: :requester # TODO: allow both, virtual with and without :on.
end
module Model
Song = Struct.new(:id, :title, :album)
Requester = Struct.new(:id, :name)
end
- let (:requester) { Model::Requester.new(1, "Greg Howe") }
- let (:song) { Model::Song.new(2, "Extraction") }
+ let (:requester) { Model::Requester.new(1, "Greg Howe").extend(Disposable::Saveable) }
+ let (:song) { Model::Song.new(2, "Extraction").extend(Disposable::Saveable) }
- let (:request) { Request.new(:song => song, :requester => requester) }
+ let (:request) { Request.new(song: song, requester: requester) }
- it { request.song_title.must_equal "Extraction" }
- it { request.name.must_equal "Greg Howe" }
+ it do
+ request.song_title.must_equal "Extraction"
+ request.song_id.must_equal 2
+ request.name.must_equal "Greg Howe"
+ request.id.must_equal 1
+ request.song_title = "Tease"
+ request.name = "Wooten"
- describe "setter" do
- before do
- request.song_title = "Tease"
- request.name = "Wooten"
+
+ request.song_title.must_equal "Tease"
+ request.name.must_equal "Wooten"
+
+ # does not write to model.
+ song.title.must_equal "Extraction"
+ requester.name.must_equal "Greg Howe"
+
+
+ request.save
+
+ # make sure models got synced and saved.
+ song.id.must_equal 2
+ song.title.must_equal "Tease"
+ requester.id.must_equal 1
+ requester.name.must_equal "Wooten"
+
+ song.saved?.must_equal true
+ requester.saved?.must_equal true
+ end
+
+ # save with block.
+ it do
+ request.song_title = "Tease"
+ request.name = "Wooten"
+ request.captcha = "Awesome!"
+
+ # does not write to model.
+ song.title.must_equal "Extraction"
+ requester.name.must_equal "Greg Howe"
+
+
+ nested_hash = nil
+ request.save do |hash|
+ nested_hash = hash
end
- it { request.song_title.must_equal "Tease" }
- # no writing to model.
- it { song.title.must_equal "Extraction" }
- it { request.name.must_equal "Wooten" }
+ nested_hash.must_equal(:song=>{"title"=>"Tease", "id"=>2}, :requester=>{"name"=>"Wooten", "id"=>1, "captcha"=>"Awesome!"})
end
end
\ No newline at end of file