repository_review_test.go.txt 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package repository
  2. import (
  3. "context"
  4. "encoding/json"
  5. "testing"
  6. "entgo.io/ent/dialect"
  7. entsql "entgo.io/ent/dialect/sql"
  8. "github.com/DATA-DOG/go-sqlmock"
  9. dbent "github.com/Wei-Shaw/sub2api/ent"
  10. "github.com/Wei-Shaw/sub2api/internal/service"
  11. "github.com/stretchr/testify/require"
  12. )
  13. // Exercises the final protection merge with a committed row supplied by a
  14. // mock SQL reader. It does not claim a real PostgreSQL concurrency test.
  15. func TestProtectionReviewStaleEmptySnapshot(t *testing.T) {
  16. for _, tt := range []struct{name string; extra map[string]any}{
  17. {"no_marker_before_first_enable", nil},
  18. {"explicit_false_marker_control", map[string]any{"anti_degradation":false}},
  19. } {
  20. t.Run(tt.name, func(t *testing.T) {
  21. db, mock, err := sqlmock.New()
  22. require.NoError(t, err)
  23. defer db.Close()
  24. client := dbent.NewClient(dbent.Driver(entsql.OpenDB(dialect.Postgres, db)))
  25. current := &service.Account{ID:501, Platform:service.PlatformOpenAI, Type:service.AccountTypeOAuth, Concurrency:32}
  26. service.PrepareNewAccountProtection(current)
  27. raw, err := json.Marshal(current.Extra)
  28. require.NoError(t, err)
  29. mock.ExpectQuery("SELECT extra FROM accounts").WithArgs(int64(501)).WillReturnRows(sqlmock.NewRows([]string{"extra"}).AddRow(raw))
  30. stale := &service.Account{ID:501, Platform:service.PlatformOpenAI, Type:service.AccountTypeOAuth, Concurrency:32, Extra:tt.extra}
  31. require.NoError(t, preserveLockedAccountProtection(context.Background(), client, stale))
  32. require.True(t, stale.AntiDegradationEnabled(), "a snapshot from before first enable must merge the committed policy before writing")
  33. require.Equal(t,16,stale.Concurrency)
  34. require.NoError(t,mock.ExpectationsWereMet())
  35. })
  36. }
  37. }