Skip to content

Commit

Permalink
Format tests
Browse files Browse the repository at this point in the history
  • Loading branch information
awaescher committed Dec 9, 2024
1 parent 7b7915e commit d6eb61b
Show file tree
Hide file tree
Showing 3 changed files with 713 additions and 766 deletions.
7 changes: 0 additions & 7 deletions test/FunctionalTests/ChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,27 @@ public class ChatTests
[SetUp]
public async Task Setup()
{
// Set up the test environment
_client = new OllamaApiClient(_baseUri);
_chat = new Chat(_client);

var modelExists = (await _client.ListLocalModelsAsync()).Any(m => m.Name == _model);

if (!modelExists)
await _client.PullModelAsync(_model).ToListAsync();
}

[TearDown]
public Task Teardown()
{
// Clean up the test environment
((IChatClient?)_client)?.Dispose();

return Task.CompletedTask;
}


[Test]
public async Task SendAsync_ShouldSucceed()
{
// Arrange
_client.SelectedModel = _model;

// Act
var response = await _chat
.SendAsync("What is the ultimate answer to " +
"life, the universe, and everything, as specified in " +
Expand All @@ -53,7 +47,6 @@ public async Task SendAsync_ShouldSucceed()
CancellationToken.None)
.StreamToEndAsync();

// Assert
response.Should().NotBeNullOrEmpty();
response.Should().ContainAny("42", "forty-two", "forty two");
}
Expand Down
55 changes: 5 additions & 50 deletions test/FunctionalTests/OllamaApiClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,41 @@ public class OllamaApiClientTests
[SetUp]
public async Task Setup()
{
// Set up the test environment
_client = new OllamaApiClient(_baseUri);

await CleanupModel(_localModel);
}

[TearDown]
public async Task Teardown()
{
// Clean up the test environment
await CleanupModel(_localModel);

((IChatClient?)_client)?.Dispose();
}

private async Task CleanupModel(string? model = null)
{
var modelExists = (await _client.ListLocalModelsAsync())
.Any(m => m.Name == (model ?? _model));
var modelExists = (await _client.ListLocalModelsAsync()).Any(m => m.Name == (model ?? _model));

if (modelExists)
await _client.DeleteModelAsync(new DeleteModelRequest { Model = model ?? _model });
}

private async Task PullIfNotExists(string model)
{
var modelExists = (await _client.ListLocalModelsAsync())
.Any(m => m.Name == model);
var modelExists = (await _client.ListLocalModelsAsync()).Any(m => m.Name == model);

if (!modelExists)
await _client.PullModelAsync(new PullModelRequest { Model = model })
.ToListAsync();
await _client.PullModelAsync(new PullModelRequest { Model = model }).ToListAsync();
}


