--[[----------------------------------------------------------------------------

WFCatSearchQueryTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--

local WFRxDocStore = require 'WFRxDocStore'
local WFRxIndexStore = require 'WFRxIndexStore'
local WFCatSearchQuery = require 'WFCatSearchQuery'
local WFRxTestCase = require 'WFRxTestCase'
local WFOzTestUtils = require 'WFOzTestUtils'

local WFCatSearchQueryTest = {}

--------------------------------------------------------------------------------

local function updateCaptureDateAndHeightFn( )
	local nextHeight = 2

	return function( baseDocValue, date )
		-- set capture date to what we want
		baseDocValue.captureDate = date
		
		-- set the height for the query ordering
		baseDocValue.importSource.originalHeight = nextHeight
		
		nextHeight = nextHeight + 1
		
		return baseDocValue
	end
	
end
		
--------------------------------------------------------------------------------

function WFCatSearchQueryTest.rxTestBasic( rxTest )

	local docStore = WFRxDocStore( ':memory:' )
	local indexStore = WFRxIndexStore( docStore, 'WFCatIndexDefinitions.lua' )
	local addAssetFn = WFOzTestUtils.v2.makeAdderOfAsset( rxTest, docStore, updateCaptureDateAndHeightFn( ) )

	addAssetFn( '1973-03-08T01:02:03.04' )

	local term = WFCatSearchQuery.privateUnitTestExports.makeTerm(  {
		sql = [[SELECT docId FROM assets]]
		}
	)

	local query = WFCatSearchQuery.resultSetQuery( 
		indexStore, 
		[[SELECT docId FROM assets @JOIN ORDER BY height DESC]],
		term,
		'assets.docId' )

	query = rxTest:observe( query )

	query:expectData { { 1 } }

	indexStore:close()
	docStore:close()

end

--------------------------------------------------------------------------------

function WFCatSearchQueryTest.rxTestCompound( rxTest )

	rxTest:setTimeLimit( 2000 )

	local docStore = WFRxDocStore( ':memory:' )

	local indexStore = WFRxIndexStore( docStore, 'WFCatIndexDefinitions.lua' )
	local addAssetFn = WFOzTestUtils.v2.makeAdderOfAsset( rxTest, docStore, updateCaptureDateAndHeightFn( ) )

	addAssetFn( '1973-03-08T01:02:03.04' )
	addAssetFn( '1995-05-27T14:00:00.00' )
	addAssetFn( '2006-01-20T16:00:00.00' )

	local lowTerm = WFCatSearchQuery.privateUnitTestExports.makeTerm( {
		sql = [[SELECT docId FROM assets WHERE height < 2.5]]
		}
	)

	local highTerm = WFCatSearchQuery.privateUnitTestExports.makeTerm( {
		sql = [[SELECT docId FROM assets WHERE height > 3.5]]
		}
	)

	local compoundOrTerm = WFCatSearchQuery.privateUnitTestExports.makeCompoundOrTerm( lowTerm, highTerm )

	local query = WFCatSearchQuery.resultSetQuery( indexStore,
		[[SELECT docId FROM assets @JOIN ORDER BY height DESC]],
		compoundOrTerm,
		'assets.docId' )

	query = rxTest:observe( query )

	query:expectData { { 3 }, { 1 } }

	local compoundAndTerm = WFCatSearchQuery.privateUnitTestExports.makeCompoundAndTerm( lowTerm, highTerm )

	local query2 = WFCatSearchQuery.resultSetQuery( indexStore, 
		[[SELECT docId FROM assets @JOIN ORDER BY height DESC]],
		compoundAndTerm,
		'assets.docId' )

	query2 = rxTest:observe( query2 )

	query2:expectData { }

	addAssetFn( '2015-05-27T24:00:00.00' )

	query:expectData { { 4 }, { 3 }, { 1 } }

	indexStore:close()
	docStore:close()

end

--==============================================================================

return WFRxTestCase.wrapSuite( 'WFCatSearchQueryTest', WFCatSearchQueryTest )
