import jingrow
from jingrow.desk.pagetype.tag.tag import add_tag
from jingrow.desk.reportview import get_stats
from jingrow.tests.utils import JingrowTestCase


class TestTag(JingrowTestCase):
	def setUp(self) -> None:
		jingrow.db.delete("Tag")
		jingrow.db.sql("UPDATE `tabPageType` set _user_tags=''")

	def test_tag_count_query(self):
		self.assertDictEqual(
			get_stats('["_user_tags"]', "PageType"),
			{"_user_tags": [["No Tags", jingrow.db.count("PageType")]]},
		)
		add_tag("Standard", "PageType", "User")
		add_tag("Standard", "PageType", "ToDo")

		# count with no filter
		self.assertDictEqual(
			get_stats('["_user_tags"]', "PageType"),
			{"_user_tags": [["Standard", 2], ["No Tags", jingrow.db.count("PageType") - 2]]},
		)

		# count with child table field filter
		self.assertDictEqual(
			get_stats(
				'["_user_tags"]',
				"PageType",
				filters='[["PageField", "fieldname", "like", "%last_name%"], ["PageType", "name", "like", "%use%"]]',
			),
			{"_user_tags": [["Standard", 1], ["No Tags", 0]]},
		)
