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

NmbFileAccessDatabaseTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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'
local WFCatMockCatalog = require 'WFCatMockCatalog'

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

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

local NmbFileAccessDatabaseTest = {}

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

local basePath = KSPathUtils.getStandardFilePath( 'carouselDocuments' )
local volumeGuid = WFFileUtils.getVolumeGuidForPath( basePath )

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

local function mapAwayDate( tbl )

	local mappedTbl = {}

	for k, v in pairs( tbl ) do 
		
		if type( v ) == 'table' then
			mappedTbl[ k ] = mapAwayDate( v )
		elseif k ~= 'lastAccessed' then
			mappedTbl[ k ] = v
		else
			mappedTbl[ k ] = '<value>'
		end
		
	end
	
	return mappedTbl

end

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

local function extractLocalId( rows )

	local mappedTbl = {}

	for k, row in pairs( rows ) do 
		
		mappedTbl[ k ] = row[ 1 ]

	end
	
	return mappedTbl

end

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

local function setUp( rxTest )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )

	rxTest:finally( function()
	
		wfCatalog:close()
	
	end )

	return wfCatalog:fileAccessDb(), wfCatalog

end

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

function NmbFileAccessDatabaseTest.rxTestSimpleLocalId( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = 1,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

function NmbFileAccessDatabaseTest.rxTestSimpleTouchMultipleIfNew( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1, 100 ) )
	
	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = 100,
			localId = 1,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump, expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touchMultipleIfNew( { 1, 2 }, 200 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = 100,
			localId = 1,
		},
		{
			lastAccessed = 200,
			localId = 2,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump, expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

local function createBasicDoc( rxTest, wfCatalog )
	
	local docId = KSIdGenerator.uuidInLowercaseHex()
	local localId
	
	local docValue = {

		_id = docId,
		type = 'asset',
		subtype = 'image',
		captureDate = '2012-08-15T15:22:29',
		importSource = {
			importTimestamp = "2015-05-26T17:34:23-07:00",
			fileName = "Image.jpg",
		},
	}
	
	rxTest:expectSingleValue( wfCatalog:docStore():setDocValue( docValue ), ':ignore:' )

	rxTest:expectSingleValue(
		wfCatalog:docStore():docValue( docId )
			:map( function( docValue )
				localId = docValue._localId
				return true
			end ), true )

	--rxTest:expectSingleValue( wfCatalog:fileAccessDb():waitUntilUpToDate(), true )

	return docId, localId

end

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

function NmbFileAccessDatabaseTest.rxTestSimpleDocId( rxTest )
	
	rxTest:setRetryLimit( 3 )
	
	rxTest:setTimeLimit( 10 )

	local _fileAccessDb, wfCatalog = setUp( rxTest )

	local docId, localId = createBasicDoc( rxTest, wfCatalog )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( docId ) )
	
	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

function NmbFileAccessDatabaseTest.rxTestAddOrUpdate( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	
	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = 1,
		},
		{
			lastAccessed = '<value>',
			localId = 2,
		},
		{
			lastAccessed = '<value>',
			localId = 3,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )

	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
		
	local shouldBeEmpty = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( shouldBeEmpty:map( mapAwayDate ), {} )

end

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

local function updateLocalProxyStatus( rxTest, _fileAccessDb, localId, volumeGuid )

	local assetInfo = {
		_localId = localId,
		volumeGuidUsedByProxy = volumeGuid or 'null',
	}
		
	rxTest:expectOnlyCompleted( _fileAccessDb:updateAssetInfo( assetInfo ) )

end

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

local function updateLocalOriginalStatus( rxTest, _fileAccessDb, localId, volumeGuid )

	local assetInfo = {
		_localId = localId,
		volumeGuidUsedByOriginal = volumeGuid or 'null',
	}
		
	rxTest:expectOnlyCompleted( _fileAccessDb:updateAssetInfo( assetInfo ) )

end

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

local function updateImportance( rxTest, _fileAccessDb, localId, isImportant )

	local assetInfo = {
		_localId = localId,
		isImportant = isImportant,
	}
		
	rxTest:expectOnlyCompleted( _fileAccessDb:updateAssetInfo( assetInfo ) )

end

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

