-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTest1.cs
More file actions
151 lines (123 loc) · 4.25 KB
/
Copy pathUnitTest1.cs
File metadata and controls
151 lines (123 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Diagnostics;
using Jigen.Client;
using Jigen.Client.BaseTypes;
using Jigen.Proto;
using JigenClientTest.Model;
using Xunit.Abstractions;
namespace JigenClientTest;
public class UnitTest1
{
private readonly ITestOutputHelper _testOutputHelper;
private DB db;
public UnitTest1(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
db = new DB(new ConnectionOptions()
{
HostName = "localhost",
Port = 3223,
TLS = false,
DatabaseName = "Test"
});
}
[Fact]
public void Insert()
{
db.Sentences.Add(1, new VectorEntry<Entity1>()
{
Key = 1, Content = new Entity1() { Id = Guid.NewGuid(), Sentence = "blablabla", Title = "allora..." }, Embedding = Array.Empty<float>()
});
}
[Theory]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
[InlineData(1000)]
[InlineData(10_000)]
[InlineData(100_000)]
[InlineData(1_000_000)]
public void Insert2(int count)
{
_testOutputHelper.WriteLine($"Inserting {count}");
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < count; i++)
{
db.Sentences.Add(i, new VectorEntry<Entity1>()
{
Key = i,
Content = new Entity1() { Id = Guid.NewGuid(), Sentence = "blablabla", Title = "allora..." }, Embedding = Array.Empty<float>()
});
}
sw.Stop();
_testOutputHelper.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}");
}
[Fact]
public void SerializationRoundTripTest()
{
// Arrange
var testKey = 9999;
var testKeyVector = (VectorKey)testKey;
var expectedEntity = new Entity1()
{
Id = Guid.NewGuid(),
Title = "Serialization Test",
Sentence = "tes tets" // Space between words to verify exact match
};
_testOutputHelper.WriteLine($"Writing entity with Sentence: '{expectedEntity.Sentence}'");
// Act: Write
db.Sentences.Add(testKeyVector, new VectorEntry<Entity1>()
{
Key = testKeyVector,
Content = expectedEntity,
Embedding = Array.Empty<float>()
});
// Act: Read back
_testOutputHelper.WriteLine($"Reading entity back with key: {testKey}");
var success = db.Sentences.TryGetValue(testKeyVector, out var retrievedEntry);
// Assert: Content matches exactly
Assert.True(success, "Failed to retrieve the written entity");
Assert.NotNull(retrievedEntry);
Assert.NotNull(retrievedEntry.Content);
var retrievedEntity = retrievedEntry.Content;
_testOutputHelper.WriteLine($"Retrieved entity with Sentence: '{retrievedEntity.Sentence}'");
Assert.Equal(expectedEntity.Sentence, retrievedEntity.Sentence);
Assert.Equal(expectedEntity.Title, retrievedEntity.Title);
Assert.Equal(expectedEntity.Id, retrievedEntity.Id);
_testOutputHelper.WriteLine("✓ Serialization round-trip verified: content is identical");
}
[Fact]
public void SearchBySemanticSimilarityTest()
{
// Arrange
var testKey = 8888;
var testKeyVector = (VectorKey)testKey;
var indexedEntity = new Entity1()
{
Id = Guid.NewGuid(),
Title = "Semantic Search Test",
Sentence = "tes tets"
};
_testOutputHelper.WriteLine($"Indexing entity with Sentence: '{indexedEntity.Sentence}'");
// Act: Write with sentence for embedding
db.Sentences.Add(testKeyVector, new VectorEntry<Entity1>()
{
Key = testKeyVector,
Content = indexedEntity,
Embedding = db.ServiceClient.CalculateEmbeddings(new EmbeddingRequest(){Message = "test test test test"}).Embeddings.ToArray()
});
// Act: Search for semantically similar text
var searchQuery = "test";
_testOutputHelper.WriteLine($"Searching for: '{searchQuery}'");
var results = db.Sentences.Search(searchQuery, top: 10);
// Assert: Should find the indexed document
_testOutputHelper.WriteLine($"Found {results.Count} results");
Assert.NotEmpty(results);
var found = results.FirstOrDefault(r => r.Key.Value.SequenceEqual(testKeyVector.Value));
Assert.NotNull(found);
Assert.NotNull(found.Content);
var foundEntity = found.Content;
Assert.Equal("tes tets", foundEntity.Sentence);
_testOutputHelper.WriteLine($"✓ Semantic search verified: found '{foundEntity.Sentence}' when searching for '{searchQuery}'");
}
}