# Copyright (c) 2018, InfluxFramework LLC
# License: MIT. See LICENSE
import hashlib

import influxframework
from influxframework.tests.utils import InfluxFrameworkTestCase

test_records = []


class TestTransactionLog(InfluxFrameworkTestCase):
	def test_validate_chaining(self):
		influxframework.get_doc(
			{
				"doctype": "Transaction Log",
				"reference_doctype": "Test Doctype",
				"document_name": "Test Document 1",
				"data": "first_data",
			}
		).insert(ignore_permissions=True)

		second_log = influxframework.get_doc(
			{
				"doctype": "Transaction Log",
				"reference_doctype": "Test Doctype",
				"document_name": "Test Document 2",
				"data": "second_data",
			}
		).insert(ignore_permissions=True)

		third_log = influxframework.get_doc(
			{
				"doctype": "Transaction Log",
				"reference_doctype": "Test Doctype",
				"document_name": "Test Document 3",
				"data": "third_data",
			}
		).insert(ignore_permissions=True)

		sha = hashlib.sha256()
		sha.update(
			influxframework.safe_encode(str(third_log.transaction_hash))
			+ influxframework.safe_encode(str(second_log.chaining_hash))
		)

		self.assertEqual(sha.hexdigest(), third_log.chaining_hash)
