Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests for mysql #525

Merged
merged 2 commits into from
May 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _add_tags_caching_methods
# to mimic the underlying behavior. While processing this first
# call to columns, we do the caching column check and dynamically add
# the class and instance methods
# FIXME: this method cannot compile in rubinius
def columns
@acts_as_taggable_on_cache_columns ||= begin
db_columns = super
Expand Down
42 changes: 21 additions & 21 deletions lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ def initialize_acts_as_taggable_on_core
# when preserving tag order, include order option so that for a 'tags' context
# the associations tag_taggings & tags are always returned in created order
has_many_with_taggable_compatibility context_taggings, as: :taggable,
dependent: :destroy,
class_name: 'ActsAsTaggableOn::Tagging',
order: taggings_order,
conditions: ["#{ActsAsTaggableOn::Tagging.table_name}.context = (?)", tags_type],
include: :tag
dependent: :destroy,
class_name: 'ActsAsTaggableOn::Tagging',
order: taggings_order,
conditions: ["#{ActsAsTaggableOn::Tagging.table_name}.context = (?)", tags_type],
include: :tag

has_many_with_taggable_compatibility context_tags, through: context_taggings,
source: :tag,
class_name: 'ActsAsTaggableOn::Tag',
order: taggings_order
source: :tag,
class_name: 'ActsAsTaggableOn::Tag',
order: taggings_order

end

Expand Down Expand Up @@ -145,9 +145,9 @@ def tagged_with(tags, options = {})
if owned_by
tagging_join << ' AND ' +
sanitize_sql([
"#{taggings_alias}.tagger_id = ? AND #{taggings_alias}.tagger_type = ?",
owned_by.id,
owned_by.class.base_class.to_s
"#{taggings_alias}.tagger_id = ? AND #{taggings_alias}.tagger_type = ?",
owned_by.id,
owned_by.class.base_class.to_s
])
end

Expand All @@ -170,9 +170,9 @@ def tagged_with(tags, options = {})
if owned_by
tagging_join << ' AND ' +
sanitize_sql([
"#{taggings_alias}.tagger_id = ? AND #{taggings_alias}.tagger_type = ?",
owned_by.id,
owned_by.class.base_class.to_s
"#{taggings_alias}.tagger_id = ? AND #{taggings_alias}.tagger_type = ?",
owned_by.id,
owned_by.class.base_class.to_s
])
end

Expand Down Expand Up @@ -202,13 +202,13 @@ def tagged_with(tags, options = {})

order_by << options[:order] if options[:order].present?

request = select(select_clause).
joins(joins.join(' ')).
where(conditions.join(' AND ')).
group(group).
having(having).
order(order_by.join(', ')).
readonly(false)
select(select_clause)
.joins(joins.join(' '))
.where(conditions.join(' AND '))
.group(group)
.having(having)
.order(order_by.join(', '))
.readonly(false)
end

def is_taggable?
Expand Down
10 changes: 5 additions & 5 deletions lib/acts_as_taggable_on/acts_as_taggable_on/related.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def group_columns(klass)
end

def related_where(klass, conditions)
klass.select("#{klass.table_name}.*, COUNT(#{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key}) AS count").
from("#{klass.table_name}, #{ActsAsTaggableOn::Tag.table_name}, #{ActsAsTaggableOn::Tagging.table_name}").
group(group_columns(klass)).
order('count DESC').
where(conditions)
klass.select("#{klass.table_name}.*, COUNT(#{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key}) AS count")
.from("#{klass.table_name}, #{ActsAsTaggableOn::Tag.table_name}, #{ActsAsTaggableOn::Tagging.table_name}")
.group(group_columns(klass))
.order('count DESC')
.where(conditions)
end
end
end
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def using_postgresql?

def postgresql_version
if using_postgresql?
connection.execute("SHOW SERVER_VERSION").first["server_version"].to_f
connection.execute('SHOW SERVER_VERSION').first['server_version'].to_f
end
end

Expand Down
12 changes: 7 additions & 5 deletions spec/acts_as_taggable_on/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
require 'db/migrate/2_add_missing_unique_indices.rb'

shared_examples_for 'without unique index' do
before { AddMissingUniqueIndices.down }
after { ActsAsTaggableOn::Tag.delete_all; AddMissingUniqueIndices.up }
prepend_before(:all) { AddMissingUniqueIndices.down }
append_after(:all) do
ActsAsTaggableOn::Tag.delete_all
AddMissingUniqueIndices.up
end
end

describe ActsAsTaggableOn::Tag do
Expand All @@ -14,10 +17,9 @@
end



describe 'named like any' do
context 'case insensitive collation and unique index on tag name' do
if ActsAsTaggableOn::Utils.using_case_insensitive_collation?
if ActsAsTaggableOn::Utils.using_case_insensitive_collation?
context 'case insensitive collation and unique index on tag name' do
before(:each) do
ActsAsTaggableOn::Tag.create(name: 'Awesome')
ActsAsTaggableOn::Tag.create(name: 'epic')
Expand Down
49 changes: 25 additions & 24 deletions spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,8 @@
it 'should return all column names joined for Tag GROUP clause' do
# NOTE: type column supports an STI Tag subclass in the test suite, though
# isn't included by default in the migration generator
expect(@taggable.grouped_column_names_for(ActsAsTaggableOn::Tag)).
to eq('tags.id, tags.name, tags.taggings_count, tags.type')
expect(@taggable.grouped_column_names_for(ActsAsTaggableOn::Tag))
.to eq('tags.id, tags.name, tags.taggings_count, tags.type')
end

