From 49a4c3c72d8b5556f2e3dd35c8e9491484ebfcb0 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 17 Mar 2008 22:37:37 +0000 Subject: [PATCH] Completed unit tests for notebook movement methods. --- controller/test/Test_controller.py | 2 + controller/test/Test_notebooks.py | 194 ++++++++++++++++++++++++++++- 2 files changed, 194 insertions(+), 2 deletions(-) diff --git a/controller/test/Test_controller.py b/controller/test/Test_controller.py index c457acc..4468d8b 100644 --- a/controller/test/Test_controller.py +++ b/controller/test/Test_controller.py @@ -79,6 +79,8 @@ class Test_controller( object ): continue notebooks.append( notebook ) + notebooks.sort( lambda a, b: a.rank is None and 1 or cmp( a.rank, b.rank ) ) + return notebooks User.sql_load_notebooks = lambda self, parents_only = False, undeleted_only = False, read_write = False: \ diff --git a/controller/test/Test_notebooks.py b/controller/test/Test_notebooks.py index 485c682..8068568 100644 --- a/controller/test/Test_notebooks.py +++ b/controller/test/Test_notebooks.py @@ -55,11 +55,31 @@ class Test_notebooks( Test_controller ): self.database.save( self.anon_notebook, commit = False ) self.database.execute( self.user.sql_save_notebook( self.notebook.object_id, read_write = True, owner = True, rank = 0 ) ) - self.database.execute( self.user.sql_save_notebook( self.notebook.trash_id, read_write = True, owner = True, rank = 0 ) ) + self.database.execute( self.user.sql_save_notebook( self.notebook.trash_id, read_write = True, owner = True ) ) self.database.execute( self.user.sql_save_notebook( self.anon_notebook.object_id, read_write = False, owner = False ) ) self.database.execute( self.user2.sql_save_notebook( self.notebook.object_id, read_write = True, owner = False, rank = 0 ) ) - self.database.execute( self.user2.sql_save_notebook( self.notebook.trash_id, read_write = True, owner = False, rank = 0 ) ) + self.database.execute( self.user2.sql_save_notebook( self.notebook.trash_id, read_write = True, owner = False ) ) + + def make_extra_notebooks( self ): + user_id = self.user.object_id + + self.trash2 = Notebook.create( self.database.next_id( Notebook ), u"trash", user_id = user_id ) + self.database.save( self.trash2, commit = False ) + self.notebook2 = Notebook.create( self.database.next_id( Notebook ), u"notebook", self.trash2.object_id, user_id = user_id ) + self.database.save( self.notebook2, commit = False ) + + self.trash3 = Notebook.create( self.database.next_id( Notebook ), u"trash", user_id = user_id ) + self.database.save( self.trash3, commit = False ) + self.notebook3 = Notebook.create( self.database.next_id( Notebook ), u"notebook", self.trash3.object_id, user_id = user_id ) + self.database.save( self.notebook3, commit = False ) + + self.database.execute( self.user.sql_save_notebook( self.notebook2.object_id, read_write = True, owner = True, rank = 1 ) ) + self.database.execute( self.user.sql_save_notebook( self.notebook2.trash_id, read_write = True, owner = True ) ) + self.database.execute( self.user.sql_save_notebook( self.notebook3.object_id, read_write = True, owner = True, rank = 2 ) ) + self.database.execute( self.user.sql_save_notebook( self.notebook3.trash_id, read_write = True, owner = True ) ) + + self.database.commit() def make_users( self ): self.user = User.create( self.database.next_id( User ), self.username, self.password, self.email_address ) @@ -2681,6 +2701,176 @@ class Test_notebooks( Test_controller ): assert notebook.trash_id assert notebook.user_id == self.user.object_id + def test_move_up( self ): + self.login() + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_up", dict( + notebook_id = self.notebook2.object_id, + ), session_id = self.session_id ) + + assert u"error" not in result + + notebooks = self.database.select_many( Notebook, self.user.sql_load_notebooks( parents_only = True, undeleted_only = True ) ) + + assert notebooks + assert len( notebooks ) == 3 + assert notebooks[ 0 ].object_id == self.notebook2.object_id + assert notebooks[ 0 ].rank == 0 + assert notebooks[ 1 ].object_id == self.notebook.object_id + assert notebooks[ 1 ].rank == 1 + assert notebooks[ 2 ].object_id == self.notebook3.object_id + assert notebooks[ 2 ].rank == 2 + + def test_move_up_and_wrap( self ): + self.login() + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_up", dict( + notebook_id = self.notebook.object_id, + ), session_id = self.session_id ) + + assert u"error" not in result + + notebooks = self.database.select_many( Notebook, self.user.sql_load_notebooks( parents_only = True, undeleted_only = True ) ) + + assert notebooks + assert len( notebooks ) == 3 + assert notebooks[ 0 ].object_id == self.notebook2.object_id + assert notebooks[ 0 ].rank == 1 + assert notebooks[ 1 ].object_id == self.notebook3.object_id + assert notebooks[ 1 ].rank == 2 + assert notebooks[ 2 ].object_id == self.notebook.object_id + assert notebooks[ 2 ].rank == 3 + + def test_move_up_without_login( self ): + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_up", dict( + notebook_id = self.notebook2.object_id, + ) ) + + assert u"access" in result[ u"error" ] + + def test_move_up_without_access( self ): + self.login2() + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_up", dict( + notebook_id = self.notebook2.object_id, + ), session_id = self.session_id ) + + assert u"access" in result[ u"error" ] + + def test_move_up_trash( self ): + self.login() + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_up", dict( + notebook_id = self.trash.object_id, + ), session_id = self.session_id ) + + assert u"access" in result[ u"error" ] + + def test_move_up_deleted_notebook( self ): + self.login() + self.make_extra_notebooks() + + self.http_post( "/notebooks/delete", dict( + notebook_id = self.notebook2.object_id, + ), session_id = self.session_id ) + + result = self.http_post( "/notebooks/move_up", dict( + notebook_id = self.notebook2.object_id, + ), session_id = self.session_id ) + + assert u"access" in result[ u"error" ] + + def test_move_down( self ): + self.login() + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_down", dict( + notebook_id = self.notebook2.object_id, + ), session_id = self.session_id ) + + assert u"error" not in result + + notebooks = self.database.select_many( Notebook, self.user.sql_load_notebooks( parents_only = True, undeleted_only = True ) ) + + assert notebooks + assert len( notebooks ) == 3 + assert notebooks[ 0 ].object_id == self.notebook.object_id + assert notebooks[ 0 ].rank == 0 + assert notebooks[ 1 ].object_id == self.notebook3.object_id + assert notebooks[ 1 ].rank == 1 + assert notebooks[ 2 ].object_id == self.notebook2.object_id + assert notebooks[ 2 ].rank == 2 + + def test_move_down_and_wrap( self ): + self.login() + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_down", dict( + notebook_id = self.notebook3.object_id, + ), session_id = self.session_id ) + + assert u"error" not in result + + notebooks = self.database.select_many( Notebook, self.user.sql_load_notebooks( parents_only = True, undeleted_only = True ) ) + + assert notebooks + assert len( notebooks ) == 3 + assert notebooks[ 0 ].object_id == self.notebook3.object_id + assert notebooks[ 0 ].rank == -1 + assert notebooks[ 1 ].object_id == self.notebook.object_id + assert notebooks[ 1 ].rank == 0 + assert notebooks[ 2 ].object_id == self.notebook2.object_id + assert notebooks[ 2 ].rank == 1 + + def test_move_down_without_login( self ): + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_down", dict( + notebook_id = self.notebook2.object_id, + ) ) + + assert u"access" in result[ u"error" ] + + def test_move_down_without_access( self ): + self.login2() + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_down", dict( + notebook_id = self.notebook2.object_id, + ), session_id = self.session_id ) + + assert u"access" in result[ u"error" ] + + def test_move_down_trash( self ): + self.login() + self.make_extra_notebooks() + + result = self.http_post( "/notebooks/move_down", dict( + notebook_id = self.trash.object_id, + ), session_id = self.session_id ) + + assert u"access" in result[ u"error" ] + + def test_move_down_deleted_notebook( self ): + self.login() + self.make_extra_notebooks() + + self.http_post( "/notebooks/delete", dict( + notebook_id = self.notebook2.object_id, + ), session_id = self.session_id ) + + result = self.http_post( "/notebooks/move_down", dict( + notebook_id = self.notebook2.object_id, + ), session_id = self.session_id ) + + assert u"access" in result[ u"error" ] + def test_recent_notes( self ): result = cherrypy.root.notebooks.load_recent_notes( self.notebook.object_id,