Skip to content

Commit

Permalink
more in-depth testing of multi class facets
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Pickett authored and pat committed Mar 14, 2009
1 parent e65d72f commit eec7a2b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 6 deletions.
6 changes: 5 additions & 1 deletion features/facets.feature
Expand Up @@ -37,7 +37,11 @@ Feature: Search and browse models by their defined facets
Given Sphinx is running
When I am requesting facet results with classes included
Then I should have valid facet results
And I should have 6 facets
And I should have 7 facets
And I should have the facet Class
And it should have a "Person" key
And I should have the facet Gender
And it should have a "female" key
And I should have the facet Country
And I should have the facet category_name
And it should have a "hello" key with 2 hits
18 changes: 17 additions & 1 deletion features/step_definitions/facet_steps.rb
Expand Up @@ -31,6 +31,22 @@
results.keys.length.should == count.to_i
end

Then /^I should have the facet (\w+)$/ do |name|
Then /^I should have the facet ([\w_]+)$/ do |name|
results[name.downcase.to_sym].should be_kind_of(Hash)
@facet = name.downcase.to_sym
end

require "ruby-debug"
Then /^it should have a "([\w\s_]+)" key with (\d+) hits$/ do |key, hit_count|
verify_presence_of(key)
results[@facet][@item].should eql(hit_count.to_i)
end

Then /^it should have a "(\w+)" key$/ do |key|
verify_presence_of(key)
end

def verify_presence_of(key)
@item = key
results[@facet].keys.include?(@item).should be_true
end
5 changes: 5 additions & 0 deletions features/support/db/migrations/create_categories.rb
@@ -0,0 +1,5 @@
ActiveRecord::Base.connection.create_table :categories, :force => true do |t|
t.column :name, :string
end

Category.create :name => "hello"
4 changes: 3 additions & 1 deletion features/support/db/migrations/create_comments.rb
Expand Up @@ -4,12 +4,14 @@
t.column :url, :string
t.column :content, :text
t.column :post_id, :integer, :null => false
t.column :category_id, :integer, :null => false
end

Comment.create(
:name => "Pat",
:content => "+1",
:post_id => 1
:post_id => 1,
:category_id => 1
)

Comment.create(
Expand Down
3 changes: 2 additions & 1 deletion features/support/db/migrations/create_posts.rb
@@ -1,6 +1,7 @@
ActiveRecord::Base.connection.create_table :posts, :force => true do |t|
t.column :subject, :string, :null => false
t.column :content, :text
t.column :category_id, :integer, :null => false
end

Post.create :subject => "Hello World", :content => "Um Text", :id => 1
Post.create :subject => "Hello World", :content => "Um Text", :id => 1, :category_id => 1
4 changes: 4 additions & 0 deletions features/support/models/category.rb
@@ -0,0 +1,4 @@
class Category < ActiveRecord::Base
has_many :posts
has_many :comments
end
9 changes: 8 additions & 1 deletion features/support/models/comment.rb
@@ -1,3 +1,10 @@
class Comment < ActiveRecord::Base
belongs_to :post
end
belongs_to :category

define_index do
indexes :content

has category.name, :facet => true, :as => :category_name, :type => :string
end
end
2 changes: 2 additions & 0 deletions features/support/models/post.rb
@@ -1,6 +1,7 @@
class Post < ActiveRecord::Base
has_many :comments, :dependent => :destroy
has_many :tags, :dependent => :destroy
belongs_to :category

define_index do
indexes subject
Expand All @@ -9,5 +10,6 @@ class Post < ActiveRecord::Base
indexes comments.content, :as => :comments

has comments(:id), :as => :comment_ids, :source => :ranged_query
has category.name, :facet => true, :as => :category_name, :type => :string
end
end
2 changes: 1 addition & 1 deletion lib/thinking_sphinx/facet_collection.rb
Expand Up @@ -17,7 +17,7 @@ def add_from_results(facet, results)
facet_value = facet.value(result, group)

self[facet.name][facet_value] ||= 0
self[facet.name][facet_value] += count
self[facet.name][facet_value] = count
@attribute_values[facet.name][facet_value] ||= group
}
end
Expand Down

0 comments on commit eec7a2b

Please sign in to comment.