Kinetica   C#   API  Version 7.2.3.1
TestContext.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using kinetica;
4 
6 {
11  public class TestContext : IDisposable
12  {
13  public kinetica.Kinetica Kinetica { get; }
14  public string SchemaName { get; }
15 
16  private bool _disposed = false;
17 
21  public static (string url, string username, string password) GetConnectionConfig()
22  {
23  var url = Environment.GetEnvironmentVariable("KINETICA_URL") ?? "http://localhost:9191";
24  var username = Environment.GetEnvironmentVariable("KINETICA_USER") ?? "admin";
25  var password = Environment.GetEnvironmentVariable("KINETICA_PASSWORD") ?? "secret";
26 
27  return (url, username, password);
28  }
29 
34  public TestContext(string testName)
35  {
36  var (url, username, password) = GetConnectionConfig();
37 
38  var options = new kinetica.Kinetica.Options
39  {
40  Username = username,
41  Password = password,
42  UseSnappy = false
43  };
44 
45  Kinetica = new kinetica.Kinetica(url, options);
46  SchemaName = $"test_{testName}";
47 
48  // Drop schema if it exists (CASCADE drops all contained objects)
49  try
50  {
51  var dropSql = $"DROP SCHEMA IF EXISTS {SchemaName} CASCADE";
52  Kinetica.executeSql(dropSql, 0, -9999);
53  }
54  catch { /* Ignore errors on drop */ }
55 
56  // Create fresh schema
57  try
58  {
59  var schemaOptions = new Dictionary<string, string>
60  {
61  { "no_error_if_exists", "true" }
62  };
63  Kinetica.createSchema(SchemaName, schemaOptions);
64  }
65  catch (Exception ex)
66  {
67  throw new Exception($"Failed to create test schema '{SchemaName}': {ex.Message}", ex);
68  }
69  }
70 
74  public string QualifiedTable(string tableName)
75  {
76  return $"{SchemaName}.{tableName}";
77  }
78 
82  public void Cleanup()
83  {
84  try
85  {
86  var dropSql = $"DROP SCHEMA IF EXISTS {SchemaName} CASCADE";
87  Kinetica.executeSql(dropSql, 0, -9999);
88  }
89  catch { /* Best effort cleanup */ }
90  }
91 
92  public void Dispose()
93  {
94  if (!_disposed)
95  {
96  Cleanup();
97  _disposed = true;
98  }
99  }
100  }
101 
105  public static class DataGenerators
106  {
107  private static readonly Random _random = new Random();
108 
109  public static string RandomString(int length)
110  {
111  const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
112  var result = new char[length];
113  for (int i = 0; i < length; i++)
114  {
115  result[i] = chars[_random.Next(chars.Length)];
116  }
117  return new string(result);
118  }
119 
120  public static int RandomInt()
121  {
122  return _random.Next();
123  }
124 
125  public static double RandomDouble()
126  {
127  return _random.NextDouble();
128  }
129  }
130 }
Test context that manages schema and cleanup for integration tests.
Definition: TestContext.cs:11
string? Username
Optional: User Name for Kinetica security
Definition: Kinetica.cs:157
static string RandomString(int length)
Definition: TestContext.cs:109
void Cleanup()
Cleanup - drop the test schema.
Definition: TestContext.cs:82
string QualifiedTable(string tableName)
Get a qualified table name (schema.table).
Definition: TestContext.cs:74
bool UseSnappy
Use Snappy
Definition: Kinetica.cs:177
TestContext(string testName)
Create a new test context with an isolated schema.
Definition: TestContext.cs:34
Connection Options
Definition: Kinetica.cs:50
static string url
Get connection configuration from environment variables.
Definition: TestContext.cs:21
Failover to clusters in a random order (default)
Data generators for testing.
Definition: TestContext.cs:105
API to talk to Kinetica Database
Definition: Kinetica.cs:40