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

WFRxThrottledBatchedBulkDocValueTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2018 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 WFRxThrottledBatchedBulkDocValue = require 'WFRxThrottledBatchedBulkDocValue'
local KSIdGenerator = require 'KSIdGenerator'

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


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

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog


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

local WFRxThrottledBatchedBulkDocValueTest = {}

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

local function batchedBulkHelper( rxTest, docStore )

	local assetIds = {}

	for i=1,100 do

		local assetId = KSIdGenerator.uuidInLowercaseHex()

		rxTest:expectSingleValue(

			docStore:setDocValue {
					_id = assetId,
					type = 'asset',
					subtype = 'jpeg',
					_localOnly = {
						batchTest = true,
					}
				},

			':ignore:' )

		assetIds[ i ] = assetId

	end

	return assetIds
	
end

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

function WFRxThrottledBatchedBulkDocValueTest.rxTestBasic( rxTest )

	rxTest:setTimeLimit( 10 )

	local fakeWFCatalog = createFakeWFCatalog( rxTest )

	local docStore = fakeWFCatalog:docStore()

	local assetIds = batchedBulkHelper( rxTest, docStore )
	
	-- Get the expected value
	
	local docValuesByBatch = {}

	local numberOfBatches = 10

	for i, assetId in ipairs( assetIds ) do
	
		local batch = math.ceil( i / numberOfBatches )
	
		if not docValuesByBatch[ batch ] then
			docValuesByBatch[ batch ] = {}
		end
	
		local fetchDocValue = docStore:docValue( assetId )
			:map( function( docValue )
				table.insert( docValuesByBatch[ batch ], docValue )
				return true
			end )
		
		rxTest:expectSingleValue( fetchDocValue, true )
		
	end
	
	local batchSize = #assetIds / numberOfBatches
	
	local bulkDocValueStream = rxTest:observe( WFRxThrottledBatchedBulkDocValue( "WFRxThrottledBatchedBulkDocValueTest_rxTestBasic", docStore, assetIds, batchSize, 10 ) )
	
	for batch = 1, numberOfBatches, 1 do
		bulkDocValueStream:expectDataWithinTime( 3, docValuesByBatch[ batch ] )
	end
	
end

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

return WFRxTestCase.wrapSuite( WFRxThrottledBatchedBulkDocValueTest )
