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

WFSQLiteUtilsTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2011 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.utils
local KSTableUtils = require 'KSTableUtils'
local KSTestUtils = require 'KSTestUtils'

	-- WF.sqlite.database
local WFSQLiteUtils = require 'WFSQLiteUtils'


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

-- @mark WFSQLiteUtils test case

local WFSQLiteUtilsTest = {}

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

local function testValuePair( luaValue, sqliteValue, sqliteDataType )

	local actualSqliteValue, actualSqliteDataType
		= WFSQLiteUtils.luaValueToSqliteValueAndType( luaValue )

	if actualSqliteValue ~= sqliteValue then
		error( string.format(
			"luaValueToSqliteValueAndType returned incorrect value: actual %s, expected %s",
				actualSqliteValue, sqliteValue ), 2 )
	end

	if actualSqliteDataType ~= sqliteDataType then
		error( string.format(
			"luaValueToSqliteValueAndType returned incorrect dataType: actual %s, expected %s",
				actualSqliteDataType, sqliteDataType ), 2 )
	end

	local actualLuaValue
		= WFSQLiteUtils.sqliteValueAndTypeToLuaValue( sqliteValue, sqliteDataType )

	if type( actualLuaValue ) ~= type( luaValue ) then
		error( string.format(
			"luaValueToSqliteValueAndType returned incorrect dataType: actual %s, expected %s",
				type( actualLuaValue ), type( luaValue ) ), 2 )
	end

	if type( luaValue ) == 'table' then

		if not KSTableUtils.deepEquals( actualLuaValue, luaValue, 10, true ) then

			error( "luaValueToSqliteValueAndType returned incorrect value (see debug log for mismatch location)", 2 )

		end

	else

		if actualLuaValue ~= luaValue then
			error( string.format(
				"luaValueToSqliteValueAndType returned incorrect value: actual %s, expected %s",
					actualLuaValue, luaValue ), 2 )
		end

	end

end


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

function WFSQLiteUtilsTest.testNil()

	testValuePair( nil, nil, nil )

end

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

function WFSQLiteUtilsTest.testBoolean()

	testValuePair( false, 'false', 'boolean' )
	testValuePair( true, 'true', 'boolean' )

end

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

function WFSQLiteUtilsTest.testString()

	testValuePair( '', '', nil )
	testValuePair( 'false', 'false', nil )
	testValuePair( 'true', 'true', nil )
	testValuePair( '42', '42', nil )
	testValuePair( '{}', '{}', nil )

end

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

function WFSQLiteUtilsTest.testNumber()

	testValuePair( 0, 0, nil )
	testValuePair( 42, 42, nil )

end

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

function WFSQLiteUtilsTest.testTable()

	testValuePair( {}, '{\n}', 'table' )
	testValuePair( { a = 42 }, '{\n\ta = 42,\n}', 'table' )

end

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

function WFSQLiteUtilsTest.testTableNameFromCreateTableStatement()

	local fn = WFSQLiteUtils.tableNameFromCreateTableStatement

	KSTestUtils.testResults( fn, 'CREATE TABLE blah' )( nil )
	KSTestUtils.testResults( fn, 'Create Table blah( foo )' )( 'blah' )
	KSTestUtils.testResults( fn, 'CREATE TABLE blah( foo )' )( 'blah' )
	KSTestUtils.testResults( fn, 'CREATE TABLE blah( foo ) bar( blop )' )( 'blah' )

end

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

function WFSQLiteUtilsTest.testCreateVariableArgList()

	local fn = WFSQLiteUtils.createVariableArgList

	KSTestUtils.testResults( fn, -1 )( '' )
	KSTestUtils.testResults( fn, 0 )( '' )
	KSTestUtils.testResults( fn, 1 )( '?' )
	KSTestUtils.testResults( fn, 5 )( '?, ?, ?, ?, ?' )

end

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

return WFSQLiteUtilsTest
