Updated Database Query error handling.

Add support for user profiles.
Updated test case.
This commit is contained in:
jvelilla
2014-09-18 18:05:20 -03:00
parent 5d551f7fe1
commit cf3c4060c8
4 changed files with 132 additions and 5 deletions

View File

@@ -8,7 +8,7 @@ class
inherit
SHARED_LOGGER
SHARED_ERROR
REFACTORING_HELPER
@@ -42,6 +42,7 @@ feature -- Intialization
a_base_selection.load_result
Result := a_base_selection.container
else
set_last_error (a_base_selection.error_message_32, generator + ".execute_reader" )
log.write_error (generator + "." + a_base_selection.error_message_32)
end
unset_map_name (a_base_selection)
@@ -55,6 +56,11 @@ feature -- Intialization
set_map_name (a_base_change)
a_base_change.set_query (query)
a_base_change.execute_query
if a_base_change.is_ok then
else
set_last_error (a_base_change.error_message_32, generator + ".execute_reader" )
log.write_error (generator + "." + a_base_change.error_message_32)
end
unset_map_name (a_base_change)
end

View File

@@ -149,6 +149,8 @@ feature -- Basic Operations
post_execution
end
feature -- Basic operations: User Roles
add_role (a_user_id: INTEGER; a_role_id: INTEGER)
-- Add Role `a_role_id' to user `a_user_id'
local
@@ -177,6 +179,72 @@ feature -- Basic Operations
post_execution
end
feature -- Basic operations: User Profiles
save_profile_item (a_user_id: INTEGER_64; a_key: READABLE_STRING_32; a_value: READABLE_STRING_32)
-- Save a profile item with (a_key and a_value) to the given user `user_id'.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".save_profile_item")
create l_parameters.make (3)
l_parameters.put (a_key, "key")
l_parameters.put (a_value, "value")
l_parameters.put (a_user_id, "users_id")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (Select_instert_profile_item, l_parameters))
db_handler.execute_change
post_execution
end
save_profile (a_user_id: INTEGER_64; a_user_profile: CMS_USER_PROFILE)
-- Save a profile item with (a_key and a_value) to the given user `user_id'.
local
l_cursor: TABLE_ITERATION_CURSOR [READABLE_STRING_8, READABLE_STRING_8]
do
log.write_information (generator + ".save_profile")
from
l_cursor := a_user_profile.new_cursor
until
l_cursor.after
loop
save_profile_item (a_user_id, l_cursor.key, l_cursor.item)
l_cursor.forth
end
post_execution
end
user_profile (a_user_id: INTEGER_64): CMS_USER_PROFILE
-- User profile for a user with id `a_user_id'.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".user_profile")
create l_parameters.make (1)
l_parameters.put (a_user_id, "users_id")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (Select_user_profile, l_parameters))
db_handler.execute_query
create Result.make
if not db_handler.after then
from
db_handler.start
until
db_handler.after
loop
if
attached db_handler.read_string (1) as l_key and then
attached db_handler.read_string (2) as l_value
then
Result.force (l_value, l_key)
end
db_handler.forth
end
end
post_execution
end
feature -- New Object
fetch_user: CMS_USER
@@ -230,6 +298,14 @@ feature {NONE} -- Sql Queries: USER_ROLES
Select_user_roles: STRING = "Select roles_id from users_roles where users_id = :user_id"
feature {NONE} -- SQL Queries: Profile
Select_instert_profile_item: STRING = "insert into profiles (profiles.key, value, users_id) values (:key, :value, :users_id);"
Select_user_profile: STRING = "Select profiles.key, value from profiles where users_id = :users_id;"
feature {NONE} -- Implementation
post_execution

View File

@@ -5,7 +5,7 @@ note
author: "EiffelStudio test wizard"
date: "$Date$"
revision: "$Revision$"
testing: "type/manual"
testing:"execution/serial"
class
USER_TEST_SET
@@ -36,6 +36,7 @@ feature {NONE} -- Events
on_clean
-- <Precursor>
do
-- (create {CLEAN_DB}).clean_db(connection)
end
feature -- Test routines
@@ -51,9 +52,9 @@ feature -- Test routines
test_user_not_exist
-- Uset test does not exist.
do
assert ("Void", user_provider.user_by_email ("test@admin.com") = Void)
assert ("Void", user_provider.user(2) = Void )
assert ("Void", user_provider.user_by_name ("test") = Void)
assert ("User by email: Void", user_provider.user_by_email ("test1@test.com") = Void)
assert ("User by id: Void", user_provider.user(5) = Void )
assert ("User by name: Void", user_provider.user_by_name ("test1") = Void)
end
test_new_user
@@ -74,6 +75,34 @@ feature -- Test routines
assert ("Not empty roles for given user", not user_provider.user_roles (1).after)
end
test_new_user_without_profile
do
user_provider.new_user ("test", "test","test@admin.com")
assert ("Empty", user_provider.user_profile (1).new_cursor.after)
end
test_new_user_with_profile
local
l_profile: CMS_USER_PROFILE
l_db_profile: CMS_USER_PROFILE
do
user_provider.new_user ("test", "test","test@admin.com")
if attached {CMS_USER} user_provider.user_by_name ("test") as l_user then
assert ("Empty", user_provider.user_profile (l_user.id).new_cursor.after)
create l_profile.make
l_profile.force ("Eiffel", "language")
l_profile.force ("Argentina", "country")
l_profile.force ("GMT-3", "time zone")
user_provider.save_profile (l_user.id, l_profile)
l_db_profile := user_provider.user_profile (l_user.id)
assert ("Not Empty", not l_db_profile.new_cursor.after)
assert ("Expected language Eiffel", attached l_db_profile.item ("language") as l_language and then l_language ~ "Eiffel")
assert ("Expected time zone GMT-3", attached l_db_profile.item ("time zone") as l_language and then l_language ~ "GMT-3")
end
end
feature {NONE} -- Implementation
user_provider: USER_DATA_PROVIDER

View File

@@ -10,6 +10,7 @@ note
date: "$Date$"
revision: "$Revision$"
EIS: "name=Database Testing", "src=http://www.agiledata.org/essays/databaseTesting.html", "protocol=uri"
testing:"execution/serial"
class
CLEAN_DB
@@ -24,6 +25,10 @@ feature
create l_parameters.make (0)
-- Clean Profiles
db_handler(a_connection).set_query (create {DATABASE_QUERY}.data_reader (Sql_delete_user_profiles, l_parameters))
db_handler(a_connection).execute_change
-- Clean Permissions
db_handler(a_connection).set_query (create {DATABASE_QUERY}.data_reader (Sql_delete_permissions, l_parameters))
db_handler(a_connection).execute_change
@@ -58,6 +63,9 @@ feature
db_handler(a_connection).set_query (create {DATABASE_QUERY}.data_reader (Rest_permissions_autoincrement, l_parameters))
db_handler(a_connection).execute_change
db_handler(a_connection).set_query (create {DATABASE_QUERY}.data_reader (Rest_profiles_autoincrement, l_parameters))
db_handler(a_connection).execute_change
end
@@ -88,6 +96,9 @@ feature -- Sql delete queries
Sql_delete_users_roles: STRING = "delete from Users_roles"
-- Clean User roles.
Sql_delete_user_profiles: STRING = "delete from profiles"
-- Clean profiles.
Rest_users_autoincrement: STRING = "ALTER TABLE Users AUTO_INCREMENT = 1"
-- reset autoincrement
@@ -100,4 +111,9 @@ feature -- Sql delete queries
Rest_permissions_autoincrement: STRING = "ALTER TABLE Permissions AUTO_INCREMENT = 1"
-- reset autoincrement.
Rest_profiles_autoincrement: STRING = "ALTER TABLE Profiles AUTO_INCREMENT = 1"
-- reset autoincrement.
end