fsdict / tests / fsdict_tests.py

# −*− coding: UTF−8 −*−
from __future__ import with_statement
from nose import with_setup
from nose.tools import nottest, assert_equal, assert_true, assert_false
from fsdict import FSDict
import pickle
from pickle import loads, dumps

def test_fsdict_pickles():
    "occasionally something goes wrong with work_dir in pickling"
    d1 = FSDict({'a':'b'})
    d2 = loads(dumps(d1))
    yield assert_equal, d1, d2
    yield assert_equal, d1.work_dir, d2.work_dir

def test_fsdict_loads():
    import pickle
    
    d = FSDict({'a':'b'})
    yield assert_true, (d.work_dir).exists()
    yield assert_true, (d.work_dir/'a').exists()
    yield assert_equal, d['a'], 'b'
    yield assert_true, 'a' in d
    yield assert_false, 'q' in d
    for key in d:
        yield assert_equal, key, 'a'
    for key, val in d.iteritems():
        yield assert_equal, key, 'a'
        yield assert_equal, val, 'b'
    for val in d.itervalues():
        yield assert_equal, val, 'b'
    yield assert_equal, d.values(), ['b']
    yield assert_equal, d['a'], 'b'
    pickle.dump('c', open(d.work_dir/'a', 'w'))
    yield assert_equal, d['a'], 'c'
    del(d['a'])
    yield assert_false, (d.work_dir/'a').exists()
    d.cleanup()
    yield assert_false, (d.work_dir).exists()
    e=FSDict()
    yield assert_true, e.work_dir.exists()
    f=FSDict()
    yield assert_true, f.work_dir.exists()
    e.cleanup()
    f.cleanup()
    yield assert_false, e.work_dir.exists()
    yield assert_false, f.work_dir.exists()

def test_fsdict_equals():
    """
    as far as equality goes, we want these things to equate as normal dicts
    """
    fsd = FSDict({'a':'b'})
    d1 = {'a':'b'}
    d2 = {'c':'b'}
    d3 = {'a':'c'}
    assert d1 == fsd
    assert d2 != fsd
    assert d3 != fsd
    assert "foo" != fsd
    
def test_fsdict_setdefault():
    """does setdefault work as expected?"""
    d = FSDict()
    yield assert_equal, d.setdefault('a',2), 2
    #sure, but did that REALLY go to the FS?
    yield assert_true, (d.work_dir/'a').exists()
    yield assert_equal, d.setdefault('a',3), 2
    d.cleanup()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.