Sha256: 5c563a740fe8fd2e5012f4c3b7fcbe7362fff0afefe7efabb686ffd3b6d88c7c

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

require 'spec_helper'

class NestedModel
  include Id::Model
  field :yak
end

class TestModel
  include Id::Model

  field :foo
  field :bar, key: 'baz'
  field :quux, default: 'kwak'

  has_one :aliased_model, type: NestedModel
  has_one :nested_model, key: 'aliased_model'
  has_one :extra_nested_model
  has_one :test_model
  has_many :nested_models

  class ExtraNestedModel
    include Id::Model
    field :cats!
  end
end

describe Id::Model do
  let (:model) { TestModel.new(foo: 3,
                               baz: 6,
                               test_model: {},
                               aliased_model: { 'yak' => 11},
                               nested_models: [{ 'yak' => 11}, { yak: 14 }],
                               extra_nested_model: { cats!: "MIAOW" }) }

  describe ".field" do
    it 'defines an accessor on the model' do
      model.foo.should eq 3
    end

    it 'allows key aliases' do
      model.bar.should eq 6
    end

    it 'allows default values' do
      model.quux.should eq 'kwak'
    end

  end

  describe ".has_one" do
    it "allows nested models" do
      model.aliased_model.should be_a NestedModel
    end
    it "allows nested models" do
      model.nested_model.should be_a NestedModel
      model.nested_model.yak.should eq 11
    end
    it "allows associations to be nested within the class" do
      model.extra_nested_model.should be_a TestModel::ExtraNestedModel
      model.extra_nested_model.cats!.should eq 'MIAOW'
    end
    it "allows recursively defined models" do
      model.test_model.should be_a TestModel
    end
  end

  describe ".has_many" do
    it 'creates an array of nested models' do
      model.nested_models.should be_a Array
      model.nested_models.first.should be_a NestedModel
      model.nested_models.first.yak.should eq 11
      model.nested_models.last.yak.should eq 14
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
id-0.0.3 spec/lib/id_spec.rb
id-0.0.2 spec/lib/id_spec.rb