Example #1
0
 def eq_dumps(self, root_type, data):
     db = jsondb.create(data)
     dbpath = db.get_path()
     db.close()
     db = jsondb.load(dbpath)
     eq_(db.dumps(), json.dumps(data))
     eq_(db.data(), data)
Example #2
0
 def setup(self):
     self.obj = {
         "glossary": {
             "title": "example glossary",
             "persons": [{"name": "foo", "tag": ["a", "B", 1]}, {"name": "bar", "tag": ["b", "B", 2]}],
             "GlossDiv": {
                 "title": "S",
                 "GlossList": {
                     "GlossEntry": {
                         "ID": "SGML",
                         "SortAs": "SGML",
                         "GlossTerm": "Standard Generalized Markup Language",
                         "Acronym": "SGML",
                         "Abbrev": "ISO 8879:1986",
                         "GlossDef": {
                             "para": "A meta-markup language, used to create markup languages such as DocBook.",
                             "GlossSeeAlso": ["GML", "XML"],
                         },
                         "GlossSee": "markup",
                     }
                 },
             },
         }
     }
     self.db = jsondb.create(self.obj)
     self.db.dumprows()
Example #3
0
    def setup(self):
        self.obj = {'Obj':[
            {
                'name': 'foo',
                'description': 'FOO',
                'parenta': 'foo.parent',
                'parent': 'foo.parent',
                'type': 'a',
                'domain': 'a',
                'shadow': {
                    '@__link__': '$.Obj[?(@.name == "bar")].description',
                }
            },
            {
                'name': 'bar',
                'description': 'BAR',
                'parenta': 'bar.parent',
                'parent': 'bar.parent',
                'type': 'b',
                'domain': 'b',
                'shadow': {
                    '@__link__': '$.Obj[?(@.name == "foo")].description',
                }
            }

        ]}

        self.db = jsondb.create(self.obj)
Example #4
0
def test_cxt():
    with jsondb.create({'name':'foo'}) as db:
        eq_(db['$.name'].data(), 'foo')
        try:
            db[True]
        except jsondb.UnsupportedOperation:
            pass
Example #5
0
 def test_list_create(self):
     """test list"""
     data = ['hello', 'world!', [1, 2.0]]
     db = jsondb.create(data)
     db.close()
     dbpath = db.get_path()
     db = jsondb.load(dbpath)
     eq_(db.data(), data)
Example #6
0
 def test_list(self):
     """test list"""
     data = ['hello', 'world!', [1, 2.0]]
     db = jsondb.create([])
     for x in data:
         db.feed(x)
     db.close()
     dbpath = db.get_path()
     db = jsondb.load(dbpath)
     eq_(db.data(), data)
Example #7
0
def test_large():
    db = jsondb.create(url='large.db')
    for i in range(1000):
        li = db.feed({str(i):{'value':str(i)}})

    db.close()

    db = jsondb.load('large.db')
    rslt = db.query('$.15.value').getone().data()
    eq_(rslt, str(15))
Example #8
0
def test_sample():
    import jsondb
    
    # Create from nothing
    db = jsondb.create({})

    # Insert
    db['name'] = 'foo'

    db['friends'] = []
    for i in range(3):
        db['friends'].append({
            'id' : i,
            'name': chr(97 + i),
        })

    # It works like an ordinary dict
    assert db['name'] == 'foo'
    assert db['friends'][0]['id'] == 0
    assert len(db['friends']) == 3
    assert db.get('nonexists', 'notfound') == 'notfound'

    # Get the *real* data
    assert db.data() == {
        'name' : 'foo',
        'friends': [
            {'id' : 0, 'name' : 'a'},
            {'id' : 1, 'name' : 'b'},
            {'id' : 2, 'name' : 'c'},
        ]
    }
   
    # Query using jsonquery
    db.query('$.name').getone() == 'foo'

    # Iterating the query result
    #   => "a b c"
    for x in db.query('$.friends.name'):
        print x.data(),

    # Conditonal query
    eq_(db.query('$.friends[?(@.id = 1)].name').getone(), 'b')


    friends = db['friends']
    eq_(friends.query('$.name').values(), ['a', 'b', 'c'])
Example #9
0
    def test_list_merge(self):
        """merge into a list"""

        data = ['initial item', 'added item1', 'item 2', 'item3-key']

        db = jsondb.create({})
        _list_id = db.feed({'root' : data[:1]})[0]
        db.feed(data[1], _list_id)
        for x in data[2:]:
            db.feed(x, _list_id)
        db.close()
        dbpath = db.get_path()

        db = jsondb.load(dbpath)

        path = '$.root'
        rslt = db.query('$.root').values()
        eq_(rslt, [data])
Example #10
0
    def setup(self):
        """test dict"""
        db = jsondb.create({})
        files = ['xxx.py', 345, None, True, 'wtf', {'foo' : ['f1', 'f2']}]
        _id = db.feed({'name': []})[0]
        db.feed({'files': files}, _id)
        db.feed({
            'bloon': "type",
            'crazy': '2'}, _id)
        db.feed({
            'bloon': "well!",
            'crazy': '4'}, _id)

        h_dom = db.feed({'dom' : []})[0]
        db.feed({'love': 1}, h_dom)
        db.close()
        dbpath = db.get_path()

        self.db = jsondb.load(dbpath)
        self.files = files
Example #11
0
def test_sample():
    import jsondb

    # Create from nothing
    db = jsondb.create({})

    # Insert
    db["name"] = "foo"

    db["items"] = []
    for i in range(3):
        db["items"].append({"id": i, "name": chr(97 + i)})

    # It works like an ordinary dict
    assert db["name"] == "foo"
    assert db["items"][0]["id"] == 0
    assert len(db["items"]) == 3
    assert db.get("nonexists", "notfound") == "notfound"

    # Get the *real* data
    assert db.data() == {
        "name": "foo",
        "items": [{"id": 0, "name": "a"}, {"id": 1, "name": "b"}, {"id": 2, "name": "c"}],
    }

    # Query using jsonquery
    db.query("$.name").getone() == "foo"

    # Iterating the query result
    #   => "a b c"
    for x in db.query("$.items.name"):
        print x.data(),

    # Conditonal query
    eq_(db.query("$.items[?(@.id = 1)].name").getone(), "b")

    items = db["items"]
    eq_(items.query("$.name").values(), ["a", "b", "c"])
Example #12
0
 def test_create(self):
     fpath = os.path.join(os.path.dirname(__file__), 'bookstore.json')
     db = jsondb.create(self.obj)
     eq_(db.dumps(), self.db.dumps())
Example #13
0
def test_from_file():
    db = jsondb.create({"foo": {}})
    db["foo"].from_file(os.path.join(os.path.dirname(__file__), "bookstore.json"))
    path = '$.store.book[?(@.author == "Evelyn Waugh")].title'
    foo = db["foo"]
    eq_(foo.query(path).values(), ["Sword of Honour"])
Example #14
0
 def setup(self):
     self.obj = 999.99
     self.db = jsondb.create(self.obj)