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

[(74, 'unittest.main', 'unittest.main', False, 'import unittest\n'), (21, 'os.mkdir', 'os.mkdir', False, 'import os\n'), (24, 'shutil.rmtree', 'shutil.rmtree', False, 'import shutil\n'), (67, 'gluon.recfile.remove', 'recfile.remove', False, 'from gluon import recfile\n'), (33, 'gluon.recfile.exists', 'recfile.exists', False, 'from gluon import recfile\n'), (35, 'gluon.recfile.remove', 'recfile.remove', False, 'from gluon import recfile\n'), (36, 'gluon.recfile.exists', 'recfile.exists', False, 'from gluon import recfile\n'), (44, 'gluon.recfile.exists', 'recfile.exists', False, 'from gluon import recfile\n'), (46, 'gluon.recfile.remove', 'recfile.remove', False, 'from gluon import recfile\n'), (47, 'gluon.recfile.exists', 'recfile.exists', False, 'from gluon import recfile\n'), (55, 'gluon.recfile.exists', 'recfile.exists', False, 'from gluon import recfile\n'), (57, 'gluon.recfile.remove', 'recfile.remove', False, 'from gluon import recfile\n'), (58, 'gluon.recfile.exists', 'recfile.exists', False, 'from gluon import recfile\n'), (65, 'gluon.recfile.exists', 'recfile.exists', False, 'from gluon import recfile\n'), (68, 'gluon.recfile.exists', 'recfile.exists', False, 'from gluon import recfile\n'), (30, 'gluon.recfile.open', 'recfile.open', False, 'from gluon import recfile\n'), (41, 'gluon.recfile.open', 'recfile.open', False, 'from gluon import recfile\n'), (52, 'gluon.recfile.open', 'recfile.open', False, 'from gluon import recfile\n'), (66, 'gluon.recfile.open', 'recfile.open', False, 'from gluon import recfile\n'), (40, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (51, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (62, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (29, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (32, 'gluon.recfile.open', 'recfile.open', False, 'from gluon import recfile\n'), (43, 'gluon.recfile.open', 'recfile.open', False, 'from gluon import recfile\n'), (51, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (54, 'gluon.recfile.open', 'recfile.open', False, 'from gluon import recfile\n')]