1. dan mackinlay
  2. fsdict


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'
    yield assert_false, (d.work_dir/'a').exists()
    yield assert_false, (d.work_dir).exists()
    yield assert_true, e.work_dir.exists()
    yield assert_true, f.work_dir.exists()
    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