spec/unit/thinking_sphinx/attribute_spec.rb in DrMark-thinking-sphinx-0.9.9 vs spec/unit/thinking_sphinx/attribute_spec.rb in DrMark-thinking-sphinx-1.1.6

- old
+ new

@@ -13,158 +13,10 @@ ThinkingSphinx::Attribute.new([1234]) }.should raise_error(RuntimeError) end end - describe "to_select_sql method with MySQL" do - before :each do - @index = Person.indexes.first - @index.link! - end - - it "should concat with spaces if there's more than one non-integer column" do - @index.attributes[0].to_select_sql.should match(/CONCAT_WS\(' ', /) - end - - it "should concat with spaces if there's more than one association for a non-integer column" do - @index.attributes[1].to_select_sql.should match(/CONCAT_WS\(' ', /) - end - - it "should concat with commas if there's multiple integer columns" do - @index.attributes[2].to_select_sql.should match(/CONCAT_WS\(',', /) - end - - it "should concat with commas if there's more than one association for an integer column" do - @index.attributes[3].to_select_sql.should match(/CONCAT_WS\(',', /) - end - - it "should group with spaces if there's string columns from a has_many or has_and_belongs_to_many association" do - @index.attributes[4].to_select_sql.should match(/GROUP_CONCAT\(.+ SEPARATOR ' '\)/) - end - - it "should group with commas if there's integer columns from a has_many or has_and_belongs_to_many association" do - @index.attributes[5].to_select_sql.should match(/GROUP_CONCAT\(.+ SEPARATOR ','\)/) - end - - it "should convert datetime values to timestamps" do - @index.attributes[6].to_select_sql.should match(/UNIX_TIMESTAMP/) - end - end - - describe "to_select_sql method with PostgreSQL" do - before :each do - @index = Person.indexes.first - Person.connection.class.stub_method( - :name => "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter" - ) - @index.link! - end - - it "should concat with spaces if there's more than one non-integer column" do - @index.attributes[0].to_select_sql.should match(/|| ' ' ||/) - end - - it "should concat with spaces if there's more than one association for a non-integer column" do - @index.attributes[1].to_select_sql.should match(/|| ' ' ||/) - end - - it "should concat with commas if there's multiple integer columns" do - @index.attributes[2].to_select_sql.should match(/|| ',' ||/) - end - - it "should concat with commas if there's more than one association for an integer column" do - @index.attributes[3].to_select_sql.should match(/|| ',' ||/) - end - - it "should group with spaces if there's string columns from a has_many or has_and_belongs_to_many association" do - @index.attributes[4].to_select_sql.should match(/array_to_string\(array_accum\(.+, ' '\)/) - end - - it "should group with commas if there's integer columns from a has_many or has_and_belongs_to_many association" do - @index.attributes[5].to_select_sql.should match(/array_to_string\(array_accum\(.+, ','\)/) - end - end - - describe "to_group_sql method" do - before :each do - @attribute = ThinkingSphinx::Attribute.new([Object.stub_instance(:__stack => [])]) - @attribute.stub_method(:is_many? => false, :is_string? => false) - - ThinkingSphinx.stub_method(:use_group_by_shortcut? => false) - end - - it "should return nil if is_many?" do - @attribute.stub_method(:is_many? => true) - - @attribute.to_group_sql.should be_nil - end - - it "should return nil if is_string?" do - @attribute.stub_method(:is_string? => true) - - @attribute.to_group_sql.should be_nil - end - - it "should return nil if group_by shortcut is allowed" do - ThinkingSphinx.stub_method(:use_group_by_shortcut? => true) - - @attribute.to_group_sql.should be_nil - end - - it "should return an array if neither is_many? or shortcut allowed" do - @attribute.stub_method(:column_with_prefix => 'hello') - @attribute.to_group_sql.should be_a_kind_of(Array) - end - - after :each do - ThinkingSphinx.unstub_method(:use_group_by_shortcut?) - end - end - - describe "to_sphinx_clause method" do - before :each do - @attribute = ThinkingSphinx::Attribute.new [Object.stub_instance(:__stack => [])] - @attribute.stub_method(:unique_name => "unique name") - end - - it "should use sql_attr_multi syntax for MVA attributes" do - @attribute.stub_method(:type => :multi) - @attribute.to_sphinx_clause.should match(/^sql_attr_multi\s+= uint unique name from field$/) - end - - it "should use sql_attr_timestamp syntax for datetime values" do - @attribute.stub_method(:type => :datetime) - @attribute.to_sphinx_clause.should match(/^sql_attr_timestamp\s+= unique name$/) - end - - it "should use sql_attr_str2ordinal for string values" do - @attribute.stub_method(:type => :string) - @attribute.to_sphinx_clause.should match(/^sql_attr_str2ordinal\s+= unique name$/) - end - - it "should use sql_attr_float for float values" do - @attribute.stub_method(:type => :float) - @attribute.to_sphinx_clause.should match(/^sql_attr_float\s+= unique name$/) - end - - it "should use sql_attr_bool for boolean values" do - @attribute.stub_method(:type => :boolean) - @attribute.to_sphinx_clause.should match(/^sql_attr_bool\s+= unique name$/) - end - - it "should use sql_attr_uint for integer values" do - @attribute.stub_method(:type => :integer) - @attribute.to_sphinx_clause.should match(/^sql_attr_uint\s+= unique name$/) - end - - it "should assume integer for any other types" do - @attribute.stub_method(:type => :unknown) - @attribute.to_sphinx_clause.should match(/^sql_attr_uint\s+= unique name$/) - end - - end - describe "unique_name method" do before :each do @attribute = ThinkingSphinx::Attribute.new [ Object.stub_instance(:__stack => [], :__name => "col_name") ] @@ -303,11 +155,11 @@ @attribute.stub_method(:is_many? => true) @attribute.send(:type).should == :multi end it "should return :string if there's more than one association" do - @attribute.associations = {:a => :assoc, :b => :assoc} + @attribute.associations = {:a => [:assoc], :b => [:assoc]} @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") @@ -353,8 +205,23 @@ ) attribute.model = Person attribute.columns.each { |col| attribute.associations[col] = [] } attribute.send(:all_ints?).should be_false + end + end + + describe "with custom queries" do + before :each do + index = CricketTeam.sphinx_indexes.first + @statement = index.to_riddle_for_core(0, 0).sql_attr_multi.first + end + + it "should track the query type accordingly" do + @statement.should match(/uint tags from query/) + end + + it "should include the SQL statement" do + @statement.should match(/SELECT cricket_team_id, id FROM tags/) end end end \ No newline at end of file