tire-mock_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tire-mock_client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 Amos King<amos.l.king@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,50 @@
1
+ Tire MockClient
2
+ ===============
3
+
4
+ Are you using Tire and ElasticSearch? Do you want to run your tests without booting
5
+ up ElasticSearch? Well hold on to your hats.
6
+
7
+ Instalation
8
+ -----------
9
+
10
+ In your gemfile add
11
+ gem 'tire-mock_client'
12
+
13
+ In the environments that you don't want to run ElasticSearch add:
14
+ Tire.configure do
15
+ client Tire::Http::Client::MockClient
16
+ end
17
+
18
+ DONE!
19
+ -----
20
+
21
+ As of version 0.0.1 the search capabilities are quite limited.
22
+
23
+ ### Contributions
24
+
25
+ 1. Fork it.
26
+ 2. Create a branch (`git checkout -b support_or_queries`)
27
+ 3. Commit your changes (`git commit -am "Added OR queries"`)
28
+ 4. Push to the branch (`git push origin support_or_queries`)
29
+ 5. Create an [Issue][1] with a link to your branch
30
+ 6. Bathe in the glory
31
+
32
+ Copyright (C) 2011 Amos King<amos.l.king@gmail.com>
33
+
34
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
35
+ this software and associated documentation files (the "Software"), to deal in
36
+ the Software without restriction, including without limitation the rights to
37
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38
+ of the Software, and to permit persons to whom the Software is furnished to do
39
+ so, subject to the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be included in all
42
+ copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
50
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,6 @@
1
+ require_relative 'tire-mock_client/http/client/mock_client'
2
+
3
+ module Tire
4
+ module Client
5
+ end
6
+ end
@@ -0,0 +1,109 @@
1
+ require 'rest_client'
2
+ require 'tire/http/response'
3
+ require 'tire/http/client'
4
+ require 'active_support/json'
5
+
6
+ module Tire
7
+ module Http
8
+ module Client
9
+ class MockClient
10
+ def initialize(*args)
11
+ raise "MockClient - initialized with #{args.inspect}"
12
+ end
13
+
14
+ def method_missing(*args)
15
+ raise "MockClient - instance method called: #{args.inspect}"
16
+ end
17
+
18
+ def self.method_missing(*args)
19
+ raise "MockClient - class method called: #{args.inspect}"
20
+ end
21
+
22
+ #when specs fail you must have this implemented
23
+ def self.to_ary
24
+ [self.to_s]
25
+ end
26
+
27
+ def self.head(address)
28
+ # Tire calls this to determine if the index needs to be created. If it returns 200, it assumes that the index has already been created.
29
+ Tire::HTTP::Response.new('', 200)
30
+ end
31
+
32
+ def self.post(address, json)
33
+ self.log "MockClient.post #{address}, #{json}"
34
+ request = address.split('/')
35
+ id = request.pop
36
+ type = request.pop
37
+ index = request.pop
38
+ words = ActiveSupport::JSON.decode(json).values.flatten.map { |value| value.to_s.split(' ') }.flatten
39
+ self.log "MockClient.post words = #{words.inspect}"
40
+ ids_to_json[id] = ActiveSupport::JSON.decode(json)
41
+ words.each do |word|
42
+ hash = {:id => id, :type => type, :index => index}
43
+ if words_to_ids[word]
44
+ words_to_ids[word] << hash unless words_to_ids[word].include?(hash)
45
+ else
46
+ words_to_ids[word] = [hash]
47
+ end
48
+ end
49
+ Tire::HTTP::Response.new(%({"ok":true,"_index":"#{index}","_type":"#{type}","_id":"#{id}","_version":1}), 201).tap do |response|
50
+ self.log "MockClient.post response = #{response}"
51
+ end
52
+ end
53
+
54
+ def self.get(address, json)
55
+ split_address = address.gsub('http://','').split("/")
56
+
57
+ the_filter = split_address[-2] if split_address[0] != split_address[-2]
58
+ self.log "MockClient.get #{address}, #{json}"
59
+ query = ActiveSupport::JSON.decode(json)['query']['query_string']['query']
60
+ ids = words_to_ids[query]
61
+
62
+ results = Array(ids).inject([]) do |collector, id|
63
+ if the_filter.nil? || id[:index] == the_filter
64
+ collector << {_index: id[:index], _type: id[:type], _id: id[:id], _score: 1.0, _source: ids_to_json[id[:id]]}
65
+ else
66
+ collector
67
+ end
68
+ end
69
+
70
+ Tire::HTTP::Response.new(%({"took":0,"timed_out":false,"_shards":{"total":20,"successful":20,"failed":0},"hits":{"total":#{results.size},"max_score":10.0,"hits":#{results.to_json}}}), 200).tap do |response|
71
+ self.log "MockClient.get response = #{response}"
72
+ end
73
+ end
74
+
75
+ def self.delete(address)
76
+ @words_to_ids = {}
77
+ @ids_to_json = {}
78
+ Tire::HTTP::Response.new('{"ok":true,"acknowledged":true}', 200)
79
+ end
80
+
81
+ def self.put(address, json)
82
+ raise "MockClient.put #{address}, #{json}"
83
+ end
84
+
85
+ def self.log(text)
86
+ logger.call text
87
+ end
88
+
89
+ private
90
+
91
+ def self.words_to_ids
92
+ @words_to_ids ||= {}
93
+ end
94
+
95
+ def self.ids_to_json
96
+ @ids_to_json ||= {}
97
+ end
98
+
99
+ def self.logger
100
+ @logger ||= Tire.logger.public_method(:info)
101
+ end
102
+
103
+ def self.logger=(val)
104
+ @logger = val
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,5 @@
1
+ module Tire
2
+ module Mockclient
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,108 @@
1
+ require_relative '../../lib/tire-mock_client'
2
+
3
+ describe Tire::Http::Client::MockClient do
4
+
5
+ before do
6
+ described_class.logger = lambda { |a|}
7
+ end
8
+
9
+ context 'head response' do
10
+ subject { described_class.head 'url' }
11
+ its(:code) { should be(200) }
12
+ end
13
+
14
+ context 'response of posting new objects to be indexed' do
15
+ let(:index) { 'questions_of_the_day' }
16
+ let(:type) { 'question_of_the_day' }
17
+ let(:id) { '4edd1353f956f718aa000001' }
18
+ let(:object) { %[{"text":"Am I tomorrow's question?","choices_text":[],"display_date":"2011-12-06"}] }
19
+ subject { described_class.post "http://url/#{index}/#{type}/#{id}", object }
20
+
21
+ its(:code) { should be(201) }
22
+ its(:body) { should == %[{"ok":true,"_index":"#{index}","_type":"#{type}","_id":"#{id}","_version":1}] }
23
+ end
24
+
25
+ describe 'getting search results for something we put in' do
26
+ #MockTireTracker.get http://localhost:9200/_search, {"query":{"query_string":{"query":"other_user"}},"filter":{"or":[{"missing":{"field":"display_date"}},{"range":{"display_date":{"from":"2011-11-05T18:54:36Z","to":"2011-12-05"}}}]},"size":10,"from":0}
27
+ #MockTireTracker.get response = 200 : {"took":6,"timed_out":false,"_shards":{"total":20,"successful":20,"failed":0},"hits":{"total":1,"max_score":2.2448173,"hits":[{"_index":"profiles","_type":"profile","_id":"4edd135cf956f718aa000020","_score":2.2448173, "_source" : {"_id":"4edd135cf956f718aa000020","user_id":"4edd135cf956f718aa00001b","nickname":"other_user","avatar_url":null,"gender":null,"tags_array":[]}}]}}
28
+
29
+ let(:id1) { "4edd135cf956f718aa000020" }
30
+ let(:index1) { "profiles" }
31
+ let(:type1) { "profile" }
32
+ let(:object1) { %[{"_id":"#{id1}","user_id":"4edd135cf956f718aa00001b","nickname":"other_user","avatar_url":null,"gender":null,"tags_array":[]}] }
33
+
34
+ let(:id2) { '4edd1353f956f718aa000001' }
35
+ let(:index2) { 'questions_of_the_day' }
36
+ let(:type2) { 'question_of_the_day' }
37
+ let(:object2) { %[{"text":"Am I tomorrow's question, other_user","choices_text":[],"display_date":"2011-12-06"}] }
38
+
39
+ before do
40
+ described_class.post "http://url/#{index1}/#{type1}/#{id1}", object1
41
+ described_class.post "http://url/#{index2}/#{type2}/#{id2}", object2
42
+ end
43
+
44
+ context 'searching everything' do
45
+ let(:query) { %[{"query":{"query_string":{"query":"other_user"}},"filter":{"or":[{"missing":{"field":"display_date"}},{"range":{"display_date":{"from":"2011-11-05T18:54:36Z","to":"2011-12-05"}}}]},"size":10,"from":0}] }
46
+
47
+ after do
48
+ described_class.delete "http://url/profiles"
49
+ end
50
+
51
+ subject { described_class.get "http://url/_search", query }
52
+
53
+ its(:body) { should == %[{"took":0,"timed_out":false,"_shards":{"total":20,"successful":20,"failed":0},"hits":{"total":2,"max_score":10.0,"hits":[{"_index":"#{index1}","_type":"#{type1}","_id":"#{id1}","_score":1.0,"_source":#{object1}},{"_index":"#{index2}","_type":"#{type2}","_id":"#{id2}","_score":1.0,"_source":#{object2}}]}}] }
54
+ its(:code) { should be(200) }
55
+ end
56
+
57
+ context 'searching a specific index' do
58
+ let(:query) { %[{"query":{"query_string":{"query":"other_user"}},"filter":{"or":[{"missing":{"field":"display_date"}},{"range":{"display_date":{"from":"2011-11-05T18:54:36Z","to":"2011-12-05"}}}]},"size":10,"from":0}] }
59
+
60
+ after do
61
+ described_class.delete "http://url/profiles"
62
+ end
63
+
64
+ subject { described_class.get "http://url/#{index1}/_search", query }
65
+
66
+ its(:body) { should == %[{"took":0,"timed_out":false,"_shards":{"total":20,"successful":20,"failed":0},"hits":{"total":1,"max_score":10.0,"hits":[{"_index":"#{index1}","_type":"#{type1}","_id":"#{id1}","_score":1.0,"_source":#{object1}}]}}] }
67
+ its(:code) { should be(200) }
68
+ end
69
+ end
70
+
71
+ describe 'deleting an index' do
72
+ let(:id) { "4edd135cf956f718aa000020" }
73
+ let(:index) { "profiles" }
74
+ let(:type) { "profile" }
75
+ let(:object) { %[{"_id":"#{id}","user_id":"4edd135cf956f718aa00001b","nickname":"other_user","avatar_url":null,"gender":null,"tags_array":[]}] }
76
+ let(:query) { %[{"query":{"query_string":{"query":"other_user"}},"filter":{"or":[{"missing":{"field":"display_date"}},{"range":{"display_date":{"from":"2011-11-05T18:54:36Z","to":"2011-12-05"}}}]},"size":10,"from":0}] }
77
+
78
+ before do
79
+ described_class.post "http://url/#{index}/#{type}/#{id}", object
80
+ end
81
+
82
+ describe 'it clears the data' do
83
+ before do
84
+ described_class.delete "http://url/profiles"
85
+ end
86
+
87
+ subject { described_class }
88
+
89
+ its(:words_to_ids) { should == {} }
90
+ its(:ids_to_json) { should == {} }
91
+ end
92
+
93
+ describe 'its response' do
94
+ subject { described_class.delete "http://url/profiles" }
95
+
96
+ its(:code) { should be(200) }
97
+ its(:body) { should == %[{"ok":true,"acknowledged":true}] }
98
+ end
99
+ end
100
+
101
+ #MockTireTracker.post http://localhost:9200/questions_of_the_day/question_of_the_day/4edd1353f956f718aa000001, {"text":"Am I tomorrow's question?","choices_text":["I don't know","yes","no"],"display_date":"2011-12-06"}
102
+ #MockTireTracker.post response = 200 : {"ok":true,"_index":"questions_of_the_day","_type":"question_of_the_day","_id":"4edd1353f956f718aa000001","_version":2}
103
+
104
+ #MockTireTracker.get http://localhost:9200/_search, {"query":{"query_string":{"query":"other_user"}},"filter":{"or":[{"missing":{"field":"display_date"}},{"range":{"display_date":{"from":"2011-11-05T18:54:36Z","to":"2011-12-05"}}}]},"size":10,"from":0}
105
+ #MockTireTracker.get response = 200 : {"took":6,"timed_out":false,"_shards":{"total":20,"successful":20,"failed":0},"hits":{"total":1,"max_score":2.2448173,"hits":[{"_index":"profiles","_type":"profile","_id":"4edd135cf956f718aa000020","_score":2.2448173, "_source" : {"_id":"4edd135cf956f718aa000020","user_id":"4edd135cf956f718aa00001b","nickname":"other_user","avatar_url":null,"gender":null,"tags_array":[]}}]}}
106
+
107
+
108
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tire-mock_client/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tire-mock_client"
7
+ s.version = Tire::Mockclient::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Amos King"]
10
+ s.email = ["amos.l.king@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Faked out Tire client.}
13
+ s.description = %q{Fake client for Tire to allows tests without running elastic search.}
14
+
15
+ s.rubyforge_project = "tire-mock_client"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.extra_rdoc_files = ['README.md', "LICENSE"]
22
+ s.add_dependency('tire', '>= 0.3.11')
23
+ s.add_dependency('activesupport')
24
+
25
+ s.add_development_dependency('rspec')
26
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tire-mock_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Amos King
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: tire
16
+ requirement: &70256611527340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.3.11
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70256611527340
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70256611526920 !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: *70256611526920
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70256611526460 !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: *70256611526460
47
+ description: Fake client for Tire to allows tests without running elastic search.
48
+ email:
49
+ - amos.l.king@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files:
53
+ - README.md
54
+ - LICENSE
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - lib/tire-mock_client.rb
62
+ - lib/tire-mock_client/http/client/mock_client.rb
63
+ - lib/tire-mock_client/version.rb
64
+ - spec/integration/mock_client_integration_spec.rb
65
+ - tire-mock_client.gemspec
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project: tire-mock_client
86
+ rubygems_version: 1.8.6
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Faked out Tire client.
90
+ test_files: []