local function updateOzProxyStatus( rxTest, _fileAccessDb, localId, ozHasProxy )

	local assetInfo = {
		_localId = localId,
		ozHasProxy = ozHasProxy,
	}
		
	rxTest:expectOnlyCompleted( _fileAccessDb:updateAssetInfo( assetInfo ) )

end

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

local function updateOzOriginalStatus( rxTest, _fileAccessDb, localId, ozHasOriginal )

	local assetInfo = {
		_localId = localId,
		ozHasOriginal = ozHasOriginal,
	}
		
	rxTest:expectOnlyCompleted( _fileAccessDb:updateAssetInfo( assetInfo ) )

end

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

local function updateProxySize( rxTest, _fileAccessDb, localId, size )

	local assetInfo = {
		_localId = localId,
		proxyFileSize = size or 'null',
	}
		
	rxTest:expectOnlyCompleted( _fileAccessDb:updateAssetInfo( assetInfo ) )

end

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

local function updateOriginalSize( rxTest, _fileAccessDb, localId, size )

	local assetInfo = {
		_localId = localId,
		originalFileSize = size or 'null',
	}
		
	rxTest:expectOnlyCompleted( _fileAccessDb:updateAssetInfo( assetInfo ) )

end

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

function NmbFileAccessDatabaseTest.rxTestUpdateLocalStatus( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	updateLocalProxyStatus( rxTest, _fileAccessDb, 1, volumeGuid )
	
	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = 1,
			volumeGuidUsedByProxy = volumeGuid,
		}
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Touching shouldn't affect proxy status.
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )

	-- Setting status to false should work.

	updateLocalProxyStatus( rxTest, _fileAccessDb, 1, nil )
	updateLocalOriginalStatus( rxTest, _fileAccessDb, 1, nil )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = 1,
		}
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )

	-- Testing Original More
	
	updateLocalOriginalStatus( rxTest, _fileAccessDb, 1, volumeGuid )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = 1,
			volumeGuidUsedByOriginal = volumeGuid,
		}
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Delete should still function

	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )

	local shouldBeEmpty = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( shouldBeEmpty:map( mapAwayDate ), {} )

end

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

function NmbFileAccessDatabaseTest.rxTestDelete( rxTest )

	local _fileAccessDb = setUp( rxTest )

	-- delete a valid rendition

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = 2,
		},
		{
			lastAccessed = '<value>',
			localId = 3,
		},
	}
	
	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )

	-- delete a valid asset

	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )

	-- delete fake entry

	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 42 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )

