Skip to content

Commit

Permalink
Adding some features for datamapper, confirming searching, deletion a…
Browse files Browse the repository at this point in the history
…nd deltas work.
  • Loading branch information
pat committed Dec 7, 2009
1 parent 7dd6213 commit 911da8d
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cucumber.yml
@@ -1 +1 @@
default: "--require features/support/env.rb --require features/step_definitions/alpha_steps.rb --require features/step_definitions/beta_steps.rb --require features/step_definitions/common_steps.rb --require features/step_definitions/extensible_delta_indexing_steps.rb --require features/step_definitions/facet_steps.rb --require features/step_definitions/find_arguments_steps.rb --require features/step_definitions/gamma_steps.rb --require features/step_definitions/scope_steps.rb --require features/step_definitions/search_steps.rb --require features/step_definitions/sphinx_steps.rb"
default: "--require features/support/env.rb --require features/step_definitions/alpha_steps.rb --require features/step_definitions/beta_steps.rb --require features/step_definitions/book_steps.rb --require features/step_definitions/common_steps.rb --require features/step_definitions/extensible_delta_indexing_steps.rb --require features/step_definitions/facet_steps.rb --require features/step_definitions/find_arguments_steps.rb --require features/step_definitions/gamma_steps.rb --require features/step_definitions/scope_steps.rb --require features/step_definitions/search_steps.rb --require features/step_definitions/sphinx_steps.rb"
9 changes: 9 additions & 0 deletions features/deleting_instances.feature
Expand Up @@ -13,6 +13,15 @@ Feature: Keeping Sphinx in line with deleted model instances
And I wait for Sphinx to catch up
And I search for three
Then I should get 0 results

Given I am searching on books
When I search for "Da Vinci"
Then I should get 1 result

When I destroy the book titled "Da Vinci Code"
And I wait for Sphinx to catch up
And I search for "Da Vinci"
Then I should get 0 results

Scenario: Deleting subclasses when the parent class is indexed
Given Sphinx is running
Expand Down
9 changes: 9 additions & 0 deletions features/handling_edits.feature
Expand Up @@ -38,6 +38,15 @@ Feature: Keeping Sphinx in line with model changes when requested

When I search for eleven
Then I should get 1 result

Given I am searching on books
When I search for "Colour of Magic"
Then I should get 0 result

When I change the title of "The Color of Magic" to "The Colour of Magic"
And I wait for Sphinx to catch up
And I search for "Colour of Magic"
Then I should get 1 result

Scenario: Returning new records if there's a delta
Given Sphinx is running
Expand Down
10 changes: 10 additions & 0 deletions features/searching_by_model.feature
Expand Up @@ -8,12 +8,22 @@ Feature: Searching on a single model
And I am searching on people
When I search for James
Then I should get 3 results

Given I am searching on books
When I search for "Neil Gaiman"
Then I should get 2 results

Scenario: Searching on a specific field
Given Sphinx is running
And I am searching on people
When I search for James on first_name
Then I should get 2 results

Scenario: Searching on a specific field with DataMapper
Given Sphinx is running
And I am searching on books
When I search for "Night Watch" on title
Then I should get 1 result

Scenario: Searching on multiple fields
Given Sphinx is running
Expand Down
7 changes: 7 additions & 0 deletions features/step_definitions/book_steps.rb
@@ -0,0 +1,7 @@
When /^I destroy the book titled "([^\"]*)"$/ do |title|
Book.first(:title => title).destroy
end

When /^I change the title of "([^\"]*)" to "([^\"]*)"$/ do |title, value|
Book.first(:title => title).update(:title => value)
end
5 changes: 5 additions & 0 deletions features/step_definitions/common_steps.rb
Expand Up @@ -56,6 +56,11 @@
@conditions[field.to_sym] = query
end

When /^I search for "([^\"]*)" on (\w+)$/ do |query, field|
@results = nil
@conditions[field.to_sym] = query
end

When /^I output the raw result data$/ do
puts results.results.inspect
end
Expand Down
5 changes: 5 additions & 0 deletions features/support/db/fixtures/books.rb
@@ -0,0 +1,5 @@
Book.create :title => "American Gods", :author => "Neil Gaiman"
Book.create :title => "Anansi Boys", :author => "Neil Gaiman"
Book.create :title => "Night Watch", :author => "Terry Pratchett"
Book.create :title => "The Color of Magic", :author => "Terry Pratchett"
Book.create :title => "Da Vinci Code", :author => "Dan Brown"
3 changes: 2 additions & 1 deletion features/support/env.rb
Expand Up @@ -3,8 +3,9 @@
require 'spec'
require 'fileutils'
require 'ginger'
require 'will_paginate'
require 'dm-core'
require 'active_record'
require 'will_paginate'

