Esempio n. 1
0
def test_relationship_descriptor_one_to_ordered():
    class Entry:
        parent = None

    class Directory(Entry):
        contents = None

    class File(Entry):
        pass

    Entry_parent = one_relationship_descriptor('parent', Directory)
    Directory_contents = ordered_relationship_descriptor('contents', Entry)
    Entry_parent.initialize(Directory_contents)
    Directory_contents.initialize(Entry_parent)

    Entry.parent = Entry_parent
    Directory.contents = Directory_contents

    root = Directory()
    etc = Directory()
    usr = Directory()
    mnt = Directory()
    var = Directory()
    root.contents.append(etc)
    root.contents.append(usr)
    root.contents.append(mnt)
    root.contents.append(var)

    root.contents = []
    assert len(root.contents) == 0
    root.contents = [etc, usr, mnt, var]
    assert len(root.contents) == 4

    assert etc.parent == usr.parent == mnt.parent == var.parent == root
    assert list(root.contents) == [etc, usr, mnt, var]

    pwd = File()
    pwd.parent = etc
    bin, lib, man = Directory(), Directory(), Directory()
    bin.parent = man.parent = lib.parent = usr
    assert list(usr.contents) == [bin, man, lib]

    var.contents.append(lib)
    assert lib.parent == var
    assert lib in var.contents
    assert lib not in usr.contents

    etc.contents.append(File())
    assert len(etc.contents) == 2
    pwd.parent = var
    assert len(etc.contents) == 1

    with pytest.raises(ValueError):
        etc.contents.append(None)
    with pytest.raises(TypeError):
        etc.contents.append(15)
    with pytest.raises(TypeError):
        etc.contents.append("adsada")
    with pytest.raises(ValueError):
        etc.contents.remove(15)
Esempio n. 2
0
def test_relationship_descriptor_one_to_one():
    
    class Man:
        pass
    
    class Woman:
        pass 

    # configure
    Man.wife = one_relationship_descriptor('wife', Woman)
    Woman.husband = one_relationship_descriptor('husband', Man)
    Man.wife.initialize(Woman.husband)
    Woman.husband.initialize(Man.wife)
    
            
    mark = Man()
    
    tom = Man()
    
    mary = Woman()
    sally = Woman()
    
    assert mark.wife is None
    assert tom.wife is None
    assert mary.husband is None
    
    mark.wife = mary
    assert mary.husband is mark
    assert mark.wife is mary
    assert tom.wife is None
    
    sally.husband = tom
    assert tom.wife is sally
    
    mark.wife = sally
    assert mary.husband is None
    assert tom.wife is None
    assert mark.wife is sally 
    assert sally.husband is mark
    
    sally.husband = None
    assert tom.wife is None
    assert mark.wife is None

    with pytest.raises(ValueError):
        tom.wife = 1
Esempio n. 3
0
def test_relationship_descriptor_one_to_ordered():
    class Entry:
        parent=None
    class Directory(Entry):
        contents=None
    class File(Entry):
        pass
    
    Entry_parent = one_relationship_descriptor('parent', Directory)
    Directory_contents = ordered_relationship_descriptor('contents', Entry)
    Entry_parent.initialize(Directory_contents)
    Directory_contents.initialize(Entry_parent)

    Entry.parent = Entry_parent
    Directory.contents = Directory_contents
    
    root = Directory()
    etc = Directory()
    usr = Directory()
    mnt = Directory()
    var = Directory()
    root.contents.append(etc)
    root.contents.append(usr)
    root.contents.append(mnt)
    root.contents.append(var)
    
    root.contents = []
    assert len(root.contents)==0
    root.contents = [etc,usr,mnt,var]
    assert len(root.contents)==4

    assert etc.parent == usr.parent == mnt.parent == var.parent == root
    assert list(root.contents) == [etc, usr, mnt, var]

    pwd = File()
    pwd.parent=etc
    bin, lib, man = Directory(), Directory(), Directory()
    bin.parent = man.parent = lib.parent = usr
    assert list(usr.contents) == [bin, man, lib]
    
    var.contents.append(lib)
    assert lib.parent == var
    assert lib in var.contents
    assert lib not in usr.contents
    
    etc.contents.append(File())
    assert len(etc.contents)==2
    pwd.parent = var
    assert len(etc.contents)==1
    
    with pytest.raises(ValueError):
        etc.contents.append(None)        
    with pytest.raises(TypeError):
        etc.contents.append(15)
    with pytest.raises(TypeError):
        etc.contents.append("adsada")
    with pytest.raises(ValueError):
        etc.contents.remove(15)
Esempio n. 4
0
def test_relationship_descriptor_one_to_one():
    class Man:
        pass

    class Woman:
        pass

    # configure
    Man.wife = one_relationship_descriptor('wife', Woman)
    Woman.husband = one_relationship_descriptor('husband', Man)
    Man.wife.initialize(Woman.husband)
    Woman.husband.initialize(Man.wife)

    mark = Man()

    tom = Man()

    mary = Woman()
    sally = Woman()

    assert mark.wife is None
    assert tom.wife is None
    assert mary.husband is None

    mark.wife = mary
    assert mary.husband is mark
    assert mark.wife is mary
    assert tom.wife is None

    sally.husband = tom
    assert tom.wife is sally

    mark.wife = sally
    assert mary.husband is None
    assert tom.wife is None
    assert mark.wife is sally
    assert sally.husband is mark

    sally.husband = None
    assert tom.wife is None
    assert mark.wife is None

    with pytest.raises(ValueError):
        tom.wife = 1
Esempio n. 5
0
def test_relationship_descriptor_one_to_many():
    class Entry:
        def __init__(self,name):
            self.name = name
        def __repr__(self):
            if self.parent:
                return repr(self.parent)+"/"+self.name
            else:
                return self.name
    class Directory(Entry):
        pass
    class File(Entry):
        pass
    
    Entry.parent = one_relationship_descriptor('parent', Directory)
    Directory.contents = many_relationship_descriptor('contents', Entry)
    Entry.parent.initialize(Directory.contents)
    Directory.contents.initialize(Entry.parent)
    
    root = Directory("")
    etc = Directory("etc")
    usr = Directory("usr")
    mnt = Directory("mnt")
    var = Directory("var")
    root.contents.add(etc)
    root.contents.add(usr)
    root.contents.add(mnt)
    root.contents.add(var)

    root.contents = []
    assert len(root.contents)==0
    root.contents = [mnt,var]
    assert len(root.contents)==2
    root.contents.add(etc)
    root.contents.add(usr) 
    assert len(root.contents)==4

    assert etc.parent == usr.parent == mnt.parent == var.parent == root

    pwd = File("pwd")
    pwd.parent=etc
    bin, lib, man = Directory("bin"), Directory("lib"), Directory("man")
    bin.parent = man.parent = lib.parent = usr
    assert usr.contents == set([bin, man, lib])
    
    var.contents.add(lib)
    assert lib.parent == var
    assert lib in var.contents
    assert lib not in usr.contents
    
    etc.contents.add(File("f1"))
    etc.contents.add(File("f2"))
    etc.contents.add(File("f3"))

    with pytest.raises(TypeError):
        etc.contents.add(15)
    with pytest.raises(ValueError):
        etc.contents.add(None)
    with pytest.raises(TypeError):
        etc.contents.add("adsada")
    with pytest.raises(KeyError):
        etc.contents.remove(15)
    etc.contents.discard(14)
Esempio n. 6
0
def test_relationship_descriptor_one_to_many():
    class Entry:
        def __init__(self, name):
            self.name = name

        def __repr__(self):
            if self.parent:
                return repr(self.parent) + "/" + self.name
            else:
                return self.name

    class Directory(Entry):
        pass

    class File(Entry):
        pass

    Entry.parent = one_relationship_descriptor('parent', Directory)
    Directory.contents = many_relationship_descriptor('contents', Entry)
    Entry.parent.initialize(Directory.contents)
    Directory.contents.initialize(Entry.parent)

    root = Directory("")
    etc = Directory("etc")
    usr = Directory("usr")
    mnt = Directory("mnt")
    var = Directory("var")
    root.contents.add(etc)
    root.contents.add(usr)
    root.contents.add(mnt)
    root.contents.add(var)

    root.contents = []
    assert len(root.contents) == 0
    root.contents = [mnt, var]
    assert len(root.contents) == 2
    root.contents.add(etc)
    root.contents.add(usr)
    assert len(root.contents) == 4

    assert etc.parent == usr.parent == mnt.parent == var.parent == root

    pwd = File("pwd")
    pwd.parent = etc
    bin, lib, man = Directory("bin"), Directory("lib"), Directory("man")
    bin.parent = man.parent = lib.parent = usr
    assert usr.contents == set([bin, man, lib])

    var.contents.add(lib)
    assert lib.parent == var
    assert lib in var.contents
    assert lib not in usr.contents

    etc.contents.add(File("f1"))
    etc.contents.add(File("f2"))
    etc.contents.add(File("f3"))

    with pytest.raises(TypeError):
        etc.contents.add(15)
    with pytest.raises(ValueError):
        etc.contents.add(None)
    with pytest.raises(TypeError):
        etc.contents.add("adsada")
    with pytest.raises(KeyError):
        etc.contents.remove(15)
    etc.contents.discard(14)