#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    Unit tests for gluon.recfile
"""
import unittest
import os
import shutil
import uuid
from .fix_path import fix_sys_path

fix_sys_path(__file__)

from gluon import recfile


class TestRecfile(unittest.TestCase):

    def setUp(self):
        os.mkdir('tests')

    def tearDown(self):
        shutil.rmtree('tests')

    def test_generation(self):
        for k in range(10):
            teststring = 'test%s' % k
            filename = os.path.join('tests', str(uuid.uuid4()) + '.test')
            with recfile.open(filename, "w") as g:
                g.write(teststring)
            self.assertEqual(recfile.open(filename, "r").read(), teststring)
            is_there = recfile.exists(filename)
            self.assertTrue(is_there)
            recfile.remove(filename)
            is_there = recfile.exists(filename)
            self.assertFalse(is_there)
        for k in range(10):
            teststring = 'test%s' % k
            filename = str(uuid.uuid4()) + '.test'
            with recfile.open(filename, "w", path='tests') as g:
                g.write(teststring)
            self.assertEqual(recfile.open(filename, "r", path='tests').read(), teststring)
            is_there = recfile.exists(filename, path='tests')
            self.assertTrue(is_there)
            recfile.remove(filename, path='tests')
            is_there = recfile.exists(filename, path='tests')
            self.assertFalse(is_there)
        for k in range(10):
            teststring = 'test%s' % k
            filename = os.path.join('tests', str(uuid.uuid4()), str(uuid.uuid4()) + '.test')
            with recfile.open(filename, "w") as g:
                g.write(teststring)
            self.assertEqual(recfile.open(filename, "r").read(), teststring)
            is_there = recfile.exists(filename)
            self.assertTrue(is_there)
            recfile.remove(filename)
            is_there = recfile.exists(filename)
            self.assertFalse(is_there)

    def test_existing(self):
        filename = os.path.join('tests', str(uuid.uuid4()) + '.test')
        with open(filename, 'w') as g:
            g.write('this file exists')
        self.assertTrue(recfile.exists(filename))
        self.assertTrue(hasattr(recfile.open(filename, "r"), 'read'))
        recfile.remove(filename, path='tests')
        self.assertFalse(recfile.exists(filename))
        self.assertRaises(IOError, recfile.remove, filename)
        self.assertRaises(IOError, recfile.open, filename, "r")


if __name__ == '__main__':
    unittest.main()

[((2565, 2580), 'unittest.main', 'unittest.main', ([], {}), '()\n', (2578, 2580), False, 'import unittest\n'), ((296, 313), 'os.mkdir', 'os.mkdir', (['"""tests"""'], {}), "('tests')\n", (304, 313), False, 'import os\n'), ((347, 369), 'shutil.rmtree', 'shutil.rmtree', (['"""tests"""'], {}), "('tests')\n", (360, 369), False, 'import shutil\n'), ((2317, 2355), 'gluon.recfile.remove', 'recfile.remove', (['filename'], {'path': '"""tests"""'}), "(filename, path='tests')\n", (2331, 2355), False, 'from gluon import recfile\n'), ((729, 753), 'gluon.recfile.exists', 'recfile.exists', (['filename'], {}), '(filename)\n', (743, 753), False, 'from gluon import recfile\n'), ((804, 828), 'gluon.recfile.remove', 'recfile.remove', (['filename'], {}), '(filename)\n', (818, 828), False, 'from gluon import recfile\n'), ((852, 876), 'gluon.recfile.exists', 'recfile.exists', (['filename'], {}), '(filename)\n', (866, 876), False, 'from gluon import recfile\n'), ((1248, 1286), 'gluon.recfile.exists', 'recfile.exists', (['filename'], {'path': '"""tests"""'}), "(filename, path='tests')\n", (1262, 1286), False, 'from gluon import recfile\n'), ((1337, 1375), 'gluon.recfile.remove', 'recfile.remove', (['filename'], {'path': '"""tests"""'}), "(filename, path='tests')\n", (1351, 1375), False, 'from gluon import recfile\n'), ((1399, 1437), 'gluon.recfile.exists', 'recfile.exists', (['filename'], {'path': '"""tests"""'}), "(filename, path='tests')\n", (1413, 1437), False, 'from gluon import recfile\n'), ((1823, 1847), 'gluon.recfile.exists', 'recfile.exists', (['filename'], {}), '(filename)\n', (1837, 1847), False, 'from gluon import recfile\n'), ((1898, 1922), 'gluon.recfile.remove', 'recfile.remove', (['filename'], {}), '(filename)\n', (1912, 1922), False, 'from gluon import recfile\n'), ((1946, 1970), 'gluon.recfile.exists', 'recfile.exists', (['filename'], {}), '(filename)\n', (1960, 1970), False, 'from gluon import recfile\n'), ((2213, 2237), 'gluon.recfile.exists', 'recfile.exists', (['filename'], {}), '(filename)\n', (2227, 2237), False, 'from gluon import recfile\n'), ((2381, 2405), 'gluon.recfile.exists', 'recfile.exists', (['filename'], {}), '(filename)\n', (2395, 2405), False, 'from gluon import recfile\n'), ((559, 586), 'gluon.recfile.open', 'recfile.open', (['filename', '"""w"""'], {}), "(filename, 'w')\n", (571, 586), False, 'from gluon import recfile\n'), ((1050, 1091), 'gluon.recfile.open', 'recfile.open', (['filename', '"""w"""'], {'path': '"""tests"""'}), "(filename, 'w', path='tests')\n", (1062, 1091), False, 'from gluon import recfile\n'), ((1653, 1680), 'gluon.recfile.open', 'recfile.open', (['filename', '"""w"""'], {}), "(filename, 'w')\n", (1665, 1680), False, 'from gluon import recfile\n'), ((2271, 2298), 'gluon.recfile.open', 'recfile.open', (['filename', '"""r"""'], {}), "(filename, 'r')\n", (2283, 2298), False, 'from gluon import recfile\n'), ((1009, 1021), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (1019, 1021), False, 'import uuid\n'), ((1592, 1604), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (1602, 1604), False, 'import uuid\n'), ((2085, 2097), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (2095, 2097), False, 'import uuid\n'), ((517, 529), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (527, 529), False, 'import uuid\n'), ((658, 685), 'gluon.recfile.open', 'recfile.open', (['filename', '"""r"""'], {}), "(filename, 'r')\n", (670, 685), False, 'from gluon import recfile\n'), ((1163, 1204), 'gluon.recfile.open', 'recfile.open', (['filename', '"""r"""'], {'path': '"""tests"""'}), "(filename, 'r', path='tests')\n", (1175, 1204), False, 'from gluon import recfile\n'), ((1611, 1623), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (1621, 1623), False, 'import uuid\n'), ((1752, 1779), 'gluon.recfile.open', 'recfile.open', (['filename', '"""r"""'], {}), "(filename, 'r')\n", (1764, 1779), False, 'from gluon import recfile\n')]