minitest-mongoid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in minitest-mongoid.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ MiniTest asserts for mongoid. More details shortly.
2
+
3
+ Notes
4
+ -----
5
+
6
+ Roughly ported from riot-mongoid
7
+
8
+ TODO
9
+ ----
10
+
11
+ Make sure it's gemified proper, and ship it to rubygems
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "lib"
8
+ t.libs << "spec"
9
+ t.pattern = "spec/*_spec.rb"
10
+ end
11
+
12
+ task :default => [:test]
13
+
@@ -0,0 +1,24 @@
1
+ module MiniTest
2
+ module Assertions
3
+ def assert_has_association(model_class, type, field_name, options = {}, msg = nil)
4
+ msg = message(msg) {
5
+ msg = "Expected #{model_class.name} to have association '#{type} :#{field_name}'"
6
+ msg << " with options '#{options.inspect}'" unless options.empty?
7
+ msg
8
+ }
9
+
10
+ assoc = model_class.relations[field_name.to_s]
11
+ assert (!assoc.nil? && assoc.macro == type.to_sym), msg
12
+
13
+ unless options.empty?
14
+ assert options.all? { |k, v| assoc.send(k) == v }, msg
15
+ end
16
+
17
+ true
18
+ end
19
+ end
20
+
21
+ module Expectations
22
+ infect_an_assertion :assert_has_association, :must_have_association, :reverse
23
+ end
24
+ end
@@ -0,0 +1,31 @@
1
+ module MiniTest
2
+ module Assertions
3
+ def assert_has_field(model_class, field_name, options = {}, msg = nil)
4
+ msg = message(msg) {
5
+ msg = "Expected #{model_class.name} to have field :#{field_name}"
6
+ msg << " with options '#{options.inspect}'" unless options.empty?
7
+ msg
8
+ }
9
+ field = model_class.fields[field_name.to_s]
10
+
11
+ assert field, msg
12
+ unless options.empty?
13
+ assert options.all? { |k,v| field.send(k) == v }, msg
14
+ end
15
+ true
16
+ end
17
+
18
+ def refute_has_field(model_class, field_name, msg = nil)
19
+ msg = message(msg) {
20
+ "Expected #{model_class.name} not to have field :#{field_name}"
21
+ }
22
+ refute model_class.fields[field_name.to_s], msg
23
+ true
24
+ end
25
+ end
26
+
27
+ module Expectations
28
+ infect_an_assertion :assert_has_field, :must_have_field, :reverse
29
+ infect_an_assertion :refute_has_field, :wont_have_field, :reverse
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ module MiniTest
2
+ module Assertions
3
+ def assert_has_key(model_class, key, msg = nil)
4
+ msg = message(msg) {
5
+ "Expected #{model_class.name} to have key :#{key}"
6
+ }
7
+ assert model_class.primary_key.include?(key.to_sym), msg
8
+ true
9
+ end
10
+ end
11
+
12
+ module Expectations
13
+ infect_an_assertion :assert_has_key, :must_have_key, :reverse
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ module MiniTest
2
+ module Assertions
3
+ def assert_has_validation(model_class, type, field_name, options = {}, msg = nil)
4
+ msg = message(msg) {
5
+ msg = "Expected #{model_class.name} to have validation '#{type} :#{field_name}'"
6
+ msg << " with options '#{options.inspect}'" unless options.empty?
7
+ msg
8
+ }
9
+
10
+ type = type.to_s.match(/validates_(.*)_of/)[1]
11
+ validation = model_class._validators[field_name].detect { |v| v.class.name =~ /#{type.camelize}/ }
12
+ assert validation, msg
13
+
14
+ if validation.class.name =~ %r{Length} && options[:within]
15
+ range = options[:within].to_a
16
+ return assert(validation.options[:minimum] == range.first && validation.options[:maximum] == range.last), msg
17
+ end
18
+
19
+ unless options.empty?
20
+ assert options.all? { |k,v| validation.options[k] == v }, msg
21
+ end
22
+ true
23
+ end
24
+
25
+ end
26
+
27
+ module Expectations
28
+ infect_an_assertion :assert_has_validation, :must_have_validation, :reverse
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module MinitestMongoid
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ gem 'minitest'
2
+ require 'minitest/spec'
3
+ require 'mongoid'
4
+
5
+ Dir[File.dirname(__FILE__) + '/minitest-mongoid/*.rb'].each {|file| require file }
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "minitest-mongoid/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "minitest-mongoid"
7
+ s.version = MinitestMongoid::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Brendon Murphy"]
10
+ s.email = ["xternal1+github@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Mongoid assertion matchers for MiniTest}
13
+ s.description = s.summary
14
+
15
+ s.rubyforge_project = "minitest-mongoid"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "mongoid"
23
+ s.add_dependency "minitest"
24
+
25
+ s.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe "association assertions" do
4
+ it "passes for an embeds_many" do
5
+ assert assert_has_association(Foobar, :embeds_many, :things)
6
+ Foobar.must_have_association(:embeds_many, :things)
7
+ end
8
+
9
+ it "passes when the association options are specified for an embedded_in" do
10
+ assert assert_has_association(Foobar, :embedded_in, :another_thing, :inverse_of => :word)
11
+ end
12
+
13
+ it "raises MiniTest::Assertion when association options mismatch" do
14
+ assert_triggers_failure do
15
+ assert_has_association(Foobar, :embedded_in, :another_thing, :inverse_of => :bogus)
16
+ end
17
+ end
18
+
19
+ it "returns useful messages" do
20
+ assert_triggers_failure "Expected Foobar to have association 'embeds_many :not_present'." do
21
+ assert_has_association(Foobar, :embeds_many, :not_present)
22
+ end
23
+ end
24
+
25
+ it "includes options in the msg for failures" do
26
+ assert_triggers_failure "Expected Foobar to have association 'embedded_in :another_thing' with options '{:inverse_of=>:bogus}'." do
27
+ assert_has_association(Foobar, :embedded_in, :another_thing, :inverse_of => :bogus)
28
+ end
29
+ end
30
+
31
+ it "passes when the association options are specified for a references_many" do
32
+ assert assert_has_association(Foobar, :references_many, :relations, :class_name => "Relation")
33
+ end
34
+ end
35
+
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe "asserting on fields" do
4
+ it "can tell if a field is present" do
5
+ assert assert_has_field(Foobar, :fizzbuzz)
6
+ end
7
+
8
+ it "can check options on the field" do
9
+ assert assert_has_field(Foobar, :fizzbuzz, :type => String)
10
+ Foobar.must_have_field(:fizzbuzz)
11
+ end
12
+
13
+ it "raises MiniTest::Assertion on an option mismatch" do
14
+ assert_triggers_failure do
15
+ assert_has_field Foobar, :fizzbuzz, :type => Array
16
+ end
17
+ end
18
+
19
+ it "raises a useful message" do
20
+ assert_triggers_failure "Expected Foobar to have field :not_present." do
21
+ assert_has_field Foobar, :not_present
22
+ end
23
+ end
24
+
25
+ it "includes options in the message" do
26
+ assert_triggers_failure "Expected Foobar to have field :fizzbuzz with options '{:type=>Array}'." do
27
+ assert_has_field Foobar, :fizzbuzz, :type => Array
28
+ end
29
+ end
30
+
31
+ it "can check with multiple options" do
32
+ assert assert_has_field(Foobar, :fizzbuzz, :type => String, :default => "FIZZBUZZ")
33
+ Foobar.must_have_field(:fizzbuzz, :type => String, :default => "FIZZBUZZ")
34
+ end
35
+
36
+ it "raises MiniTest::Assertion on a single option mismatch" do
37
+ assert_triggers_failure do
38
+ assert_has_field Foobar, :fizzbuzz, :type => String, :default => "BOGUS"
39
+ end
40
+ end
41
+
42
+ it "can refute the presence of a field" do
43
+ refute_has_field Foobar, :not_present
44
+ Foobar.wont_have_field(:not_present)
45
+ end
46
+
47
+ it "will include a useful message for field refutation" do
48
+ assert_triggers_failure "Expected Foobar not to have field :fizzbuzz." do
49
+ refute_has_field Foobar, :fizzbuzz
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe "has key assertions" do
4
+ it "passes when the key is specified" do
5
+ assert assert_has_key(Foobar, :fizzbuzz)
6
+ Foobar.must_have_key(:fizzbuzz)
7
+ end
8
+
9
+ it "fails when the key doesn't match" do
10
+ assert_triggers_failure do
11
+ assert_has_key(Foobar, :bogus)
12
+ end
13
+ end
14
+
15
+ it "includes a helpful failure message" do
16
+ assert_triggers_failure "Expected Foobar to have key :bogus." do
17
+ assert_has_key(Foobar, :bogus)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe "asserting on validations" do
4
+ it "can tell if a validation is present" do
5
+ assert_has_validation Foobar, :validates_format_of, :fizzbuzz
6
+ Foobar.must_have_validation(:validates_format_of, :fizzbuzz)
7
+ end
8
+
9
+ it "raises MiniTest::Assertion if a validation is not present" do
10
+ assert_triggers_failure do
11
+ assert_has_validation Foobar, :validates_uniqueness_of, :fizzbuzz
12
+ end
13
+ end
14
+
15
+ it "includes a useful failure message" do
16
+ assert_triggers_failure "Expected Foobar to have validation 'uniqueness :fizzbuzz'." do
17
+ assert_has_validation Foobar, :validates_uniqueness_of, :fizzbuzz
18
+ end
19
+ end
20
+
21
+ it "includes options in failure messages" do
22
+ assert_triggers_failure "Expected Foobar to have validation 'format :fizzbuzz' with options '{:format=>/bogus/}'." do
23
+ assert_has_validation Foobar, :validates_format_of, :fizzbuzz, :format => /bogus/
24
+ end
25
+ end
26
+
27
+ it "can check the options for the validation" do
28
+ assert assert_has_validation(Foobar, :validates_format_of, :fizzbuzz, :with => /[A-Za-z]/)
29
+ Foobar.must_have_validation(:validates_format_of, :fizzbuzz, :with => /[A-Za-z]/)
30
+ end
31
+
32
+ it "raises MiniTest::Assertion on an option mismatch" do
33
+ assert_triggers_failure do
34
+ assert_has_validation Foobar, :validates_format_of, :fizzbuzz, :with => /[0-9]/
35
+ end
36
+ end
37
+
38
+ it "passes when the validation options is specified using within" do
39
+ assert assert_has_validation(Foobar, :validates_length_of, :fizzbuzz, :within => 4..40)
40
+ Foobar.must_have_validation(:validates_length_of, :fizzbuzz, :within => 4..40)
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ gem "minitest"
2
+ require "minitest/spec"
3
+ require "minitest/autorun"
4
+
5
+ require "minitest-mongoid"
6
+
7
+ Mongoid.configure do |config|
8
+ config.master = Mongo::Connection.new.db("mintest_mongoid_specs")
9
+ end
10
+
11
+ # TODO bring this into relevant spec files
12
+ # so it's easier to read
13
+ class Foobar
14
+ include Mongoid::Document
15
+
16
+ field :fizzbuzz, :type => String, :default => "FIZZBUZZ"
17
+
18
+ validates_format_of :fizzbuzz, :with => /[A-Za-z]/
19
+ validates_length_of :fizzbuzz, :within => 4..40
20
+
21
+ embeds_many :things
22
+ embedded_in :another_thing, :inverse_of => :word
23
+ references_many :relations, :class_name => "Relation"
24
+
25
+ key :fizzbuzz
26
+ end
27
+
28
+ module MiniTest
29
+ module Assertions
30
+ def assert_triggers_failure(expected = nil, klass = MiniTest::Assertion)
31
+ e = assert_raises(klass) do
32
+ yield
33
+ end
34
+
35
+ if expected
36
+ msg = e.message.sub(/(---Backtrace---).*/m, '\1')
37
+ msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)')
38
+
39
+ assert_equal expected, msg
40
+ end
41
+
42
+ true
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-mongoid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brendon Murphy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: &2153565800 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2153565800
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &2153565380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2153565380
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &2153564960 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2153564960
47
+ description: Mongoid assertion matchers for MiniTest
48
+ email:
49
+ - xternal1+github@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - README.md
57
+ - Rakefile
58
+ - lib/minitest-mongoid.rb
59
+ - lib/minitest-mongoid/has_association.rb
60
+ - lib/minitest-mongoid/has_field.rb
61
+ - lib/minitest-mongoid/has_key.rb
62
+ - lib/minitest-mongoid/has_validation.rb
63
+ - lib/minitest-mongoid/version.rb
64
+ - minitest-mongoid.gemspec
65
+ - spec/has_association_spec.rb
66
+ - spec/has_field_spec.rb
67
+ - spec/has_key_spec.rb
68
+ - spec/has_validation_spec.rb
69
+ - spec/spec_helper.rb
70
+ homepage: ''
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project: minitest-mongoid
90
+ rubygems_version: 1.8.11
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Mongoid assertion matchers for MiniTest
94
+ test_files:
95
+ - spec/has_association_spec.rb
96
+ - spec/has_field_spec.rb
97
+ - spec/has_key_spec.rb
98
+ - spec/has_validation_spec.rb
99
+ - spec/spec_helper.rb