urls_storage_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package storage
  2. import (
  3. "database/sql"
  4. "testing"
  5. "time"
  6. "ohurlshortener/core"
  7. "github.com/bxcodec/faker/v3"
  8. )
  9. func TestInsertShortUrls(t *testing.T) {
  10. init4Test(t)
  11. for i := 0; i < 10000; i++ {
  12. destUrl := faker.URL()
  13. shortUrl, _ := core.GenerateShortLink(destUrl)
  14. url := core.ShortUrl{DestUrl: destUrl, ShortUrl: shortUrl, CreatedAt: time.Now(), Valid: true, Memo: sql.NullString{String: destUrl, Valid: true}}
  15. err := InsertShortUrl(url)
  16. if err != nil {
  17. t.Error(err)
  18. }
  19. }
  20. }
  21. func TestDeleteShortUrlWithAccessLogs(t *testing.T) {
  22. init4Test(t)
  23. url1 := core.ShortUrl{ShortUrl: "hello"}
  24. url2 := core.ShortUrl{ShortUrl: "hello"}
  25. url3 := core.ShortUrl{ShortUrl: "hello"}
  26. type args struct {
  27. shortUrl core.ShortUrl
  28. }
  29. tests := []struct {
  30. name string
  31. args args
  32. wantErr bool
  33. }{
  34. {name: "TestCase1", args: args{url1}, wantErr: false},
  35. {name: "TestCase1", args: args{url2}, wantErr: false},
  36. {name: "TestCase1", args: args{url3}, wantErr: false},
  37. }
  38. for _, tt := range tests {
  39. t.Run(tt.name, func(t *testing.T) {
  40. if err := DeleteShortUrlWithAccessLogs(tt.args.shortUrl); (err != nil) != tt.wantErr {
  41. t.Errorf("DeleteShortUrlWithAccessLogs() error = %v, wantErr %v", err, tt.wantErr)
  42. }
  43. })
  44. }
  45. }