strategy_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package strategy
  2. import (
  3. "github.com/FGM/kurz/storage"
  4. "github.com/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. defer storage.Service.Close()
  27. sourceUrl := url.LongUrl{
  28. Value: "http://www.example.com",
  29. }
  30. alias, err := strategy.Alias(sourceUrl, storage.Service)
  31. if err != nil {
  32. t.Errorf("Failed during Alias(): %+v", err)
  33. }
  34. if alias.ShortFor != sourceUrl {
  35. t.Errorf("Aliasing does not point to proper long URL: expected %+V, got %+V", sourceUrl, alias.ShortFor)
  36. }
  37. if alias.Value != sourceUrl.Value {
  38. t.Errorf("Aliasing does not build the proper URL: expected %+V, got %+V", sourceUrl.Value, alias.Value)
  39. }
  40. storage.Service.DB.Exec("TRUNCATE shorturl")
  41. }
  42. func TestUseCounts(t *testing.T) {
  43. const BASE = "base"
  44. account := storage.User{
  45. DefaultStrategy: BASE,
  46. }
  47. strategy := Strategies[account.DefaultStrategy]
  48. if strategy.Name() != BASE {
  49. t.Fatalf("Strategy: expected %s, got %s", BASE, strategy.Name())
  50. }
  51. initTestStorage(t)
  52. defer storage.Service.Close()
  53. initialCount := strategy.UseCount(storage.Service)
  54. if initialCount != 0 {
  55. t.Errorf("Found %d record(s) in test database, expecting none.", initialCount)
  56. }
  57. sourceUrl := url.LongUrl{
  58. Value: "http://www.example.com",
  59. }
  60. _, err := strategy.Alias(sourceUrl, storage.Service)
  61. if err != nil {
  62. t.Errorf("Failed during Alias(): %+v", err)
  63. }
  64. nextCount := strategy.UseCount(storage.Service)
  65. if nextCount != initialCount+1 {
  66. t.Errorf("Found %d record(s) in test database, expecting %d.", nextCount, initialCount+1)
  67. }
  68. sourceUrl = url.LongUrl{
  69. Value: "http://www2.example.com",
  70. }
  71. _, err = strategy.Alias(sourceUrl, storage.Service)
  72. if err != nil {
  73. t.Errorf("Failed during Alias(): %+v", err)
  74. }
  75. nextCount = strategy.UseCount(storage.Service)
  76. if nextCount != initialCount+2 {
  77. t.Errorf("Found %d record(s) in test database, expecting %d.", nextCount, initialCount+2)
  78. }
  79. storage.Service.DB.Exec("TRUNCATE shorturl")
  80. }