Class: FinderTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/six-updater-web/vendor/plugins/active_scaffold/test/misc/finder_test.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



9
10
11
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/test/misc/finder_test.rb', line 9

def setup
  @klass = ClassWithFinder.new
end

#test_build_order_clauseObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/test/misc/finder_test.rb', line 38

def test_build_order_clause
  columns = ActiveScaffold::DataStructures::Columns.new(ModelStub, :a, :b, :c, :d)
  sorting = ActiveScaffold::DataStructures::Sorting.new(columns)

  assert @klass.send(:build_order_clause, nil).nil?
  assert @klass.send(:build_order_clause, sorting).nil?

  sorting << [:a, 'desc']
  sorting << [:b, 'asc']

  assert_equal 'model_stubs.a DESC, model_stubs.b ASC', @klass.send(:build_order_clause, sorting)
end

#test_create_conditions_for_columnsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/test/misc/finder_test.rb', line 13

def test_create_conditions_for_columns
  columns = [
    ActiveScaffold::DataStructures::Column.new(:a, ModelStub),
    ActiveScaffold::DataStructures::Column.new(:b, ModelStub)
  ]
  tokens = [
    'foo',
    'bar'
  ]

  expected_conditions = [
	'(LOWER(model_stubs.a) LIKE ? OR LOWER(model_stubs.b) LIKE ?) AND (LOWER(model_stubs.a) LIKE ? OR LOWER(model_stubs.b) LIKE ?)',
  '%foo%', '%foo%', '%bar%', '%bar%'
]
  assert_equal expected_conditions, ActiveScaffold::Finder.create_conditions_for_columns(tokens, columns)

  expected_conditions = [
    '(LOWER(model_stubs.a) LIKE ? OR LOWER(model_stubs.b) LIKE ?)',
    '%foo%', '%foo%'
  ]
  assert_equal expected_conditions, ActiveScaffold::Finder.create_conditions_for_columns('foo', columns)

  assert_equal nil, ActiveScaffold::Finder.create_conditions_for_columns('foo', [])
end

#test_method_sortingObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/test/misc/finder_test.rb', line 51

def test_method_sorting
  column = ActiveScaffold::DataStructures::Column.new('a', ModelStub)
  column.sort_by :method => proc{self}

  collection = [16000, 2853, 98765, 6188, 4]
  assert_equal collection.sort, @klass.send(:sort_collection_by_column, collection, column, 'asc')
  assert_equal collection.sort.reverse, @klass.send(:sort_collection_by_column, collection, column, 'desc')

  collection = ['a', nil, 'b']
  result = nil
  assert_nothing_raised do
    result = @klass.send(:sort_collection_by_column, collection, column, 'asc')
  end
  assert_equal [nil, 'a', 'b'], result

  column.sort_by :method => 'self'
  collection = [3, 1, 2]
  assert_equal collection.sort, @klass.send(:sort_collection_by_column, collection, column, 'asc')
end