import logging
import os
import shutil
import sys
import unittest

import fiona

logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

class ReadAccess(unittest.TestCase):
    # To check that we'll be able to get multiple 'r' connections to layers
    # in a single file.
    
    def setUp(self):
        self.c = fiona.open("docs/data/test_uk.shp", "r", layer="test_uk")
    
    def tearDown(self):
        self.c.close()

    def test_meta(self):
        with fiona.open("docs/data/test_uk.shp", "r", layer="test_uk") as c2:
            self.assertEqual(len(self.c), len(c2))
            self.assertEqual(sorted(self.c.schema.items()), sorted(c2.schema.items()))

    def test_meta(self):
        f1 = next(self.c)
        with fiona.open("docs/data/test_uk.shp", "r", layer="test_uk") as c2:
            f2 = next(c2)
            self.assertEqual(repr(f1), repr(f2))

class ReadWriteAccess(unittest.TestCase):
    # To check that we'll be able to read from a file that we're
    # writing to.
    
    def setUp(self):
        self.c = fiona.open(
            "/tmp/multi_write_test.shp",
            "w",
            driver="ESRI Shapefile",
            schema={
                'geometry': 'Point', 
                'properties': {'title': 'str', 'date': 'date'}},
            crs={'init': "epsg:4326", 'no_defs': True},
            encoding='utf-8')
        self.f = {
            'geometry': {'type': 'Point', 'coordinates': (0.0, 0.1)},
            'properties': {'title': 'point one', 'date': "2012-01-29"}}
        self.c.writerecords([self.f])
        self.c.flush()

    def tearDown(self):
        self.c.close()

    def test_meta(self):
        c2 = fiona.open("/tmp/multi_write_test.shp", "r")
        self.assertEqual(len(self.c), len(c2))
        self.assertEqual(sorted(self.c.schema.items()), sorted(c2.schema.items()))

    def test_read(self):
        c2 = fiona.open("/tmp/multi_write_test.shp", "r")
        f2 = next(c2)
        del f2['id']
        self.assertEqual(repr(self.f), repr(f2))

    def test_read_after_close(self):
        c2 = fiona.open("/tmp/multi_write_test.shp", "r")
        self.c.close()
        f2 = next(c2)
        del f2['id']
        self.assertEqual(repr(self.f), repr(f2))

