spec/thinking_sphinx/attribute_spec.rb in warp-thinking-sphinx-1.3.13 vs spec/thinking_sphinx/attribute_spec.rb in warp-thinking-sphinx-1.3.16
- old
+ new
@@ -45,51 +45,10 @@
it "should use the column name if there's no alias and just one column" do
@attribute.unique_name.should == "col_name"
end
end
- describe '#column_with_prefix' do
- before :each do
- @attribute = ThinkingSphinx::Attribute.new @source, [
- ThinkingSphinx::Index::FauxColumn.new(:col_name)
- ]
- @attribute.columns.each { |col| @attribute.associations[col] = [] }
- @attribute.model = Person
-
- @first_join = Object.new
- @first_join.stub!(:aliased_table_name => "tabular")
- @second_join = Object.new
- @second_join.stub!(:aliased_table_name => "data")
-
- @first_assoc = ThinkingSphinx::Association.new nil, nil
- @first_assoc.stub!(:join => @first_join, :has_column? => true)
- @second_assoc = ThinkingSphinx::Association.new nil, nil
- @second_assoc.stub!(:join => @second_join, :has_column? => true)
- end
-
- it "should return the column name if the column is a string" do
- @attribute.columns = [ThinkingSphinx::Index::FauxColumn.new("string")]
- @attribute.send(:column_with_prefix, @attribute.columns.first).should == "string"
- end
-
- it "should return the column with model's table prefix if there's no associations for the column" do
- @attribute.send(:column_with_prefix, @attribute.columns.first).should == "`people`.`col_name`"
- end
-
- it "should return the column with its join table prefix if an association exists" do
- column = @attribute.columns.first
- @attribute.associations[column] = [@first_assoc]
- @attribute.send(:column_with_prefix, column).should == "`tabular`.`col_name`"
- end
-
- it "should return multiple columns concatenated if more than one association exists" do
- column = @attribute.columns.first
- @attribute.associations[column] = [@first_assoc, @second_assoc]
- @attribute.send(:column_with_prefix, column).should == "`tabular`.`col_name`, `data`.`col_name`"
- end
- end
-
describe '#to_select_sql' do
it "should convert a mixture of dates and datetimes to timestamps" do
attribute = ThinkingSphinx::Attribute.new(@source,
[ ThinkingSphinx::Index::FauxColumn.new(:created_at),
ThinkingSphinx::Index::FauxColumn.new(:created_on) ],
@@ -97,10 +56,20 @@
)
attribute.model = Friendship
attribute.to_select_sql.should == "CONCAT_WS(',', UNIX_TIMESTAMP(`friendships`.`created_at`), UNIX_TIMESTAMP(`friendships`.`created_on`)) AS `times`"
end
+
+ it "should handle columns which don't exist for polymorphic joins" do
+ attribute = ThinkingSphinx::Attribute.new(@source,
+ [ ThinkingSphinx::Index::FauxColumn.new(:team, :name),
+ ThinkingSphinx::Index::FauxColumn.new(:team, :league) ],
+ :as => :team
+ )
+
+ attribute.to_select_sql.should == "CONCAT_WS(' ', IFNULL(`cricket_teams`.`name`, ''), IFNULL(`football_teams`.`name`, ''), IFNULL(`football_teams`.`league`, '')) AS `team`"
+ end
end
describe '#is_many?' do
before :each do
@assoc_a = stub('assoc', :is_many? => true)
@@ -181,27 +150,36 @@
@attribute.send(:type).should == :string
end
it "should return the column type from the database if not :multi or more than one association" do
@column.send(:instance_variable_set, :@name, "birthday")
- @attribute.send(:type).should == :datetime
+ @attribute.type.should == :datetime
@attribute.send(:instance_variable_set, :@type, nil)
@column.send(:instance_variable_set, :@name, "first_name")
- @attribute.send(:type).should == :string
+ @attribute.type.should == :string
@attribute.send(:instance_variable_set, :@type, nil)
@column.send(:instance_variable_set, :@name, "id")
- @attribute.send(:type).should == :integer
+ @attribute.type.should == :integer
end
it "should return :multi if the columns return multiple datetimes" do
@attribute.stub!(:is_many? => true)
@attribute.stub!(:all_datetimes? => true)
@attribute.type.should == :multi
end
+
+ it "should return :bigint for 64bit integers" do
+ Person.columns.detect { |col|
+ col.name == 'id'
+ }.stub!(:sql_type => 'BIGINT(20)')
+ @column.send(:instance_variable_set, :@name, 'id')
+
+ @attribute.type.should == :bigint
+ end
end
describe '#all_ints?' do
it "should return true if all columns are integers" do
attribute = ThinkingSphinx::Attribute.new(@source,
@@ -556,7 +534,15 @@
stub('column', :__stack => [:assoc_name], :__name => :id)
]
@instance.stub!(:assoc_name => stub('object', :id => 42))
@attribute.live_value(@instance).should == 42
end
+
+ it "should translate crc strings to their integer values" do
+ @attribute = ThinkingSphinx::Attribute.new @source, [
+ stub('column', :__stack => [], :__name => "col_name")
+ ], :crc => true, :type => :string
+ @instance.stub!(:col_name => 'foo')
+ @attribute.live_value(@instance).should == 'foo'.to_crc32
+ end
end
-end
\ No newline at end of file
+end