[Test, Category("Ft"), Order(1), Ignore("Prevent the model from being downloaded each test run")]
[Test, Order(1), Ignore("Prevent the model from being downloaded each test run")]
public async Task PullModel()
{
// Act
var response = await _client
.PullModelAsync(new PullModelRequest { Model = _model })
.ToListAsync();

// Assert
var models = await _client.ListLocalModelsAsync();
models.Should().Contain(m => m.Name == _model);

Expand All @@ -77,7 +68,6 @@ public async Task PullModel()
[Test, Order(2)]
public async Task CreateModel()
{
// Arrange
await PullIfNotExists(_localModel);

var model = new CreateModelRequest
Expand All @@ -94,12 +84,10 @@ PARAMETER num_ctx 100
"""
};

// Act
var response = await _client
.CreateModelAsync(model)
.ToListAsync();

// Assert
var models = await _client.ListLocalModelsAsync();
models.Should().Contain(m => m.Name.StartsWith(_localModel));

Expand All @@ -110,34 +98,27 @@ PARAMETER num_ctx 100
[Test, Order(3)]
public async Task CopyModel()
{
// Arrange
await PullIfNotExists(_localModel);

var model = new CopyModelRequest { Source = _localModel, Destination = $"{_localModel}-copy" };

// Act
await _client.CopyModelAsync(model);

// Assert
var models = await _client.ListLocalModelsAsync();
models.Should().Contain(m => m.Name == $"{_localModel}-copy:latest");

// Clean up
await _client.DeleteModelAsync(new DeleteModelRequest { Model = $"{_localModel}-copy:latest" });
}

[Test]
public async Task Embed()
{
// Arrange
await PullIfNotExists(_embeddingModel);

var request = new EmbedRequest { Model = _embeddingModel, Input = ["Hello, world!"] };

// Act
var response = await _client.EmbedAsync(request);

// Assert
response.Should().NotBeNull();
response.Embeddings.Should().NotBeEmpty();
response.LoadDuration.Should().BeGreaterThan(100, "Because loading the model should take some time");
Expand All @@ -147,18 +128,15 @@ public async Task Embed()
[Test]
public async Task ListLocalModels()
{
// Act
var models = (await _client.ListLocalModelsAsync()).ToList();

// Assert
models.Should().NotBeEmpty();
models.Should().Contain(m => m.Name == _model);
}

[Test]
public async Task ListRunningModels()
{
// Arrange
await PullIfNotExists(_model);
var backgroundTask = Task.Run(async () =>
{
Expand All @@ -171,28 +149,21 @@ public async Task ListRunningModels()
await generate;
});

// Act
var modelsTask = _client.ListRunningModelsAsync();

await Task.WhenAll(backgroundTask, modelsTask);

// Assert
var models = modelsTask.Result.ToList();

models.Should().NotBeEmpty();
models.Should().Contain(m => m.Name == _model);
}

[Test]
public async Task ShowModel()
{
// Arrange
await PullIfNotExists(_model);

// Act
var response = await _client.ShowModelAsync(new ShowModelRequest { Model = _model });

// Assert
response.Should().NotBeNull();
response.Info.Should().NotBeNull();
response.Info.Architecture.Should().Be("llama");
Expand All @@ -204,34 +175,28 @@ public async Task ShowModel()
[Test]
public async Task DeleteModel()
{
// Arrange
await PullIfNotExists(_localModel);
await _client.CopyModelAsync(new CopyModelRequest
{
Source = _localModel,
Destination = $"{_localModel}-copy"
});

var exists = (await _client.ListLocalModelsAsync())
.Any(m => m.Name == $"{_localModel}-copy:latest");
var exists = (await _client.ListLocalModelsAsync()).Any(m => m.Name == $"{_localModel}-copy:latest");

exists.Should().BeTrue();

// Act
await _client.DeleteModelAsync(new DeleteModelRequest { Model = $"{_localModel}-copy:latest" });

// Assert
var models = await _client.ListLocalModelsAsync();
models.Should().NotContain(m => m.Name == $"{_localModel}-copy:latest");
}

[Test]
public async Task GenerateAsync()
{
// Arrange
await PullIfNotExists(_model);

// Act
var response = await _client.GenerateAsync(new GenerateRequest
{
Model = _model,
Expand All @@ -242,18 +207,15 @@ public async Task GenerateAsync()

var joined = string.Join("", response.Select(r => r.Response));

// Assert
response.Should().NotBeEmpty();
joined.Should().Contain("42");
}

[Test]
public async Task ChatAsync()
{
// Arrange
await PullIfNotExists(_model);

// Act
var response = await _client.ChatAsync(new ChatRequest
{
Model = _model,
Expand All @@ -280,28 +242,21 @@ public async Task ChatAsync()

var joined = string.Join("", response.Select(r => r.Message.Content));

// Assert
response.Should().NotBeEmpty();
joined.Should().Contain("Douglas Adams");
}

[Test]
public async Task IsRunningAsync()
{
// Act
var response = await _client.IsRunningAsync();

// Assert
response.Should().BeTrue();
}

[Test]
public async Task GetVersionAsync()
{
// Act
var response = await _client.GetVersionAsync();

// Assert
response.Should().NotBeNull();
}
}
Loading

0 comments on commit d6eb61b

Please sign in to comment.