identity_repo_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // IdentityRepo 仓储集成测试(t_identity 登录身份,(type,identifier) 唯一键)。
  2. //
  3. // 依赖 test_helpers_test.go 的 setup/insertFixture;未设置 UNIFIED_TEST_DSN 时跳过。
  4. package repository
  5. import (
  6. "context"
  7. stderrors "errors"
  8. "testing"
  9. apperrors "yuansiwei/unified-backend/internal/infra/errors"
  10. identitymodel "yuansiwei/unified-backend/internal/identity/model"
  11. )
  12. // TestIdentityRepo_GetByTypeIdentifier (type,identifier) 唯一键命中 / 未命中。
  13. func TestIdentityRepo_GetByTypeIdentifier(t *testing.T) {
  14. gdb := setup(t)
  15. ctx := context.Background()
  16. fx := insertFixture(t, gdb, ctx)
  17. repo := NewIdentityRepo(gdb)
  18. t.Run("命中_phone", func(t *testing.T) {
  19. it, err := repo.GetByTypeIdentifier(ctx, identitymodel.IdentityTypePhone, fx.phone)
  20. if err != nil {
  21. t.Fatalf("GetByTypeIdentifier phone: %v", err)
  22. }
  23. if it.CanonicalUserID != fx.canonical {
  24. t.Errorf("返回 canonical_user_id = %q,期望 %q", it.CanonicalUserID, fx.canonical)
  25. }
  26. if !it.Verified {
  27. t.Errorf("phone 身份应 verified=true")
  28. }
  29. })
  30. t.Run("命中_email", func(t *testing.T) {
  31. it, err := repo.GetByTypeIdentifier(ctx, identitymodel.IdentityTypeEmail, fx.email)
  32. if err != nil {
  33. t.Fatalf("GetByTypeIdentifier email: %v", err)
  34. }
  35. if it.CanonicalUserID != fx.canonical {
  36. t.Errorf("返回 canonical_user_id = %q,期望 %q", it.CanonicalUserID, fx.canonical)
  37. }
  38. if it.Verified {
  39. t.Errorf("email 身份应 verified=false")
  40. }
  41. })
  42. t.Run("未命中_返回ErrNotFound", func(t *testing.T) {
  43. _, err := repo.GetByTypeIdentifier(ctx, identitymodel.IdentityTypeWechat, "unknown_openid")
  44. if !stderrors.Is(err, apperrors.ErrNotFound) {
  45. t.Fatalf("期望 ErrNotFound,实际 %v", err)
  46. }
  47. })
  48. }