188 lines
3.8 KiB
Go
188 lines
3.8 KiB
Go
package entity
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.heldm.com/held/qrcode/internal/qrcode"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestQR_New_Success(t *testing.T) {
|
|
userID := uuid.New()
|
|
|
|
got, err := NewQR("https://example.com", userID)
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got %v", err)
|
|
}
|
|
|
|
if got == nil {
|
|
t.Fatal("expected qr, got nil")
|
|
}
|
|
|
|
if got.ID() == uuid.Nil {
|
|
t.Fatal("expected id to be set")
|
|
}
|
|
|
|
if got.UserID() != userID {
|
|
t.Fatalf("expected userID %v, got %v", userID, got.UserID())
|
|
}
|
|
|
|
if got.URL() != "https://example.com" {
|
|
t.Fatalf("expected url https://example.com, got %q", got.URL())
|
|
}
|
|
|
|
if got.Slug() == "" {
|
|
t.Fatal("expected slug to be set")
|
|
}
|
|
|
|
if got.Visits() != 0 {
|
|
t.Fatalf("expected visits 0, got %d", got.Visits())
|
|
}
|
|
|
|
if got.CreatedAt().IsZero() {
|
|
t.Fatal("expected createdAt to be set")
|
|
}
|
|
|
|
if got.UpdatedAt().IsZero() {
|
|
t.Fatal("expected updatedAt to be set")
|
|
}
|
|
|
|
if got.DeletedAt() != nil {
|
|
t.Fatalf("expected deletedAt nil, got %v", got.DeletedAt())
|
|
}
|
|
}
|
|
|
|
func TestQR_New_TrimURL(t *testing.T) {
|
|
userID := uuid.New()
|
|
|
|
got, err := NewQR(" https://example.com/path ", userID)
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got %v", err)
|
|
}
|
|
|
|
if got.URL() != "https://example.com/path" {
|
|
t.Fatalf("expected trimmed url, got %q", got.URL())
|
|
}
|
|
}
|
|
|
|
func TestQR_New_InvalidURL_Empty(t *testing.T) {
|
|
userID := uuid.New()
|
|
|
|
got, err := NewQR(" ", userID)
|
|
if got != nil {
|
|
t.Fatalf("expected nil qr, got %#v", got)
|
|
}
|
|
|
|
if !errors.Is(err, qrcode.ErrInvalidURL) {
|
|
t.Fatalf("expected ErrInvalidURL, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestQR_New_InvalidURL_BadScheme(t *testing.T) {
|
|
userID := uuid.New()
|
|
|
|
got, err := NewQR("ftup://example.com", userID)
|
|
if got != nil {
|
|
t.Fatalf("expected nil qr, got %#v", got)
|
|
}
|
|
|
|
if !errors.Is(err, qrcode.ErrInvalidURL) {
|
|
t.Fatalf("expected ErrInvalidURL, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestQR_New_InvalidURL_NotURL(t *testing.T) {
|
|
userID := uuid.New()
|
|
|
|
got, err := NewQR("not-a-url", userID)
|
|
if got != nil {
|
|
t.Fatalf("expected nil qr, got %#v", got)
|
|
}
|
|
|
|
if !errors.Is(err, qrcode.ErrInvalidURL) {
|
|
t.Fatalf("expected ErrInvalidURL, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestQR_New_SetsDifferentIDsAndSlugs(t *testing.T) {
|
|
userID := uuid.New()
|
|
|
|
got1, err := NewQR("https://example.com/1", userID)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
got2, err := NewQR("https://example.com/2", userID)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if got1.ID() == got2.ID() {
|
|
t.Fatal("expected different ids")
|
|
}
|
|
|
|
if strings.TrimSpace(got1.Slug()) == "" || strings.TrimSpace(got2.Slug()) == "" {
|
|
t.Fatal("expected non-empty slugs")
|
|
}
|
|
|
|
if got1.Slug() == got2.Slug() {
|
|
t.Fatal("expected different slugs")
|
|
}
|
|
}
|
|
|
|
func TestQR_Restore(t *testing.T) {
|
|
var qr QR
|
|
|
|
id := uuid.New()
|
|
userID := uuid.New()
|
|
createdAt := time.Now().Add(-2 * time.Hour)
|
|
updatedAt := time.Now().Add(-1 * time.Hour)
|
|
deletedAt := time.Now()
|
|
|
|
qr.Restore(
|
|
id,
|
|
userID,
|
|
"slug123",
|
|
"https://example.com",
|
|
7,
|
|
createdAt,
|
|
updatedAt,
|
|
&deletedAt,
|
|
)
|
|
|
|
if qr.ID() != id {
|
|
t.Fatalf("expected id %v, got %v", id, qr.ID())
|
|
}
|
|
|
|
if qr.UserID() != userID {
|
|
t.Fatalf("expected userID %v, got %v", userID, qr.UserID())
|
|
}
|
|
|
|
if qr.Slug() != "slug123" {
|
|
t.Fatalf("expected slug slug123, got %q", qr.Slug())
|
|
}
|
|
|
|
if qr.URL() != "https://example.com" {
|
|
t.Fatalf("expected url https://example.com, got %q", qr.URL())
|
|
}
|
|
|
|
if qr.Visits() != 7 {
|
|
t.Fatalf("expected visits 7, got %d", qr.Visits())
|
|
}
|
|
|
|
if !qr.CreatedAt().Equal(createdAt) {
|
|
t.Fatalf("expected createdAt %v, got %v", createdAt, qr.CreatedAt())
|
|
}
|
|
|
|
if !qr.UpdatedAt().Equal(updatedAt) {
|
|
t.Fatalf("expected updatedAt %v, got %v", updatedAt, qr.UpdatedAt())
|
|
}
|
|
|
|
if qr.DeletedAt() == nil || !qr.DeletedAt().Equal(deletedAt) {
|
|
t.Fatalf("expected deletedAt %v, got %v", deletedAt, qr.DeletedAt())
|
|
}
|
|
}
|