Sha256: b32e9668d30fd2f59ba653c8bfc14ec523cb40abd3edd4f7b141301e041d3a3d

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

require 'test_helper'

# Disposable::Twin::Composition.
class TwinCompositionTest < MiniTest::Spec
  class Request < Disposable::Twin
    include Sync
    include Save
    include Composition

    property :song_title, on: :song, from: :title
    property :song_id,    on: :song, from: :id

    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").extend(Disposable::Saveable) }
  let (:song) { Model::Song.new(2, "Extraction").extend(Disposable::Saveable) }

  let (:request) { Request.new(song: song, requester: requester) }

  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"


    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

    nested_hash.must_equal(:song=>{"title"=>"Tease", "id"=>2}, :requester=>{"name"=>"Wooten", "id"=>1, "captcha"=>"Awesome!"})
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
disposable-0.1.2 test/twin/composition_test.rb
disposable-0.1.1 test/twin/composition_test.rb
disposable-0.1.0 test/twin/composition_test.rb