main_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "testing"
  4. )
  5. func TestDB(t *testing.T) {
  6. s := Server{Alias: "myalias", Hostname: "myhostname", Notes: "mynotes"}
  7. dbw := DBWrapper{}
  8. // Test adding a server
  9. err := dbw.AddServer(s)
  10. if err != nil {
  11. t.Errorf("Failed to add a server to DB. err: %s\n", err.Error())
  12. }
  13. //Test retrieving a server
  14. r, err := dbw.GetServer(s.Alias)
  15. if err != nil {
  16. t.Errorf("Failed to get server from DB. err: %s\n", err.Error())
  17. }
  18. if !compareServers(s, r) {
  19. t.Errorf("Retrieved wrong content from DB.\n")
  20. }
  21. // Test searching for a server
  22. as, err := dbw.ServerSearch(s.Alias)
  23. if err != nil {
  24. t.Errorf("Failed to get server from DB. err: %s\n", err.Error())
  25. }
  26. if len(as) != 1 {
  27. t.Errorf("Incorrect number of search results for alias search: %d\n", len(as))
  28. }
  29. if !compareServers(s, as[0]) {
  30. t.Errorf("Retrieved wrong content from DB searching by alias.\n")
  31. }
  32. // Test updating a server
  33. updated := Server{Alias: s.Alias, Hostname: "updated-hostname", Notes: "update-notes"}
  34. err = dbw.UpdateServer(updated)
  35. if err != nil {
  36. t.Errorf("Failed to update server. err: %s\n", err.Error())
  37. }
  38. updated2, err := dbw.GetServer(s.Alias)
  39. if err != nil {
  40. t.Errorf("Failed to get updated server.\n")
  41. }
  42. if !compareServers(updated, updated2) {
  43. t.Errorf("Updated server did not retrieve with the same values.\n")
  44. }
  45. // Test deleting a server
  46. err = dbw.DeleteServer(s.Alias)
  47. if err != nil {
  48. t.Errorf("Failed to remove a server from DB. err: %s\n", err.Error())
  49. }
  50. }
  51. func compareServers(a, b Server) bool {
  52. return a.Alias == b.Alias && a.Hostname == b.Hostname && a.Notes == b.Notes
  53. }