Permalink
Browse files

Rework test setup to have reasonable relationships.

[git-p4: depot-paths = "//src/Sphincter/dev/": change = 3357]
  • Loading branch information...
1 parent b75fd3f commit cb29f4a82f851fe07a918bdfa67da831ba264d31 @drbrain drbrain committed Aug 3, 2007
View
@@ -94,6 +94,11 @@ module Sphincter
VERSION = '1.0.0'
+ ##
+ # Sphincter error base class.
+
+ class Error < RuntimeError; end
+
end
require 'sphincter/configure'
@@ -209,6 +209,11 @@ def self.get_sources
assoc.name == as_name.intern
end
+ if as_assoc.nil? then
+ raise Sphincter::Error,
+ "could not find association \"#{as_name}\" in model \"#{klass.name}\""
+ end
+
as_klass = as_assoc.class_name.constantize
as_table = as_klass.table_name
as_pkey = conn.quote_column_name as_assoc.primary_key_name.to_s
@@ -5,7 +5,7 @@
$TESTING = true
class String
- def constantize() SphincterTestCase::Other end
+ def constantize() SphincterTestCase::BelongsTo end
end
require 'sphincter'
@@ -102,14 +102,14 @@ def initialize(macro, name)
@options = {}
end
- def class_name() @name.to_s.capitalize end
+ def class_name() @name.to_s.sub(/s$/, '').capitalize end
def primary_key_name() "#{@name}_id" end
end
class Model
- @reflections = [Reflection.new(:belongs_to, 'other'),
- Reflection.new(:has_many, 'other')]
+ @reflections = [Reflection.new(:belongs_to, 'belongs_to'),
+ Reflection.new(:has_many, 'manys')]
class << self; attr_accessor :reflections; end
@@ -142,15 +142,23 @@ def self.table_name() 'models' end
end
- class Other < Model
- @reflections = [Reflection.new(:belongs_to, 'model'),
- Reflection.new(:has_many, 'model')]
+ class BelongsTo < Model
+ @reflections = [Reflection.new(:belongs_to, 'something'),
+ Reflection.new(:has_many, 'models')]
- def self.table_name() 'others' end
+ def self.table_name() 'belongs_tos' end
def id() 42 end
end
+ class HasMany < Model
+ @reflections = [Reflection.new(:belongs_to, 'models')]
+
+ def self.table_name() 'has_manys' end
+
+ def id() 84 end
+ end
+
class Model
extend Sphincter::Search
end
@@ -176,7 +184,7 @@ def setup
Sphincter::Configure.instance_variable_set '@index_count', nil if
Sphincter::Configure.instance_variables.include? '@index_count'
- Other.reflections.last.options.delete :extend
+ BelongsTo.reflections.last.options.delete :extend
end
def teardown
@@ -9,7 +9,7 @@ class Proxy
attr_accessor :reflection
def initialize
- @reflection = SphincterTestCase::Reflection.new :has_many, 'other'
+ @reflection = SphincterTestCase::BelongsTo.reflections.last
klass = Object.new
def klass.search_args() @search_args end
def klass.search(*args) @search_args = args; :searched end
@@ -21,7 +21,7 @@ def proxy_reflection()
end
def proxy_owner()
- SphincterTestCase::Other.new
+ SphincterTestCase::BelongsTo.new
end
end
@@ -31,7 +31,7 @@ def test_search
results = proxy.search 'words'
assert_equal :searched, results
- assert_equal ['words', { :conditions => { 'other_id' => 42 } } ],
+ assert_equal ['words', { :conditions => { 'models_id' => 42 } } ],
proxy.proxy_reflection.klass.search_args
end
@@ -151,10 +151,24 @@ def test_self_get_sources
assert_equal expected, Sphincter::Configure.get_sources
end
- def test_self_get_sources_include
+ def test_self_get_sources_include_nonexistent_association
Sphincter::Search.indexes[Model] << {
:fields => %w[text],
- :include => %w[other.string]
+ :include => %w[nonexistent.string]
+ }
+
+ e = assert_raise Sphincter::Error do
+ Sphincter::Configure.get_sources
+ end
+
+ assert_equal "could not find association \"nonexistent\" in model \"Model\"",
+ e.message
+ end
+
+ def test_self_get_sources_include_belongs_to
+ Sphincter::Search.indexes[Model] << {
+ :fields => %w[text],
+ :include => %w[belongs_to.string]
}
expected = {
@@ -170,9 +184,9 @@ def test_self_get_sources_include
"0 AS sphincter_index_id, " \
"'Model' AS sphincter_klass, " \
"models.`text` AS `text`, " \
- "others.`string` AS `others_string` " \
- "FROM models, others " \
- "WHERE models.`other_id` = others.`id` AND "\
+ "belongs_tos.`string` AS `belongs_tos_string` " \
+ "FROM models, belongs_tos " \
+ "WHERE models.`belongs_to_id` = belongs_tos.`id` AND "\
"models.`id` >= $start AND " \
"models.`id` <= $end"
}
@@ -12,15 +12,15 @@ def test_self_indexes
def test_add_index
Model.add_index :fields => %w[text]
- assert_equal [{ :fields => %w[text other_id] }],
+ assert_equal [{ :fields => %w[text belongs_to_id] }],
Sphincter::Search.indexes[Model]
- other_belongs_to = Other.reflections.first
- other_has_many = Other.reflections.last
+ belongs_to_belongs_to = BelongsTo.reflections.first
+ belongs_to_has_many = BelongsTo.reflections.last
- assert_equal({}, other_belongs_to.options, 'Other belongs_to')
+ assert_equal({}, belongs_to_belongs_to.options, 'BelongsTo belongs_to')
assert_equal({ :extend => [Sphincter::AssociationSearcher] },
- other_has_many.options, 'Other has_many')
+ belongs_to_has_many.options, 'BelongsTo has_many')
end
def test_sphincter_convert_values

0 comments on commit cb29f4a

Please sign in to comment.