end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessed( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:setTimeLimit( 5 )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )

	local expectedValues = { 1, 2, 3 }
	
	local purgeCandidates = _fileAccessDb:getLeastRecentlyAccessed()
	
	rxTest:expectSingleValue( purgeCandidates:map( extractLocalId ), expectedValues )
	
	rxTest:sleep( 0.1 )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	expectedValues = { 2, 3, 1 }
	
	rxTest:sleep( 0.1 )
	
	purgeCandidates = _fileAccessDb:getLeastRecentlyAccessed()
	
	rxTest:expectSingleValue( purgeCandidates:map( extractLocalId ), expectedValues )
	
	expectedValues = { 2 }
	
	purgeCandidates = _fileAccessDb:getLeastRecentlyAccessed( 1 )
	
	rxTest:expectSingleValue( purgeCandidates:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	
end

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

-- FIXME [jandrew 01/28/2016]: These tests frequently fails on Android - Android
-- tests that contain calls to sleep are extremely sensitive.

if not WFPlatformInfo.isAndroid then

local secondsToDays = function( seconds )
	return seconds / 60 / 60 / 24
end

local timeWindow = {
	afterNDaysAgo = 10,
	beforeNDaysAgo = secondsToDays( 1 )
}

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

local function addProxies ( rxTest, _fileAccessDb, assets )

	for _, i in pairs( assets ) do
	
		local assetInfo = {
			_localId = i,
			volumeGuidUsedByProxy = volumeGuid,
			proxyFileSize = 50,
			ozHasProxy = true,
			isImportant = true,
			sufficientRenditionFoundOnOz = true,
			isMarkedForOfflineUse = false,
		}
		
		_fileAccessDb:updateAssetInfo( assetInfo )

	end

end

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

local function addOriginals( rxTest, _fileAccessDb, assets )

	for _, i in pairs( assets ) do
	
		local assetInfo = {
			_localId = i,
			volumeGuidUsedByOriginal = volumeGuid,
			originalFileSize = 100,
			ozHasOriginal = true,
			isImportant = true,
			sufficientRenditionFoundOnOz = true,
			isMarkedForOfflineUse = false,
		}
		
		_fileAccessDb:updateAssetInfo( assetInfo )
		
	end

end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:setTimeLimit( 10 )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	rxTest:sleep( 2 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 4 ) )

	addProxies( rxTest, _fileAccessDb, { 1, 2, 3, 4 } )
	addOriginals( rxTest, _fileAccessDb, { 1, 2, 3, 4 } )
	
	local expectedValues = { 1, 2, 3 }
	
	local purgeCandidatesStream = _fileAccessDb:getLeastRecentlyAccessedInWindow( volumeGuid, timeWindow )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	expectedValues = { 2 }
	
	local limit = 1 
	local skip = nil
	
	local getNextBatch
	
	rxTest:expectSingleValue( _fileAccessDb:getLeastRecentlyAccessedInWindow( volumeGuid, timeWindow, limit, skip )
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = { 3 }
	
	rxTest:expectSingleValue( getNextBatch()
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = {}
	
	rxTest:expectSingleValue( getNextBatch()
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	
end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedProxiesInWindow( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:setTimeLimit( 10 )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	rxTest:sleep( 2 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 4 ) )

	addProxies( rxTest, _fileAccessDb, { 1, 3, 4 } )

	local expectedValues = { 1, 3 }
	
	local purgeCandidatesStream = _fileAccessDb:getLeastRecentlyAccessedProxiesInWindow( volumeGuid, timeWindow )
	
	rxTest:expectSingleValue( purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	expectedValues = { 3 }
	
	local limit = 1 
	local skip = nil
	
	local getNextBatch
	
	rxTest:expectSingleValue( _fileAccessDb:getLeastRecentlyAccessedProxiesInWindow( volumeGuid, timeWindow, limit, skip )
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = {}
	
	rxTest:expectSingleValue( getNextBatch()
		:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	
end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedOriginalsInWindow( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:setTimeLimit( 10 )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	rxTest:sleep( 2 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 4 ) )

	addOriginals( rxTest, _fileAccessDb, { 1, 3, 4 } )

	local expectedValues = { 1, 3 }
	
	local purgeCandidatesStream = _fileAccessDb:getLeastRecentlyAccessedOriginalsInWindow( volumeGuid, timeWindow )
	
	rxTest:expectSingleValue( purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	expectedValues = { 3 }
	
	local limit = 1 
	local skip = nil
	
	local getNextBatch
	
	rxTest:expectSingleValue( _fileAccessDb:getLeastRecentlyAccessedOriginalsInWindow( volumeGuid, timeWindow, limit, skip )
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = {}
	
	rxTest:expectSingleValue( getNextBatch()
		:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	
end

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

local function addAssets( rxTest, fileAccessDb, assetArray )

	for _, i in pairs( assetArray ) do
	
		rxTest:expectOnlyCompleted( fileAccessDb:touch( i ) )
	
		rxTest:sleep( 0.01 )
	
	end

	addProxies( rxTest, fileAccessDb, assetArray )
	addOriginals( rxTest, fileAccessDb, assetArray )

	rxTest:sleep( 0.01 )

end

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

local function testWindowSortOrder( rxTest, binaryType )

	local _fileAccessDb = setUp( rxTest )

	local testOriginal = binaryType == 'original' or binaryType =='both'
	local testProxy = binaryType == 'proxy' or binaryType =='both'

	local windowFunc

	if testOriginal and testProxy then
		windowFunc = _fileAccessDb.getLeastRecentlyAccessedInWindow
	elseif testOriginal then
		windowFunc = _fileAccessDb.getLeastRecentlyAccessedOriginalsInWindow
	else
		windowFunc = _fileAccessDb.getLeastRecentlyAccessedProxiesInWindow
	end

	local window = {
		afterNDaysAgo = 10,
		beforeNDaysAgo = 0
	}

	rxTest:setTimeLimit( 10 )

	addAssets( rxTest, _fileAccessDb, { 1, 2, 3, 4, 5, 6 } )

	local expectedValues = { 1, 2, 3, 4, 5, 6 }
		-- Nothing to Sort
		
	local purgeCandidatesStream = windowFunc( _fileAccessDb, volumeGuid, window )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )

	updateImportance( rxTest, _fileAccessDb, 1, false )
	updateImportance( rxTest, _fileAccessDb, 3, false )
	updateImportance( rxTest, _fileAccessDb, 5, false )
	
	expectedValues = { 1, 3, 5, 2, 4, 6 }
		-- First, Unimportant Assets should come first
		
	purgeCandidatesStream = windowFunc( _fileAccessDb, volumeGuid, window )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	if testOriginal then
		updateOriginalSize( rxTest, _fileAccessDb, 5, 1000 )
		updateOriginalSize( rxTest, _fileAccessDb, 4, 1000 )
	end
	
	if testProxy then
		updateProxySize( rxTest, _fileAccessDb, 5, 1000 )
		updateProxySize( rxTest, _fileAccessDb, 4, 1000 )
	end
	
	expectedValues = { 5, 1, 3, 4, 2, 6 }
		-- Next, Large assets should come before smaller assets.
		
	purgeCandidatesStream = windowFunc( _fileAccessDb, volumeGuid, window )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	_fileAccessDb:touch( 1 )
	_fileAccessDb:touch( 2 )
	
	rxTest:sleep( 0.1 )
	
	expectedValues = { 5, 3, 1, 4, 6, 2 }
		-- Finally, older assets should come before newer assets.
		
	purgeCandidatesStream = windowFunc( _fileAccessDb, volumeGuid, window )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 5 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 6 ) )

end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow_SortOrder_Both( rxTest )
	
	testWindowSortOrder( rxTest, 'both' )
	
end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow_SortOrder_Originals( rxTest )
	
	testWindowSortOrder( rxTest, 'original' )
	
end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow_SortOrder_Proxies( rxTest )
	
	testWindowSortOrder( rxTest, 'proxy' )
	
end

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

local function testLeastRecentlyAccessedInWindow_IgnoreAssetsOnOtherVolumes( rxTest, binaryType )

	local _fileAccessDb = setUp( rxTest )

	local testOriginal = binaryType == 'original' or binaryType =='both'
	local testProxy = binaryType == 'proxy' or binaryType =='both'

	local windowFunc

	if testOriginal and testProxy then
		windowFunc = _fileAccessDb.getLeastRecentlyAccessedInWindow
	elseif testOriginal then
		windowFunc = _fileAccessDb.getLeastRecentlyAccessedOriginalsInWindow
	else
		windowFunc = _fileAccessDb.getLeastRecentlyAccessedProxiesInWindow
	end

	local window = {
		afterNDaysAgo = 10,
		beforeNDaysAgo = 0
	}

	rxTest:setTimeLimit( 10 )

	addAssets( rxTest, _fileAccessDb, { 1, 2, 3, 4, 5, 6 } )

	local expectedValues = { 1, 2, 3, 4, 5, 6 }
		-- Nothing to Sort
		
	local purgeCandidatesStream = windowFunc( _fileAccessDb, volumeGuid, window )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )

	if testOriginal then
		updateLocalOriginalStatus( rxTest, _fileAccessDb, 5, nil ) -- No Local Original
		updateLocalOriginalStatus( rxTest, _fileAccessDb, 4, 'foo' ) -- Original on different volume
	end
	
	if testProxy then
		updateLocalProxyStatus( rxTest, _fileAccessDb, 5, nil ) -- No Local Proxy
		updateLocalProxyStatus( rxTest, _fileAccessDb, 4, 'foo' ) -- Proxy on different volume
	end
	
	expectedValues = { 1, 2, 3, 6 }
		
	purgeCandidatesStream = windowFunc( _fileAccessDb, volumeGuid, window )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 5 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 6 ) )

end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow_IgnoreAssetsOnOtherVolumes_Both( rxTest )
	
	testLeastRecentlyAccessedInWindow_IgnoreAssetsOnOtherVolumes( rxTest, 'both' )
	
end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow_IgnoreAssetsOnOtherVolumes_Originals( rxTest )

	testLeastRecentlyAccessedInWindow_IgnoreAssetsOnOtherVolumes( rxTest, 'original' )
	
end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow_IgnoreAssetsOnOtherVolumes_Proxies( rxTest )

	testLeastRecentlyAccessedInWindow_IgnoreAssetsOnOtherVolumes( rxTest, 'proxy' )
	
end

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

local function rxTestLeastRecentlyAccessedInWindow_IgnoreUnsyncedAssets( rxTest, binaryType )

	local _fileAccessDb = setUp( rxTest )

	local testOriginal = binaryType == 'original' or binaryType =='both'
	local testProxy = binaryType == 'proxy' or binaryType =='both'

	local windowFunc

	if testOriginal and testProxy then
		windowFunc = _fileAccessDb.getLeastRecentlyAccessedInWindow
	elseif testOriginal then
		windowFunc = _fileAccessDb.getLeastRecentlyAccessedOriginalsInWindow
	else
		windowFunc = _fileAccessDb.getLeastRecentlyAccessedProxiesInWindow
	end

	local window = {
		afterNDaysAgo = 10,
		beforeNDaysAgo = 0
	}

	rxTest:setTimeLimit( 10 )

	addAssets( rxTest, _fileAccessDb, { 1, 2, 3, 4, 5, 6 } )

	local expectedValues = { 1, 2, 3, 4, 5, 6 }
		
	local purgeCandidatesStream = windowFunc( _fileAccessDb, volumeGuid, window )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )

	if testOriginal then
		updateOzOriginalStatus( rxTest, _fileAccessDb, 5, false )
		updateOzOriginalStatus( rxTest, _fileAccessDb, 4, false )
	end
	
	if testProxy then
		updateOzProxyStatus( rxTest, _fileAccessDb, 5, false )
		updateOzProxyStatus( rxTest, _fileAccessDb, 4, false )
	end
	
	expectedValues = { 1, 2, 3, 6 }
		
	purgeCandidatesStream = windowFunc( _fileAccessDb, volumeGuid, window )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 5 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 6 ) )

