From fbb5c57b8feffe56e0c119e6dece6e1e13e2f06b Mon Sep 17 00:00:00 2001 From: jvelilla Date: Tue, 16 Sep 2014 17:16:19 -0300 Subject: [PATCH] Update CMS_STORAGE interface. Added test cases. --- .../mysql/src/cms_storage_mysql.e | 3 +- .../mysql/src/provider/user_data_provider.e | 24 ++ .../mysql/tests/storage/storage_test_set.e | 323 ++++++++++++++++++ persistence/interface/cms_storage.e | 10 +- 4 files changed, 354 insertions(+), 6 deletions(-) create mode 100644 persistence/implementation/mysql/tests/storage/storage_test_set.e diff --git a/persistence/implementation/mysql/src/cms_storage_mysql.e b/persistence/implementation/mysql/src/cms_storage_mysql.e index c6279fe..b134f72 100644 --- a/persistence/implementation/mysql/src/cms_storage_mysql.e +++ b/persistence/implementation/mysql/src/cms_storage_mysql.e @@ -34,7 +34,8 @@ feature -- Access: user has_user: BOOLEAN -- Has any user? do - to_implement("Not implemented!!!") + Result := user_provider.has_user + post_user_provider_execution end diff --git a/persistence/implementation/mysql/src/provider/user_data_provider.e b/persistence/implementation/mysql/src/provider/user_data_provider.e index f66df25..e603c05 100644 --- a/persistence/implementation/mysql/src/provider/user_data_provider.e +++ b/persistence/implementation/mysql/src/provider/user_data_provider.e @@ -37,6 +37,12 @@ feature -- Status Report Result := db_handler.successful end + has_user: BOOLEAN + -- Has any user? + do + Result := count > 0 + end + feature -- Basic Operations new_user (a_user_name: READABLE_STRING_32; a_password: READABLE_STRING_32; a_email: READABLE_STRING_32) @@ -128,6 +134,21 @@ feature -- Basic Operations post_execution end + count: INTEGER + -- Number of items users. + local + l_parameters: STRING_TABLE [ANY] + do + log.write_information (generator + ".count") + create l_parameters.make (0) + db_handler.set_query (create {DATABASE_QUERY}.data_reader (select_count, l_parameters)) + db_handler.execute_query + if db_handler.count = 1 then + Result := db_handler.read_integer_32 (1) + end + post_execution + end + feature -- New Object fetch_user: CMS_USER @@ -149,6 +170,9 @@ feature -- New Object feature -- Sql Queries + Select_count: STRING = "select count(*) from Users;" + -- Number of users. + Select_user_by_id: STRING = "select * from Users where id =:id;" -- Retrieve user by id if exists. diff --git a/persistence/implementation/mysql/tests/storage/storage_test_set.e b/persistence/implementation/mysql/tests/storage/storage_test_set.e new file mode 100644 index 0000000..8a02131 --- /dev/null +++ b/persistence/implementation/mysql/tests/storage/storage_test_set.e @@ -0,0 +1,323 @@ +note + description: "Summary description for {STORAGE_TEST_SET}." + author: "" + date: "$Date$" + revision: "$Revision$" + +class + STORAGE_TEST_SET + +inherit + EQA_TEST_SET + redefine + on_prepare, + on_clean + select + default_create + end + ABSTRACT_DB_TEST + rename + default_create as default_db_test + end + + +feature {NONE} -- Events + + on_prepare + -- + do + (create {CLEAN_DB}).clean_db(connection) + end + + on_clean + -- + do + end + +feature -- Test routines + + test_has_user + do + assert ("Not has user", not storage.has_user) + end + + test_all_users + do + assert ("to implement all_users", False) + end + + test_user_by_id_not_exist + do + assert ("User does not exist", storage.user_by_id (1) = Void) + end + + test_user_by_name_not_exist + do + assert ("User does not exist", storage.user_by_name ("test") = Void) + end + + test_user_by_email_not_exist + do + assert ("User does not exist", storage.user_by_name ("test@test.com") = Void) + end + + test_user_with_bad_id + local + l_retry: BOOLEAN + l_user: detachable CMS_USER + do + if not l_retry then + l_user := storage.user_by_id (0) + assert ("Precondition does not get the wrong value", False) + else + assert ("Expected precondition violation", True) + end + rescue + l_retry := True + retry + end + + test_user_with_bad_name_empty + local + l_retry: BOOLEAN + l_user: detachable CMS_USER + do + if not l_retry then + l_user := storage.user_by_name ("") + assert ("Precondition does not get the wrong value", False) + else + assert ("Expected precondition violation", True) + end + rescue + l_retry := True + retry + end + + test_user_with_bad_email_empty + local + l_retry: BOOLEAN + l_user: detachable CMS_USER + do + if not l_retry then + l_user := storage.user_by_email ("") + assert ("Precondition does not get the wrong value", False) + else + assert ("Expected precondition violation", True) + end + rescue + l_retry := True + retry + end + + test_save_user + do + storage.save_user (default_user) + assert ("Has user", storage.has_user) + end + + test_user_by_id + do + storage.save_user (default_user) + assert ("Has user", storage.has_user) + if attached {CMS_USER} storage.user_by_id (1) as l_user then + assert ("Exist", True) + assert ("User test", l_user.name ~ "test") + assert ("User id = 1", l_user.id = 1) + else + assert ("Wrong Implementation", False) + end + end + + test_user_by_name + do + storage.save_user (default_user) + assert ("Has user", storage.has_user) + if attached {CMS_USER} storage.user_by_name ("test") as l_user then + assert ("Exist", True) + assert ("User nane: test", l_user.name ~ "test") + else + assert ("Wrong Implementation", False) + end + end + + test_user_by_email + do + storage.save_user (default_user) + assert ("Has user", storage.has_user) + if attached {CMS_USER} storage.user_by_email ("test@test.com") as l_user then + assert ("Exist", True) + assert ("User email: test@test.com", l_user.email ~ "test@test.com") + else + assert ("Wrong Implementation", False) + end + end + + test_invalid_credential + do + storage.save_user (default_user) + assert ("Has user test", attached storage.user_by_name ("test")) + assert ("Wrong password", not storage.is_valid_credential ("test", "test")) + assert ("Wrong user", not storage.is_valid_credential ("test1", "test")) + end + + test_valid_credential + do + storage.save_user (default_user) + assert ("Has user test", attached storage.user_by_name ("test")) + assert ("Valid password", storage.is_valid_credential ("test", "password")) + end + + test_recent_nodes_empty + do + assert ("No recent nodes", storage.recent_nodes (0, 10).is_empty) + end + + test_recent_nodes + local + l_nodes: LIST[CMS_NODE] + do + across 1 |..| 10 as c loop + storage.save_node (custom_node ("Content_" + c.item.out, "Summary_" + c.item.out, "Title_" + c.item.out)) + end + l_nodes := storage.recent_nodes (0, 10) + assert ("10 recent nodes", l_nodes.count = 10) + assert ("First node id=10", l_nodes.first.id = 10) + assert ("Last node id=1", l_nodes.last.id = 1) + + + l_nodes := storage.recent_nodes (5, 10) + assert ("5 recent nodes", l_nodes.count = 5) + assert ("First node id=5", l_nodes.first.id = 5) + assert ("Last node id=1", l_nodes.last.id = 1) + + l_nodes := storage.recent_nodes (9, 10) + assert ("1 recent nodes", l_nodes.count = 1) + assert ("First node id=1", l_nodes.first.id = 1) + assert ("Last node id=1", l_nodes.last.id = 1) + + l_nodes := storage.recent_nodes (10, 10) + assert ("Is empty", l_nodes.is_empty) + end + + test_node_does_not_exist + local + l_nodes: LIST[CMS_NODE] + do + across 1 |..| 10 as c loop + storage.save_node (custom_node ("Content_" + c.item.out, "Summary_" + c.item.out, "Title_" + c.item.out)) + end + assert ("Not exist node id: 12", storage.node (12) = Void) + end + + test_node + local + l_nodes: LIST[CMS_NODE] + do + across 1 |..| 10 as c loop + storage.save_node (custom_node ("Content_" + c.item.out, "Summary_" + c.item.out, "Title_" + c.item.out)) + end + assert ("Node id: 10", attached storage.node (10) as l_node and then l_node.title ~ "Title_10" ) + end + + test_update_node + local + l_nodes: LIST[CMS_NODE] + l_node: CMS_NODE + do + storage.save_node (custom_node ("Content", "Summary", "Title")) + if attached {CMS_NODE} storage.node (1) as ll_node then + l_node := ll_node.twin + l_node.set_content ("New Content") + l_node.set_summary ("New Summary") + l_node.set_title("New Title") + + storage.update_node (l_node) + assert ("Updated", attached {CMS_NODE} storage.node (1) as u_node and then not (u_node.title ~ ll_node.title) and then not (u_node.content ~ ll_node.content) and then not (u_node.summary ~ ll_node.summary)) + end + end + + test_update_node_title + local + l_nodes: LIST[CMS_NODE] + l_node: CMS_NODE + do + storage.save_node (custom_node ("Content", "Summary", "Title")) + if attached {CMS_NODE} storage.node (1) as ll_node then + storage.update_node_title (ll_node.id, "New Title") + assert ("Updated", attached {CMS_NODE} storage.node (1) as u_node and then not (u_node.title ~ ll_node.title) and then u_node.content ~ ll_node.content and then u_node.summary ~ ll_node.summary) + end + end + + test_update_node_summary + local + l_nodes: LIST[CMS_NODE] + l_node: CMS_NODE + do + storage.save_node (custom_node ("Content", "Summary", "Title")) + if attached {CMS_NODE} storage.node (1) as ll_node then + storage.update_node_summary (ll_node.id, "New Summary") + assert ("Updated", attached {CMS_NODE} storage.node (1) as u_node and then u_node.title ~ ll_node.title and then u_node.content ~ ll_node.content and then not (u_node.summary ~ ll_node.summary)) + end + end + + test_update_node_content + local + l_nodes: LIST[CMS_NODE] + l_node: CMS_NODE + do + storage.save_node (custom_node ("Content", "Summary", "Title")) + if attached {CMS_NODE} storage.node (1) as ll_node then + storage.update_node_content (ll_node.id, "New Content") + assert ("Updated", attached {CMS_NODE} storage.node (1) as u_node and then u_node.title ~ ll_node.title and then not (u_node.content ~ ll_node.content) and then u_node.summary ~ ll_node.summary) + end + end + + test_delete_node + local + l_nodes: LIST[CMS_NODE] + do + across 1 |..| 10 as c loop + storage.save_node (custom_node ("Content_" + c.item.out, "Summary_" + c.item.out, "Title_" + c.item.out)) + end + assert ("Exist node id: 10", attached storage.node (10) as l_node and then l_node.title ~ "Title_10" ) + storage.delete_node (10) + assert ("Not exist node id: 10", storage.node (10) = Void) + end + + + +feature {NONE} -- Implementation + + storage: CMS_STORAGE + -- Storage + once + create {CMS_STORAGE_MYSQL}Result.make (connection) + end + +feature {NONE} -- Fixture Factory: Users + + default_user: CMS_USER + do + Result := custom_user ("test", "password", "test@test.com") + end + + custom_user (a_name, a_password, a_email: READABLE_STRING_32): CMS_USER + do + create Result.make (a_name) + Result.set_password (a_password) + Result.set_email (a_email) + end + +feature {NONE} -- Fixture Factories: Nodes + + default_node: CMS_NODE + do + Result := custom_node ("Default content", "default summary", "Default") + end + + custom_node (a_content, a_summary, a_title: READABLE_STRING_32): CMS_NODE + do + create Result.make (a_content, a_summary, a_title) + end + +end diff --git a/persistence/interface/cms_storage.e b/persistence/interface/cms_storage.e index 5085328..22ea3a1 100644 --- a/persistence/interface/cms_storage.e +++ b/persistence/interface/cms_storage.e @@ -36,7 +36,7 @@ feature -- Access: user deferred ensure same_id: Result /= Void implies Result.id = a_id - no_password: Result /= Void implies Result.password = Void + password: Result /= Void implies Result.password /= Void end user_by_name (a_name: like {CMS_USER}.name): detachable CMS_USER @@ -44,13 +44,15 @@ feature -- Access: user a_name /= Void and then not a_name.is_empty deferred ensure - no_password: Result /= Void implies Result.password = Void + same_name: Result /= Void implies a_name ~ Result.name + password: Result /= Void implies Result.password /= Void end user_by_email (a_email: like {CMS_USER}.email): detachable CMS_USER deferred ensure - no_password: Result /= Void implies Result.password = Void + same_email: Result /= Void implies a_email ~ Result.email + password: Result /= Void implies Result.password /= Void end is_valid_credential (u, p: READABLE_STRING_32): BOOLEAN @@ -61,8 +63,6 @@ feature -- Change: user save_user (a_user: CMS_USER) deferred - ensure - a_user_password_is_encoded: a_user.password = Void end feature -- Access: roles and permissions