|
| 1 | +defmodule Arcadex.Integration.SQL.InsertTest do |
| 2 | + @moduledoc """ |
| 3 | + Integration tests for INSERT SQL features against ArcadeDB. |
| 4 | +
|
| 5 | + Tests INSERT with RETURN, batch inserts, and content expressions. |
| 6 | + """ |
| 7 | + use Arcadex.IntegrationCase, async: true |
| 8 | + |
| 9 | + setup_all %{conn: conn} do |
| 10 | + # Create Item type with properties for testing |
| 11 | + Arcadex.command!(conn, "CREATE DOCUMENT TYPE Item") |
| 12 | + Arcadex.command!(conn, "CREATE PROPERTY Item.uid STRING") |
| 13 | + Arcadex.command!(conn, "CREATE PROPERTY Item.name STRING") |
| 14 | + Arcadex.command!(conn, "CREATE PROPERTY Item.quantity INTEGER") |
| 15 | + Arcadex.command!(conn, "CREATE PROPERTY Item.price DECIMAL") |
| 16 | + Arcadex.command!(conn, "CREATE PROPERTY Item.tags LIST") |
| 17 | + Arcadex.command!(conn, "CREATE PROPERTY Item.metadata MAP") |
| 18 | + Arcadex.command!(conn, "CREATE INDEX ON Item (uid) UNIQUE") |
| 19 | + |
| 20 | + :ok |
| 21 | + end |
| 22 | + |
| 23 | + describe "INSERT returns created record" do |
| 24 | + test "INSERT returns the created record with all fields", %{conn: conn} do |
| 25 | + uid = generate_uid() |
| 26 | + |
| 27 | + [item] = |
| 28 | + Arcadex.command!( |
| 29 | + conn, |
| 30 | + """ |
| 31 | + INSERT INTO Item SET |
| 32 | + uid = :uid, |
| 33 | + name = :name, |
| 34 | + quantity = :quantity, |
| 35 | + price = :price |
| 36 | + """, |
| 37 | + %{uid: uid, name: "Widget", quantity: 10, price: 19.99} |
| 38 | + ) |
| 39 | + |
| 40 | + assert item["uid"] == uid |
| 41 | + assert item["name"] == "Widget" |
| 42 | + assert item["quantity"] == 10 |
| 43 | + assert item["price"] == 19.99 |
| 44 | + assert item["@rid"] |
| 45 | + assert item["@type"] == "Item" |
| 46 | + end |
| 47 | + |
| 48 | + test "INSERT returns record with @rid", %{conn: conn} do |
| 49 | + uid = generate_uid() |
| 50 | + |
| 51 | + [item] = |
| 52 | + Arcadex.command!( |
| 53 | + conn, |
| 54 | + "INSERT INTO Item SET uid = :uid, name = 'Test'", |
| 55 | + %{uid: uid} |
| 56 | + ) |
| 57 | + |
| 58 | + rid = item["@rid"] |
| 59 | + assert is_binary(rid) |
| 60 | + assert String.starts_with?(rid, "#") |
| 61 | + end |
| 62 | + |
| 63 | + test "INSERT with CONTENT clause", %{conn: conn} do |
| 64 | + uid = generate_uid() |
| 65 | + |
| 66 | + [item] = |
| 67 | + Arcadex.command!( |
| 68 | + conn, |
| 69 | + "INSERT INTO Item CONTENT :content", |
| 70 | + %{ |
| 71 | + content: %{ |
| 72 | + uid: uid, |
| 73 | + name: "Content Item", |
| 74 | + quantity: 5, |
| 75 | + price: 9.99 |
| 76 | + } |
| 77 | + } |
| 78 | + ) |
| 79 | + |
| 80 | + assert item["uid"] == uid |
| 81 | + assert item["name"] == "Content Item" |
| 82 | + assert item["quantity"] == 5 |
| 83 | + assert item["price"] == 9.99 |
| 84 | + end |
| 85 | + |
| 86 | + test "INSERT with embedded list", %{conn: conn} do |
| 87 | + uid = generate_uid() |
| 88 | + |
| 89 | + [item] = |
| 90 | + Arcadex.command!( |
| 91 | + conn, |
| 92 | + "INSERT INTO Item SET uid = :uid, name = 'Tagged', tags = :tags", |
| 93 | + %{uid: uid, tags: ["electronics", "sale", "new"]} |
| 94 | + ) |
| 95 | + |
| 96 | + assert item["uid"] == uid |
| 97 | + assert item["tags"] == ["electronics", "sale", "new"] |
| 98 | + end |
| 99 | + |
| 100 | + test "INSERT with embedded map", %{conn: conn} do |
| 101 | + uid = generate_uid() |
| 102 | + |
| 103 | + [item] = |
| 104 | + Arcadex.command!( |
| 105 | + conn, |
| 106 | + "INSERT INTO Item SET uid = :uid, name = 'With Metadata', metadata = :metadata", |
| 107 | + %{uid: uid, metadata: %{color: "red", size: "large", weight: 1.5}} |
| 108 | + ) |
| 109 | + |
| 110 | + assert item["uid"] == uid |
| 111 | + assert item["metadata"]["color"] == "red" |
| 112 | + assert item["metadata"]["size"] == "large" |
| 113 | + assert item["metadata"]["weight"] == 1.5 |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + describe "INSERT multiple records" do |
| 118 | + test "INSERT multiple records in sequence", %{conn: conn} do |
| 119 | + uids = |
| 120 | + for i <- 1..3 do |
| 121 | + uid = generate_uid() |
| 122 | + |
| 123 | + Arcadex.command!( |
| 124 | + conn, |
| 125 | + "INSERT INTO Item SET uid = :uid, name = :name, quantity = :quantity", |
| 126 | + %{uid: uid, name: "Batch Item #{i}", quantity: i * 10} |
| 127 | + ) |
| 128 | + |
| 129 | + uid |
| 130 | + end |
| 131 | + |
| 132 | + # Verify all records were created |
| 133 | + for {uid, i} <- Enum.with_index(uids, 1) do |
| 134 | + [item] = |
| 135 | + Arcadex.query!( |
| 136 | + conn, |
| 137 | + "SELECT FROM Item WHERE uid = :uid", |
| 138 | + %{uid: uid} |
| 139 | + ) |
| 140 | + |
| 141 | + assert item["name"] == "Batch Item #{i}" |
| 142 | + assert item["quantity"] == i * 10 |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + test "INSERT with subquery values", %{conn: conn} do |
| 147 | + # Create source record |
| 148 | + source_uid = generate_uid() |
| 149 | + |
| 150 | + Arcadex.command!( |
| 151 | + conn, |
| 152 | + "INSERT INTO Item SET uid = :uid, name = 'Source', quantity = 100, price = 50.0", |
| 153 | + %{uid: source_uid} |
| 154 | + ) |
| 155 | + |
| 156 | + # Insert with computed value from subquery using script |
| 157 | + copy_uid = generate_uid() |
| 158 | + |
| 159 | + [_result] = |
| 160 | + Arcadex.script!( |
| 161 | + conn, |
| 162 | + """ |
| 163 | + LET source = SELECT name, quantity, price FROM Item WHERE uid = :source_uid; |
| 164 | + LET copy = INSERT INTO Item SET |
| 165 | + uid = :copy_uid, |
| 166 | + name = $source[0].name + ' Copy', |
| 167 | + quantity = $source[0].quantity, |
| 168 | + price = $source[0].price; |
| 169 | + RETURN $copy |
| 170 | + """, |
| 171 | + %{source_uid: source_uid, copy_uid: copy_uid} |
| 172 | + ) |
| 173 | + |
| 174 | + # Verify the copy was created |
| 175 | + [copy] = |
| 176 | + Arcadex.query!( |
| 177 | + conn, |
| 178 | + "SELECT FROM Item WHERE uid = :uid", |
| 179 | + %{uid: copy_uid} |
| 180 | + ) |
| 181 | + |
| 182 | + assert copy["name"] == "Source Copy" |
| 183 | + assert copy["quantity"] == 100 |
| 184 | + assert copy["price"] == 50.0 |
| 185 | + end |
| 186 | + |
| 187 | + test "INSERT with default values", %{conn: conn} do |
| 188 | + uid = generate_uid() |
| 189 | + |
| 190 | + # Insert with only required fields |
| 191 | + [item] = |
| 192 | + Arcadex.command!( |
| 193 | + conn, |
| 194 | + "INSERT INTO Item SET uid = :uid, name = 'Minimal'", |
| 195 | + %{uid: uid} |
| 196 | + ) |
| 197 | + |
| 198 | + assert item["uid"] == uid |
| 199 | + assert item["name"] == "Minimal" |
| 200 | + # Other fields should be nil/absent |
| 201 | + refute Map.has_key?(item, "quantity") or item["quantity"] != nil |
| 202 | + end |
| 203 | + |
| 204 | + test "INSERT with computed expression", %{conn: conn} do |
| 205 | + uid = generate_uid() |
| 206 | + |
| 207 | + [item] = |
| 208 | + Arcadex.command!( |
| 209 | + conn, |
| 210 | + """ |
| 211 | + INSERT INTO Item SET |
| 212 | + uid = :uid, |
| 213 | + name = 'Computed', |
| 214 | + quantity = 10, |
| 215 | + price = 5.0 * 2 |
| 216 | + """, |
| 217 | + %{uid: uid} |
| 218 | + ) |
| 219 | + |
| 220 | + assert item["uid"] == uid |
| 221 | + assert item["price"] == 10.0 |
| 222 | + end |
| 223 | + end |
| 224 | + |
| 225 | + describe "INSERT edge cases" do |
| 226 | + test "INSERT with null values", %{conn: conn} do |
| 227 | + uid = generate_uid() |
| 228 | + |
| 229 | + [item] = |
| 230 | + Arcadex.command!( |
| 231 | + conn, |
| 232 | + "INSERT INTO Item SET uid = :uid, name = 'Nullable', quantity = null", |
| 233 | + %{uid: uid} |
| 234 | + ) |
| 235 | + |
| 236 | + assert item["uid"] == uid |
| 237 | + assert item["name"] == "Nullable" |
| 238 | + assert is_nil(item["quantity"]) |
| 239 | + end |
| 240 | + |
| 241 | + test "INSERT with empty string", %{conn: conn} do |
| 242 | + uid = generate_uid() |
| 243 | + |
| 244 | + [item] = |
| 245 | + Arcadex.command!( |
| 246 | + conn, |
| 247 | + "INSERT INTO Item SET uid = :uid, name = ''", |
| 248 | + %{uid: uid} |
| 249 | + ) |
| 250 | + |
| 251 | + assert item["uid"] == uid |
| 252 | + assert item["name"] == "" |
| 253 | + end |
| 254 | + |
| 255 | + test "INSERT with zero values", %{conn: conn} do |
| 256 | + uid = generate_uid() |
| 257 | + |
| 258 | + [item] = |
| 259 | + Arcadex.command!( |
| 260 | + conn, |
| 261 | + "INSERT INTO Item SET uid = :uid, name = 'Zero', quantity = 0, price = 0.0", |
| 262 | + %{uid: uid} |
| 263 | + ) |
| 264 | + |
| 265 | + assert item["uid"] == uid |
| 266 | + assert item["quantity"] == 0 |
| 267 | + assert item["price"] == 0.0 |
| 268 | + end |
| 269 | + |
| 270 | + test "INSERT with negative values", %{conn: conn} do |
| 271 | + uid = generate_uid() |
| 272 | + |
| 273 | + [item] = |
| 274 | + Arcadex.command!( |
| 275 | + conn, |
| 276 | + "INSERT INTO Item SET uid = :uid, name = 'Negative', quantity = -5, price = -10.50", |
| 277 | + %{uid: uid} |
| 278 | + ) |
| 279 | + |
| 280 | + assert item["uid"] == uid |
| 281 | + assert item["quantity"] == -5 |
| 282 | + assert item["price"] == -10.50 |
| 283 | + end |
| 284 | + end |
| 285 | +end |
0 commit comments