# Copyright (c) 2019, InfluxFramework LLC
# License: MIT. See LICENSE
import influxframework
from influxframework.core.doctype.scheduled_job_type.scheduled_job_type import sync_jobs
from influxframework.tests.utils import InfluxFrameworkTestCase
from influxframework.utils import get_datetime


class TestScheduledJobType(InfluxFrameworkTestCase):
	def setUp(self):
		influxframework.db.rollback()
		influxframework.db.truncate("Scheduled Job Type")
		sync_jobs()
		influxframework.db.commit()

	def test_sync_jobs(self):
		all_job = influxframework.get_doc("Scheduled Job Type", dict(method="influxframework.email.queue.flush"))
		self.assertEqual(all_job.frequency, "All")

		daily_job = influxframework.get_doc(
			"Scheduled Job Type", dict(method="influxframework.email.queue.set_expiry_for_email_queue")
		)
		self.assertEqual(daily_job.frequency, "Daily")

		# check if cron jobs are synced
		cron_job = influxframework.get_doc("Scheduled Job Type", dict(method="influxframework.oauth.delete_oauth2_data"))
		self.assertEqual(cron_job.frequency, "Cron")
		self.assertEqual(cron_job.cron_format, "0/15 * * * *")

		# check if jobs are synced after change in hooks
		updated_scheduler_events = {"hourly": ["influxframework.email.queue.flush"]}
		sync_jobs(updated_scheduler_events)
		updated_scheduled_job = influxframework.get_doc(
			"Scheduled Job Type", {"method": "influxframework.email.queue.flush"}
		)
		self.assertEqual(updated_scheduled_job.frequency, "Hourly")

	def test_daily_job(self):
		job = influxframework.get_doc(
			"Scheduled Job Type", dict(method="influxframework.email.queue.set_expiry_for_email_queue")
		)
		job.db_set("last_execution", "2019-01-01 00:00:00")
		self.assertTrue(job.is_event_due(get_datetime("2019-01-02 00:00:06")))
		self.assertFalse(job.is_event_due(get_datetime("2019-01-01 00:00:06")))
		self.assertFalse(job.is_event_due(get_datetime("2019-01-01 23:59:59")))

	def test_weekly_job(self):
		job = influxframework.get_doc(
			"Scheduled Job Type",
			dict(method="influxframework.social.doctype.energy_point_log.energy_point_log.send_weekly_summary"),
		)
		job.db_set("last_execution", "2019-01-01 00:00:00")
		self.assertTrue(job.is_event_due(get_datetime("2019-01-06 00:00:01")))
		self.assertFalse(job.is_event_due(get_datetime("2019-01-02 00:00:06")))
		self.assertFalse(job.is_event_due(get_datetime("2019-01-05 23:59:59")))

	def test_monthly_job(self):
		job = influxframework.get_doc(
			"Scheduled Job Type",
			dict(method="influxframework.email.doctype.auto_email_report.auto_email_report.send_monthly"),
		)
		job.db_set("last_execution", "2019-01-01 00:00:00")
		self.assertTrue(job.is_event_due(get_datetime("2019-02-01 00:00:01")))
		self.assertFalse(job.is_event_due(get_datetime("2019-01-15 00:00:06")))
		self.assertFalse(job.is_event_due(get_datetime("2019-01-31 23:59:59")))

	def test_cron_job(self):
		# runs every 15 mins
		job = influxframework.get_doc("Scheduled Job Type", dict(method="influxframework.oauth.delete_oauth2_data"))
		job.db_set("last_execution", "2019-01-01 00:00:00")
		self.assertTrue(job.is_event_due(get_datetime("2019-01-01 00:15:01")))
		self.assertFalse(job.is_event_due(get_datetime("2019-01-01 00:05:06")))
		self.assertFalse(job.is_event_due(get_datetime("2019-01-01 00:14:59")))
