# Copyright (c) 2019, Xhive Global Ltd. and Contributors
# License: MIT. See LICENSE

import sqlparse

import xhiveframework
import xhiveframework.recorder
from xhiveframework.tests.utils import XhiveFrameworkTestCase
from xhiveframework.utils import set_request
from xhiveframework.website.serve import get_response_content


class TestRecorder(XhiveFrameworkTestCase):
	def setUp(self):
		xhiveframework.recorder.stop()
		xhiveframework.recorder.delete()
		set_request()
		xhiveframework.recorder.start()
		xhiveframework.recorder.record()

	def test_start(self):
		xhiveframework.recorder.dump()
		requests = xhiveframework.recorder.get()
		self.assertEqual(len(requests), 1)

	def test_do_not_record(self):
		xhiveframework.recorder.do_not_record(xhiveframework.get_all)("DocType")
		xhiveframework.recorder.dump()
		requests = xhiveframework.recorder.get()
		self.assertEqual(len(requests), 0)

	def test_get(self):
		xhiveframework.recorder.dump()

		requests = xhiveframework.recorder.get()
		self.assertEqual(len(requests), 1)

		request = xhiveframework.recorder.get(requests[0]["uuid"])
		self.assertTrue(request)

	def test_delete(self):
		xhiveframework.recorder.dump()

		requests = xhiveframework.recorder.get()
		self.assertEqual(len(requests), 1)

		xhiveframework.recorder.delete()

		requests = xhiveframework.recorder.get()
		self.assertEqual(len(requests), 0)

	def test_record_without_sql_queries(self):
		xhiveframework.recorder.dump()

		requests = xhiveframework.recorder.get()
		request = xhiveframework.recorder.get(requests[0]["uuid"])

		self.assertEqual(len(request["calls"]), 0)

	def test_record_with_sql_queries(self):
		xhiveframework.get_all("DocType")
		xhiveframework.recorder.dump()

		requests = xhiveframework.recorder.get()
		request = xhiveframework.recorder.get(requests[0]["uuid"])

		self.assertNotEqual(len(request["calls"]), 0)

	def test_explain(self):
		xhiveframework.db.sql("SELECT * FROM tabDocType")
		xhiveframework.db.sql("COMMIT")
		xhiveframework.recorder.dump()

		requests = xhiveframework.recorder.get()
		request = xhiveframework.recorder.get(requests[0]["uuid"])

		self.assertEqual(len(request["calls"][0]["explain_result"]), 1)
		self.assertEqual(len(request["calls"][1]["explain_result"]), 0)

	def test_multiple_queries(self):
		queries = [
			{"mariadb": "SELECT * FROM tabDocType", "postgres": 'SELECT * FROM "tabDocType"'},
			{"mariadb": "SELECT COUNT(*) FROM tabDocType", "postgres": 'SELECT COUNT(*) FROM "tabDocType"'},
			{"mariadb": "COMMIT", "postgres": "COMMIT"},
		]

		sql_dialect = xhiveframework.db.db_type or "mariadb"
		for query in queries:
			xhiveframework.db.sql(query[sql_dialect])

		xhiveframework.recorder.dump()

		requests = xhiveframework.recorder.get()
		request = xhiveframework.recorder.get(requests[0]["uuid"])

		self.assertEqual(len(request["calls"]), len(queries))

		for query, call in zip(queries, request["calls"]):
			self.assertEqual(
				call["query"], sqlparse.format(query[sql_dialect].strip(), keyword_case="upper", reindent=True)
			)

	def test_duplicate_queries(self):
		queries = [
			("SELECT * FROM tabDocType", 2),
			("SELECT COUNT(*) FROM tabDocType", 1),
			("select * from tabDocType", 2),
			("COMMIT", 3),
			("COMMIT", 3),
			("COMMIT", 3),
		]
		for query in queries:
			xhiveframework.db.sql(query[0])

		xhiveframework.recorder.dump()

		requests = xhiveframework.recorder.get()
		request = xhiveframework.recorder.get(requests[0]["uuid"])

		for query, call in zip(queries, request["calls"]):
			self.assertEqual(call["exact_copies"], query[1])

	def test_error_page_rendering(self):
		content = get_response_content("error")
		self.assertIn("Error", content)


class TestRecorderDeco(XhiveFrameworkTestCase):
	def test_recorder_flag(self):
		xhiveframework.recorder.delete()

		@xhiveframework.recorder.record_queries
		def test():
			xhiveframework.get_all("User")

		test()
		self.assertTrue(xhiveframework.recorder.get())
