#!/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()

[((333, 348), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (338, 348), False, 'from flask import Flask\n'), ((2786, 2792), 'unittest.main', 'main', ([], {}), '()\n', (2790, 2792), False, 'from unittest import TestCase, main\n'), ((474, 535), 'airflow.configuration.conf.set', 'configuration.conf.set', (['PLUGIN_NAME', '"""git_init_repo"""', '"""False"""'], {}), "(PLUGIN_NAME, 'git_init_repo', 'False')\n", (496, 535), False, 'from airflow import configuration\n'), ((544, 612), 'airflow.configuration.conf.set', 'configuration.conf.set', (['PLUGIN_NAME', '"""root_directory"""', 'self.root_dir'], {}), "(PLUGIN_NAME, 'root_directory', self.root_dir)\n", (566, 612), False, 'from airflow import configuration\n'), ((1841, 1902), 'airflow.configuration.conf.set', 'configuration.conf.set', (['PLUGIN_NAME', '"""git_init_repo"""', '"""False"""'], {}), "(PLUGIN_NAME, 'git_init_repo', 'False')\n", (1863, 1902), False, 'from airflow import configuration\n'), ((1911, 1979), 'airflow.configuration.conf.set', 'configuration.conf.set', (['PLUGIN_NAME', '"""root_directory"""', 'self.root_dir'], {}), "(PLUGIN_NAME, 'root_directory', self.root_dir)\n", (1933, 1979), False, 'from airflow import configuration\n'), ((1988, 2047), 'airflow.configuration.conf.set', 'configuration.conf.set', (['PLUGIN_NAME', '"""git_enabled"""', '"""False"""'], {}), "(PLUGIN_NAME, 'git_enabled', 'False')\n", (2010, 2047), False, 'from airflow import configuration\n'), ((438, 464), 'os.path.realpath', 'os.path.realpath', (['__file__'], {}), '(__file__)\n', (454, 464), False, 'import os\n'), ((687, 697), 'airflow_code_editor.tree.get_tree', 'get_tree', ([], {}), '()\n', (695, 697), False, 'from airflow_code_editor.tree import get_tree\n'), ((871, 887), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""tags"""'], {}), "('tags')\n", (879, 887), False, 'from airflow_code_editor.tree import get_tree\n'), ((1008, 1034), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""local-branches"""'], {}), "('local-branches')\n", (1016, 1034), False, 'from airflow_code_editor.tree import get_tree\n'), ((1156, 1183), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""remote-branches"""'], {}), "('remote-branches')\n", (1164, 1183), False, 'from airflow_code_editor.tree import get_tree\n'), ((1295, 1312), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""files"""'], {}), "('files')\n", (1303, 1312), False, 'from airflow_code_editor.tree import get_tree\n'), ((1457, 1481), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""files/folder"""'], {}), "('files/folder')\n", (1465, 1481), False, 'from airflow_code_editor.tree import get_tree\n'), ((1641, 1661), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""git/HEAD"""'], {}), "('git/HEAD')\n", (1649, 1661), False, 'from airflow_code_editor.tree import get_tree\n'), ((1805, 1831), 'os.path.realpath', 'os.path.realpath', (['__file__'], {}), '(__file__)\n', (1821, 1831), False, 'import os\n'), ((2122, 2132), 'airflow_code_editor.tree.get_tree', 'get_tree', ([], {}), '()\n', (2130, 2132), False, 'from airflow_code_editor.tree import get_tree\n'), ((2252, 2268), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""tags"""'], {}), "('tags')\n", (2260, 2268), False, 'from airflow_code_editor.tree import get_tree\n'), ((2321, 2347), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""local-branches"""'], {}), "('local-branches')\n", (2329, 2347), False, 'from airflow_code_editor.tree import get_tree\n'), ((2400, 2427), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""remote-branches"""'], {}), "('remote-branches')\n", (2408, 2427), False, 'from airflow_code_editor.tree import get_tree\n'), ((2480, 2497), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""files"""'], {}), "('files')\n", (2488, 2497), False, 'from airflow_code_editor.tree import get_tree\n'), ((2642, 2666), 'airflow_code_editor.tree.get_tree', 'get_tree', (['"""files/folder"""'], {}), "('files/folder')\n", (2650, 2666), False, 'from airflow_code_editor.tree import get_tree\n')]