# -------------------------------------------------------
# File: "RecordCmd.test"
#                        Created: 2004-09-07 15:43:09
#              Last modification: 2009-03-06 06:36:16
# Author: Bernard Desgraupes
# e-mail: <bdesgraupes@users.sourceforge.net.fr>
# www: <http://tcladdressbook.sourceforge.net>
# (c) Copyright: Bernard Desgraupes, 2004-2009
# All rights reserved.
# This software is free software with BSD licence.
# Versions history: see the Changes.Log file.
# 
# Test suite for the Tcl [addressbook record] command defined 
# in the Tcladdressbook extension.
# 
# -------------------------------------------------------

source [file join [file dir [info script]] TclABCommonTests.tcl]

namespace eval ::addressbook::test {
	namespace import ::tcltest::*

	# Person records
	# ==============
	test AddressbookRecord-1-1 {Check person record's UID} -setup {
	} -body {
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		set rec [addressbook record $PersonID]
		set theUID [keylget rec UID]
		return [expr {$theUID eq $PersonID}]
	} -cleanup {
	} -result "1"
		
	test AddressbookRecord-1-2 {Check person record's name} -setup {
	} -body {
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		set rec [addressbook record $PersonID]
		set theName [keylget rec Last]
		return [expr {$theName eq $PersonName}]
	} -cleanup {
	} -result "1"
		
	test AddressbookRecord-1-3 {Check person record's creation date} -setup {
	} -body {
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		set rec [addressbook record $PersonID]
		set theCreation [keylget rec Creation]
		set currDay [clock format [clock seconds] -format "%Y-%m-%d"]
		set creationDay [clock format $theCreation -format "%Y-%m-%d"]
		return [expr {$creationDay eq $currDay}]
	} -cleanup {
	} -result "1"
		
	test AddressbookRecord-1-4 {Check person record's length} -setup {
	} -body {
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		set rec [addressbook record $PersonID]
		return [expr {[llength $rec] > 0}]
	} -cleanup {
	} -result "1"

	test AddressbookRecord-1-5 {Check person record's keys} -setup {
	} -body {
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		set rec [addressbook record $PersonID]
		set keyList [keylkeys rec]
		return [expr {("Creation" in $keyList) && ("Last" in $keyList) && ("UID" in $keyList)}]
	} -cleanup {
	} -result "1"


	# Group records
	# =============
	test AddressbookRecord-2-1 {Check group record's UID} -setup {
	} -body {
		set GroupName "TclABGroupsTestGroup[incr GroupNum]"
		set GroupID [addressbook create group $GroupName]
		set rec [addressbook record $GroupID]
		set theUID [keylget rec UID]
		return [expr {$theUID eq $GroupID}]
	} -cleanup {
	} -result "1"
		
	test AddressbookRecord-2-2 {Check group record's name} -setup {
	} -body {
		set GroupName "TclABGroupsTestGroup[incr GroupNum]"
		set GroupID [addressbook create group $GroupName]
		set rec [addressbook record $GroupID]
		set theName [keylget rec GroupName]
		return [expr {$theName eq $GroupName}]
	} -cleanup {
	} -result "1"
		
	test AddressbookRecord-2-3 {Check group record's creation date} -setup {
	} -body {
		set GroupName "TclABGroupsTestGroup[incr GroupNum]"
		set GroupID [addressbook create group $GroupName]
		set rec [addressbook record $GroupID]
		set theCreation [keylget rec Creation]
		set currDay [clock format [clock seconds] -format "%Y-%m-%d"]
		set creationDay [clock format $theCreation -format "%Y-%m-%d"]
		return [expr {$creationDay eq $currDay}]
	} -cleanup {
	} -result "1"
		
	test AddressbookRecord-2-4 {Check group record's length} -setup {
	} -body {
		set GroupName "TclABGroupsTestGroup[incr GroupNum]"
		set GroupID [addressbook create group $GroupName]
		set rec [addressbook record $GroupID]
		return [expr {[llength $rec] > 0}]
	} -cleanup {
	} -result "1"

	test AddressbookRecord-2-5 {Check group record's keys} -setup {
	} -body {
		set GroupName "TclABGroupsTestGroup[incr GroupNum]"
		set GroupID [addressbook create group $GroupName]
		set rec [addressbook record $GroupID]
		set keyList [keylkeys rec]
		return [expr {("Creation" in $keyList) && ("GroupName" in $keyList) && ("UID" in $keyList)}]
	} -cleanup {
	} -result "1"


	# Usage string
	# ============
	test AddressbookRecord-2-1 {Usage string for wrong num args} -setup {
	} -body {
		addressbook record
	} -cleanup {
	} -returnCodes error \
	  -result "wrong # args: should be \"addressbook record recordID\""

	# Cleanup
	::tcltest::cleanupTests
}

namespace delete ::addressbook::test

