 		test.done()
 
 	testErrors: (test) ->
-		clearTables 'uniusers', 'characters'
 		test.throws(
 			-> character.switchCharacter(conn, 1, 2)
 			Error

		'should throw if something bad happened'
	)

	test.done()


exports.deleteCharacter = (test) ->
	clearTables 'uniusers', 'characters', 'items', 'items_proto', 'battle_participants', 'battles'
	insert 'characters', id: 1, player: 2
	insert 'characters', id: 2, player: 1
	insert 'characters', id: 3, player: 1
	insert 'characters', id: 4, player: 1
	insert 'characters', id: 5, player: 3
	insert 'uniusers', id: 1, character_id: 2
	[1,2,2,3,4,4].forEach (c) -> insert 'items', owner: c

	itemOwners = -> query.all("SELECT owner FROM items ORDER BY owner").map 'owner'

	# deleting inactive character
	res = character.deleteCharacter conn, 1, 4
	user = query.row "SELECT * FROM uniusers"
	chars = query.all "SELECT id FROM characters WHERE player = 1 ORDER BY id"

	test.deepEqual res, {result: 'ok'}, 'should return "ok" if deleted'
	test.deepEqual chars, [{id:2}, {id:3}], 'should delete inactive character'
	test.strictEqual user.character_id, 2, 'should not switch character'
	test.deepEqual itemOwners(), [1,2,2,3], "should delete character's items"

	# deleting current character
	res = character.deleteCharacter conn, 1, 2
	user = query.row "SELECT * FROM uniusers"
	chars = query.all "SELECT id FROM characters WHERE player = 1 ORDER BY id"

	test.deepEqual res, {result: 'ok'}, 'should return "ok" if deleted'
	test.deepEqual chars, [{id:3}], 'should delete active character'
	test.strictEqual user.character_id, null, "should clear user's character if deleted was active"
	test.deepEqual itemOwners(), [1,3], "should delete character's items"

	# deleting character of other user
	res = character.deleteCharacter conn, 1, 5
	count = +query.val "SELECT count(*) FROM characters"

	test.strictEqual res.result, 'fail', "should fail if character belongs to other user"
	test.strictEqual res.reason, 'character #5 of user #1 not found',
		'should describe failure if trying to delete in-battle character'
	test.strictEqual count, 3, "should refuse and not delete character if character belongs to other user"
	test.deepEqual itemOwners(), [1,3], "should refuse and not delete items if character belongs to other user"

	# deleting character while in battle
	clearTables 'uniusers', 'characters', 'battle_participants', 'items'
	insert 'characters', id: 1, player: 2
	insert 'uniusers', id: 2, character_id: 3
	insert 'battle_participants', character_id: 1, battle: 5
	insert 'items', owner: 1

	res = character.deleteCharacter conn, 2, 1
	count = +query.val "SELECT count(*) FROM characters"

	test.strictEqual res.result, 'fail', 'should fail if trying to delete in-battle character'
	test.strictEqual res.reason, 'character #1 is in battle #5',
		'should describe failure if trying to delete in-battle character'
	test.strictEqual count, 1, "should refuse and don't delete character if trying to delete in-battle character"
	test.deepEqual itemOwners(), [1], "should refuse and don't delete items if trying to delete in-battle character"

	# FORCE deleting character while in battle
	res = character.deleteCharacter conn, 2, 1, true
	count = +query.val "SELECT count(*) FROM characters"

	test.deepEqual res, {result: 'ok'}, 'should return "ok" if force-deleting in-battle character'
	test.strictEqual count, 0, "should delete character if force-deleting in-battle character"
	test.deepEqual itemOwners(), [], "should  delete items if force-deleting in-battle character"
	test.done()


exports.switchCharacter =
	testNoErrors: (test) ->
		clearTables 'uniusers', 'characters'
		insert 'uniusers', id: 1, character_id: 10
		insert 'characters', id: 2, player: 1


		test.strictEqual char.energy_max, x[2], "should set correct energy_max value for #{x[1]} #{x[0]}"
		test.strictEqual char.energy, x[2], "should set correct energy value for #{x[1]} #{x[0]}"

	console.log '5555'

	test.throws(
		-> character.createCharacter(conn, 1, 'My First Character', 'murloc', 'female')
		Error
		'should not allow weird races'
	)

	console.log '6666'

	test.throws(
		-> character.createCharacter(conn, 1, 'My First Character', 'orc', 'it')
		Error
		'should not allow weird genders'
	)

	console.log '7777'

	query "UPDATE locations SET initial = 1"
	test.throws(
