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

WFCatalogTest.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.

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

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

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

	-- WF.oz_client.managed_catalog
local WFCatalog = require 'WFCatalog'
local WFCatSpec = require 'WFCatSpec'
local WFCatUser = require 'WFCatUser'
local WFCatFileSpec = require 'WFCatFileSpec'

	-- WF.sqlite.doc_store
local WFRxDocStore = require 'WFRxDocStore'

	-- WF.sqlite.index_store
local WFRxIndexStore = require 'WFRxIndexStore'
local WFRxIndexStoreSchema = require 'WFRxIndexStoreSchema'

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

local WFCatalogTest = {}

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

local kUser1 = string.rep( '1', 32 )
local kCatalogId = string.rep( 'c', 32 )

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

-- Put a very simple index definition here.  It doesn't even have any indexes.

local indexStoreSchema = WFRxIndexStoreSchema()

indexStoreSchema:addTable {
	sql = [[CREATE TABLE example( thing, stuff )]],

	relevantTypeSubtypeTuples = {
		{ '*', '*' }
	},

	map = function( docValue, emit )

		if docValue and not docValue._deleted and docValue._docType == 'example' then

			local thing = docValue.thing
			local stuff = docValue.stuff

			if thing and stuff then

				emit( thing, stuff )

			end

		end

	end,

}

WFCatalogTest.indexStoreSchema = indexStoreSchema

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

function WFCatalogTest.rxTestBasic( rxTest )

	rxTest:setTimeLimit( 5 )

	local user1 = WFCatUser( 'app://server-base', kUser1, ':memory:' )
	rxTest:addCloseTarget( user1 )

	local tempRootDir = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatalogTest' )
	
	rxTest:finally( function()
		WFFileUtils.delete( tempRootDir )
	end )
	
	local spec = WFCatSpec( user1, kCatalogId )
	local catalog = WFCatalog( spec, tempRootDir, {
		docStorePath = ":memory:" .. tostring( KSIdGenerator.localCounter() ),
		deletionBacklogPath = ":memory:",
		indexSpecScript = "WFCatalogTest.lua",
	} )

	rxTest:addCloseTarget( catalog )

	rxTest:expectEqual( spec, catalog:ozCatalogSpec() )
	rxTest:expectEqual( kCatalogId, catalog:ozCatalogId() )
	rxTest:expectEqual( user1, catalog:ozUser() )
	rxTest:expectEqual( kUser1, catalog:ozUserId() )
	
	rxTest:expectEqual( true, WFRxDocStore:isClassOf( catalog:docStore() ) )
	rxTest:expectEqual( true, WFRxIndexStore:isClassOf( catalog:indexStore() ) )


	local indexStore = catalog:indexStore()
		-- By waiting for one simple query to return we ensure doc store and
		-- index store will shut down cleanly.

	local queryIndexStore = rxTest:observe( indexStore:query {
		sql = [[SELECT stuff, thing FROM example;]]
	} )

	queryIndexStore:expectData {}
	
	rxTest:expectEqual( 'string', type( tostring( catalog ) ) )

	rxTest:expectEqual( true,  WFCatFileSpec:isClassOf( catalog:fileSpec() ) )

end

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

return WFRxTestCase.wrapSuite( 'WFCatalogTest', WFCatalogTest )