end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow_IgnoreUnsyncedAssets_Both( rxTest )
	
	rxTestLeastRecentlyAccessedInWindow_IgnoreUnsyncedAssets( rxTest, 'both' )
	
end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow_IgnoreUnsyncedAssets_Originals( rxTest )
	
	rxTestLeastRecentlyAccessedInWindow_IgnoreUnsyncedAssets( rxTest, 'both' )
	
end

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

function NmbFileAccessDatabaseTest.rxTestLeastRecentlyAccessedInWindow_IgnoreUnsyncedAssets_Proxies( rxTest )
	
	rxTestLeastRecentlyAccessedInWindow_IgnoreUnsyncedAssets( rxTest, 'proxy' )
	
end


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

function NmbFileAccessDatabaseTest.rxTestSkipNMostRecent( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:setTimeLimit( 10 )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 4 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 5 ) )
	rxTest:sleep( 2 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 6 ) )

	addOriginals( rxTest, _fileAccessDb, { 1, 2, 3, 4, 5, 6 } )

	local expectedValues = { 1, 2, 3, 4 }
	
	local purgeCandidatesStream = _fileAccessDb:getLeastRecentlyAccessedInWindow( volumeGuid, timeWindow, nil, 2 )
	
	rxTest:expectSingleValue( purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	rxTest:sleep( 0.5 )
	
	expectedValues = { 2 }
	
	local limit = 1 
	local skip = 4
	
	local getNextBatch
	
	rxTest:expectSingleValue( _fileAccessDb:getLeastRecentlyAccessedInWindow( volumeGuid, timeWindow, limit, skip )
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = { 3 }
	
	rxTest:expectSingleValue( getNextBatch()
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = {}
	
	rxTest:expectSingleValue( getNextBatch()
		:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 5 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 6 ) )
	
end


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

function NmbFileAccessDatabaseTest.rxTestWaitUntilUpToDate( rxTest )

	rxTest:setTimeLimit( 5 )

	local _fileAccessDb, wfCatalog = setUp( rxTest )

	createBasicDoc( rxTest, wfCatalog )

	local fired = false

	local observer = rxTest:observe( _fileAccessDb:waitUntilUpToDate()
		:tap( function()
			fired = true
		end ))
		
	rxTest:sleep( 0.2 )
	
	rxTest:expectEqual( false, fired )
		
	rxTest:expectSingleValue( wfCatalog._docStore:changeCounter()
		:take( 1 )
		:tap( function( changeCounter )
			return _fileAccessDb:updateChangeCounter( changeCounter )
		end ), ':ignore:' )
		
	observer:expectDataWithinTime( 1, true )
	
	rxTest:expectEqual( true, fired )
	
end

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

function NmbFileAccessDatabaseTest.rxTestGetInactiveAssetsInWindow( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:setTimeLimit( 10 )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	rxTest:sleep( 2 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 4 ) )

	addProxies( rxTest, _fileAccessDb, { 1, 2, 3, 4 } )
	addOriginals( rxTest, _fileAccessDb, { 1, 2, 3, 4 } )
	
	local expectedValues = {} -- Everything is important
	
	local purgeCandidatesStream = _fileAccessDb:getInactiveAssetsInWindow( volumeGuid, timeWindow )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	updateImportance( rxTest, _fileAccessDb, 1, false )
	updateImportance( rxTest, _fileAccessDb, 2, false )
	updateImportance( rxTest, _fileAccessDb, 3, false )
	
	expectedValues = { 1, 2, 3 }
	
	purgeCandidatesStream = _fileAccessDb:getInactiveAssetsInWindow( volumeGuid, timeWindow )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	expectedValues = { 2 }
	
	local limit = 1 
	local skip = nil
	
	local getNextBatch
	
	rxTest:expectSingleValue( _fileAccessDb:getInactiveAssetsInWindow( volumeGuid, timeWindow, limit, skip )
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = { 3 }
	
	rxTest:expectSingleValue( getNextBatch()
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = {}
	
	rxTest:expectSingleValue( getNextBatch()
		:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	
end

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

function NmbFileAccessDatabaseTest.rxTestGetInactiveProxiesInWindow( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:setTimeLimit( 10 )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	rxTest:sleep( 2 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 4 ) )

	addProxies( rxTest, _fileAccessDb, { 1, 3, 4 } )

	local expectedValues = {} -- Everything is important
	
	local purgeCandidatesStream = _fileAccessDb:getInactiveProxiesInWindow( volumeGuid, timeWindow )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	updateImportance( rxTest, _fileAccessDb, 1, false )
	updateImportance( rxTest, _fileAccessDb, 2, false )
	updateImportance( rxTest, _fileAccessDb, 3, false )
	
	expectedValues = { 1, 3 }
	
	purgeCandidatesStream = _fileAccessDb:getInactiveProxiesInWindow( volumeGuid, timeWindow )
	
	rxTest:expectSingleValue( purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	expectedValues = { 3 }
	
	local limit = 1 
	local skip = nil
	
	local getNextBatch
	
	rxTest:expectSingleValue( _fileAccessDb:getInactiveProxiesInWindow( volumeGuid, timeWindow, limit, skip )
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = {}
	
	rxTest:expectSingleValue( getNextBatch()
		:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	
end

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

function NmbFileAccessDatabaseTest.rxTestGetInactiveOriginalsInWindow( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:setTimeLimit( 10 )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:sleep( 0.01 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )
	rxTest:sleep( 2 )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 4 ) )

	addOriginals( rxTest, _fileAccessDb, { 1, 3, 4 } )

	local expectedValues = {} -- Everything is important
	
	local purgeCandidatesStream = _fileAccessDb:getInactiveOriginalsInWindow( volumeGuid, timeWindow )
	
	rxTest:expectSingleValue(  purgeCandidatesStream:map( extractLocalId ), expectedValues )
	
	updateImportance( rxTest, _fileAccessDb, 1, false )
	updateImportance( rxTest, _fileAccessDb, 2, false )
	updateImportance( rxTest, _fileAccessDb, 3, false )
	
	expectedValues = { 1, 3 }
	
	purgeCandidatesStream = _fileAccessDb:getInactiveOriginalsInWindow( volumeGuid, timeWindow )
	
	rxTest:expectSingleValue( purgeCandidatesStream:map( extractLocalId ), expectedValues )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	
	expectedValues = { 3 }
	
	local limit = 1 
	local skip = nil
	
	local getNextBatch
	
	rxTest:expectSingleValue( _fileAccessDb:getInactiveOriginalsInWindow( volumeGuid, timeWindow, limit, skip )
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = {}
	
	rxTest:expectSingleValue( getNextBatch()
		:map( extractLocalId ), expectedValues )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 4 ) )
	
end

end

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

function NmbFileAccessDatabaseTest.rxTestQueryBatching( rxTest )

	local _fileAccessDb = setUp( rxTest )
	
	rxTest:setTimeLimit( 5 )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:touch( 3 ) )

	local expectedValues = { 1 }
	
	local limit = 1
	local skip = nil

	local getNextBatch
	
	rxTest:expectSingleValue( _fileAccessDb:getLeastRecentlyAccessed( limit, skip )
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = { 2 }
	
	rxTest:expectSingleValue( getNextBatch()
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = { 3 }
	
	rxTest:expectSingleValue( getNextBatch()
	
		:tap( function( _, nextBatchFn )
		
			getNextBatch = nextBatchFn
		
		end )
		:map( extractLocalId ), expectedValues )
	
	expectedValues = {}
	
	rxTest:expectSingleValue( getNextBatch()
		:map( extractLocalId ), expectedValues )

	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 1 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 2 ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( 3 ) )
	
end

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

function NmbFileAccessDatabaseTest.rxTestLastAccessChangeCounter( rxTest )

	local _fileAccessDb = setUp( rxTest )

	rxTest:expectSingleValue( _fileAccessDb:getContinueFromChangeCounter(), 0 )
	
	_fileAccessDb:updateChangeCounter( 5 )
	
	rxTest:expectSingleValue( _fileAccessDb:getContinueFromChangeCounter(), 5 )
	
	_fileAccessDb:updateChangeCounter( 10 )
	_fileAccessDb:updateChangeCounter( 7 )
	_fileAccessDb:updateChangeCounter( 2 )
	_fileAccessDb:updateChangeCounter( 13 )
	_fileAccessDb:updateChangeCounter( 1 )
	
	rxTest:expectSingleValue( _fileAccessDb:getContinueFromChangeCounter(), 13 )

	_fileAccessDb:updateChangeCounter( 1 )

	rxTest:expectSingleValue( _fileAccessDb:getContinueFromChangeCounter(), 13 )

end

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

function NmbFileAccessDatabaseTest.rxTestUpdateImportance( rxTest )

	local _fileAccessDb, wfCatalog = setUp( rxTest )

	local _, localId = createBasicDoc( rxTest, wfCatalog )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( localId ) )

 	-- Setting importance to true
	
	updateImportance( rxTest, _fileAccessDb, localId, true )

	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			isImportant = 1,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Setting importance to false
	
	updateImportance( rxTest, _fileAccessDb, localId, false )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			isImportant = 0,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Setting importance of untracked asset
	
	updateImportance( rxTest, _fileAccessDb, localId + 1, true )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			isImportant = 0,
		},
		{
			lastAccessed = '<value>',
			localId = localId + 1,
			isImportant = 1,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId + 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

function NmbFileAccessDatabaseTest.rxTestUpdateOzOriginalStatus( rxTest )

	local _fileAccessDb, wfCatalog = setUp( rxTest )

	local _, localId = createBasicDoc( rxTest, wfCatalog )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( localId ) )

 	-- Setting oz original status to true
	
	updateOzOriginalStatus( rxTest, _fileAccessDb, localId, true )

	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			ozHasOriginal = 1,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Setting oz original status to false
	
	updateOzOriginalStatus( rxTest, _fileAccessDb, localId, false )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			ozHasOriginal = 0,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Setting oz original status of untracked asset
	
	updateOzOriginalStatus( rxTest, _fileAccessDb, localId + 1, true )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			ozHasOriginal = 0,
		},
		{
			lastAccessed = '<value>',
			localId = localId + 1,
			ozHasOriginal = 1,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId + 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

function NmbFileAccessDatabaseTest.rxTestUpdateOzProxyStatus( rxTest )

	local _fileAccessDb, wfCatalog = setUp( rxTest )

	local _, localId = createBasicDoc( rxTest, wfCatalog )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( localId ) )

 	-- Setting oz proxy status to true
	
	updateOzProxyStatus( rxTest, _fileAccessDb, localId, true )

	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			ozHasProxy = 1,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Setting oz proxy status to false
	
	updateOzProxyStatus( rxTest, _fileAccessDb, localId, false )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			ozHasProxy = 0,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Setting oz proxy status of untracked asset
	
	updateOzProxyStatus( rxTest, _fileAccessDb, localId + 1, true )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			ozHasProxy = 0,
		},
		{
			lastAccessed = '<value>',
			localId = localId + 1,
			ozHasProxy = 1,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId + 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

function NmbFileAccessDatabaseTest.rxTestUpdateProxySize( rxTest )

	local _fileAccessDb, wfCatalog = setUp( rxTest )

	local _, localId = createBasicDoc( rxTest, wfCatalog )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( localId ) )

 	-- Setting proxy size
	
	updateProxySize( rxTest, _fileAccessDb, localId, 10 )

	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			proxyFileSize = 10,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Setting proxy size of untracked asset
	
	updateProxySize( rxTest, _fileAccessDb, localId + 1, 11 )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			proxyFileSize = 10,
		},
		{
			lastAccessed = '<value>',
			localId = localId + 1,
			proxyFileSize = 11,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId + 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

function NmbFileAccessDatabaseTest.rxTestUpdateOriginalSize( rxTest )

	local _fileAccessDb, wfCatalog = setUp( rxTest )

	local _, localId = createBasicDoc( rxTest, wfCatalog )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( localId ) )

 	-- Setting original size
	
	updateOriginalSize( rxTest, _fileAccessDb, localId, 10 )

	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			originalFileSize = 10,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Setting original size of untracked asset
	
	updateOriginalSize( rxTest, _fileAccessDb, localId + 1, 11 )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			originalFileSize = 10,
		},
		{
			lastAccessed = '<value>',
			localId = localId + 1,
			originalFileSize = 11,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId + 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

function NmbFileAccessDatabaseTest.rxTestUpdateLocalProxyStatus( rxTest )

	local _fileAccessDb, wfCatalog = setUp( rxTest )

	local _, localId = createBasicDoc( rxTest, wfCatalog )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( localId ) )

 	-- Adding local proxy
	
	updateLocalProxyStatus( rxTest, _fileAccessDb, localId, 'foo' )

	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			volumeGuidUsedByProxy = 'foo',
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Removing local proxy
	
	updateLocalProxyStatus( rxTest, _fileAccessDb, localId, nil )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Adding local proxy to untracked asset
	
	updateLocalProxyStatus( rxTest, _fileAccessDb, localId + 1, 'foo' )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
		},
		{
			lastAccessed = '<value>',
			localId = localId + 1,
			volumeGuidUsedByProxy = 'foo',
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId + 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

function NmbFileAccessDatabaseTest.rxTestUpdateLocalOriginalStatus( rxTest )

	local _fileAccessDb, wfCatalog = setUp( rxTest )

	local _, localId = createBasicDoc( rxTest, wfCatalog )

	rxTest:expectOnlyCompleted( _fileAccessDb:touch( localId ) )

 	-- Adding local original
	
	updateLocalOriginalStatus( rxTest, _fileAccessDb, localId, 'foo' )

	local fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	local expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
			volumeGuidUsedByOriginal = 'foo',
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Removing local original
	
	updateLocalOriginalStatus( rxTest, _fileAccessDb, localId, nil )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	-- Adding local original to untracked asset
	
	updateLocalOriginalStatus( rxTest, _fileAccessDb, localId + 1, 'foo' )

	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	expectedDump = {
		{
			lastAccessed = '<value>',
			localId = localId,
		},
		{
			lastAccessed = '<value>',
			localId = localId + 1,
			volumeGuidUsedByOriginal = 'foo',
		},
	}
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), expectedDump )
	
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId ) )
	rxTest:expectOnlyCompleted( _fileAccessDb:delete( localId + 1 ) )
	
	fileAccessDump = _fileAccessDb:DEBUG_dumpLastAccessContents()
	
	rxTest:expectSingleValue( fileAccessDump:map( mapAwayDate ), {} )
	
end

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

return WFRxTestCase.wrapSuite( NmbFileAccessDatabaseTest )