$:.unshift File.dirname(__FILE__) + '/../../lib'
Dir[File.join(File.dirname(__FILE__), '../../vendor/*/lib')].each do |path|
Expand Down
2 changes: 2 additions & 0 deletions features/support/models/beta.rb
@@ -1,4 +1,6 @@
class Beta < ActiveRecord::Base
set_table_name 'betas'

define_index do
indexes :name, :sortable => true
has value
Expand Down
17 changes: 17 additions & 0 deletions features/support/models/book.rb
@@ -0,0 +1,17 @@
class Book
include DataMapper::Resource
include ThinkingSphinx::Base
include ThinkingSphinx::DataMapper

property :id, Serial
property :title, String
property :author, String
property :delta, Boolean

define_index do
indexes title
indexes author

set_property :delta => true
end
end
2 changes: 2 additions & 0 deletions features/support/models/extensible_beta.rb
@@ -1,6 +1,8 @@
require File.join(File.dirname(__FILE__), "..", "lib", "generic_delta_handler")

class ExtensibleBeta < ActiveRecord::Base
set_table_name 'extensible_betas'

define_index do
indexes :name, :sortable => true

Expand Down
8 changes: 7 additions & 1 deletion lib/cucumber/thinking_sphinx/internal_world.rb
Expand Up @@ -36,7 +36,10 @@ def setup
end

def configure_database
ActiveRecord::Base.establish_connection database_settings
db_settings = database_settings
ActiveRecord::Base.establish_connection db_settings
DataMapper.setup :default, "#{db_settings['adapter']}://#{db_settings['username']}:#{db_settings['password']}@#{db_settings['host']}/#{db_settings['database']}"

self
end

Expand Down Expand Up @@ -102,6 +105,9 @@ def prepare_data

load_files migrations_directory
load_files models_directory

DataMapper.auto_migrate!

load_files fixtures_directory

::ThinkingSphinx.deltas_enabled = true
Expand Down
6 changes: 2 additions & 4 deletions lib/thinking_sphinx/active_record.rb
Expand Up @@ -97,11 +97,9 @@ def sphinx_adapter_type
def primary_key_for_sphinx
@primary_key_for_sphinx ||= read_attribute(self.class.primary_key_for_sphinx)
end

private

def process_indexes
self.class.process_indexes
def new_record_for_sphinx?
new_record?
end
end

Expand Down
6 changes: 6 additions & 0 deletions lib/thinking_sphinx/base.rb
Expand Up @@ -213,4 +213,10 @@ def sphinx_document_id
def primary_key_for_sphinx
-1
end

private

def process_indexes
self.class.process_indexes
end
end
23 changes: 14 additions & 9 deletions lib/thinking_sphinx/data_mapper.rb
Expand Up @@ -15,31 +15,32 @@ def sphinx_tailor_for(source)
end

def find_for_sphinx(ids, options, index_options)
all(primary_key_for_sphinx.to_sym => ids)
all(
primary_key_for_sphinx.to_sym => ids,
:order => (options[:sql_order] || index_options[:sql_order])
)
end

private

def add_initial_sphinx_callbacks
super
# before_validation :process_indexes
# before_destroy :process_indexes

before :save, :process_indexes # is before_validation in AR
before :destroy, :process_indexes
end

def add_standard_sphinx_callbacks
super

# after_destroy :toggle_deleted

# include ThinkingSphinx::ActiveRecord::AttributeUpdates
after :destroy, :toggle_deleted
end

def add_delta_sphinx_callbacks
super

# before_save :toggle_delta
# after_commit :index_delta
before :save, :toggle_delta
after :save, :index_delta
end

def sphinx_adapter_type
Expand All @@ -57,6 +58,10 @@ def sphinx_adapter_type
def primary_key_for_sphinx
id
end

def new_record_for_sphinx?
new?
end
end

require 'thinking_sphinx/data_mapper/tailor'
2 changes: 1 addition & 1 deletion lib/thinking_sphinx/delta.rb
Expand Up @@ -33,7 +33,7 @@ def index_delta
end

def should_toggle_delta?
new_record? || indexed_data_changed?
new_record_for_sphinx? || indexed_data_changed?
end

def indexed_data_changed?
Expand Down
4 changes: 2 additions & 2 deletions lib/thinking_sphinx/deltas/default_delta.rb
Expand Up @@ -34,8 +34,8 @@ def reset_query(tailor)
end

def clause(tailor, toggled)
"#{tailor.quoted_table_name}.#{tailor.quote_column_name(@column.to_s)}" +
" = #{adapter.boolean(toggled)}"
column = "#{tailor.quoted_table_name}.#{tailor.quote_column_name(@column.to_s)}"
"#{adapter.convert_nulls(column, 0)} = #{adapter.boolean(toggled)}"
end

private
Expand Down

0 comments on commit 911da8d

Please sign in to comment.