strategy_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package strategy
  2. import (
  3. "code.osinet.fr/fgm/kurz/storage"
  4. "code.osinet.fr/fgm/kurz/url"
  5. "testing"
  6. )
  7. // FIXME stop using constant credentials
  8. func initTestStorage(t *testing.T) {
  9. var DSN = "root:@tcp(localhost:3306)/go_kurz_test"
  10. storage.Service.SetDSN(DSN)
  11. err := storage.Service.Open()
  12. if err != nil {
  13. t.Fatalf("Failed opening the test database: %+v", err)
  14. }
  15. }
  16. func TestBaseAlias(t *testing.T) {
  17. const BASE = "base"
  18. account := storage.User{
  19. DefaultStrategy: BASE,
  20. }
  21. strategy := Strategies[account.DefaultStrategy]
  22. if strategy.Name() != BASE {
  23. t.Fatalf("Strategy: expected %s, got %s", BASE, strategy.Name())
  24. }
  25. initTestStorage(t)
  26. // defers are executed LIFO
  27. defer storage.Service.Close()
  28. storage.Service.Truncate("shorturl")
  29. storage.Service.Truncate("longurl")
  30. storage.Service.AddToTruncateList("shorturl")
  31. storage.Service.AddToTruncateList("longurl")
  32. sourceUrl := url.LongUrl{
  33. Value: "http://www.example.com",
  34. }
  35. alias, err := strategy.Alias(&sourceUrl, storage.Service)
  36. if err != nil {
  37. t.Errorf("Failed during Alias(): %+v", err)
  38. }
  39. if alias.ShortFor.Id != sourceUrl.Id {
  40. t.Errorf("Aliasing does not point to proper long URL: expected %+v, got %+v", sourceUrl, alias.ShortFor)
  41. }
  42. if alias.Value != sourceUrl.Value {
  43. t.Errorf("Aliasing does not build the proper URL: expected %+v, got %+v", sourceUrl.Value, alias.Value)
  44. }
  45. }
  46. func TestUseCounts(t *testing.T) {
  47. const BASE = "base"
  48. account := storage.User{
  49. DefaultStrategy: BASE,
  50. }
  51. strategy := Strategies[account.DefaultStrategy]
  52. if strategy.Name() != BASE {
  53. t.Fatalf("Strategy: expected %s, got %s", BASE, strategy.Name())
  54. }
  55. initTestStorage(t)
  56. defer storage.Service.Close()
  57. storage.Service.Truncate("shorturl")
  58. storage.Service.Truncate("longurl")
  59. storage.Service.AddToTruncateList("shorturl")
  60. storage.Service.AddToTruncateList("longurl")
  61. initialCount := strategy.UseCount(storage.Service)
  62. if initialCount != 0 {
  63. t.Errorf("Found %d record(s) in test database, expecting none.", initialCount)
  64. }
  65. sourceUrl := url.LongUrl{
  66. Value: "http://www.example.com",
  67. }
  68. _, err := strategy.Alias(&sourceUrl, storage.Service)
  69. if err != nil {
  70. t.Errorf("Failed during Alias(): %+v", err)
  71. }
  72. nextCount := strategy.UseCount(storage.Service)
  73. if nextCount != initialCount+1 {
  74. t.Errorf("Found %d record(s) in test database, expecting %d.", nextCount, initialCount+1)
  75. }
  76. sourceUrl = url.LongUrl{
  77. Value: "http://www2.example.com",
  78. }
  79. _, err = strategy.Alias(&sourceUrl, storage.Service)
  80. if err != nil {
  81. t.Errorf("Failed during Alias(): %+v", err)
  82. }
  83. nextCount = strategy.UseCount(storage.Service)
  84. if nextCount != initialCount+2 {
  85. t.Errorf("Found %d record(s) in test database, expecting %d.", nextCount, initialCount+2)
  86. }
  87. }