Sha256: 7f43be39a2f14067e8e9867ac7569f43c5e73a64ffe5edca1300f8ba45497780

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 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 "count should be 0" do
        @city.inhabitants.should be_empty
        @city.inhabitants_count.should eq 0
      end
    end
  end

  inhabitants_number = 5
  context "when adding #{inhabitants_number} inhabitants" do
    before do
      inhabitants_number.times do
        @city.inhabitants.create!
      end
      @city.reload
    end

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

      it "count should be #{inhabitants_number}" do
        @city.inhabitants.should have(inhabitants_number).inhabitants
        @city.inhabitants_count.should eq inhabitants_number
      end

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

        it "count should be #{inhabitants_number - 2}" do
          @city.inhabitants.should have(inhabitants_number - 2).inhabitants
          @city.inhabitants_count.should eq(inhabitants_number - 2)
        end
      end

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

        it "count should be 0" do
          @city.inhabitants.should be_empty
          @city.inhabitants_count.should eq 0
        end
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid_max_denormalize-0.0.7 spec/cases/city_and_inhabitants_spec.rb
mongoid_max_denormalize-0.0.6 spec/cases/city_and_inhabitants_spec.rb