#!/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, 6, 16, 21), 'flask.Flask', 'Flask', ({(16, 12, 16, 20): '__name__'}, {}), '(__name__)', False, 'from flask import Flask\n'), ((88, 4, 88, 10), 'unittest.main', 'main', ({}, {}), '()', False, 'from unittest import TestCase, main\n'), ((22, 8, 22, 69), 'airflow.configuration.conf.set', 'configuration.conf.set', ({(22, 31, 22, 42): 'PLUGIN_NAME', (22, 44, 22, 59): '"""git_init_repo"""', (22, 61, 22, 68): '"""False"""'}, {}), "(PLUGIN_NAME, 'git_init_repo', 'False')", False, 'from airflow import configuration\n'), ((23, 8, 23, 76), 'airflow.configuration.conf.set', 'configuration.conf.set', ({(23, 31, 23, 42): 'PLUGIN_NAME', (23, 44, 23, 60): '"""root_directory"""', (23, 62, 23, 75): 'self.root_dir'}, {}), "(PLUGIN_NAME, 'root_directory', self.root_dir)", False, 'from airflow import configuration\n'), ((64, 8, 64, 69), 'airflow.configuration.conf.set', 'configuration.conf.set', ({(64, 31, 64, 42): 'PLUGIN_NAME', (64, 44, 64, 59): '"""git_init_repo"""', (64, 61, 64, 68): '"""False"""'}, {}), "(PLUGIN_NAME, 'git_init_repo', 'False')", False, 'from airflow import configuration\n'), ((65, 8, 65, 76), 'airflow.configuration.conf.set', 'configuration.conf.set', ({(65, 31, 65, 42): 'PLUGIN_NAME', (65, 44, 65, 60): '"""root_directory"""', (65, 62, 65, 75): 'self.root_dir'}, {}), "(PLUGIN_NAME, 'root_directory', self.root_dir)", False, 'from airflow import configuration\n'), ((66, 8, 66, 67), 'airflow.configuration.conf.set', 'configuration.conf.set', ({(66, 31, 66, 42): 'PLUGIN_NAME', (66, 44, 66, 57): '"""git_enabled"""', (66, 59, 66, 66): '"""False"""'}, {}), "(PLUGIN_NAME, 'git_enabled', 'False')", False, 'from airflow import configuration\n'), ((21, 40, 21, 66), 'os.path.realpath', 'os.path.realpath', ({(21, 57, 21, 65): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((27, 16, 27, 26), 'airflow_code_editor.tree.get_tree', 'get_tree', ({}, {}), '()', False, 'from airflow_code_editor.tree import get_tree\n'), ((33, 16, 33, 32), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(33, 25, 33, 31): '"""tags"""'}, {}), "('tags')", False, 'from airflow_code_editor.tree import get_tree\n'), ((38, 16, 38, 42), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(38, 25, 38, 41): '"""local-branches"""'}, {}), "('local-branches')", False, 'from airflow_code_editor.tree import get_tree\n'), ((43, 16, 43, 43), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(43, 25, 43, 42): '"""remote-branches"""'}, {}), "('remote-branches')", False, 'from airflow_code_editor.tree import get_tree\n'), ((48, 16, 48, 33), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(48, 25, 48, 32): '"""files"""'}, {}), "('files')", False, 'from airflow_code_editor.tree import get_tree\n'), ((52, 16, 52, 40), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(52, 25, 52, 39): '"""files/folder"""'}, {}), "('files/folder')", False, 'from airflow_code_editor.tree import get_tree\n'), ((57, 16, 57, 36), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(57, 25, 57, 35): '"""git/HEAD"""'}, {}), "('git/HEAD')", False, 'from airflow_code_editor.tree import get_tree\n'), ((63, 40, 63, 66), 'os.path.realpath', 'os.path.realpath', ({(63, 57, 63, 65): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((70, 16, 70, 26), 'airflow_code_editor.tree.get_tree', 'get_tree', ({}, {}), '()', False, 'from airflow_code_editor.tree import get_tree\n'), ((73, 16, 73, 32), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(73, 25, 73, 31): '"""tags"""'}, {}), "('tags')", False, 'from airflow_code_editor.tree import get_tree\n'), ((75, 16, 75, 42), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(75, 25, 75, 41): '"""local-branches"""'}, {}), "('local-branches')", False, 'from airflow_code_editor.tree import get_tree\n'), ((77, 16, 77, 43), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(77, 25, 77, 42): '"""remote-branches"""'}, {}), "('remote-branches')", False, 'from airflow_code_editor.tree import get_tree\n'), ((79, 16, 79, 33), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(79, 25, 79, 32): '"""files"""'}, {}), "('files')", False, 'from airflow_code_editor.tree import get_tree\n'), ((83, 16, 83, 40), 'airflow_code_editor.tree.get_tree', 'get_tree', ({(83, 25, 83, 39): '"""files/folder"""'}, {}), "('files/folder')", False, 'from airflow_code_editor.tree import get_tree\n')]