import unittest
import io
from json import loads

from fridge import Fridge


class FridgeTest(unittest.TestCase):

    def setUp(self):
        self.buf = io.StringIO()

    def rewind(self):
        self.buf.seek(0)

    def test_file(self):
        with Fridge(file=self.buf) as fridge:
            pass
        self.rewind()
        with Fridge(file=self.buf) as fridge:
            pass
        self.assertEqual(self.buf.getvalue(), '{}')

    def test_insertion(self):
        with Fridge(file=self.buf) as fridge:
            fridge['a'] = 'a'
        self.assertEqual(self.buf.getvalue(), '{"a": "a"}')
        self.rewind()
        with Fridge(file=self.buf) as fridge:
            fridge['b'] = 'b'
            fridge['a'] = 'new_a'
        self.assertEqual(loads(self.buf.getvalue()), {'a': 'new_a', 'b': 'b'})

    def test_update(self):
        with Fridge(file=self.buf) as fridge:
            fridge['n'] = 2
            fridge['l'] = []
        self.assertEqual(loads(self.buf.getvalue()), {'n': 2, 'l': []})
        self.rewind()
        with Fridge(file=self.buf) as fridge:
            fridge['n'] += 1
            fridge['l'].append('str')
        self.assertEqual(loads(self.buf.getvalue()), {'n': 3, 'l': ['str']})

    def test_deletion(self):
        with Fridge(file=self.buf) as fridge:
            fridge['a'] = 'a'
            fridge['b'] = 'b'
            fridge['c'] = 'c'
        self.rewind()
        with Fridge(file=self.buf) as fridge:
            fridge['b'] = 'new_b'
            del fridge['c']
        self.assertEqual(loads(self.buf.getvalue()), {'a': 'a', 'b': 'new_b'})
    
    def test_load(self):
        with Fridge(file=self.buf) as fridge:
            fridge['a'] = 'a'
            self.assertEqual(fridge, {'a': 'a'})
            self.buf.truncate(0)
            self.rewind()
            fridge.load()
            self.assertEqual(fridge, {})

    def test_types(self):
        with Fridge(file=self.buf) as fridge:
            fridge['str'] = 'a'
            fridge['int'] = 42
            fridge['float'] = 3.14
            fridge['list'] = [{'num': 2}, [], 8.65]
        self.rewind()
        with Fridge(file=self.buf) as fridge:
            data = {'str': 'a',
                    'int': 42,
                    'float': 3.14,
                    'list': [{'num': 2}, [], 8.65]}
            self.assertEqual(fridge, data)

    def test_close(self):
        fridge = Fridge(file=self.buf)
        fridge['a'] = 'a'
        fridge.close()
        self.assertTrue(fridge.closed)
        self.assertFalse(self.buf.closed)
        self.assertEqual(self.buf.getvalue(), '{"a": "a"}')
        with self.assertRaises(ValueError):
            fridge.load()
        with self.assertRaises(ValueError):
            fridge.save()

@@ -13,18 +13,8 @@
 est%0A
-import io%0A
 from
@@ -31,16 +31,103 @@
 rt loads
+%0Atry:%0A    from StringIO import StringIO%0Aexcept ImportError:%0A    from io import StringIO
 %0A%0Afrom f
@@ -199,32 +199,32 @@
 ef setUp(self):%0A
+
         self.buf
@@ -230,11 +230,8 @@
 f = 
-io.
 Stri

Fix tests for python 2