Commits

Rufus Pollock committed 1d94ba5

[model/collection][m]: new collection object with passing tests :-).

Comments (0)

Files changed (2)

openbiblio/model/collection.py

+from .base import *
+from .entry import Entry
+from .account import Account
+    
+class Collection(AnnotatibleTerms, DomainObject):
+    '''Collections of entries (books, articles etc).
+    '''
+    rdfclass = BIBO.Collection
+    label = predicate(RDFS.label)
+    # owner = object_predicate(
+    entries = object_predicate(RDFS.member, Entry)
+    namespace = 'http://bibliographica.org/collection/'
+    owner = object_predicate(BIBLIO_ONT.owner, Account)
+
+    def __init__(self, *av, **kw):
+        super(Collection, self).__init__(*av, **kw)
+        self.type = BIBO.Collection
+
+    @classmethod
+    def by_user(cls, user):
+        q = u"""
+        PREFIX bb: <http://bibliographica.org/onto#>
+
+        SELECT DISTINCT ?doc
+        WHERE { 
+          ?doc a bibo:Collection . 
+          ?doc bb:owner %s
+        }
+        """ % user.n3()
+        for collection, in handler.query(q):
+            return cls.get_by_uri(collection)
+

openbiblio/tests/test_model.py

 from ordf.term import URIRef
-# from openbiblio.tests import 
+
+from openbiblio.tests import delete_all
+from openbiblio import handler
 import openbiblio.model as model
 
 class TestAccount:
 
     @classmethod
     def teardown_class(self):
-        model.Account.purge(self.account_id)
+        delete_all()
 
     def test_01_get_null(self):
         out = model.Account.get_by_openid(self.openid + 'madeup')
         entry = model.Entry.get_by_uri(self.entry_id)
         assert entry.label[0] == self.label, entry.label
 
+
+class TestCollection:
+    entrylabel = 'my-test-entry'
+    label = 'my-test-collection'
+    openid = 'http://myopen.id/2'
+
+    @classmethod
+    def setup_class(self):
+        account = model.Account.create(self.openid)
+        self.account_id = account.identifier
+        entry = model.Entry.create()
+        entry.label = self.entrylabel
+
+        collection = model.Collection.create()
+        collection.label = self.label
+        collection.entries = [ entry ]
+        collection.owner = account
+
+        self.collection_id = collection.identifier
+        self.entry_id = entry.identifier
+
+        ctx = handler.context('ouruser', '')
+        ctx.add(account.graph)
+        ctx.add(collection.graph)
+        ctx.add(entry.graph)
+        ctx.commit()
+
+    @classmethod
+    def teardown_class(self):
+        delete_all()
+
+    def test_01_get(self):
+        collection = model.Collection.get_by_uri(self.collection_id)
+        assert collection.label[0] == self.label, collection.label
+        account = list(collection.owner)[0]
+        assert account.identifier == self.account_id, account
+        entries = list(collection.entries)
+        assert len(entries) == 1, entries
+
+    def test_02_by_user(self):
+        collection = model.Collection.by_user(self.account_id)
+        assert collection.label[0] == self.label, collection
+
+    def test_03_find(self):
+        out = model.Collection.find()
+        assert len(out) == 1, out
+        assert self.collection_id == out[0].identifier, out
+
+    def test_03_asdict(self):
+        pass
+