rspec-arel-matchers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1df1f0f6b5251bda5155fdf445f3846c0603ac1e
4
+ data.tar.gz: 0d2f0251b8a00598684088c56d7c2182089f2d3d
5
+ SHA512:
6
+ metadata.gz: 476da62128504d9227ec39da17b3eaceeb02a452465b83b75869751219fa571b08721e06660337798b141bd7e14635bb6711d2049c7d1c0de82498d49fd8cc1d
7
+ data.tar.gz: bceeced45edf1306bcb98704e6d5bd8ce7a90bdc8c61780dd3de726bb8d31366ec17748cd6416ab4e766dc2b25bae411743cda4fe3892b3c6965764a6e8f90b3
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'activerecord-nulldb-adapter'
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rspec-arel-matchers (0.1.0)
5
+ activerecord (~> 4.0)
6
+ rspec (~> 3.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.1.8)
12
+ activesupport (= 4.1.8)
13
+ builder (~> 3.1)
14
+ activerecord (4.1.8)
15
+ activemodel (= 4.1.8)
16
+ activesupport (= 4.1.8)
17
+ arel (~> 5.0.0)
18
+ activerecord-nulldb-adapter (0.3.1)
19
+ activerecord (>= 2.0.0)
20
+ activesupport (4.1.8)
21
+ i18n (~> 0.6, >= 0.6.9)
22
+ json (~> 1.7, >= 1.7.7)
23
+ minitest (~> 5.1)
24
+ thread_safe (~> 0.1)
25
+ tzinfo (~> 1.1)
26
+ arel (5.0.1.20140414130214)
27
+ builder (3.2.2)
28
+ diff-lcs (1.2.5)
29
+ i18n (0.6.11)
30
+ json (1.8.1)
31
+ minitest (5.4.3)
32
+ rspec (3.1.0)
33
+ rspec-core (~> 3.1.0)
34
+ rspec-expectations (~> 3.1.0)
35
+ rspec-mocks (~> 3.1.0)
36
+ rspec-core (3.1.7)
37
+ rspec-support (~> 3.1.0)
38
+ rspec-expectations (3.1.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.1.0)
41
+ rspec-mocks (3.1.3)
42
+ rspec-support (~> 3.1.0)
43
+ rspec-support (3.1.2)
44
+ thread_safe (0.3.4)
45
+ tzinfo (1.2.2)
46
+ thread_safe (~> 0.1)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ activerecord-nulldb-adapter
53
+ rspec-arel-matchers!
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2014 Henk van der Veen
2
+ All rights reserved. Released under the MIT license.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # RSpec Arel Matchers
2
+
3
+ An experiment to test the scope of complex queries in bitesize pieces without creating a hundred objects.
4
+
5
+ Or just to test something when controllers are too simple to really have to test, so your coverage doesn't suffer :-P
6
+
7
+ You can only test where clauses for now, but will add things like order and joins at some point. Probably.
8
+
9
+
10
+ ## Examples
11
+
12
+ ```
13
+ # @post = Post.joins(:comments).merge(Comment.public).distinct
14
+ it 'should only select posts with public comments' do
15
+ expect(assigns[:posts]).to have_where_clause(Comment, :ctype, :eq, 'public')
16
+ end
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ In spec_helper add
22
+
23
+ ```
24
+ require 'rspec/arel_matchers'
25
+ RSpec.configure do |config|
26
+ config.include RSpec::ArelMatchers::Matchers
27
+ end
28
+ ```
29
+
30
+ ## Authors
31
+
32
+ Authored by Henk van der Veen.
33
+
34
+ Maintained by [Henk van der Veen](http://hampei.github.io/).
35
+
36
+ ## License
37
+
38
+ Copyright (c) 2014 [Henk van der Veen IT.](http://hampei.github.io/).
39
+ All rights reserved. Released under the MIT license.
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining a copy
42
+ of this software and associated documentation files (the "Software"), to deal
43
+ in the Software without restriction, including without limitation the rights
44
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
45
+ copies of the Software, and to permit persons to whom the Software is
46
+ furnished to do so, subject to the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be included in
49
+ all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
52
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
53
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
54
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
56
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
57
+ THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ require 'rspec/core'
2
+ require 'rspec/arel_matchers/helpers/indifferent_string'
3
+ require 'rspec/arel_matchers/matchers/have_where_clause'
@@ -0,0 +1,14 @@
1
+ module RSpec
2
+ module ArelMatchers
3
+ module Helpers
4
+ module IndifferentString
5
+ def ==(other)
6
+ super(other.to_s)
7
+ end
8
+ end
9
+ def indifferent_string(str_or_sym)
10
+ str_or_sym.to_s.tap { |str| str.extend IndifferentString }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,40 @@
1
+ module RSpec
2
+ module ArelMatchers
3
+ module Matchers
4
+ class HaveWhereClause < RSpec::Matchers::BuiltIn::BaseMatcher
5
+ include ::RSpec::ArelMatchers::Helpers
6
+
7
+ def initialize(cls, left, operator, right)
8
+ @cls = cls
9
+ # left operant is converted by active_record to a string in some cases, but not all
10
+ @left = indifferent_string left
11
+ @operator = operator
12
+ @right = right
13
+ end
14
+
15
+ def matches?(actual)
16
+ @actual = actual
17
+ actual.where_values.any? { |v| expected == v }
18
+ end
19
+
20
+ def expected
21
+ @cls.arel_table[@left].send(@operator, @right)
22
+ end
23
+
24
+ def failure_message
25
+ "expected\n#{PP.pp actual.where_values, ''}\nto contain\n" \
26
+ "#{PP.pp expected, ''}"
27
+ end
28
+
29
+ def failure_message_when_negated
30
+ "expected\n#{PP.pp actual.where_values, ''}\nto not contain\n" \
31
+ "#{PP.pp expected, ''}"
32
+ end
33
+ end
34
+
35
+ def have_where_clause(*args)
36
+ HaveWhereClause.new(*args)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ module Rspec
2
+ module ArelMatchers
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/rspec/arel_matchers/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "rspec-arel-matchers"
7
+ gem.version = Rspec::ArelMatchers::VERSION
8
+ gem.summary = %q{Efficient way to test complex queries}
9
+ gem.description = %q{Adds rspec matchers to test complex active_record queries in parts}
10
+ gem.license = "MIT"
11
+ gem.authors = ["Henk van der Veen"]
12
+ gem.email = "henk@vanderveen.name"
13
+ gem.homepage = "https://github.com/hampei/rspec_arel_matchers"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^spec/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_dependency 'rspec', '~> 3.0'
21
+ gem.add_dependency 'activerecord', '~> 4.0'
22
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'have_where_clause' do
4
+
5
+ class Test < ActiveRecord::Base
6
+ OLD_CUTOFF = 10.days.ago
7
+ scope :active, -> { where(active: true) }
8
+ scope :old, -> { where(Test.arel_table[:updated_at].lt(OLD_CUTOFF)) }
9
+ end
10
+ class Test2 < ActiveRecord::Base
11
+ has_many :tests
12
+ end
13
+
14
+ it 'should return true with a simple correct match' do
15
+ expect(Test.where(name: 'john')).to have_where_clause(Test, :name, :eq, 'john')
16
+ end
17
+
18
+ it 'should work for joined tables' do
19
+ expect(Test2.joins(:tests).merge(Test.active)).to have_where_clause(Test, :active, :eq, true)
20
+ end
21
+
22
+ it 'should fail when a clause is not there' do
23
+ expect(Test.where(name: 'john')).to_not have_where_clause(Test, :name, :eq, 'doe')
24
+ end
25
+
26
+ it 'can do other comparisons' do
27
+ expect(Test.old).to have_where_clause(Test, :updated_at, :lt, Test::OLD_CUTOFF)
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ require 'rspec'
2
+ require 'active_record'
3
+ require 'rspec/arel_matchers'
4
+
5
+ require 'nulldb_rspec'
6
+ include NullDB::RSpec::NullifiedDatabase
7
+
8
+ RSpec.configure do |config|
9
+ config.before :all do
10
+ ActiveRecord::Base.establish_connection :adapter => :nulldb
11
+ end
12
+ config.include RSpec::ArelMatchers::Matchers
13
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-arel-matchers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Henk van der Veen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ description: Adds rspec matchers to test complex active_record queries in parts
42
+ email: henk@vanderveen.name
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE.txt
51
+ - README.md
52
+ - lib/rspec/arel_matchers.rb
53
+ - lib/rspec/arel_matchers/helpers/indifferent_string.rb
54
+ - lib/rspec/arel_matchers/matchers/have_where_clause.rb
55
+ - lib/rspec/arel_matchers/version.rb
56
+ - rspec_arel_matchers.gemspec
57
+ - spec/rspec/arel_matchers/matchers/have_where_clause_spec.rb
58
+ - spec/spec_helper.rb
59
+ homepage: https://github.com/hampei/rspec_arel_matchers
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Efficient way to test complex queries
83
+ test_files:
84
+ - spec/rspec/arel_matchers/matchers/have_where_clause_spec.rb
85
+ - spec/spec_helper.rb
86
+ has_rdoc: