示例#1
0
class AnnotationDB_Test(unittest.TestCase):
    """
    Test for all of the basic dictionary functions on 'AnnotationDB'.
    """
    def setUp(self):
        class Annotation(object):
            def __init__(self, **kwargs):
                self.__dict__.update(kwargs)
        slicedb = dict(annot1=Annotation(id='seq', start=0, stop=10),
                       annot2=Annotation(id='seq', start=5, stop=9))
        sequence_dict = dict(seq = Sequence('ATGGGGCCGATTG', 'seq'))
        self.db = AnnotationDB(slicedb, sequence_dict)

    def test_keys(self):
        "AnnotationDB keys"
        k = self.db.keys()
        k.sort()
        assert k == ['annot1', 'annot2'], k

    def test_contains(self):
        "AnnotationDB contains"
        assert 'annot1' in self.db, self.db.keys()
        assert 'annot2' in self.db
        assert 'foo' not in self.db

    def test_has_key(self):
        "AnnotationDB has key"
        assert self.db.has_key('annot1')
        assert self.db.has_key('annot2')
        assert not self.db.has_key('foo')

    def test_get(self):
        "AnnotationDB get"
        assert self.db.get('foo') is None
        assert self.db.get('annot1') is not None
        assert str(self.db.get('annot1').sequence).startswith('ATGGGGC')
        assert self.db.get('annot2') is not None
        assert str(self.db.get('annot2').sequence).startswith('GCCG')
    
    def test_items(self):
        "AnnotationDB items"
        i = [ k for (k,v) in self.db.items() ]
        i.sort()
        assert i == ['annot1', 'annot2']
    
    def test_iterkeys(self):
        "AnnotationDB iterkeys"
        kk = self.db.keys()
        kk.sort()
        ik = list(self.db.iterkeys())
        ik.sort()
        assert kk == ik
    
    def test_itervalues(self):
        "AnnotationDB itervalues"
        kv = self.db.values()
        kv.sort()
        iv = list(self.db.itervalues())
        iv.sort()
        assert kv[0] == iv[0]
        assert kv == iv, (kv, iv)
    
    def test_iteritems(self):
        "AnnotationDB iteritems"
        ki = self.db.items()
        ki.sort()
        ii = list(self.db.iteritems())
        ii.sort()
        assert ki == ii, (ki, ii)
    
    def test_readonly(self):
        "AnnotationDB readonly"
        try:
            self.db.copy()              # what should 'copy' do on AD?
            assert 0, 'this method should raise NotImplementedError'
        except NotImplementedError:
            pass
        try:                           # what should 'setdefault' do on AD?
            self.db.setdefault('foo')
            assert 0, 'this method should raise NotImplementedError'
        except NotImplementedError:
            pass
        try:                           # what should 'update' do on AD?
            self.db.update({})
            assert 0, 'this method should raise NotImplementedError'
        except NotImplementedError:
            pass
        try:
            self.db.clear()
            assert 0, 'this method should raise NotImplementedError'
        except NotImplementedError:
            pass
        try:
            self.db.pop()
            assert 0, 'this method should raise NotImplementedError'
        except NotImplementedError:
            pass
        try:
            self.db.popitem()
            assert 0, 'this method should raise NotImplementedError'
        except NotImplementedError:
            pass
    
    def test_equality(self):
        "AnnotationDB equality"
        # Check that separately generated annotation objects test equal"
        key = 'annot1'
        db = self.db
        x = db.sliceAnnotation(key, db.sliceDB[key])
        y = db.sliceAnnotation(key, db.sliceDB[key])
        assert x == y
    
    def test_bad_seqdict(self):
        "AnnotationDB bad seqdict"
        class Annotation(object):
            def __init__(self, **kwargs):
                self.__dict__.update(kwargs)
        slicedb = dict(annot1=Annotation(id='seq', start=0, stop=10),
                       annot2=Annotation(id='seq', start=5, stop=9))
        foo_dict = dict(foo=Sequence('ATGGGGCCGATTG', 'foo'))
        try:
            db = AnnotationDB(slicedb, foo_dict)
            assert 0, "incorrect seqdb; key error should be raised"
        except KeyError:
            pass