Sha256: 3c5506de98ee3a7b26dc5662beb9845b71075be2faa2d871679b65928ec8adb2

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8
require 'spec_helper'

#
# The case
#
# This is to demonstrate when we denormalize a Many to One
# with no fields and only an option :count => true
#
class Inhabitant
  include Mongoid::Document

  belongs_to :city
end

class City
  include Mongoid::Document
  include Mongoid::Max::Denormalize

  has_many :inhabitants

  denormalize :inhabitants, count: true
end


#
# The specs
#
describe "Case: a city and his inhabitants" do

  before do
    @city = City.create!
  end

  context "when nothing" do
    context "considering the city" do
      subject { @city }

      it "should not have inhabitants" do
        @city.inhabitants.should be_empty
        @city.inhabitants_count.should eq 0
      end
    end
  end

  context "when adding 20 inhabitants" do
    before do
      5.times do
        @city.inhabitants.create!
      end
      @city.reload
    end

    context "considering the city" do
      subject { @city }

      its(:inhabitants) { should have(5).inhabitants }
      its(:inhabitants_count) { should eq 5 }

      context "when destroying 2 inhabitants" do
        before do
          2.times do
            Inhabitant.first.destroy
          end
          @city.reload
        end

        its(:inhabitants) { should have(3).inhabitants }
        its(:inhabitants_count) { should eq 3 }
      end

      context "when destroying all inhabitants" do
        before do
          Inhabitant.destroy_all
          @city.reload
        end

        its(:inhabitants) { should have(0).inhabitant }
        its(:inhabitants_count) { should eq 0 }
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid_max_denormalize-0.0.5 spec/cases/city_and_inhabitants_spec.rb
mongoid_max_denormalize-0.0.4 spec/cases/city_and_inhabitants_spec.rb