import os
from tempfile import TemporaryDirectory

from quickbase_client.utils.pywriting_utils import BasicPyFileWriter
from quickbase_client.utils.pywriting_utils import PyPackageWriter


class TestBasicFileWriter:

    def test_outputs_lines(self):
        w = BasicPyFileWriter()
        w.add_line('import abc')
        w.add_line('import os').space()
        s = w.get_file_as_string()
        assert s == 'import abc\nimport os\n'

    def test_indent_dedent(self):
        w = BasicPyFileWriter()
        w.add_line('def foo():').indent().add_line('return 5').dedent().space()
        s = w.get_file_as_string()
        assert s == 'def foo():\n    return 5\n'

    def test_use_refs(self):
        w = BasicPyFileWriter()
        w.add_line('a = "A"')
        ref = w.make_ref()
        w.add_line('d = "D"')
        ref.add_line('b = "B"').add_line('c = "C"')
        s = w.get_file_as_string()
        lns = s.split('\n')
        assert 'a' in lns[0]
        assert 'b' in lns[1]
        assert 'c' in lns[2]
        assert 'd' in lns[3]


class TestPyPackageWriter:

    def test_includes_init(self):
        with TemporaryDirectory() as d:
            w = PyPackageWriter(pkg_name='foo', parent_dir=d)
            assert '__init__' in w.modules
            assert w.has_module_name('__init__')
            assert w.pkg_path == os.path.join(d, 'foo')
            w.write()
            assert os.path.exists(d)
            assert os.path.exists(os.path.join(d, 'foo'))
            assert os.path.exists(os.path.join(d, 'foo', '__init__.py'))

[((11, 12, 11, 31), 'quickbase_client.utils.pywriting_utils.BasicPyFileWriter', 'BasicPyFileWriter', ({}, {}), '()', False, 'from quickbase_client.utils.pywriting_utils import BasicPyFileWriter\n'), ((18, 12, 18, 31), 'quickbase_client.utils.pywriting_utils.BasicPyFileWriter', 'BasicPyFileWriter', ({}, {}), '()', False, 'from quickbase_client.utils.pywriting_utils import BasicPyFileWriter\n'), ((24, 12, 24, 31), 'quickbase_client.utils.pywriting_utils.BasicPyFileWriter', 'BasicPyFileWriter', ({}, {}), '()', False, 'from quickbase_client.utils.pywriting_utils import BasicPyFileWriter\n'), ((40, 13, 40, 33), 'tempfile.TemporaryDirectory', 'TemporaryDirectory', ({}, {}), '()', False, 'from tempfile import TemporaryDirectory\n'), ((41, 16, 41, 61), 'quickbase_client.utils.pywriting_utils.PyPackageWriter', 'PyPackageWriter', (), '', False, 'from quickbase_client.utils.pywriting_utils import PyPackageWriter\n'), ((46, 19, 46, 36), 'os.path.exists', 'os.path.exists', ({(46, 34, 46, 35): 'd'}, {}), '(d)', False, 'import os\n'), ((44, 33, 44, 55), 'os.path.join', 'os.path.join', ({(44, 46, 44, 47): 'd', (44, 49, 44, 54): '"""foo"""'}, {}), "(d, 'foo')", False, 'import os\n'), ((47, 34, 47, 56), 'os.path.join', 'os.path.join', ({(47, 47, 47, 48): 'd', (47, 50, 47, 55): '"""foo"""'}, {}), "(d, 'foo')", False, 'import os\n'), ((48, 34, 48, 71), 'os.path.join', 'os.path.join', ({(48, 47, 48, 48): 'd', (48, 50, 48, 55): '"""foo"""', (48, 57, 48, 70): '"""__init__.py"""'}, {}), "(d, 'foo', '__init__.py')", False, 'import os\n')]