TestQueryEngine_PostgresPortability exercises the three SQL shapes the external review flagged as failing on PostgreSQL: - Aggregate uses `FROM ( … ) AS agg` (PG rejects unaliased derived tables in FROM). - GetGmailIDsByFilter avoids SELECT DISTINCT entirely (PG rejects DISTINCT when ORDER BY refer
(t *testing.T)
| 34 | // to. The test never asserts on dialect-specific error text; a bug |
| 35 | // would surface as a generic Scan/Exec failure on PG. |
| 36 | func TestQueryEngine_PostgresPortability(t *testing.T) { |
| 37 | require := requirepkg.New(t) |
| 38 | st := testutil.NewTestStore(t) |
| 39 | src, err := st.GetOrCreateSource("gmail", "pgcompat@example.com") |
| 40 | require.NoError(err, "GetOrCreateSource") |
| 41 | |
| 42 | convID, err := st.EnsureConversation(src.ID, "thread-1", "Thread 1") |
| 43 | require.NoError(err, "EnsureConversation") |
| 44 | |
| 45 | aliceID, err := st.EnsureParticipant("alice@example.com", "Alice", "example.com") |
| 46 | require.NoError(err, "EnsureParticipant alice") |
| 47 | bobID, err := st.EnsureParticipant("bob@example.com", "Bob", "example.com") |
| 48 | require.NoError(err, "EnsureParticipant bob") |
| 49 | |
| 50 | labelID, err := st.EnsureLabel(src.ID, "Label_1", "Important", "user") |
| 51 | require.NoError(err, "EnsureLabel") |
| 52 | |
| 53 | base := time.Date(2024, 6, 1, 12, 0, 0, 0, time.UTC) |
| 54 | for i := range 4 { |
| 55 | mid, err := st.UpsertMessage(&store.Message{ |
| 56 | ConversationID: convID, |
| 57 | SourceID: src.ID, |
| 58 | SourceMessageID: gmailSourceID(i), |
| 59 | MessageType: "email", |
| 60 | SentAt: sql.NullTime{Time: base.Add(time.Duration(i) * time.Hour), Valid: true}, |
| 61 | Subject: sql.NullString{String: subjectFor(i), Valid: true}, |
| 62 | Snippet: sql.NullString{String: "snippet", Valid: true}, |
| 63 | SizeEstimate: int64(1000 + i*250), |
| 64 | }) |
| 65 | require.NoError(err, "UpsertMessage") |
| 66 | require.NoError(st.ReplaceMessageRecipients(mid, "from", []int64{aliceID}, []string{"Alice"}), |
| 67 | "ReplaceMessageRecipients from") |
| 68 | require.NoError(st.ReplaceMessageRecipients(mid, "to", []int64{bobID}, []string{"Bob"}), |
| 69 | "ReplaceMessageRecipients to") |
| 70 | require.NoError(st.ReplaceMessageLabels(mid, []int64{labelID}), |
| 71 | "ReplaceMessageLabels") |
| 72 | } |
| 73 | |
| 74 | eng := query.NewEngine(st.DB(), st.IsPostgreSQL()) |
| 75 | ctx := context.Background() |
| 76 | |
| 77 | // (1) Aggregate — must not error with "syntax error near ')'" on PG. |
| 78 | t.Run("aggregate_senders", func(t *testing.T) { |
| 79 | rows, err := eng.Aggregate(ctx, query.ViewSenders, query.AggregateOptions{ |
| 80 | SortField: query.SortByCount, |
| 81 | SortDirection: query.SortDesc, |
| 82 | Limit: 50, |
| 83 | }) |
| 84 | requirepkg.NoError(t, err, "Aggregate") |
| 85 | requirepkg.NotEmpty(t, rows, "Aggregate returned no rows; expected at least the Alice sender bucket") |
| 86 | }) |
| 87 | |
| 88 | // (2) GetGmailIDsByFilter — must not error from a SELECT DISTINCT + |
| 89 | // ORDER BY collision on PG. Use a label filter to exercise the |
| 90 | // previously-multiplying join (now an EXISTS subquery). |
| 91 | t.Run("gmail_ids_by_filter_label", func(t *testing.T) { |
| 92 | ids, err := eng.GetGmailIDsByFilter(ctx, query.MessageFilter{ |
| 93 | SourceID: &src.ID, |
nothing calls this directly
no test coverage detected