File: truncation_spec.rb

package info (click to toggle)
ruby-database-cleaner 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 768 kB
  • sloc: ruby: 4,854; makefile: 10; sh: 4
file content (80 lines) | stat: -rw-r--r-- 2,683 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require File.dirname(__FILE__) + '/../../spec_helper'
require 'moped'
require 'database_cleaner/moped/truncation'
require File.dirname(__FILE__) + '/moped_examples'

module DatabaseCleaner
  module Moped

    describe Truncation do
      let(:args) {{}}
      let(:truncation) { described_class.new(args) }
      #doing this in the file root breaks autospec, doing it before(:all) just fails the specs
      before(:all) do
        @test_db = 'database_cleaner_specs'
        @session = ::Moped::Session.new(['127.0.0.1:27017'], database: @test_db)
      end

      before(:each) do
        truncation.db = @test_db
      end

      after(:each) do
        @session.drop
      end

      def ensure_counts(expected_counts)
        # I had to add this sanity_check garbage because I was getting non-determinisc results from mongo at times..
        # very odd and disconcerting...
        expected_counts.each do |model_class, expected_count|
          model_class.count.should eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{model_class.count}"
        end
      end

      def create_widget(attrs={})
        MopedTest::Widget.new({:name => 'some widget'}.merge(attrs)).save!
      end

      def create_gadget(attrs={})
        MopedTest::Gadget.new({:name => 'some gadget'}.merge(attrs)).save!
      end

      def create_system(attrs={})
        MopedTest::System.new({:name => 'some system'}.merge(attrs)).save!
      end

      it "truncates all collections by default" do
        create_widget
        create_gadget
        create_system
        ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1, MopedTest::System => 1)
        truncation.clean
        ensure_counts(MopedTest::Widget => 0, MopedTest::Gadget => 0, MopedTest::System => 0)
      end

      context "when collections are provided to the :only option" do
        let(:args) {{:only => ['MopedTest::Widget']}}
        it "only truncates the specified collections" do
          create_widget
          create_gadget
          ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1)
          truncation.clean
          ensure_counts(MopedTest::Widget => 0, MopedTest::Gadget => 1)
        end
      end

      context "when collections are provided to the :except option" do
        let(:args) {{:except => ['MopedTest::Widget']}}
        it "truncates all but the specified collections" do
          create_widget
          create_gadget
          ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1)
          truncation.clean
          ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 0)
        end
      end

    end

  end
end