# -*- coding: utf-8 -*-
# Copyright (c) 2019, JINGROW
# See license.txt


import unittest
from typing import TYPE_CHECKING

import jingrow

from jcloud.jcloud.pagetype.team.test_team import create_test_team

if TYPE_CHECKING:
	from jcloud.jcloud.pagetype.app.app import App


def create_test_app(name: str = "jingrow", title: str = "Jingrow Framework") -> "App":
	return jingrow.get_pg({"pagetype": "App", "name": name, "title": title}).insert(
		ignore_if_duplicate=True
	)


class TestApp(unittest.TestCase):
	def tearDown(self):
		jingrow.db.rollback()

	def test_create_jingrow_app(self):
		app = create_test_app("jingrow", "Jingrow Framework")
		self.assertEqual(app.jingrow, True)

		source = app.add_source(
			"v0.2",
			"http://git.jingrow.com/jingrow/jingrow",
			"version-12",
			create_test_team().name,
		)
		self.assertEqual(source.repository, "jingrow")
		self.assertEqual(source.repository_owner, "jingrow")

		self.assertEqual(len(source.versions), 1)
		self.assertEqual(source.versions[0].version, "v0.2")

	def test_create_non_jingrow_app(self):
		app = create_test_app("jerp", "JERP")
		self.assertEqual(app.jingrow, False)

		source = app.add_source(
			"v0.2",
			"http://git.jingrow.com/jingrow/jerp",
			"version-12",
			create_test_team().name,
		)
		self.assertEqual(source.repository, "jerp")
		self.assertEqual(source.repository_owner, "jingrow")

		self.assertEqual(len(source.versions), 1)
		self.assertEqual(source.versions[0].version, "v0.2")

	def test_create_app_with_multiple_sources(self):
		app = create_test_app("jingrow", "Jingrow Framework")

		source_1 = app.add_source(
			"v0.2",
			"http://git.jingrow.com/jingrow/jingrow",
			"version-12",
			create_test_team().name,
		)
		source_2 = app.add_source(
			"v0.3",
			"http://git.jingrow.com/jingrow/jingrow",
			"version-13",
			create_test_team().name,
		)
		self.assertEqual(source_1.branch, "version-12")
		self.assertEqual(len(source_1.versions), 1)
		self.assertEqual(source_1.versions[0].version, "v0.2")

		self.assertEqual(source_2.branch, "version-13")
		self.assertEqual(len(source_2.versions), 1)
		self.assertEqual(source_2.versions[0].version, "v0.3")

	def test_create_app_with_one_source_multiple_versions(self):
		app = create_test_app("jerp_documentation", "JERP Documentation")
		team_name = create_test_team().name

		source_1 = app.add_source(
			"v0.2",
			"http://git.jingrow.com/jingrow/jerp_documentation",
			"master",
			team_name,
		)
		self.assertEqual(source_1.branch, "master")
		self.assertEqual(len(source_1.versions), 1)
		self.assertEqual(source_1.versions[0].version, "v0.2")

		source_2 = app.add_source(
			"v0.3",
			"http://git.jingrow.com/jingrow/jerp_documentation",
			"master",
			team_name,
		)

		self.assertEqual(source_1.name, source_2.name)
		self.assertEqual(len(source_2.versions), 2)
		self.assertEqual(source_2.versions[0].version, "v0.2")
		self.assertEqual(source_2.versions[1].version, "v0.3")

	def test_create_app_add_second_source_after_insert(self):
		app = create_test_app("jingrow", "Jingrow Framework")
		source_1 = app.add_source(
			"v0.2",
			"http://git.jingrow.com/jingrow/jingrow",
			"version-12",
			create_test_team().name,
		)
		self.assertEqual(source_1.branch, "version-12")
		self.assertEqual(len(source_1.versions), 1)
		self.assertEqual(source_1.versions[0].version, "v0.2")

		source_2 = app.add_source(
			"v0.3",
			"http://git.jingrow.com/jingrow/jingrow",
			"version-13",
			create_test_team().name,
		)
		self.assertEqual(source_1.branch, "version-12")
		self.assertEqual(len(source_1.versions), 1)
		self.assertEqual(source_1.versions[0].version, "v0.2")

		self.assertEqual(source_2.branch, "version-13")
		self.assertEqual(len(source_2.versions), 1)
		self.assertEqual(source_2.versions[0].version, "v0.3")
