@@ -215,4 +215,242 @@ pub fn delete_password(password_manager_db_path: PathBuf, id: String) -> usize {
215215 errorlib:: ExitErrorCode :: DBConnection
216216 ) ;
217217 }
218+ }
219+
220+
221+ #[ cfg( test) ]
222+ mod tests {
223+
224+ use std:: path:: PathBuf ;
225+ use super :: filelib:: create_file;
226+
227+ #[ test]
228+ fn create_passwords_table ( ) {
229+ let temp_dir = PathBuf :: new ( )
230+ . join ( "./temp/create_passwords_table" ) ;
231+ if temp_dir. exists ( ) {
232+ std:: fs:: remove_dir_all ( temp_dir. clone ( ) )
233+ . expect ( "Can NOT delete temp dir!!" ) ;
234+ }
235+ let db_path = temp_dir. join ( "test.db" ) ;
236+ create_file ( db_path. clone ( ) ) ;
237+ assert_eq ! ( db_path. exists( ) , true , "Can NOT create the test file!!" ) ;
238+
239+ // This will panic and exit the program if an error occurs.
240+ // Warning: This will save to the log manager database the
241+ // creatation of the table.
242+ super :: create_passwords_table ( db_path) ;
243+
244+ std:: fs:: remove_dir_all ( temp_dir)
245+ . expect ( "Can NOT delete temp dir!!" ) ;
246+ }
247+
248+ #[ test]
249+ fn save_password ( ) {
250+ let temp_dir = PathBuf :: new ( )
251+ . join ( "./temp/save_password" ) ;
252+ if temp_dir. exists ( ) {
253+ std:: fs:: remove_dir_all ( temp_dir. clone ( ) )
254+ . expect ( "Can NOT delete temp dir!!" ) ;
255+ }
256+ let db_path = temp_dir. join ( "test.db" ) ;
257+ create_file ( db_path. clone ( ) ) ;
258+ assert_eq ! ( db_path. exists( ) , true , "Can NOT create the test file!!" ) ;
259+
260+ // This will panic and exit the program if an error occurs.
261+ // Warning: This will save to the log manager database the
262+ // creatation of the table.
263+ super :: create_passwords_table ( db_path. clone ( ) ) ;
264+ super :: save_password (
265+ db_path. clone ( ) ,
266+ "test" . to_string ( ) ,
267+ "test123" . to_string ( )
268+ ) ;
269+
270+ std:: fs:: remove_dir_all ( temp_dir)
271+ . expect ( "Can NOT delete temp dir!!" ) ;
272+ }
273+
274+ #[ test]
275+ fn find_password ( ) {
276+ let temp_dir = PathBuf :: new ( )
277+ . join ( "./temp/find_password" ) ;
278+ if temp_dir. exists ( ) {
279+ std:: fs:: remove_dir_all ( temp_dir. clone ( ) )
280+ . expect ( "Can NOT delete temp dir!!" ) ;
281+ }
282+ let db_path = temp_dir. join ( "test.db" ) ;
283+ create_file ( db_path. clone ( ) ) ;
284+ assert_eq ! ( db_path. exists( ) , true , "Can NOT create the test file!!" ) ;
285+
286+ // This will panic and exit the program if an error occurs.
287+ // Warning: This will save to the log manager database the
288+ // creatation of the table.
289+ super :: create_passwords_table ( db_path. clone ( ) ) ;
290+ super :: save_password (
291+ db_path. clone ( ) ,
292+ "test" . to_string ( ) ,
293+ "test123" . to_string ( )
294+ ) ;
295+ let passwords = super :: find_password (
296+ db_path. clone ( ) ,
297+ "test" . to_string ( )
298+ ) ;
299+ assert ! (
300+ passwords[ 0 ] . name == "test" &&
301+ passwords[ 0 ] . password == "test123" ,
302+ "Password NOT match!!" ,
303+ ) ;
304+
305+ std:: fs:: remove_dir_all ( temp_dir)
306+ . expect ( "Can NOT delete temp dir!!" ) ;
307+ }
308+
309+ #[ test]
310+ fn get_passwords ( ) {
311+ let temp_dir = PathBuf :: new ( )
312+ . join ( "./temp/get_passwords" ) ;
313+ if temp_dir. exists ( ) {
314+ std:: fs:: remove_dir_all ( temp_dir. clone ( ) )
315+ . expect ( "Can NOT delete temp dir!!" ) ;
316+ }
317+ let db_path = temp_dir. join ( "test.db" ) ;
318+ create_file ( db_path. clone ( ) ) ;
319+ assert_eq ! ( db_path. exists( ) , true , "Can NOT create the test file!!" ) ;
320+
321+ // This will panic and exit the program if an error occurs.
322+ // Warning: This will save to the log manager database the
323+ // creatation of the table.
324+ super :: create_passwords_table ( db_path. clone ( ) ) ;
325+ super :: save_password (
326+ db_path. clone ( ) ,
327+ "test-1" . to_string ( ) ,
328+ "test123" . to_string ( )
329+ ) ;
330+ super :: save_password (
331+ db_path. clone ( ) ,
332+ "test-2" . to_string ( ) ,
333+ "test123" . to_string ( )
334+ ) ;
335+ let passwords = super :: get_passwords ( db_path. clone ( ) ) ;
336+ assert_eq ! ( passwords. len( ) , 2 , "Can NOT get all passwords!!" ) ;
337+ assert ! (
338+ passwords[ 0 ] . name == "test-1" &&
339+ passwords[ 0 ] . password == "test123" ,
340+ "Password NOT match!!" ,
341+ ) ;
342+ assert ! (
343+ passwords[ 1 ] . name == "test-2" &&
344+ passwords[ 1 ] . password == "test123" ,
345+ "Password NOT match!!" ,
346+ ) ;
347+
348+ std:: fs:: remove_dir_all ( temp_dir)
349+ . expect ( "Can NOT delete temp dir!!" ) ;
350+ }
351+
352+ #[ test]
353+ fn update_password_and_name ( ) {
354+ let temp_dir = PathBuf :: new ( )
355+ . join ( "./temp/update_password_and_name" ) ;
356+ if temp_dir. exists ( ) {
357+ std:: fs:: remove_dir_all ( temp_dir. clone ( ) )
358+ . expect ( "Can NOT delete temp dir!!" ) ;
359+ }
360+ let db_path = temp_dir. join ( "test.db" ) ;
361+ create_file ( db_path. clone ( ) ) ;
362+ assert_eq ! ( db_path. exists( ) , true , "Can NOT create the test file!!" ) ;
363+
364+ // This will panic and exit the program if an error occurs.
365+ // Warning: This will save to the log manager database the
366+ // creatation of the table.
367+ super :: create_passwords_table ( db_path. clone ( ) ) ;
368+ super :: save_password (
369+ db_path. clone ( ) ,
370+ "test" . to_string ( ) ,
371+ "test123" . to_string ( )
372+ ) ;
373+ let mut passwords = super :: get_passwords ( db_path. clone ( ) ) ;
374+ assert ! (
375+ passwords[ 0 ] . name == "test" &&
376+ passwords[ 0 ] . password == "test123" ,
377+ "Password NOT match!!" ,
378+ ) ;
379+ super :: update_password (
380+ db_path. clone ( ) ,
381+ 1 . to_string ( ) ,
382+ "new123" . to_string ( )
383+ ) ;
384+ passwords = super :: get_passwords ( db_path. clone ( ) ) ;
385+ assert_eq ! ( passwords[ 0 ] . password, "new123" , "The password NOT updated!!" ) ;
386+ super :: update_password_name (
387+ db_path. clone ( ) ,
388+ 1 . to_string ( ) ,
389+ "new" . to_string ( )
390+ ) ;
391+ passwords = super :: get_passwords ( db_path. clone ( ) ) ;
392+ assert_eq ! ( passwords[ 0 ] . name, "new" , "The password name NOT updated!!" ) ;
393+
394+ std:: fs:: remove_dir_all ( temp_dir)
395+ . expect ( "Can NOT delete temp dir!!" ) ;
396+ }
397+
398+ #[ test]
399+ fn get_passwords_number ( ) {
400+ let temp_dir = PathBuf :: new ( )
401+ . join ( "./temp/get_passwords_number" ) ;
402+ if temp_dir. exists ( ) {
403+ std:: fs:: remove_dir_all ( temp_dir. clone ( ) )
404+ . expect ( "Can NOT delete temp dir!!" ) ;
405+ }
406+ let db_path = temp_dir. join ( "test.db" ) ;
407+ create_file ( db_path. clone ( ) ) ;
408+ assert_eq ! ( db_path. exists( ) , true , "Can NOT create the test file!!" ) ;
409+
410+ // This will panic and exit the program if an error occurs.
411+ // Warning: This will save to the log manager database the
412+ // creatation of the table.
413+ super :: create_passwords_table ( db_path. clone ( ) ) ;
414+ super :: save_password (
415+ db_path. clone ( ) ,
416+ "test" . to_string ( ) ,
417+ "test123" . to_string ( )
418+ ) ;
419+ let number = super :: get_passwords_number ( db_path. clone ( ) ) ;
420+ assert_eq ! ( number, 1 , "Get password number NOT match!!" ) ;
421+
422+ std:: fs:: remove_dir_all ( temp_dir)
423+ . expect ( "Can NOT delete temp dir!!" ) ;
424+ }
425+
426+ #[ test]
427+ fn delete_password ( ) {
428+ let temp_dir = PathBuf :: new ( )
429+ . join ( "./temp/delete_password" ) ;
430+ if temp_dir. exists ( ) {
431+ std:: fs:: remove_dir_all ( temp_dir. clone ( ) )
432+ . expect ( "Can NOT delete temp dir!!" ) ;
433+ }
434+ let db_path = temp_dir. join ( "test.db" ) ;
435+ create_file ( db_path. clone ( ) ) ;
436+ assert_eq ! ( db_path. exists( ) , true , "Can NOT create the test file!!" ) ;
437+
438+ // This will panic and exit the program if an error occurs.
439+ // Warning: This will save to the log manager database the
440+ // creatation of the table.
441+ super :: create_passwords_table ( db_path. clone ( ) ) ;
442+ super :: save_password (
443+ db_path. clone ( ) ,
444+ "test" . to_string ( ) ,
445+ "test123" . to_string ( )
446+ ) ;
447+ let mut number = super :: get_passwords_number ( db_path. clone ( ) ) ;
448+ assert_eq ! ( number, 1 , "Number of passwords NOT match!!" ) ;
449+ super :: delete_password ( db_path. clone ( ) , 1 . to_string ( ) ) ;
450+ number = super :: get_passwords_number ( db_path. clone ( ) ) ;
451+ assert_eq ! ( number, 0 , "Can NOT delete the password!!" ) ;
452+
453+ std:: fs:: remove_dir_all ( temp_dir)
454+ . expect ( "Can NOT delete temp dir!!" ) ;
455+ }
218456}
0 commit comments