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

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

NOTICE: All information contained herein is, and remain 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 may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.oz_client.catalog
local OzCatalogUrls = require 'OzCatalogUrls'

	-- WF.core.os
local KSPathUtils = require 'KSPathUtils'

	-- WF.oz_client.managed_catalog
local WFCatMockCatalog = require 'WFCatMockCatalog'

	-- WF.core.utils
local KSIdGenerator = require 'KSIdGenerator'

	-- Lrmobile_models
local LrmSearchModelImp = require 'LrmSearchModelImp'

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

local LrmSearchModelTest = {}

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

local function mockCatalogSetUp( rxTest )

	local catalogOptions = {
		indexDefinitions = 'LrmCatalogIndexDefinitions.lua'
	}

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, nil, catalogOptions )

	return wfCatalog

end

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

function LrmSearchModelTest.rxTestUpdateSearchOne( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = mockCatalogSetUp( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local assetIds = { assetId }

	local searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )

	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 0, assetIds )

		, ':ignore:' )


	rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId FROM coreInfo
				UNION
				SELECT docId FROM TEMP_search]]
	})
	:expectData { { assetId } }

end

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

function LrmSearchModelTest.rxTestUpdateSearchBulk( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = mockCatalogSetUp( rxTest )

	local assetId1 = KSIdGenerator.uuidInLowercaseHex()
	local assetId2 = KSIdGenerator.uuidInLowercaseHex()
	local assetId3 = KSIdGenerator.uuidInLowercaseHex()

	local assetIds = {
		assetId1,
		assetId2,
		assetId3,
	}

	local searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )

	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 0, assetIds )

		, ':ignore:' )

	rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId, height FROM coreInfo
				UNION
				SELECT docId, sortOrder FROM TEMP_search
				ORDER BY sortOrder]]
	})
	:expectData {
			{ assetId1, 1 },
			{ assetId2, 2 },
			{ assetId3, 3 },
	}

end

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

function LrmSearchModelTest.rxTestClearSearchTable( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = mockCatalogSetUp( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local assetIds = { assetId }

	local searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )

	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 0, assetIds )

		, ':ignore:' )


	rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId FROM coreInfo
				UNION
				SELECT docId FROM TEMP_search]]
	})
	:expectData { { assetId } }


	rxTest:expectSingleValue(

		searchModel.events[ 'clearSearchTable' ]()

		, ':ignore:' )

	rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId FROM coreInfo
				UNION
				SELECT docId FROM TEMP_search]]
	})
	:expectData { }

end

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

function LrmSearchModelTest.rxTestUpdateSearchQuery( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = mockCatalogSetUp( rxTest )

	local assetId1 = KSIdGenerator.uuidInLowercaseHex()
	local assetId2 = KSIdGenerator.uuidInLowercaseHex()
	local assetId3 = KSIdGenerator.uuidInLowercaseHex()
	local assetId4 = KSIdGenerator.uuidInLowercaseHex()
	local assetId5 = KSIdGenerator.uuidInLowercaseHex()

	local searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )

	local result = rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId, height FROM coreInfo
				UNION
				SELECT docId, sortOrder FROM TEMP_search
				ORDER BY sortOrder]]
	})


	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 0, { assetId1 } )

		, ':ignore:' )

	result:expectData { { assetId1, 1 } }


	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 1, { assetId2 } )

		, ':ignore:' )

	result:expectData {
		{ assetId1, 1 },
		{ assetId2, 2 },
	}


	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 2, { assetId3 } )

		, ':ignore:' )

	result:expectData {
		{ assetId1, 1 },
		{ assetId2, 2 },
		{ assetId3, 3 },
	}


	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 3, { assetId4 } )

		, ':ignore:' )

	result:expectData {
		{ assetId1, 1 },
		{ assetId2, 2 },
		{ assetId3, 3 },
		{ assetId4, 4 },
	}


	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 4, { assetId5 } )

		, ':ignore:' )

	result:expectData {
		{ assetId1, 1 },
		{ assetId2, 2 },
		{ assetId3, 3 },
		{ assetId4, 4 },
		{ assetId5, 5 },
	}
end

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

function LrmSearchModelTest.rxTestUpdateSearchFilterCountQuery( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = mockCatalogSetUp( rxTest )

	local searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )

	local result = rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT filterType, filterCount FROM TEMP_searchFilterCount]]
	})


	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchFilterCountTable' ]( nil, "pick", 5 )

		, ':ignore:' )

	result:expectData { { "pick", 5 } }


	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchFilterCountTable' ]( nil, "reject", 0 )

		, ':ignore:' )

	result:expectData {
		{ "pick", 5 },
		{ "reject", 0 },
	}

	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchFilterCountTable' ]( nil, "unflagged", 10 )

		, ':ignore:' )

	result:expectData {
		{ "pick", 5 },
		{ "reject", 0 },
		{ "unflagged", 10 },
	}

end

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

return WFRxTestCase.wrapSuite( 'LrmSearchModelImpTest', LrmSearchModelTest )
