module(..., lunit.testcase, package.seeall)

local oo = require "loop.base"

local m_connectionManager = require("LuaOrm.Connection")
local rpairs = require"LuaOrm.Associations.AssociationArray".Iterator
local m_dataSource = oo.new(LuaOrm.DBMS.DataSource) 
local m_dataBase = {
	driver = "{SQL Server}",
	server = "192.168.20.66",
	database = "mmprodat",
	uid = "UsrAccMW",
	pwd = "inc2001"
}
local m_mapper = nil
local m_models = {}

function test()
	if not m_connectionManager.Connect(m_dataSource, m_dataBase) then
		error("Failure connecting to database")
	end
	
	m_models.AttributeHistoricWindow = LuaOrm.Model {
		tableName = "AttributeHistoricWindow",
		dataSource = m_dataSource,
		Associations = {
			--hasMany("User"),
			--hasMany("GroupPermission", nil, true),
			--hasMany("GroupApplication", nil, true)
		},
		Attributes = {
			--ImageGroup = function(entity) 
				--return {name = 'NON_SYSTEM_GROUP', value = ''}
			--end
		}
	}
	
	m_mapper = LuaOrm.Mapper.new()
	m_connectionManager.ValidateDataSources({m_dataSource})
	
	local id = "b0db7232-875a-4bc8-924b-ade344362ffb"
	--os.execute("pause")
	local rows = m_models.AttributeHistoricWindow:FindAll("AttributeId = ? Order By Timestamp Asc", { id })
	assert_table(rows)
	print(rows[1].TimeStamp)
	print(os.date("!%Y-%m-%d %H:%M:%S", rows[1].TimeStamp:time()))
end