Skip to content

Commit

Permalink
spec: get acts_as_list tests running again
Browse files Browse the repository at this point in the history
test plan:
check jenkins output
confirm it runs now

Change-Id: I25c9e677ae754e1cfcb9562aef6585ff6e40b0b7
Reviewed-on: https://gerrit.instructure.com/107796
Tested-by: Jenkins
Reviewed-by: Landon Wilkins <[email protected]>
Product-Review: Landon Wilkins <[email protected]>
QA-Review: Landon Wilkins <[email protected]>
  • Loading branch information
jenseng committed Apr 6, 2017
1 parent cb53fc0 commit 2f90c12
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
6 changes: 2 additions & 4 deletions gems/acts_as_list/lib/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def list_scope_base
when Hash
scope
else
raise InvalidArgument.new("scope must be nil, a symbol, an array, or a hash")
raise ArgumentError.new("scope must be nil, a symbol, an array, or a hash")
end
# expand assocations to their foreign keys
new_scope = {}
Expand Down Expand Up @@ -126,9 +126,7 @@ def list_scope
RUBY

if position_column != 'position'
class_eval do
alias_method :position, self.class.position_column.to_sym
end
define_method(:position) { read_attribute(self.class.position_column.to_sym) }
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions gems/acts_as_list/lib/acts_as_list.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require 'active_record/acts/list'
ActiveRecord::Base.class_eval { include ActiveRecord::Acts::List }
require_relative "active_record/acts/list"
ActiveRecord::Base.class_eval { include ActiveRecord::Acts::List }
5 changes: 5 additions & 0 deletions gems/acts_as_list/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -e

bundle check || bundle install
ruby test/list_test.rb
35 changes: 12 additions & 23 deletions gems/acts_as_list/test/list_test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require 'test/unit'

require 'rubygems'
gem 'activerecord', '>= 1.15.4.7794'
require 'active_record'

require "#{File.dirname(__FILE__)}/../init"
require_relative "../lib/acts_as_list"

ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")

def setup_db
ActiveRecord::Schema.define(:version => 1) do
Expand All @@ -26,12 +25,13 @@ def teardown_db
end

class Mixin < ActiveRecord::Base
def self.nulls(first_or_last, column, direction = nil)
"#{column} IS#{" NOT" unless first_or_last == :last} NULL, #{column} #{direction.to_s.upcase}".strip
end
end

class ListMixin < Mixin
acts_as_list :column => "pos", :scope => :parent

def self.table_name() "mixins" end
acts_as_list :column => "pos", :scope => :parent_id
end

class ListMixinSub1 < ListMixin
Expand All @@ -40,13 +40,10 @@ class ListMixinSub1 < ListMixin
class ListMixinSub2 < ListMixin
end

class ListWithStringScopeMixin < ActiveRecord::Base
acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'

def self.table_name() "mixins" end
class UnscopedListMixin < Mixin
acts_as_list :column => "pos"
end


class ListTest < Test::Unit::TestCase

def setup
Expand Down Expand Up @@ -82,7 +79,7 @@ def test_move_to_bottom_with_next_to_last_item

def test_injection
item = ListMixin.new(:parent_id => 1)
assert_equal "parent_id = 1", item.scope_condition
assert_equal({parent_id: 1}, item.scope_condition)
assert_equal "pos", item.class.position_column
end

Expand Down Expand Up @@ -155,24 +152,16 @@ def test_delete_middle
assert_equal 3, ListMixin.find(4).pos

ListMixin.find(1).destroy

assert_equal [3, 4], ListMixin.where('parent_id = 5').order('pos').pluck(:id)

assert_equal 1, ListMixin.find(3).pos
assert_equal 2, ListMixin.find(4).pos
end

def test_with_string_based_scope
new = ListWithStringScopeMixin.create(:parent_id => 500)
assert_equal 1, new.pos
assert new.first?
assert new.last?
end

def test_nil_scope
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
new1, new2, new3 = UnscopedListMixin.create, UnscopedListMixin.create, UnscopedListMixin.create
new2.move_to_top
assert_equal [new2, new1, new3], ListMixin.where('parent_id IS NULL').order('pos').to_a
assert_equal [new2, new1, new3], UnscopedListMixin.where('parent_id IS NULL').order('pos').to_a
end


Expand Down Expand Up @@ -245,7 +234,7 @@ def test_move_to_bottom_with_next_to_last_item

def test_injection
item = ListMixin.new("parent_id"=>1)
assert_equal "parent_id = 1", item.scope_condition
assert_equal({parent_id: 1}, item.scope_condition)
assert_equal "pos", item.class.position_column
end

Expand Down

0 comments on commit 2f90c12

Please sign in to comment.