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

LrDocStoreTypeMigratorPerformanceTest.lua

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

Copyright 2020 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
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.

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

	-- LrDocStoreMigrator
local WFCatalogTestUtils = require 'WFCatalogTestUtils'

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

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

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

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

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

local LrDocStoreTypeMigratorTest = {}

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

function LrDocStoreTypeMigratorTest.rxTestNewDocType_luaArchiveStyleCatalog_testPerformance( rxTest )

	rxTest:setTimeLimit( 1600 )

	local docStorePath
	local docStoreFolderPath
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local catalogPath = WFCatalogTestUtils.getCachedCatalogPath("luaArchiveStyle")
	
	--Check if catalogs are cached, if they're not cached download them from Artifactory
	rxTest:waitFor( WFCatalogTestUtils.getCatalogsFromArtifactory( "luaArchiveStyle"))
	
	-- Copy catalog. This is so that we don't need to redownload it after it has been changed.
	local docStore
	docStoreFolderPath = KSPathUtils.getStandardFilePath( "home" ) 
	
	-- Check if the folder exists
	if not WFFileUtils.exists(docStoreFolderPath) then
		WFFileUtils.mkdir( docStoreFolderPath )
	end
	
	docStorePath = KSPathUtils.child( docStoreFolderPath, 'rxTestLrDocStoreMigratorTest-Managed Catalog.mcat' )
	docStorePath = WFFileUtils.ensureFileNameIsUnique( docStorePath )
	WFFileUtils.copy( catalogPath, docStorePath ) 
		
	-- cleanup after the test
	rxTest:finally( function()
		if docStorePath then
			WFFileUtils.delete( docStorePath )
			WFFileUtils.delete( docStorePath .. '-shm' )
			WFFileUtils.delete( docStorePath .. '-wal' )
		end
	end)

	rxTest:measurePerformance(function()
				docStore = WFRxDocStore( docStorePath )
				rxTest:waitFor( docStore
						:docValue( assetId )
						:take( 1 ) )
				end )

	-- Halt the docstore 
	rxTest:expectOnlyCompleted( docStore:halt() )
	docStore.close()
	docStore = nil

end

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

function LrDocStoreTypeMigratorTest.rxTestNewDocType_mixedStyleCatalog_testPerformance( rxTest )

	rxTest:setTimeLimit( 1600 )

	local docStorePath
	local docStoreFolderPath
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local catalogPath = WFCatalogTestUtils.getCachedCatalogPath("mixedStyle")
	
	--Check if catalogs are cached, if they're not cached download them from Artifactory
	rxTest:waitFor( WFCatalogTestUtils.getCatalogsFromArtifactory( "mixedStyle"))
	
	-- Copy catalog. This is so that we don't need to redownload it after it has been changed.
	local docStore
	docStoreFolderPath = KSPathUtils.getStandardFilePath( "home" ) 
	
	-- Check if the folder exists
	if not WFFileUtils.exists(docStoreFolderPath) then
		WFFileUtils.mkdir( docStoreFolderPath )
	end
	
	docStorePath = KSPathUtils.child( docStoreFolderPath, 'rxTestLrDocStoreMigratorTest-Managed Catalog.mcat' )
	docStorePath = WFFileUtils.ensureFileNameIsUnique( docStorePath )
	WFFileUtils.copy( catalogPath, docStorePath ) 
		
	-- cleanup after the test
	rxTest:finally( function()
		if docStorePath then
			WFFileUtils.delete( docStorePath )
			WFFileUtils.delete( docStorePath .. '-shm' )
			WFFileUtils.delete( docStorePath .. '-wal' )
		end
	end)

	rxTest:measurePerformance(function()
				docStore = WFRxDocStore( docStorePath )
				rxTest:waitFor( docStore
						:docValue( assetId )
						:take( 1 ) )
				end )

	-- Halt the docstore 
	rxTest:expectOnlyCompleted( docStore:halt() )
	docStore.close()
	docStore = nil
	
end

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

function LrDocStoreTypeMigratorTest.rxTestNewDocType_messagePackedStyleCatalog_testPerformance( rxTest )

	rxTest:setTimeLimit( 1600 )

	local docStorePath
	local docStoreFolderPath
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local catalogPath = WFCatalogTestUtils.getCachedCatalogPath("messagePackedStyle")
	
	--Check if catalogs are cached, if they're not cached download them from Artifactory
	rxTest:waitFor( WFCatalogTestUtils.getCatalogsFromArtifactory( "messagePackedStyle"))
	
	-- Copy catalog. This is so that we don't need to redownload it after it has been changed.
	local docStore
	docStoreFolderPath = KSPathUtils.getStandardFilePath( "home" ) 
	
	-- Check if the folder exists
	if not WFFileUtils.exists(docStoreFolderPath) then
		WFFileUtils.mkdir( docStoreFolderPath )
	end
	
	docStorePath = KSPathUtils.child( docStoreFolderPath, 'rxTestLrDocStoreMigratorTest-Managed Catalog.mcat' )
	docStorePath = WFFileUtils.ensureFileNameIsUnique( docStorePath )
	WFFileUtils.copy( catalogPath, docStorePath ) 
		
	-- cleanup after the test
	rxTest:finally( function()
		if docStorePath then
			WFFileUtils.delete( docStorePath )
			WFFileUtils.delete( docStorePath .. '-shm' )
			WFFileUtils.delete( docStorePath .. '-wal' )
		end
	end)
	
	rxTest:measurePerformance(function()
				docStore = WFRxDocStore( docStorePath )
				rxTest:waitFor( docStore
						:docValue( assetId )
						:take( 1 ) )
				end )

	-- Halt the docstore 
	rxTest:expectOnlyCompleted( docStore:halt() )
	docStore.close()
	docStore = nil

end
--------------------------------------------------------------------------------

return WFRxTestCase.wrapSuite( LrDocStoreTypeMigratorTest )
