Commits

Jesper Nøhr committed 85d3492

support sub-serialization of DictAbstract's

  • Participants
  • Parent commits f0e5992

Comments (0)

Files changed (2)

src/rewsfeed/models.py

+import copy
 import redis
 
 try:
     FOLLOWERS_KEY = 'f+'
     BLOCKS_KEY = 'B+'
     BLOCKED_KEY = 'b+'
+    FROBNICATE = 'FROB+'
     
 class Network(object):
     def __init__(self, network_name, host='127.0.0.1', port=6379, password=None, db=0):
         self.network = network
         self.kw = kwargs
 
+        for k, v in self.kw.iteritems():
+            if isinstance(v, basestring) and v.startswith(Op.FROBNICATE):
+                self.kw[k] = fac.class_from_prefix(v[5:7])\
+                    (self.network, **self.network.hgetall(Op.DATA_KEY+v[5:]))
+        
     def save(self):
-        self.network.hmset(Op.DATA_KEY+self.key(), self.kw)
+        dc = copy.copy(self.kw)
+
+        def frobnicate(d):
+            for k, v in d.iteritems():
+                if isinstance(v, DictAbstracted):
+                    d[k] = Op.FROBNICATE+v.key()
+                elif isinstance(v, dict):
+                    d[k] = frobnicate(d[k])
+            return d
+        
+        self.network.hmset(Op.DATA_KEY+self.key(), frobnicate(dc))
 
         return self
 
     # --
 
     def __repr__(self):
-        return '<'+self.__class__.__name__+': '+', '.join([ '%s=%s' % (k, v[0:255]) for k, v in self.kw.iteritems() ])+'>'
+        return '<'+self.__class__.__name__+': '+', '.join([ '%s=%s' % (k, str(v)[0:255])
+                                                            for k, v in self.kw.iteritems() ])+'>'
 
 class FollowAbleConstruct(object):
     def __init__(self):
         for who in self.repo.followers():
             self.assertTrue(repr(who).startswith("<User"))
             self.assertTrue(isinstance(who, User))
+
+class TestUsersAndRepos(unittest.TestCase):
+    def setUp(self):
+        self.n = Network('events')
+        self.n.conn.flushdb()
+
+        self.u1 = User(self.n, username='user1')
+        self.u1.save()
+
+        self.u2 = User(self.n, username='user2')
+        self.u2.save()
+
+        self.repos = [ ]
+        
+        for idx, rs in enumerate(('test1', 'test2', 'test3', 'test4', 'test5',
+                                  'test6', 'test7', 'test8', 'test9', 'test10',
+                                  'test11', 'test12', 'test13', 'test14', 'test15',
+                                  'test16', 'test17', 'test18', 'test19', 'test20')):
+            self.repos.append(Repository(self.n, username='user1', slug=rs, idx=str(idx)))
+            self.repos[-1].save()
             
+            if (idx % 3) == 0:
+                self.u1.follow(self.repos[-1])
+            elif (idx % 4) == 0:
+                self.u2.follow(self.repos[-1])
+
+    def test_list_follows(self):
+        for repo in self.u1.following():
+            self.assertTrue(int(repo.idx) % 3 == 0)
+
+        for repo in self.u2.following():
+            self.assertTrue(int(repo.idx) % 4 == 0)
+
+    def test_repo_events(self):
+        num_events = 100
+        
+        for i in xrange(1, num_events):
+            for idx, repo in enumerate(self.repos):
+                BaseEvent.add_event(self.n, repo,
+                                    timestamp=random.randint(0, sys.maxint),
+                                    pk=str(i)+'/'+str(idx),
+                                    repository=repo)
+
+        for event in self.u1.newsfeed(BaseEvent):
+            self.assertTrue(isinstance(event.repository, Repository))
+                
 if __name__ == "__main__":
     unittest.main()