it 'should return all column names joined for TaggableModel GROUP clause' do
Expand Down Expand Up @@ -847,33 +847,34 @@
end
end


if ActsAsTaggableOn::Utils.using_postgresql?
describe 'Taggable model with json columns' do
before(:each) do
@taggable = TaggableModelWithJson.new(:name => 'Bob Jones')
@taggables = [@taggable, TaggableModelWithJson.new(:name => 'John Doe')]
end
if ActsAsTaggableOn::Utils.postgresql_support_json?
describe 'Taggable model with json columns' do
before(:each) do
@taggable = TaggableModelWithJson.new(:name => 'Bob Jones')
@taggables = [@taggable, TaggableModelWithJson.new(:name => 'John Doe')]
end

it 'should be able to find by tag with context' do
@taggable.skill_list = 'ruby, rails, css'
@taggable.tag_list = 'bob, charlie'
@taggable.save
it 'should be able to find by tag with context' do
@taggable.skill_list = 'ruby, rails, css'
@taggable.tag_list = 'bob, charlie'
@taggable.save

expect(TaggableModelWithJson.tagged_with('ruby').first).to eq(@taggable)
expect(TaggableModelWithJson.tagged_with('ruby, css').first).to eq(@taggable)
expect(TaggableModelWithJson.tagged_with('bob', :on => :skills).first).to_not eq(@taggable)
expect(TaggableModelWithJson.tagged_with('bob', :on => :tags).first).to eq(@taggable)
end
expect(TaggableModelWithJson.tagged_with('ruby').first).to eq(@taggable)
expect(TaggableModelWithJson.tagged_with('ruby, css').first).to eq(@taggable)
expect(TaggableModelWithJson.tagged_with('bob', :on => :skills).first).to_not eq(@taggable)
expect(TaggableModelWithJson.tagged_with('bob', :on => :tags).first).to eq(@taggable)
end

it 'should be able to find tagged with any tag' do
bob = TaggableModelWithJson.create(:name => 'Bob', :tag_list => 'fitter, happier, more productive', :skill_list => 'ruby, rails, css')
frank = TaggableModelWithJson.create(:name => 'Frank', :tag_list => 'weaker, depressed, inefficient', :skill_list => 'ruby, rails, css')
steve = TaggableModelWithJson.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, ruby')
it 'should be able to find tagged with any tag' do
bob = TaggableModelWithJson.create(:name => 'Bob', :tag_list => 'fitter, happier, more productive', :skill_list => 'ruby, rails, css')
frank = TaggableModelWithJson.create(:name => 'Frank', :tag_list => 'weaker, depressed, inefficient', :skill_list => 'ruby, rails, css')
steve = TaggableModelWithJson.create(:name => 'Steve', :tag_list => 'fitter, happier, more productive', :skill_list => 'c++, java, ruby')

expect(TaggableModelWithJson.tagged_with(%w(ruby java), :order => 'taggable_model_with_jsons.name', :any => true).to_a).to eq([bob, frank, steve])
expect(TaggableModelWithJson.tagged_with(%w(c++ fitter), :order => 'taggable_model_with_jsons.name', :any => true).to_a).to eq([bob, steve])
expect(TaggableModelWithJson.tagged_with(%w(depressed css), :order => 'taggable_model_with_jsons.name', :any => true).to_a).to eq([bob, frank])
expect(TaggableModelWithJson.tagged_with(%w(ruby java), :order => 'taggable_model_with_jsons.name', :any => true).to_a).to eq([bob, frank, steve])
expect(TaggableModelWithJson.tagged_with(%w(c++ fitter), :order => 'taggable_model_with_jsons.name', :any => true).to_a).to eq([bob, steve])
expect(TaggableModelWithJson.tagged_with(%w(depressed css), :order => 'taggable_model_with_jsons.name', :any => true).to_a).to eq([bob, frank])
end
end
end
end
12 changes: 3 additions & 9 deletions spec/acts_as_taggable_on/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
require 'spec_helper'

describe ActsAsTaggableOn::Utils do
describe 'like_operator' do
before(:each) do
TaggableModel.acts_as_taggable_on(:tags, :languages, :skills, :needs, :offerings)
@taggable = TaggableModel.new(name: 'Bob Jones')
end


describe '#like_operator' do
it 'should return \'ILIKE\' when the adapter is PostgreSQL' do
allow(TaggableModel.connection).to receive(:adapter_name) { 'PostgreSQL' }
expect(TaggableModel.send(:like_operator)).to eq('ILIKE')
expect(TaggableModel.like_operator).to eq('ILIKE')
end

it 'should return \'LIKE\' when the adapter is not PostgreSQL' do
allow(TaggableModel.connection).to receive(:adapter_name) { 'MySQL' }
expect(TaggableModel.send(:like_operator)).to eq('LIKE')
expect(TaggableModel.like_operator).to eq('LIKE')
end
end
end
2 changes: 1 addition & 1 deletion spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
t.column :cached_glass_list, :string, array: true
end

if self.
if ActsAsTaggableOn::Utils.postgresql_support_json?
create_table :taggable_model_with_jsons, :force => true do |t|
t.column :name, :string
t.column :type, :string
Expand Down
4 changes: 4 additions & 0 deletions spec/support/database_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
DatabaseCleaner.clean
end

config.after(:suite) do
DatabaseCleaner.clean
end

config.before(:each) do
DatabaseCleaner.start
end
Expand Down