mochening 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mochening.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Benny Hallett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Mochening
2
+
3
+ TODO: Write a gem description
4
+ Mochening makes it easy to define expectations and stubs for your database when using Sequel
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'mochening'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mochening
19
+
20
+ ## Usage
21
+
22
+ Mochening allows you to easily use Mocha to mock the calls onto a Sequel database object.
23
+
24
+ The common pattern of usage is:
25
+
26
+ Mochening::Expect.from(mock_database) do |db|
27
+ db[:table].where(a: 'a', b: 'b').select(:c, :d).all [{ c: '1', d: '1' }, { c: '2', d: '2' }]
28
+ end
29
+
30
+
31
+ In the above example, we're given a Mocha mock object, and inside the block we're easily able to apply expectations to the database in the same style that the Sequel database object is called.
32
+
33
+ The `.all` and `.first` methods expect that a return value is provided, so that Mochening knows what it should return when the method is called.
34
+
35
+ ## Available Sequel methods
36
+
37
+ At the moment, only a subset of Sequel functionality is able to be mocked. Only expectations are able to be set at this time, but stubs are planned for the future.
38
+
39
+ **Expectations**
40
+
41
+ * [:table_name]
42
+ * where
43
+ * select
44
+ * insert
45
+ * all
46
+ * first
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => [:spec]
5
+
6
+ desc "Run the specs."
7
+ RSpec::Core::RakeTask.new do |t|
8
+ t.pattern = "spec/**/*_spec.rb"
9
+ end
data/lib/mochening.rb ADDED
@@ -0,0 +1,43 @@
1
+ require "mochening/version"
2
+
3
+ module Mochening
4
+ class Expect
5
+ def self.from(db)
6
+ raise "Cannot expect anything from a nil database" if db.nil?
7
+ raise "Expected a block" unless block_given?
8
+ yield Mochening::Expect.new(db)
9
+ end
10
+
11
+ def initialize(db)
12
+ @db = db
13
+ end
14
+
15
+ def [](table)
16
+ @db.expects(:[]).with(table)
17
+ self
18
+ end
19
+
20
+ def where(restriction)
21
+ @db.expects(:where).with(restriction)
22
+ self
23
+ end
24
+
25
+ def select(*columns)
26
+ @db.expects(:select).with(*columns)
27
+ self
28
+ end
29
+
30
+ def insert(value)
31
+ @db.expects(:insert).with(value)
32
+ self
33
+ end
34
+
35
+ def all(return_value)
36
+ @db.expects(:all).returns(return_value)
37
+ end
38
+
39
+ def first(return_value)
40
+ @db.expects(:first).returns(return_value)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Mochening
2
+ VERSION = "0.0.1"
3
+ end
data/mochening.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mochening/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mochening"
8
+ spec.version = Mochening::VERSION
9
+ spec.authors = ["Benny Hallett"]
10
+ spec.email = ["benny.hallett@gmail.com"]
11
+ spec.description = %q{Mochening allows simpler mocking of Sequel database objects with Mocha.}
12
+ spec.summary = %q{Mochening makes mocking Sequel database objects with Mocha simpler}
13
+ spec.homepage = "http://github.com/bennyhallett/mochening"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'rspec-mocks'
25
+ end
@@ -0,0 +1,93 @@
1
+ require 'mochening'
2
+
3
+ describe Mochening do
4
+ it "raises error when called with nil database" do
5
+ expect { Mochening::Expect.from(nil) }.to raise_error(RuntimeError)
6
+ end
7
+
8
+ it "raises error when called without a block" do
9
+ db = double('database')
10
+ expect { Mochening::Expect.from(db) }.to raise_error(RuntimeError)
11
+ end
12
+
13
+ it "sets an expectation on the db that a dataset is created" do
14
+ db = double('database')
15
+ expect(db).to receive(:expects).with(:[]).and_return(db)
16
+ expect(db).to receive(:with).with(:table)
17
+
18
+ Mochening::Expect.from(db) do |dbase|
19
+ dbase[:table]
20
+ end
21
+ end
22
+
23
+ it "sets an expectation on the db that the dataset is restricted to a set of values" do
24
+ restriction = { a: 'a', b: 'b' }
25
+ db = double('database')
26
+ expect(db).to receive(:expects).with(:[]).and_return(db)
27
+ expect(db).to receive(:with).with(:table).and_return(db)
28
+ expect(db).to receive(:expects).with(:where).and_return(db)
29
+ expect(db).to receive(:with).with(restriction)
30
+
31
+ Mochening::Expect.from(db) do |dbase|
32
+ dbase[:table].where(a: 'a', b: 'b')
33
+ end
34
+ end
35
+
36
+ it "sets an expectations on the db that a set of values is selected from the dataset" do
37
+ db = double('database')
38
+ expect(db).to receive(:expects).with(:[]).and_return(db)
39
+ expect(db).to receive(:with).with(:table).and_return(db)
40
+ expect(db).to receive(:expects).with(:select).and_return(db)
41
+ expect(db).to receive(:with).with(:a, :b)
42
+
43
+ Mochening::Expect.from(db) do |dbase|
44
+ dbase[:table].select(:a, :b)
45
+ end
46
+ end
47
+
48
+ it "sets an expectation on the db that a set of values in inserted into the table" do
49
+ db = double('database')
50
+ expect(db).to receive(:expects).with(:[]).and_return(db)
51
+ expect(db).to receive(:with).with(:table).and_return(db)
52
+ expect(db).to receive(:expects).with(:insert).and_return(db)
53
+ expect(db).to receive(:with).with({ a: 'a', b: 'b' })
54
+
55
+ Mochening::Expect.from(db) do |dbase|
56
+ dbase[:table].insert({ a: 'a', b: 'b' })
57
+ end
58
+ end
59
+
60
+ it "returns all values of a selected data set" do
61
+ return_value = [ { a: 'aa', b: 'bb' }, { a: '12', b: '34' } ]
62
+
63
+ db = double('database')
64
+ expect(db).to receive(:expects).with(:[]).and_return(db)
65
+ expect(db).to receive(:with).with(:table)
66
+ expect(db).to receive(:expects).with(:where).and_return(db)
67
+ expect(db).to receive(:with).with(c: 'c')
68
+ expect(db).to receive(:expects).with(:select).and_return(db)
69
+ expect(db).to receive(:with).with(:a, :b)
70
+ expect(db).to receive(:expects).with(:all).and_return(db)
71
+ expect(db).to receive(:returns).with(return_value)
72
+
73
+ Mochening::Expect.from(db) do |dbase|
74
+ dbase[:table].where(c: 'c').select(:a, :b).all return_value
75
+ end
76
+ end
77
+
78
+ it "returns first value of a selected data set" do
79
+ return_value = { a: 'aa', b: 'bb', c: 'c' }
80
+
81
+ db = double('database')
82
+ expect(db).to receive(:expects).with(:[]).and_return(db)
83
+ expect(db).to receive(:with).with(:table)
84
+ expect(db).to receive(:expects).with(:where).and_return(db)
85
+ expect(db).to receive(:with).with(c: 'c')
86
+ expect(db).to receive(:expects).with(:first).and_return(db)
87
+ expect(db).to receive(:returns).with(return_value)
88
+
89
+ Mochening::Expect.from(db) do |dbase|
90
+ dbase[:table].where(c: 'c').first return_value
91
+ end
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mochening
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benny Hallett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-mocks
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Mochening allows simpler mocking of Sequel database objects with Mocha.
79
+ email:
80
+ - benny.hallett@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - lib/mochening.rb
91
+ - lib/mochening/version.rb
92
+ - mochening.gemspec
93
+ - spec/mochening_spec.rb
94
+ homepage: http://github.com/bennyhallett/mochening
95
+ licenses:
96
+ - MIT
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.23
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Mochening makes mocking Sequel database objects with Mocha simpler
119
+ test_files:
120
+ - spec/mochening_spec.rb