tire-erez 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. data/.gitignore +14 -0
  2. data/.travis.yml +32 -0
  3. data/.yardopts +1 -0
  4. data/Gemfile +10 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.markdown +775 -0
  7. data/Rakefile +51 -0
  8. data/examples/rails-application-template.rb +263 -0
  9. data/examples/tire-dsl.rb +932 -0
  10. data/lib/tire.rb +59 -0
  11. data/lib/tire/alias.rb +296 -0
  12. data/lib/tire/configuration.rb +38 -0
  13. data/lib/tire/count.rb +85 -0
  14. data/lib/tire/dsl.rb +114 -0
  15. data/lib/tire/http/client.rb +62 -0
  16. data/lib/tire/http/clients/curb.rb +61 -0
  17. data/lib/tire/http/clients/faraday.rb +71 -0
  18. data/lib/tire/http/response.rb +27 -0
  19. data/lib/tire/index.rb +443 -0
  20. data/lib/tire/logger.rb +60 -0
  21. data/lib/tire/model/callbacks.rb +40 -0
  22. data/lib/tire/model/import.rb +27 -0
  23. data/lib/tire/model/indexing.rb +134 -0
  24. data/lib/tire/model/naming.rb +100 -0
  25. data/lib/tire/model/percolate.rb +99 -0
  26. data/lib/tire/model/persistence.rb +72 -0
  27. data/lib/tire/model/persistence/attributes.rb +148 -0
  28. data/lib/tire/model/persistence/finders.rb +54 -0
  29. data/lib/tire/model/persistence/storage.rb +77 -0
  30. data/lib/tire/model/search.rb +322 -0
  31. data/lib/tire/multi_search.rb +263 -0
  32. data/lib/tire/results/collection.rb +156 -0
  33. data/lib/tire/results/item.rb +94 -0
  34. data/lib/tire/results/pagination.rb +68 -0
  35. data/lib/tire/rubyext/hash.rb +8 -0
  36. data/lib/tire/rubyext/ruby_1_8.rb +1 -0
  37. data/lib/tire/rubyext/symbol.rb +11 -0
  38. data/lib/tire/rubyext/uri_escape.rb +74 -0
  39. data/lib/tire/search.rb +211 -0
  40. data/lib/tire/search/facet.rb +81 -0
  41. data/lib/tire/search/filter.rb +28 -0
  42. data/lib/tire/search/highlight.rb +37 -0
  43. data/lib/tire/search/queries/match.rb +40 -0
  44. data/lib/tire/search/query.rb +250 -0
  45. data/lib/tire/search/scan.rb +114 -0
  46. data/lib/tire/search/script_field.rb +23 -0
  47. data/lib/tire/search/sort.rb +25 -0
  48. data/lib/tire/tasks.rb +138 -0
  49. data/lib/tire/utils.rb +17 -0
  50. data/lib/tire/version.rb +18 -0
  51. data/test/fixtures/articles/1.json +1 -0
  52. data/test/fixtures/articles/2.json +1 -0
  53. data/test/fixtures/articles/3.json +1 -0
  54. data/test/fixtures/articles/4.json +1 -0
  55. data/test/fixtures/articles/5.json +1 -0
  56. data/test/integration/active_model_indexing_test.rb +51 -0
  57. data/test/integration/active_model_searchable_test.rb +114 -0
  58. data/test/integration/active_record_searchable_test.rb +620 -0
  59. data/test/integration/boolean_queries_test.rb +43 -0
  60. data/test/integration/boosting_queries_test.rb +32 -0
  61. data/test/integration/bulk_test.rb +86 -0
  62. data/test/integration/count_test.rb +64 -0
  63. data/test/integration/custom_score_queries_test.rb +89 -0
  64. data/test/integration/dis_max_queries_test.rb +68 -0
  65. data/test/integration/dsl_search_test.rb +30 -0
  66. data/test/integration/explanation_test.rb +44 -0
  67. data/test/integration/facets_test.rb +311 -0
  68. data/test/integration/filtered_queries_test.rb +66 -0
  69. data/test/integration/filters_test.rb +75 -0
  70. data/test/integration/fuzzy_queries_test.rb +20 -0
  71. data/test/integration/highlight_test.rb +64 -0
  72. data/test/integration/index_aliases_test.rb +122 -0
  73. data/test/integration/index_mapping_test.rb +43 -0
  74. data/test/integration/index_store_test.rb +112 -0
  75. data/test/integration/index_update_document_test.rb +121 -0
  76. data/test/integration/match_query_test.rb +79 -0
  77. data/test/integration/mongoid_searchable_test.rb +309 -0
  78. data/test/integration/multi_search_test.rb +114 -0
  79. data/test/integration/nested_query_test.rb +135 -0
  80. data/test/integration/percolator_test.rb +111 -0
  81. data/test/integration/persistent_model_test.rb +205 -0
  82. data/test/integration/prefix_query_test.rb +43 -0
  83. data/test/integration/query_return_version_test.rb +70 -0
  84. data/test/integration/query_string_test.rb +52 -0
  85. data/test/integration/range_queries_test.rb +36 -0
  86. data/test/integration/reindex_test.rb +56 -0
  87. data/test/integration/results_test.rb +58 -0
  88. data/test/integration/scan_test.rb +56 -0
  89. data/test/integration/script_fields_test.rb +38 -0
  90. data/test/integration/sort_test.rb +52 -0
  91. data/test/integration/text_query_test.rb +39 -0
  92. data/test/models/active_model_article.rb +31 -0
  93. data/test/models/active_model_article_with_callbacks.rb +49 -0
  94. data/test/models/active_model_article_with_custom_document_type.rb +7 -0
  95. data/test/models/active_model_article_with_custom_index_name.rb +7 -0
  96. data/test/models/active_record_models.rb +131 -0
  97. data/test/models/article.rb +15 -0
  98. data/test/models/mongoid_models.rb +85 -0
  99. data/test/models/persistent_article.rb +11 -0
  100. data/test/models/persistent_article_in_index.rb +16 -0
  101. data/test/models/persistent_article_in_namespace.rb +12 -0
  102. data/test/models/persistent_article_with_casting.rb +28 -0
  103. data/test/models/persistent_article_with_defaults.rb +12 -0
  104. data/test/models/persistent_article_with_percolation.rb +5 -0
  105. data/test/models/persistent_articles_with_custom_index_name.rb +10 -0
  106. data/test/models/supermodel_article.rb +17 -0
  107. data/test/models/validated_model.rb +11 -0
  108. data/test/test_helper.rb +118 -0
  109. data/test/unit/active_model_lint_test.rb +17 -0
  110. data/test/unit/configuration_test.rb +84 -0
  111. data/test/unit/count_test.rb +67 -0
  112. data/test/unit/http_client_test.rb +79 -0
  113. data/test/unit/http_response_test.rb +49 -0
  114. data/test/unit/index_alias_test.rb +335 -0
  115. data/test/unit/index_test.rb +1098 -0
  116. data/test/unit/logger_test.rb +125 -0
  117. data/test/unit/model_callbacks_test.rb +116 -0
  118. data/test/unit/model_import_test.rb +75 -0
  119. data/test/unit/model_initialization_test.rb +31 -0
  120. data/test/unit/model_persistence_test.rb +548 -0
  121. data/test/unit/model_search_test.rb +964 -0
  122. data/test/unit/multi_search_test.rb +304 -0
  123. data/test/unit/results_collection_test.rb +372 -0
  124. data/test/unit/results_item_test.rb +173 -0
  125. data/test/unit/rubyext_test.rb +66 -0
  126. data/test/unit/search_facet_test.rb +186 -0
  127. data/test/unit/search_filter_test.rb +42 -0
  128. data/test/unit/search_highlight_test.rb +46 -0
  129. data/test/unit/search_query_test.rb +419 -0
  130. data/test/unit/search_scan_test.rb +113 -0
  131. data/test/unit/search_script_field_test.rb +26 -0
  132. data/test/unit/search_sort_test.rb +50 -0
  133. data/test/unit/search_test.rb +556 -0
  134. data/test/unit/tire_test.rb +144 -0
  135. data/tire.gemspec +83 -0
  136. metadata +586 -0
