# Copyright (c) 2015, XhiveFramework Technologies and Contributors
# License: MIT. See LICENSE
import xhiveframework
from xhiveframework.contacts.doctype.address_template.address_template import get_default_address_template
from xhiveframework.tests.utils import XhiveFrameworkTestCase
from xhiveframework.utils.jinja import validate_template


class TestAddressTemplate(XhiveFrameworkTestCase):
	def setUp(self) -> None:
		xhiveframework.db.delete("Address Template", {"country": "India"})
		xhiveframework.db.delete("Address Template", {"country": "Brazil"})

	def test_default_address_template(self):
		validate_template(get_default_address_template())

	def test_default_is_unset(self):
		xhiveframework.get_doc({"doctype": "Address Template", "country": "India", "is_default": 1}).insert()

		self.assertEqual(xhiveframework.db.get_value("Address Template", "India", "is_default"), 1)

		xhiveframework.get_doc({"doctype": "Address Template", "country": "Brazil", "is_default": 1}).insert()

		self.assertEqual(xhiveframework.db.get_value("Address Template", "India", "is_default"), 0)
		self.assertEqual(xhiveframework.db.get_value("Address Template", "Brazil", "is_default"), 1)

	def test_delete_address_template(self):
		india = xhiveframework.get_doc({"doctype": "Address Template", "country": "India", "is_default": 0}).insert()

		brazil = xhiveframework.get_doc(
			{"doctype": "Address Template", "country": "Brazil", "is_default": 1}
		).insert()

		india.reload()  # might have been modified by the second template
		india.delete()  # should not raise an error

		self.assertRaises(xhiveframework.ValidationError, brazil.delete)
