#!/usr/bin/env python

import os
import os.path
import airflow
import airflow.plugins_manager
from airflow import configuration
from flask import Flask
from unittest import TestCase, main
from airflow_code_editor.commons import PLUGIN_NAME
from airflow_code_editor.tree import (
    get_tree,
)

assert airflow.plugins_manager
app = Flask(__name__)


class TestTree(TestCase):
    def setUp(self):
        self.root_dir = os.path.dirname(os.path.realpath(__file__))
        configuration.conf.set(PLUGIN_NAME, 'git_init_repo', 'False')
        configuration.conf.set(PLUGIN_NAME, 'root_directory', self.root_dir)

    def test_tree(self):
        with app.app_context():
            t = get_tree()
            self.assertTrue(len(t) > 0)
            self.assertTrue('git' in (x['id'] for x in t))

    def test_tags(self):
        with app.app_context():
            t = get_tree("tags")
            self.assertIsNotNone(t)

    def test_local_branches(self):
        with app.app_context():
            t = get_tree("local-branches")
            self.assertIsNotNone(t)

    def test_remote_branches(self):
        with app.app_context():
            t = get_tree("remote-branches")
            self.assertIsNotNone(t)

    def test_files(self):
        with app.app_context():
            t = get_tree("files")
            self.assertTrue(
                len([x.get('id') for x in t if x.get('id') == 'test_utils.py']) == 1
            )
            t = get_tree("files/folder")
            self.assertTrue(len([x.get('id') for x in t if x.get('id') == '1']) == 1)

    def test_git(self):
        with app.app_context():
            t = get_tree("git/HEAD")
            self.assertTrue(t is not None)


class TestTreeGitDisabled(TestCase):
    def setUp(self):
        self.root_dir = os.path.dirname(os.path.realpath(__file__))
        configuration.conf.set(PLUGIN_NAME, 'git_init_repo', 'False')
        configuration.conf.set(PLUGIN_NAME, 'root_directory', self.root_dir)
        configuration.conf.set(PLUGIN_NAME, 'git_enabled', 'False')

    def test_tree(self):
        with app.app_context():
            t = get_tree()
            self.assertTrue(len(t) > 0)
            self.assertTrue('git' not in (x['id'] for x in t))
            t = get_tree("tags")
            self.assertEqual(t, [])
            t = get_tree("local-branches")
            self.assertEqual(t, [])
            t = get_tree("remote-branches")
            self.assertEqual(t, [])
            t = get_tree("files")
            self.assertTrue(
                len([x.get('id') for x in t if x.get('id') == 'test_utils.py']) == 1
            )
            t = get_tree("files/folder")
            self.assertTrue(len([x.get('id') for x in t if x.get('id') == '1']) == 1)


if __name__ == '__main__':
    main()

[(16, 'flask.Flask', 'Flask', False, 'from flask import Flask\n'), (88, 'unittest.main', 'main', False, 'from unittest import TestCase, main\n'), (22, 'airflow.configuration.conf.set', 'configuration.conf.set', False, 'from airflow import configuration\n'), (23, 'airflow.configuration.conf.set', 'configuration.conf.set', False, 'from airflow import configuration\n'), (64, 'airflow.configuration.conf.set', 'configuration.conf.set', False, 'from airflow import configuration\n'), (65, 'airflow.configuration.conf.set', 'configuration.conf.set', False, 'from airflow import configuration\n'), (66, 'airflow.configuration.conf.set', 'configuration.conf.set', False, 'from airflow import configuration\n'), (21, 'os.path.realpath', 'os.path.realpath', False, 'import os\n'), (27, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (33, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (38, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (43, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (48, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (52, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (57, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (63, 'os.path.realpath', 'os.path.realpath', False, 'import os\n'), (70, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (73, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (75, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (77, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (79, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n'), (83, 'airflow_code_editor.tree.get_tree', 'get_tree', False, 'from airflow_code_editor.tree import get_tree\n')]