Commits

Alan Franzoni committed ceaaffe

Enh: better examples.

Comments (0)

Files changed (2)

ep2011/repository.py

 
 
 class HardcodedFSBasedUserRepo(object):
-    PERSISTENCE_DIR = "/tmp/"
+    PERSISTENCE_DIR = "/this_dir_should_be_configured"
     def save(self, user):
         with open(self.PERSISTENCE_DIR + str(user.id), "w") as f:
             dump(user, f)
 from twisted.python.filepath import FilePath
 
 class RefactoredHardcodedRepo(object):
-    PERSISTENCE_DIR = "/tmp/"
+    PERSISTENCE_DIR = "/this_dir_should_be_configured"
     base_path = FilePath(PERSISTENCE_DIR)
 
     def save(self, user):
             dump(user, f)
 
     def load_by_id(self, user_id):
-        pass
+        with self.base_path.child(str(user_id)).open() as f:
+            return load(f)

ep2011/test/test_repository.py

     def tearDown(self):
         rmtree(self.tmpdir)
 
-
-    # patching __builtin__.open can have side effects out of our control.
     @patch("ep2011.repository.open", create=True)
     def test_repository_can_retrieve_saved_object(self, mock_open):
         persist = open(self.tmpdir + os.sep + "persist", "w")
 
     @patch("ep2011.repository.open", create=True)
     def test_refactored_repository_saves_objects(self, mock_open):
-        t = TemporaryFile("w+")
-        mock_open.return_value = t
+        persist = open(self.tmpdir + os.sep + "persist", "w")
+        mock_open.return_value = persist
         repo = RefactoredHardcodedRepo()
-        repo.save(MockUser())
-        # assertion about usage - 
-        mock_open.assert_called_with(repo.PERSISTENCE_DIR + "123", "w")
+        user = MockUser(123)
+        repo.save(user)
+        mock_open.return_value = open(self.tmpdir + os.sep + "persist")
+        load_user = repo.load_by_id(123)
+        self.assertEquals(user, load_user)