MCPcopy
hub / github.com/kenn-io/msgvault / TestQueryEngine_PostgresPortability

Function TestQueryEngine_PostgresPortability

internal/query/pg_compat_test.go:36–138  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

34// to. The test never asserts on dialect-specific error text; a bug
35// would surface as a generic Scan/Exec failure on PG.
36func 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,

Callers

nothing calls this directly

Calls 15

AggregateMethod · 0.95
GetGmailIDsByFilterMethod · 0.95
ListMessagesMethod · 0.95
NewTestStoreFunction · 0.92
NewEngineFunction · 0.92
gmailSourceIDFunction · 0.85
subjectForFunction · 0.85
GetOrCreateSourceMethod · 0.80
NoErrorMethod · 0.80
EnsureConversationMethod · 0.80
EnsureLabelMethod · 0.80
DateMethod · 0.80

Tested by

no test coverage detected