@@ -0,0 +1,12 @@
1
+ class PersistentArticleWithDefaults
2
+
3
+ include Tire::Model::Persistence
4
+
5
+ property :title
6
+ property :published_on
7
+ property :tags, :default => []
8
+ property :hidden, :default => false
9
+ property :options, :default => {:switches => []}
10
+ property :created_at, :default => lambda { Time.now }
11
+
12
+ end
@@ -0,0 +1,5 @@
1
+ class PersistentArticleWithPercolation
2
+ include Tire::Model::Persistence
3
+ property :title
4
+ percolate!
5
+ end
@@ -0,0 +1,10 @@
1
+ # Example class with Elasticsearch persistence and custom index name
2
+
3
+ class PersistentArticleWithCustomIndexName
4
+
5
+ include Tire::Model::Persistence
6
+
7
+ property :title
8
+
9
+ index_name 'custom-index-name'
10
+ end
@@ -0,0 +1,17 @@
1
+ # Example ActiveModel class for testing :searchable mode
2
+
3
+ require 'rubygems'
4
+ require 'redis/persistence'
5
+
6
+ class SupermodelArticle
7
+ include Redis::Persistence
8
+
9
+ include Tire::Model::Search
10
+ include Tire::Model::Callbacks
11
+
12
+ property :title
13
+
14
+ mapping do
15
+ indexes :title, :type => 'string', :boost => 15, :analyzer => 'czech'
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ # Example ActiveModel with validations
2
+
3
+ class ValidatedModel
4
+
5
+ include Tire::Model::Persistence
6
+
7
+ property :name
8
+
9
+ validates_presence_of :name
10
+
11
+ end
@@ -0,0 +1,118 @@
1
+ ENV['DEBUG'] = 'true'
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+
6
+ require 'pathname'
7
+ require 'test/unit'
8
+
9
+ JRUBY = defined?(JRUBY_VERSION)
10
+
11
+ if ENV['JSON_LIBRARY']
12
+ puts "Using '#{ENV['JSON_LIBRARY']}' JSON library"
13
+ require ENV['JSON_LIBRARY']
14
+ elsif JRUBY
15
+ require 'json'
16
+ else
17
+ require 'yajl/json_gem'
18
+ end
19
+
20
+ if JRUBY
21
+ require 'jdbc/sqlite3'
22
+ require 'active_record'
23
+ require 'active_record/connection_adapters/jdbcsqlite3_adapter'
24
+ else
25
+ require 'sqlite3'
26
+ end
27
+
28
+ require 'shoulda'
29
+ require 'turn/autorun' unless ENV["TM_FILEPATH"] || JRUBY
30
+ require 'mocha/setup'
31
+
32
+ require 'active_support/core_ext/hash/indifferent_access'
33
+
34
+ require 'tire'
35
+ if ENV['CURB']
36
+ puts "Using 'curb' as the HTTP library"
37
+ require 'tire/http/clients/curb'
38
+ Tire.configure { client Tire::HTTP::Client::Curb }
39
+ end
40
+
41
+ # Require basic model files
42
+ #
43
+ require File.dirname(__FILE__) + '/models/active_model_article'
44
+ require File.dirname(__FILE__) + '/models/active_model_article_with_callbacks'
45
+ require File.dirname(__FILE__) + '/models/active_model_article_with_custom_document_type'
46
+ require File.dirname(__FILE__) + '/models/active_model_article_with_custom_index_name'
47
+ require File.dirname(__FILE__) + '/models/active_record_models'
48
+ require File.dirname(__FILE__) + '/models/article'
49
+ require File.dirname(__FILE__) + '/models/persistent_article'
50
+ require File.dirname(__FILE__) + '/models/persistent_article_in_index'
51
+ require File.dirname(__FILE__) + '/models/persistent_article_in_namespace'
52
+ require File.dirname(__FILE__) + '/models/persistent_article_with_casting'
53
+ require File.dirname(__FILE__) + '/models/persistent_article_with_defaults'
54
+ require File.dirname(__FILE__) + '/models/persistent_articles_with_custom_index_name'
55
+ require File.dirname(__FILE__) + '/models/validated_model'
56
+
57
+ class Test::Unit::TestCase
58
+
59
+ def assert_block(message=nil)
60
+ raise Test::Unit::AssertionFailedError.new(message.to_s) if (! yield)
61
+ return true
62
+ end if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
63
+
64
+ def mock_response(body, code=200, headers={})
65
+ Tire::HTTP::Response.new(body, code, headers)
66
+ end
67
+
68
+ def fixtures_path
69
+ Pathname( File.expand_path( 'fixtures', File.dirname(__FILE__) ) )
70
+ end
71
+
72
+ def fixture_file(path)
73
+ File.read File.expand_path( path, fixtures_path )
74
+ end
75
+
76
+ end
77
+
78
+ module Test::Integration
79
+ URL = "http://localhost:9200"
80
+
81
+ def setup
82
+ begin; Object.send(:remove_const, :Rails); rescue; end
83
+
84
+ begin
85
+ ::RestClient.get URL
86
+ rescue Errno::ECONNREFUSED
87
+ abort "\n\n#{'-'*87}\n[ABORTED] You have to run Elasticsearch on #{URL} for integration tests\n#{'-'*87}\n\n"
88
+ end
89
+
90
+ ::RestClient.delete "#{URL}/articles-test" rescue nil
91
+ ::RestClient.post "#{URL}/articles-test", ''
92
+ fixtures_path.join('articles').entries.each do |f|
93
+ filename = f.to_s
94
+ next if filename =~ /^\./
95
+ ::RestClient.put "#{URL}/articles-test/article/#{File.basename(filename, '.*')}",
96
+ fixtures_path.join('articles').join(f).read
97
+ end
98
+ ::RestClient.post "#{URL}/articles-test/_refresh", ''
99
+
100
+ Dir[File.dirname(__FILE__) + '/models/**/*.rb'].each { |m| load m }
101
+ end
102
+
103
+ def teardown
104
+ %w[
105
+ articles-test
106
+ active_record_articles
107
+ active_model_article_with_custom_as_serializations
108
+ active_record_class_with_tire_methods
109
+ mongoid_articles
110
+ mongoid_class_with_tire_methods
111
+ supermodel_articles
112
+ dynamic_index
113
+ model_with_nested_documents
114
+ model_with_incorrect_mappings ].each do |index|
115
+ ::RestClient.delete "#{URL}/#{index}" rescue nil
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+ module Model
5
+
6
+ class ActiveModelLintTest < Test::Unit::TestCase
7
+
8
+ include ActiveModel::Lint::Tests
9
+
10
+ def setup
11
+ @model = PersistentArticle.new :title => 'Test'
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,84 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class ConfigurationTest < Test::Unit::TestCase
6
+
7
+ def teardown
8
+ Tire::Configuration.reset
9
+ ENV['ELASTICSEARCH_URL'] = nil
10
+ end
11
+
12
+ context "Configuration" do
13
+ setup do
14
+ Configuration.instance_variable_set(:@url, nil)
15
+ Configuration.instance_variable_set(:@client, nil)
16
+ end
17
+
18
+ teardown do
19
+ Configuration.reset
20
+ end
21
+
22
+ should "return default URL" do
23
+ assert_equal 'http://localhost:9200', Configuration.url
24
+ end
25
+
26
+ should "use environment variable, if present" do
27
+ ENV['ELASTICSEARCH_URL'] = 'http://es.example.com'
28
+ assert_equal 'http://es.example.com', Configuration.url
29
+ end
30
+
31
+ should "allow setting and retrieving the URL" do
32
+ assert_nothing_raised { Configuration.url 'http://example.com' }
33
+ assert_equal 'http://example.com', Configuration.url
34
+ end
35
+
36
+ should "strip trailing slash from the URL" do
37
+ assert_nothing_raised { Configuration.url 'http://slash.com:9200/' }
38
+ assert_equal 'http://slash.com:9200', Configuration.url
39
+ end
40
+
41
+ should "return default client" do
42
+ assert_equal HTTP::Client::RestClient, Configuration.client
43
+ end
44
+
45
+ should "return nil as logger by default" do
46
+ assert_nil Configuration.logger
47
+ end
48
+
49
+ should "return set and return logger" do
50
+ Configuration.logger STDERR
51
+ assert_not_nil Configuration.logger
52
+ assert_instance_of Tire::Logger, Configuration.logger
53
+ end
54
+
55
+ should "set pretty option to true by default" do
56
+ assert_not_nil Configuration.pretty
57
+ assert Configuration.pretty, "Should be true, but is: #{Configuration.pretty.inspect}"
58
+ end
59
+
60
+ should "set the pretty option to false" do
61
+ Configuration.pretty(false)
62
+ assert ! Configuration.pretty, "Should be falsy, but is: #{Configuration.pretty.inspect}"
63
+ end
64
+
65
+ should "allow to reset the configuration for specific property" do
66
+ Configuration.url 'http://example.com'
67
+ assert_equal 'http://example.com', Configuration.url
68
+ Configuration.reset :url
69
+ assert_equal 'http://localhost:9200', Configuration.url
70
+ end
71
+
72
+ should "allow to reset the configuration for all properties" do
73
+ Configuration.url 'http://example.com'
74
+ Configuration.wrapper Hash
75
+ assert_equal 'http://example.com', Configuration.url
76
+ Configuration.reset
77
+ assert_equal 'http://localhost:9200', Configuration.url
78
+ assert_equal HTTP::Client::RestClient, Configuration.client
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,67 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+ class CountTest < Test::Unit::TestCase
5
+
6
+ context "Count" do
7
+ setup { Configuration.reset }
8
+
9
+ should "be initialized with single index" do
10
+ c = Search::Count.new('index')
11
+ assert_equal ['index'], c.indices
12
+ assert_match %r|/index/_count|, c.url
13
+ end
14
+
15
+ should "count all documents by the leaving index empty" do
16
+ c = Search::Count.new
17
+ assert c.indices.empty?, "#{c.indices.inspect} should be empty"
18
+ assert_match %r|localhost:9200/_count|, c.url
19
+ end
20
+
21
+ should "limit count with document type" do
22
+ c = Search::Count.new('index', :type => 'bar')
23
+ assert_equal ['bar'], c.types
24
+ assert_match %r|index/bar/_count|, c.url
25
+ end
26
+
27
+ should "pass URL parameters" do
28
+ c = Search::Count.new('index', :routing => 123)
29
+ assert ! c.params.empty?
30
+ assert_match %r|routing=123|, c.params
31
+ end
32
+
33
+ should "evaluate the query" do
34
+ Search::Query.any_instance.expects(:instance_eval)
35
+
36
+ c = Search::Count.new('index') { string 'foo' }
37
+ assert_not_nil c.query
38
+ end
39
+
40
+ should "allow access to the JSON and the response" do
41
+ Configuration.client.expects(:get).returns(mock_response( '{"count":1}', 200 ))
42
+ c = Search::Count.new('index')
43
+ c.perform
44
+ assert_equal 1, c.json['count']
45
+ assert_equal 200, c.response.code
46
+ end
47
+
48
+ should "return curl snippet for debugging" do
49
+ c = Search::Count.new('index') { term :title, 'foo' }
50
+ assert_match %r|curl \-X GET 'http://localhost:9200/index/_count\?pretty' -d |, c.to_curl
51
+ assert_match %r|"term"\s*:\s*"foo"|, c.to_curl
52
+ end
53
+
54
+ should "log the request when logger is set" do
55
+ Configuration.logger STDERR
56
+
57
+ Configuration.client.expects(:get).returns(mock_response( '{"count":1}', 200 ))
58
+ Configuration.logger.expects(:log_request).returns(true)
59
+ Configuration.logger.expects(:log_response).returns(true)
60
+
61
+ Search::Count.new('index').value
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,79 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+ module HTTP
5
+
6
+ class ClientTest < Test::Unit::TestCase
7
+
8
+ context "RestClient" do
9
+
10
+ should "be default" do
11
+ assert_equal Client::RestClient, Configuration.client
12
+ end
13
+
14
+ should "respond to HTTP methods" do
15
+ assert_respond_to Client::RestClient, :get
16
+ assert_respond_to Client::RestClient, :post
17
+ assert_respond_to Client::RestClient, :put
18
+ assert_respond_to Client::RestClient, :delete
19
+ assert_respond_to Client::RestClient, :head
20
+ end
21
+
22
+ should "not rescue generic exceptions" do
23
+ Client::RestClient.expects(:get).raises(RuntimeError, "Something bad happened in YOUR code")
24
+
25
+ assert_raise(RuntimeError) do
26
+ Client::RestClient.get 'http://example.com'
27
+ end
28
+ end
29
+
30
+ should "not rescue ServerBrokeConnection errors" do
31
+ Client::RestClient.expects(:get).raises(RestClient::ServerBrokeConnection)
32
+
33
+ assert_raise(RestClient::ServerBrokeConnection) do
34
+ Client::RestClient.get 'http://example.com'
35
+ end
36
+ end
37
+
38
+ should "not rescue RequestTimeout errors" do
39
+ Client::RestClient.expects(:get).raises(RestClient::RequestTimeout)
40
+
41
+ assert_raise(RestClient::RequestTimeout) do
42
+ Client::RestClient.get 'http://example.com'
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ if defined?(Curl)
49
+ require 'tire/http/clients/curb'
50
+
51
+ context "Curb" do
52
+ setup do
53
+ Configuration.client Client::Curb
54
+ end
55
+
56
+ teardown do
57
+ Configuration.client Client::RestClient
58
+ end
59
+
60
+ should "use POST method if request body passed" do
61
+ ::Curl::Easy.any_instance.expects(:http_post)
62
+
63
+ response = Configuration.client.get "http://localhost:3000", '{ "query_string" : { "query" : "apple" }}'
64
+ end
65
+
66
+ should "use GET method if request body is nil" do
67
+ ::Curl::Easy.any_instance.expects(:http_get)
68
+
69
+ response = Configuration.client.get "http://localhost:9200/articles/article/1"
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+ module HTTP
5
+
6
+ class ResponseTest < Test::Unit::TestCase
7
+
8
+ context "Response" do
9
+
10
+ should "take response body, code and headers on initialization" do
11
+ response = Response.new "http response body",
12
+ 200,
13
+ :content_length => 20,
14
+ :content_encoding => 'gzip'
15
+
16
+ assert_equal "http response body", response.body
17
+ assert_equal 200, response.code
18
+ end
19
+
20
+ should "not require headers" do
21
+ assert_nothing_raised do
22
+ Response.new "Forbidden", 403
23
+ end
24
+ end
25
+
26
+ should "return success" do
27
+ responses = []
28
+ responses << Response.new('OK', 200)
29
+ responses << Response.new('Redirect', 302)
30
+
31
+ responses.each { |response| assert response.success? }
32
+
33
+ assert ! Response.new('NotFound', 404).success?
34
+ end
35
+
36
+ should "return failure" do
37
+ assert Response.new('NotFound', 404).failure?
38
+ end
39
+
40
+ should "return string representation" do
41
+ assert_equal "200 : Hello", Response.new('Hello', 200).to_s
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end