diff --git a/.gitignore b/.gitignore index e9cd89e..50a350e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +*.log +.byebug_history /.bundle/ /.yardoc /Gemfile.lock diff --git a/.travis.yml b/.travis.yml index 22437a4..9f67a52 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,16 @@ rvm: - 2.3.4 - 2.4.1 +env: + - DB=sqlite + - DB=postgresql + - DB=mysql + +services: + - mysql +addons: + postgresql: "9.3" + gemfile: - gemfiles/rails_4.gemfile - gemfiles/rails_5.gemfile diff --git a/acts_as_votable.gemspec b/acts_as_votable.gemspec index 55823f3..e109999 100644 --- a/acts_as_votable.gemspec +++ b/acts_as_votable.gemspec @@ -24,6 +24,8 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec", "~> 3.6" s.add_development_dependency "sqlite3", "~> 1.3" + s.add_development_dependency "mysql2", "~> 0.4" + s.add_development_dependency "pg", "~> 0.15" s.add_development_dependency "rubocop", "~> 0.49.1" s.add_development_dependency "simplecov", "~> 0.15.0" s.add_development_dependency "appraisal", "~> 2.2" diff --git a/spec/shared_example/votable_model.rb b/spec/shared_example/votable_model.rb index da4d290..79985c1 100644 --- a/spec/shared_example/votable_model.rb +++ b/spec/shared_example/votable_model.rb @@ -406,7 +406,7 @@ it do expect(votable_cache.cached_votes_total).to eq(1) - expect(votable_cache.updated_at).to_not eq updated_at + expect(votable_cache.updated_at.to_i).to_not eq(updated_at.to_i) end end @@ -415,7 +415,7 @@ it do expect(votable_cache.cached_votes_total).to eq(1) - expect(votable_cache.updated_at).to eq updated_at + expect(votable_cache.updated_at.to_i).to eq(updated_at.to_i) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a261405..3b4ec85 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,139 +1,13 @@ # frozen_string_literal: true $LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib") -require "sqlite3" + require "simplecov" require "acts_as_votable" require "factory_girl" +require_relative "support/database" Dir["./spec/shared_example/**/*.rb"].sort.each { |f| require f } Dir["./spec/support/**/*.rb"].sort.each { |f| require f } SimpleCov.start - -ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") - -ActiveRecord::Schema.define(version: 1) do - create_table :votes do |t| - t.references :votable, polymorphic: true - t.references :voter, polymorphic: true - - t.boolean :vote_flag - t.string :vote_scope - t.integer :vote_weight - - t.timestamps - end - - add_index :votes, [:votable_id, :votable_type] - add_index :votes, [:voter_id, :voter_type] - add_index :votes, [:voter_id, :voter_type, :vote_scope] - add_index :votes, [:votable_id, :votable_type, :vote_scope] - - create_table :voters do |t| - t.string :name - end - - create_table :not_voters do |t| - t.string :name - end - - create_table :votables do |t| - t.string :name - end - - create_table :votable_voters do |t| - t.string :name - end - - create_table :sti_votables do |t| - t.string :name - t.string :type - end - - create_table :sti_not_votables do |t| - t.string :name - t.string :type - end - - create_table :not_votables do |t| - t.string :name - end - - create_table :votable_caches do |t| - t.string :name - t.integer :cached_votes_total - t.integer :cached_votes_score - t.integer :cached_votes_up - t.integer :cached_votes_down - t.integer :cached_weighted_total - t.integer :cached_weighted_score - t.float :cached_weighted_average - - t.integer :cached_scoped_test_votes_total - t.integer :cached_scoped_test_votes_score - t.integer :cached_scoped_test_votes_up - t.integer :cached_scoped_test_votes_down - t.integer :cached_scoped_weighted_total - t.integer :cached_scoped_weighted_score - t.float :cached_scoped_weighted_average - - t.timestamps - end - -end - - -class Voter < ActiveRecord::Base - acts_as_voter -end - -class NotVoter < ActiveRecord::Base -end - -class Votable < ActiveRecord::Base - acts_as_votable - validates_presence_of :name -end - -class VotableVoter < ActiveRecord::Base - acts_as_votable - acts_as_voter -end - -class StiVotable < ActiveRecord::Base - acts_as_votable -end - -class ChildOfStiVotable < StiVotable -end - -class StiNotVotable < ActiveRecord::Base - validates_presence_of :name -end - -class ChildOfStiNotVotable < StiNotVotable - acts_as_votable -end - -class NotVotable < ActiveRecord::Base -end - -class VotableCache < ActiveRecord::Base - acts_as_votable - validates_presence_of :name -end - -class VotableCacheUpdateAttributes < VotableCache - acts_as_votable cacheable_strategy: :update_attributes -end - -class VotableCacheUpdateColumns < VotableCache - acts_as_votable cacheable_strategy: :update_columns -end - -class ABoringClass - def self.hw - "hello world" - end -end diff --git a/spec/support/database.rb b/spec/support/database.rb new file mode 100644 index 0000000..438bc5e --- /dev/null +++ b/spec/support/database.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "logger" +require "yaml" + +ActiveRecord::Base.configurations = YAML.load_file(File.expand_path("db/database.yml", __dir__)) +db_config = ActiveRecord::Base.configurations.fetch(ENV["DB"] || "sqlite") + +begin + ActiveRecord::Base.establish_connection(db_config) + ActiveRecord::Base.connection +rescue + ActiveRecord::Base.establish_connection(db_config.merge("database" => nil)) + ActiveRecord::Base.connection.create_database(db_config["database"], db_config) + ActiveRecord::Base.establish_connection(db_config) +end + +ActiveRecord::Base.logger = Logger.new(File.join(__dir__, "debug.log")) +ActiveRecord::Base.logger.level = ENV["CI"] ? ::Logger::ERROR : ::Logger::DEBUG +ActiveRecord::Migration.verbose = false diff --git a/spec/support/db/database.yml b/spec/support/db/database.yml new file mode 100644 index 0000000..bb69ac4 --- /dev/null +++ b/spec/support/db/database.yml @@ -0,0 +1,16 @@ +sqlite: + adapter: sqlite3 + database: ":memory:" + +postgresql: + adapter: postgresql + username: postgres + password: + database: acts_as_votable_test + +mysql: + adapter: mysql2 + host: localhost + username: root + password: + database: acts_as_votable_test diff --git a/spec/support/db/schema.rb b/spec/support/db/schema.rb new file mode 100644 index 0000000..f41e939 --- /dev/null +++ b/spec/support/db/schema.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +ActiveRecord::Schema.define(version: 1) do + create_table :votes, force: true do |t| + t.references :votable, polymorphic: true + t.references :voter, polymorphic: true + + t.boolean :vote_flag + t.string :vote_scope + t.integer :vote_weight + + t.timestamps(null: false, precision: 6) + end + + add_index :votes, [:votable_id, :votable_type] + add_index :votes, [:voter_id, :voter_type] + add_index :votes, [:voter_id, :voter_type, :vote_scope] + add_index :votes, [:votable_id, :votable_type, :vote_scope] + + create_table :voters, force: true do |t| + t.string :name + end + + create_table :not_voters, force: true do |t| + t.string :name + end + + create_table :votables, force: true do |t| + t.string :name + end + + create_table :votable_voters, force: true do |t| + t.string :name + end + + create_table :sti_votables, force: true do |t| + t.string :name + t.string :type + end + + create_table :sti_not_votables, force: true do |t| + t.string :name + t.string :type + end + + create_table :not_votables, force: true do |t| + t.string :name + end + + create_table :votable_caches, force: true do |t| + t.string :name + t.integer :cached_votes_total + t.integer :cached_votes_score + t.integer :cached_votes_up + t.integer :cached_votes_down + t.integer :cached_weighted_total + t.integer :cached_weighted_score + t.float :cached_weighted_average + + t.integer :cached_scoped_test_votes_total + t.integer :cached_scoped_test_votes_score + t.integer :cached_scoped_test_votes_up + t.integer :cached_scoped_test_votes_down + t.integer :cached_scoped_weighted_total + t.integer :cached_scoped_weighted_score + t.float :cached_scoped_weighted_average + + t.timestamps(null: false, precision: 6) + end +end diff --git a/spec/support/models.rb b/spec/support/models.rb new file mode 100644 index 0000000..c4ec9a1 --- /dev/null +++ b/spec/support/models.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +class Voter < ActiveRecord::Base + acts_as_voter +end + +class NotVoter < ActiveRecord::Base +end + +class Votable < ActiveRecord::Base + acts_as_votable + validates_presence_of :name +end + +class VotableVoter < ActiveRecord::Base + acts_as_votable + acts_as_voter +end + +class StiVotable < ActiveRecord::Base + acts_as_votable +end + +class ChildOfStiVotable < StiVotable +end + +class StiNotVotable < ActiveRecord::Base + validates_presence_of :name +end + +class ChildOfStiNotVotable < StiNotVotable + acts_as_votable +end + +class NotVotable < ActiveRecord::Base +end + +class VotableCache < ActiveRecord::Base + acts_as_votable + validates_presence_of :name +end + +class VotableCacheUpdateAttributes < VotableCache + acts_as_votable cacheable_strategy: :update_attributes +end + +class VotableCacheUpdateColumns < VotableCache + acts_as_votable cacheable_strategy: :update_columns +end + +class ABoringClass + def self.hw + "hello world" + end +end