import os
import unittest

import livejson


class testDatabase(unittest.TestCase):
    """ Test the magical JSON database class """
    def setUp(self):
        # Path to JSON file
        self.dbpath = "test_database.json"

    def test_dict_database(self):
        """ Test that databases in which the base object is a dict work """
        # Test that a blank database can be properly created
        db = livejson.Database(self.dbpath)
        self.assertTrue(os.path.exists(self.dbpath))
        with open(self.dbpath, "r") as f:
            self.assertEqual(f.read(), "{}")

        # Test writing to a database
        db["a"] = "b"

        # Test reading values from an existing database
        newInstance = livejson.Database(self.dbpath).data
        self.assertEqual(newInstance["a"], "b")

        # Test the methods inherited from collections.MutableMapping
        # Test 'get'
        self.assertEqual(db.get("a"), "b")
        self.assertIsNone(db.get("penguins"))
        self.assertEqual(db.get("penguins", "fallback"), "fallback")
        # Test '__contains__'
        self.assertIn("a", db)
        # Test '__eq__'
        self.assertEqual(db, {"a": "b"})

        # Test the extra API I added
        # Test 'data' (get a vanilla dict object)
        self.assertEqual(db.data, {"a": "b"})

    def test_list_database(self):
        """ Test that databases in which the base object is an array work """
        # Create the database. Automatically, a blank database has a dict at
        # the base. So we write "[]" into the file manually so that livejson
        # detects the databse as a ListDatabase
        with open(self.dbpath, "w") as f:
            f.write("[]")
        db = livejson.Database(self.dbpath)
        # Test append, extend, and insert
        db.append("dogs")
        db.extend(["cats", "penguins"])
        db.insert(0, "turtles")
        self.assertIsInstance(db.data, list)
        self.assertEqual(db.data, ["turtles", "dogs", "cats", "penguins"])

    def tearDown(self):
        os.remove(self.dbpath)

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

@@ -1994,16 +1994,937 @@
 ins%22%5D)%0A%0A
+    def test_nesting(self):%0A        %22%22%22 Test that you can also work with dicts and lists that appear inside%0A        the database, rather than as the top-level object %22%22%22%0A        my_db = livejson.Database(self.dbpath)%0A        my_db.cleardata()%0A        # Test nested dicts%0A        my_db%5B%22stored_data%22%5D = %7B%7D%0A        my_db%5B%22stored_data%22%5D%5B%22test%22%5D = %22value%22%0A        self.assertEqual(my_db.data, %7B%22stored_data%22: %7B%22test%22: %22value%22%7D%7D)%0A        # Test nested lists%0A        my_db%5B%22stored_data%22%5D = %5B%5D%0A        my_db%5B%22stored_data%22%5D.append(%22test%22)%0A        self.assertEqual(my_db.data, %7B%22stored_data%22: %5B%22test%22%5D%7D)%0A        # Test more complex multilevel nesting%0A        my_db%5B%22stored_data%22%5D = %5B%5D%0A        my_db%5B%22stored_data%22%5D.append(%7B%7D)%0A        my_db%5B%22stored_data%22%5D%5B0%5D%5B%22colors%22%5D = %5B%22green%22, %22purple%22%5D%0A        self.assertEqual(my_db.data,%0A                         %7B%22stored_data%22: %5B%7B%22colors%22: %5B%22green%22, %22purple%22%5D%7D%5D%7D%0A                         )%0A%0A
     def 

Add tests