|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package transform |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | +) |
| 20 | + |
| 21 | +func TestAddReturning_OnInsert(t *testing.T) { |
| 22 | + stmt := mustParse(t, "INSERT INTO users (name) VALUES ('alice')") |
| 23 | + |
| 24 | + err := Apply(stmt, AddReturning("id")) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("AddReturning on INSERT: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + out := format(stmt) |
| 30 | + assertContains(t, out, "RETURNING") |
| 31 | + assertContains(t, out, "id") |
| 32 | +} |
| 33 | + |
| 34 | +func TestAddReturning_OnUpdate(t *testing.T) { |
| 35 | + stmt := mustParse(t, "UPDATE users SET status = 'active' WHERE id = 1") |
| 36 | + |
| 37 | + err := Apply(stmt, AddReturning("id", "updated_at")) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("AddReturning on UPDATE: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + out := format(stmt) |
| 43 | + assertContains(t, out, "RETURNING") |
| 44 | + assertContains(t, out, "id") |
| 45 | + assertContains(t, out, "updated_at") |
| 46 | +} |
| 47 | + |
| 48 | +func TestAddReturning_OnDelete(t *testing.T) { |
| 49 | + stmt := mustParse(t, "DELETE FROM users WHERE id = 1") |
| 50 | + |
| 51 | + err := Apply(stmt, AddReturning("id")) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("AddReturning on DELETE: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + out := format(stmt) |
| 57 | + assertContains(t, out, "RETURNING") |
| 58 | + assertContains(t, out, "id") |
| 59 | +} |
| 60 | + |
| 61 | +func TestAddReturning_MultipleColumns(t *testing.T) { |
| 62 | + stmt := mustParse(t, "INSERT INTO orders (product_id, qty) VALUES (1, 5)") |
| 63 | + |
| 64 | + err := Apply(stmt, AddReturning("id", "created_at", "total")) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("AddReturning multiple columns: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + out := format(stmt) |
| 70 | + assertContains(t, out, "RETURNING") |
| 71 | + assertContains(t, out, "id") |
| 72 | + assertContains(t, out, "created_at") |
| 73 | + assertContains(t, out, "total") |
| 74 | +} |
| 75 | + |
| 76 | +func TestAddReturning_AppendsToPreviousReturning(t *testing.T) { |
| 77 | + stmt := mustParse(t, "INSERT INTO users (name) VALUES ('alice')") |
| 78 | + |
| 79 | + // Add returning in two steps |
| 80 | + _ = Apply(stmt, AddReturning("id")) |
| 81 | + err := Apply(stmt, AddReturning("created_at")) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("second AddReturning: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + out := format(stmt) |
| 87 | + assertContains(t, out, "id") |
| 88 | + assertContains(t, out, "created_at") |
| 89 | +} |
| 90 | + |
| 91 | +func TestRemoveReturning_RemovesClause(t *testing.T) { |
| 92 | + stmt := mustParse(t, "DELETE FROM users WHERE id = 1") |
| 93 | + |
| 94 | + _ = Apply(stmt, AddReturning("id")) |
| 95 | + err := Apply(stmt, RemoveReturning()) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("RemoveReturning: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + out := format(stmt) |
| 101 | + assertNotContains(t, out, "RETURNING") |
| 102 | +} |
| 103 | + |
| 104 | +func TestRemoveReturning_OnUpdate(t *testing.T) { |
| 105 | + stmt := mustParse(t, "UPDATE users SET status = 'active'") |
| 106 | + |
| 107 | + _ = Apply(stmt, AddReturning("id", "status")) |
| 108 | + err := Apply(stmt, RemoveReturning()) |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("RemoveReturning on UPDATE: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + out := format(stmt) |
| 114 | + assertNotContains(t, out, "RETURNING") |
| 115 | +} |
| 116 | + |
| 117 | +func TestRemoveReturning_WhenAlreadyEmpty_NoError(t *testing.T) { |
| 118 | + stmt := mustParse(t, "DELETE FROM users WHERE active = false") |
| 119 | + |
| 120 | + err := Apply(stmt, RemoveReturning()) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("RemoveReturning on empty clause should not error: %v", err) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestAddReturning_OnSelect_ReturnsError(t *testing.T) { |
| 127 | + stmt := mustParse(t, "SELECT * FROM users") |
| 128 | + |
| 129 | + err := Apply(stmt, AddReturning("id")) |
| 130 | + if err == nil { |
| 131 | + t.Error("expected error applying AddReturning to SELECT statement") |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestRemoveReturning_OnSelect_ReturnsError(t *testing.T) { |
| 136 | + stmt := mustParse(t, "SELECT * FROM users") |
| 137 | + |
| 138 | + err := Apply(stmt, RemoveReturning()) |
| 139 | + if err == nil { |
| 140 | + t.Error("expected error applying RemoveReturning to SELECT statement") |
| 141 | + } |
| 142 | +} |
0 commit comments