json_api_client_mock 0.0.1

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: 45902ed709aa4974099a06459be994b4a4095a33
4
+ data.tar.gz: 0af1f842e8d9143335378a39216251d8040f73a0
5
+ SHA512:
6
+ metadata.gz: 2704c569141fb4a0c85d84b87ee1289d5341977f7f247a10710051bbb02f9343cecbd89f89b0ef65677d71eda38c248f68049e197b98ced00a12cdd10eae64c6
7
+ data.tar.gz: 3094b0b15152f8c42efae3b69efe49c154807db704e8ad7b693b4baa90b6248cc905652a4c42e1433786a6b9165c6a9e5713b6bb499921e1ee4a3a10a6e49eb4
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # JsonApiClientMock [![Build Status](https://travis-ci.org/chingor13/json_api_client_mock.png)](https://travis-ci.org/chingor13/json_api_client_mock)
2
+
3
+ This gem allows you to easily test [json_api_client](json_api_client) gem.
4
+
5
+ ## Usage
6
+
7
+ In your `Gemfile`:
8
+
9
+ ```
10
+ group :test do
11
+ gem 'json_api_client_mock', '~> 0'
12
+ end
13
+ ```
14
+
15
+ To set a global mock for `MyResource`:
16
+
17
+ ```
18
+ MyResource.set_test_results([
19
+ {foo: 'bar', qwer: 'asdf'},
20
+ {foo: 'wat', qwer: 'zxcv'}
21
+ ])
22
+
23
+ MyResource.all
24
+ => [<#MyResource foo:'bar', qwer:'asdf'>, <#MyResource foo:'wat', qwer:'zxcv'>]
25
+
26
+ MyResource.where(condition1: 'value', condition2: 'value2').all
27
+ => [<#MyResource foo:'bar', qwer:'asdf'>, <#MyResource foo:'wat', qwer:'zxcv'>]
28
+ ```
29
+
30
+ To set a targeted mock for `MyResource`:
31
+
32
+ ```
33
+ MyResource.set_test_results([
34
+ {foo: 'asdf', 'qwer': 'bar'}
35
+ ], {
36
+ condition1: 'value1',
37
+ condition2: 'value2',
38
+ })
39
+
40
+ MyResource.where(condition1: 'value1', condition2: 'value2').all
41
+ => [<#MyResource foo:'asdf', qwer:'bar'>]
42
+
43
+ # condition order should not matter
44
+ MyResource.where(condition2: 'value2', condition1: 'value1').all
45
+ => [<#MyResource foo:'asdf', qwer:'bar'>]
46
+ ```
47
+
48
+ ### Mock Priority
49
+
50
+ If you set multiple mocks that match the same query, the last matching result set should be returned. If no targetted match is found, we will return the untargeted result set you've specified (if it exists). If no mock is found, we will raise a `JsonApiClientMock::MissingMock` exception.
51
+
52
+ ## Contributing
53
+
54
+ Please fork and send me a pull request.
55
+
56
+ ## License
57
+
58
+ This project rocks and uses MIT-LICENSE.
59
+
60
+ [json_api_client]: http://github.com/chingor13/json_api_client
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'JsonApiClientMock'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ module JsonApiClientMock
2
+ class MissingMock < StandardError
3
+ end
4
+ end
@@ -0,0 +1,41 @@
1
+ require 'ostruct'
2
+
3
+ module JsonApiClientMock
4
+ class MockConnection
5
+ def self.instance
6
+ @instance ||= self.new
7
+ end
8
+
9
+ def initialize
10
+ # ignored
11
+ @mocks = {}
12
+ end
13
+
14
+ def execute(query)
15
+ if results = find_test_results(query)
16
+ OpenStruct.new(:body => {
17
+ query.klass.table_name => results[:results]
18
+ })
19
+ else
20
+ raise MissingMock, "no test results set for #{query.klass.name} with conditions: #{query.params.inspect}"
21
+ end
22
+ end
23
+
24
+ def set_test_results(klass, results, conditions = nil)
25
+ @mocks[klass.name] ||= []
26
+ @mocks[klass.name].unshift({results: results, conditions: conditions})
27
+ end
28
+
29
+ def clear_test_results
30
+ @mocks = {}
31
+ end
32
+
33
+ protected
34
+
35
+ def find_test_results(query)
36
+ class_mocks = @mocks.fetch(query.klass.name, [])
37
+ class_mocks.detect{|mock| mock[:conditions] == query.params} ||
38
+ class_mocks.detect{|mock| mock[:conditions].nil?}
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ module JsonApiClientMock
2
+ module ResourceExtensions
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def connection
7
+ MockConnection.instance
8
+ end
9
+
10
+ def set_test_results(results, conditions = nil)
11
+ connection.set_test_results(self, results, conditions)
12
+ end
13
+
14
+ def clear_test_results
15
+ connection.clear_test_results
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module JsonApiClientMock
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'json_api_client'
2
+
3
+ module JsonApiClientMock
4
+ autoload :MissingMock, "json_api_client_mock/missing_mock"
5
+ autoload :MockConnection, "json_api_client_mock/mock_connection"
6
+ autoload :ResourceExtensions, "json_api_client_mock/resource_extensions"
7
+ end
8
+
9
+ JsonApiClient::Resource.send(:include, JsonApiClientMock::ResourceExtensions)
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :json_api_client_mock do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ class JsonApiClientMockTest < MiniTest::Unit::TestCase
4
+
5
+ def teardown
6
+ JsonApiClient::Resource.clear_test_results
7
+ super
8
+ end
9
+
10
+ def test_conditionless_mocking
11
+ BarResource.set_test_results([{foo: 'bar', qwer: 'asdf'}])
12
+ results = BarResource.all
13
+
14
+ assert_equal(JsonApiClient::ResultSet, results.class)
15
+ assert_equal(1, results.length)
16
+
17
+ first = results.first
18
+ assert_equal(BarResource, first.class)
19
+ assert_equal('bar', first.foo)
20
+ assert_equal('asdf', first.qwer)
21
+
22
+ conditioned_results = BarResource.where(something: 'else').all
23
+ assert_equal(JsonApiClient::ResultSet, results.class)
24
+ assert_equal(1, conditioned_results.length)
25
+
26
+ first = conditioned_results.first
27
+ assert_equal(BarResource, first.class)
28
+ assert_equal('bar', first.foo)
29
+ assert_equal('asdf', first.qwer)
30
+ end
31
+
32
+ def test_missing_mock
33
+ assert_raises(JsonApiClientMock::MissingMock) do
34
+ BarResource.all
35
+ end
36
+ end
37
+
38
+ def test_conditional_mocking
39
+ BarResource.set_test_results([{foo: 'bar', qwer: 'asdf'}], {foo: 'bar'})
40
+ assert_raises(JsonApiClientMock::MissingMock) do
41
+ BarResource.all
42
+ end
43
+
44
+ results = BarResource.where(foo: 'bar').all
45
+ assert_equal(1, results.length)
46
+
47
+ first = results.first
48
+ assert_equal(BarResource, first.class)
49
+ assert_equal('bar', first.foo)
50
+ assert_equal('asdf', first.qwer)
51
+ end
52
+
53
+ def test_conditional_mocking_param_order
54
+ BarResource.set_test_results([{foo: 'bar', qwer: 'asdf'}], {foo: 'bar', qwer: 'asdf'})
55
+
56
+ results = BarResource.where(foo: 'bar', qwer: 'asdf').all
57
+ assert_equal(1, results.length)
58
+
59
+ results = BarResource.where(qwer: 'asdf', foo: 'bar').all
60
+ assert_equal(1, results.length)
61
+ end
62
+
63
+ def test_mocks_are_stored_by_class
64
+ BarResource.set_test_results([{foo: 'bar', qwer: 'asdf'}])
65
+ assert_raises(JsonApiClientMock::MissingMock) do
66
+ FooResource.all
67
+ end
68
+ end
69
+
70
+ def test_inherited_mocking
71
+ BarResource.set_test_results([{foo: 'bar', qwer: 'asdf'}])
72
+ assert_raises(JsonApiClientMock::MissingMock) do
73
+ BarExtendedResource.all
74
+ end
75
+ end
76
+
77
+ end
@@ -0,0 +1,19 @@
1
+ Bundler.require(:default, :test)
2
+
3
+ require 'minitest/autorun'
4
+ require 'pp'
5
+
6
+ # test classes
7
+
8
+ class BaseResource < JsonApiClient::Resource
9
+ self.site = "http://test.host/api/1"
10
+ end
11
+
12
+ class BarResource < BaseResource
13
+ end
14
+
15
+ class FooResource < BaseResource
16
+ end
17
+
18
+ class BarExtendedResource < BarResource
19
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_api_client_mock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Ching
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json_api_client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: mocha
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Adds methods to JsonApiClient allowing you to easily mock test results
56
+ email:
57
+ - ching.jeff@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/json_api_client_mock.rb
66
+ - lib/json_api_client_mock/missing_mock.rb
67
+ - lib/json_api_client_mock/mock_connection.rb
68
+ - lib/json_api_client_mock/resource_extensions.rb
69
+ - lib/json_api_client_mock/version.rb
70
+ - lib/tasks/json_api_client_mock_tasks.rake
71
+ - test/json_api_client_mock_test.rb
72
+ - test/test_helper.rb
73
+ homepage: http://github.com/chingor13/json_api_client_mock
74
+ licenses: []
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.0
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Easily mock JsonApiClient queries
96
+ test_files:
97
+ - test/json_api_client_mock_test.rb
98
+ - test/test_helper.rb