dnif 0.0.1.alpha.5 → 0.0.1.alpha.6

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.
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dnif}
8
- s.version = "0.0.1.alpha.5"
8
+ s.version = "0.0.1.alpha.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rafael Souza"]
@@ -3,10 +3,6 @@ module Dnif
3
3
  def self.search(query, options = {})
4
4
  options.reverse_merge!(:index => '*')
5
5
 
6
- Dnif.root_path ||= File.expand_path(File.dirname("."))
7
- searchd = Dnif::Configuration.options_for("searchd", File.join(Dnif.root_path, "config/sphinx", Dnif.environment + ".erb"))
8
- client = Riddle::Client.new(*searchd["listen"].split(":"))
9
-
10
6
  if not options[:class].nil?
11
7
  filter_value = Dnif::MultiAttribute.encode(options[:class]).split(",").map(&:to_i)
12
8
  client.filters << Riddle::Client::Filter.new("class_id", filter_value)
@@ -29,6 +25,23 @@ module Dnif
29
25
  end.flatten
30
26
  end
31
27
 
28
+ def self.client
29
+ searchd = Dnif::Configuration.options_for("searchd", config_path)
30
+ if searchd["listen"]
31
+ address, port = searchd["listen"].split(":")
32
+ else
33
+ address = searchd["address"] || "127.0.0.1"
34
+ port = searchd["port"] || 3313
35
+ end
36
+
37
+ client = Riddle::Client.new(address, port)
38
+ end
39
+
40
+ def self.config_path
41
+ Dnif.root_path ||= File.expand_path(File.dirname("."))
42
+ File.join(Dnif.root_path, "config/sphinx", Dnif.environment + ".erb")
43
+ end
44
+
32
45
  module Search
33
46
 
34
47
  def search(query)
@@ -3,7 +3,63 @@ require 'test_helper'
3
3
 
4
4
  class TestSearch < Test::Unit::TestCase
5
5
 
6
- test "search" do
6
+ test ".config_path with nil root_path" do
7
+ File.expects(:expand_path).returns("/expanded/path")
8
+ Dnif.expects(:root_path).twice.returns(nil, ".")
9
+ Dnif.expects(:root_path=).with("/expanded/path")
10
+ Dnif.expects(:environment).returns("development")
11
+
12
+ assert_equal "./config/sphinx/development.erb", Dnif.config_path
13
+ end
14
+
15
+ test ".config_path with defined root_path" do
16
+ Dnif.expects(:root_path).twice.returns("/root/path", "/root/path")
17
+ Dnif.expects(:environment).returns("development")
18
+
19
+ assert_equal "/root/path/config/sphinx/development.erb", Dnif.config_path
20
+ end
21
+
22
+ test ".client when config uses listen" do
23
+ Dnif.expects(:config_path).returns("config/path.erb")
24
+ Dnif::Configuration.expects(:options_for).with("searchd", "config/path.erb").returns({ "listen" => "127.0.0.1:3313" })
25
+ Riddle::Client.expects(:new).with("127.0.0.1", "3313")
26
+
27
+ Dnif.client
28
+ end
29
+
30
+ test ".client when config uses address and port" do
31
+ Dnif.expects(:config_path).returns("config/path.erb")
32
+ Dnif::Configuration.expects(:options_for).with("searchd", "config/path.erb").returns({ "address" => "127.0.0.1", "port" => "3313" })
33
+ Riddle::Client.expects(:new).with("127.0.0.1", "3313")
34
+
35
+ Dnif.client
36
+ end
37
+
38
+ test ".search" do
39
+ results = {
40
+ :matches => [{
41
+ :doc => 2983,
42
+ :attributes => {
43
+ "class_id" => "336,623,883,1140"
44
+ }
45
+ }, {
46
+ :doc => 7893,
47
+ :attributes => {
48
+ "class_id" => "323,623,877,1133,1381,1646,1908"
49
+ }
50
+ }]
51
+ }
52
+
53
+ riddle = mock("Riddle")
54
+ riddle.expects(:query).with("my search", "*").returns(results)
55
+ Dnif.expects(:client).returns(riddle)
56
+ Post.expects(:find_all_by_id).once.with([1])
57
+ Comment.expects(:find_all_by_id).once.with([2])
58
+
59
+ Dnif.search("my search")
60
+ end
61
+
62
+ test ".search through models" do
7
63
  results_for_post = {
8
64
  :matches => [{
9
65
  :doc => 2983,
@@ -21,13 +77,19 @@ class TestSearch < Test::Unit::TestCase
21
77
  }]
22
78
  }
23
79
 
24
- Dnif::Configuration.expects(:options_for).twice.returns({ "listen" => "127.0.0.1:3333" })
25
- Riddle::Client.any_instance.expects(:query).twice.with("post", "*").returns(results_for_post, results_for_comment)
80
+ riddle = mock("Riddle")
81
+ riddle.expects(:query).with("post", "*").returns(results_for_post)
82
+ riddle.expects(:query).with("comment", "*").returns(results_for_comment)
83
+ riddle.expects(:filters).twice.returns([])
84
+ Dnif.expects(:client).times(4).returns(riddle)
85
+
86
+ Riddle::Client::Filter.expects(:new).with("class_id", [336, 623, 883, 1140])
87
+ Riddle::Client::Filter.expects(:new).with("class_id", [323, 623, 877, 1133, 1381, 1646, 1908])
26
88
 
27
89
  Post.expects(:find_all_by_id).once.with([1])
28
90
  Comment.expects(:find_all_by_id).once.with([2])
29
91
 
30
92
  Post.search("post")
31
- Comment.search("post")
93
+ Comment.search("comment")
32
94
  end
33
95
  end
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 1
9
9
  - alpha
10
- - 5
11
- version: 0.0.1.alpha.5
10
+ - 6
11
+ version: 0.0.1.alpha.6
12
12
  platform: ruby
13
13
  authors:
14
14
  - Rafael Souza