Kinetica   C#   API  Version 7.2.3.1
InsertRecordsTests.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using Xunit;
4 using Kinetica.Tests.Common;
5 using kinetica;
6 
8 {
13  [Trait("Category", "Integration")]
14  public class InsertRecordsTests
15  {
16  [Fact]
17  public void TestInsertRecordsJson()
18  {
19  using var ctx = new TestContext("insert_json");
20 
21  // Create type
22  var typeDef = @"{""type"":""record"",""name"":""test_rec"",""fields"":[{""name"":""id"",""type"":""int""},{""name"":""name"",""type"":""string""},{""name"":""value"",""type"":""double""}]}";
23  var props = new Dictionary<string, IList<string>>
24  {
25  { "id", new List<string> { "int", "primary_key" } }
26  };
27 
28  var typeResp = ctx.Kinetica.createType(typeDef, "insert_test_type", props, new Dictionary<string, string>());
29 
30  // Create table
31  var tableName = ctx.QualifiedTable("test_table");
32  ctx.Kinetica.createTable(tableName, typeResp.type_id, new Dictionary<string, string>());
33 
34  // Insert records using SQL
35  ctx.Kinetica.executeSql($"INSERT INTO {tableName} (id, name, value) VALUES (1, 'Alice', 1.1)");
36  ctx.Kinetica.executeSql($"INSERT INTO {tableName} (id, name, value) VALUES (2, 'Bob', 2.2)");
37  ctx.Kinetica.executeSql($"INSERT INTO {tableName} (id, name, value) VALUES (3, 'Charlie', 3.3)");
38 
39  // Verify records were inserted using showTable with get_sizes option
40  var showTableResp = ctx.Kinetica.showTable(tableName, new Dictionary<string, string> { { "get_sizes", "true" } });
41  Assert.Equal(3, showTableResp.total_size);
42  }
43 
44  [Fact]
46  {
47  using var ctx = new TestContext("insert_update_pk");
48 
49  // Create type with primary key
50  var typeDef = @"{""type"":""record"",""name"":""pk_rec"",""fields"":[{""name"":""id"",""type"":""int""},{""name"":""value"",""type"":""string""}]}";
51  var props = new Dictionary<string, IList<string>>
52  {
53  { "id", new List<string> { "int", "primary_key" } }
54  };
55 
56  var typeResp = ctx.Kinetica.createType(typeDef, "pk_test_type", props, new Dictionary<string, string>());
57 
58  var tableName = ctx.QualifiedTable("pk_table");
59  ctx.Kinetica.createTable(tableName, typeResp.type_id, new Dictionary<string, string>());
60 
61  // Insert initial records
62  ctx.Kinetica.executeSql($"INSERT INTO {tableName} (id, value) VALUES (1, 'original')");
63  ctx.Kinetica.executeSql($"INSERT INTO {tableName} (id, value) VALUES (2, 'data')");
64 
65  // Verify 2 records exist using showTable with get_sizes option
66  var showTableResp = ctx.Kinetica.showTable(tableName, new Dictionary<string, string> { { "get_sizes", "true" } });
67  Assert.Equal(2, showTableResp.total_size);
68 
69  // Update existing record using UPDATE statement
70  ctx.Kinetica.executeSql($"UPDATE {tableName} SET value = 'updated' WHERE id = 1");
71 
72  // Should still have 2 records
73  var showTableResp2 = ctx.Kinetica.showTable(tableName, new Dictionary<string, string> { { "get_sizes", "true" } });
74  Assert.Equal(2, showTableResp2.total_size);
75 
76  // Verify the value was updated
77  var selectResp = ctx.Kinetica.executeSql($"SELECT value FROM {tableName} WHERE id = 1", 0, -9999);
78  Assert.Equal(1, selectResp.total_number_of_records);
79  }
80 
81  [Fact]
83  {
84  using var ctx = new TestContext("insert_large");
85 
86  // Create table using SQL
87  ctx.Kinetica.executeSql($"CREATE TABLE {ctx.QualifiedTable("batch_table")} (id INT NOT NULL, x DOUBLE, y DOUBLE, PRIMARY KEY (id))");
88 
89  var tableName = ctx.QualifiedTable("batch_table");
90 
91  // Insert 100 records (reduced from 1000 for faster testing)
92  var numRecords = 100;
93  for (int i = 0; i < numRecords; i++)
94  {
95  ctx.Kinetica.executeSql($"INSERT INTO {tableName} (id, x, y) VALUES ({i}, {i * 0.1}, {i * 0.2})");
96  }
97 
98  // Verify count using showTable with get_sizes option
99  var showTableResp = ctx.Kinetica.showTable(tableName, new Dictionary<string, string> { { "get_sizes", "true" } });
100  Assert.Equal(numRecords, showTableResp.total_size);
101  }
102  }
103 }
Test context that manages schema and cleanup for integration tests.
Definition: TestContext.cs:11