import os
import unittest

from storages import metadata

class SchemeTest(unittest.TestCase):

    def test_create_scheme(self):
        
        scheme = metadata.Scheme()
        self.assertEqual(type(scheme), metadata.Scheme)


    def test_scheme_as_dict(self):

        scheme = metadata.Scheme()
        self.assertEqual(scheme.as_dict(), {'item_id': None, 'key': None, 'value': None})        


class MetadataTest(unittest.TestCase):

    def test_create_metadata(self):

        database_path = ':memory:'
        meta = metadata.Metadata(database_path)
        self.assertTrue(os.path.exists(database_path))

    def test_store_metadata(self):

        database_path = ':memory:'
        meta = metadata.Metadata(database_path)
        self.assertTrue(os.path.exists(database_path))

        self.assertRaises(RuntimeError, meta.put, None, None)
        self.assertRaises(RuntimeError, meta.put, 'a1', None)
        meta.put('b1', [('k1','v1'),])
        meta.put('b1', [('k1','v1'),('k2','v2')])
        meta.put('b1', [('k2','v3'),('k3','v2')])
        self.assertEqual(meta.get('b1'), ('b1', [('k1','v1'),('k2','v2'),('k2','v3'),('k3','v2')]))

        meta.delete('b1')
        self.assertEqual(meta.get('b1'), ('b1', []))


    def test_get_metadata(self):

        database_path = ':memory:'
        meta = metadata.Metadata(database_path)
        self.assertTrue(os.path.exists(database_path))

        meta.put('b1', [('k1','v1')])
        meta.put('b2', [('k2','v2')])
        meta.put('b3', [('k3','v3')])
        self.assertEqual(meta.get(), [(u'b1', [(u'k1', u'v1')]), (u'b2', [(u'k2', u'v2')]), (u'b3', [(u'k3', u'v3')])])

        meta.delete(['b1', 'b2', 'b3'])
        self.assertEqual(meta.count(),0)
        

    def test_search_metadata(self):

        database_path = ':memory:'
        meta = metadata.Metadata(database_path)
        self.assertTrue(os.path.exists(database_path))

        meta.put('b1', [('k1','v1')])
        meta.put('b2', [('k1','v2')])
        meta.put('b3', [('k1','v3')])

        self.assertEqual(meta.search('k1', 'v2'), ['b2'])
        


@@ -584,37 +584,37 @@
 rue(
-os.path.exists(database_path)
+type(meta), metadata.Metadata
 )%0A%0A 
@@ -734,63 +734,8 @@
 ath)
-%0A        self.assertTrue(os.path.exists(database_path))
 %0A%0A  
@@ -1297,63 +1297,8 @@
 ath)
-%0A        self.assertTrue(os.path.exists(database_path))
 %0A%0A  
@@ -1685,32 +1685,32 @@
 th = ':memory:'%0A
+
         meta = m
@@ -1744,63 +1744,8 @@
 ath)
-%0A        self.assertTrue(os.path.exists(database_path))
 %0A%0A  

remove old references to file based db