require 'helper'

class TestSimpleSearch < Test::Unit::TestCase

  should "work with arel where conditions" do
		posts_comments = Post.select('posts.*, comments.body as comment').joins(:comments)
		result = posts_comments.simplesearch('id_eq'=>1, 'subject_eq'=>'subject foo', 'body_eq'=>'body foo')
		assert_equal 1, result.first.id

		result = posts_comments.simplesearch('id_gt'=>1, 'id_lt'=>3)
		assert_equal 2, result.first.id

		result = posts_comments.simplesearch('id_ge'=>1, 'id_le'=>3)
		assert_equal 6, result.length

		result = posts_comments.simplesearch('id_ne'=>1)
		assert_equal 4, result.length

		result = posts_comments.simplesearch('id_in'=>'1,2')
		assert_equal 1, result.first.id
		assert_equal 2, result.last.id

		result = posts_comments.simplesearch('id_bt'=>'1..3')
		assert_equal 1, result.first.id
		assert_equal 3, result.last.id

		result = posts_comments.simplesearch('subject_sw'=>'subject')
		assert_equal 6, result.length

		result = posts_comments.simplesearch('subject_ew'=>'foo')
		assert_equal 2, result.length

		result = posts_comments.simplesearch('body_ct'=>'body')
		assert_equal 6, result.length
		
		result = posts_comments.simplesearch('body_nc'=>'foo')
		assert_equal 4, result.length

		result = posts_comments.simplesearch('comments.spam_flag_is'=>'NULL')
		assert_equal 4, result.length

		result = posts_comments.simplesearch('comments.spam_flag_isnot'=>'NULL')
		assert_equal 2, result.length
	end

  should "work with arel group conditions" do
		posts_comments = Post.select('posts.*, count(comments.id) as num_comment').joins(:comments)
		result = posts_comments.simplesearch('group_by'=>'posts.id')
		assert_equal 3, result.length
  end

  should "work with arel order conditions" do
		posts_comments = Post.select('posts.*, comments.id as comment_id, comments.body as comment').joins(:comments)
		result = posts_comments.simplesearch('order_by'=>'comment_id')
		assert_equal 11, result.first.comment_id
		result = posts_comments.simplesearch('order_by'=>'comment_id desc')
		assert_equal 32, result.first.comment_id
  end

  should "work with arel limit conditions" do
		true
  end

end