|
|
|
|
|
|
|
|
using OllamaStudy.Core;
|
|
|
|
|
|
namespace OllamaStudy.CoreTest;
|
|
|
|
|
|
public class OllamaServerConfigTest
|
|
|
{
|
|
|
private readonly ITestOutputHelper _testOutput;
|
|
|
private readonly string _configName = "OllamaServerConfig.json";
|
|
|
private string _defaultConfigText = string.Empty;
|
|
|
private readonly OllamaServerOption expectedOption = new OllamaServerOption();
|
|
|
|
|
|
public OllamaServerConfigTest(ITestOutputHelper testOutput)
|
|
|
{
|
|
|
_testOutput = testOutput;
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void Config_Test()
|
|
|
{
|
|
|
//Arrange(准备阶段)
|
|
|
var configBuilder = new ConfigurationBuilder()
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
.AddJsonFile(_configName, false, true);
|
|
|
|
|
|
var config = configBuilder.Build();
|
|
|
|
|
|
//Act(执行阶段)
|
|
|
var serverUrl = config.GetValue<string>("OllamaServer:OllamaServerUrl");
|
|
|
var modelId = config.GetValue<string>("OllamaServer:Model");
|
|
|
|
|
|
//Assert(断言阶段)
|
|
|
Assert.NotNull(serverUrl);
|
|
|
Assert.NotNull(modelId);
|
|
|
Assert.Equal("http://localhost:11434", serverUrl);
|
|
|
Assert.Equal("qwen3:0.6b",modelId);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void Config_Bind_Test()
|
|
|
{
|
|
|
var configBuild = new ConfigurationBuilder()
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
.AddJsonFile(_configName, false, true);
|
|
|
|
|
|
var config = configBuild.Build();
|
|
|
|
|
|
//邦定
|
|
|
var option = new OllamaServerOption() { OllamaServerUrl="",Model=""};
|
|
|
|
|
|
//config.Bind(option);
|
|
|
config.GetSection("OllamaServer").Bind(option);
|
|
|
|
|
|
//断言
|
|
|
Assert.Equal("http://localhost:11434",option.OllamaServerUrl);
|
|
|
Assert.Equal("qwen3:0.6b",option.Model);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void Get_ServerOptions_Test()
|
|
|
{
|
|
|
//Arrange(准备阶段)
|
|
|
var configBuilder = new ConfigurationBuilder()
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
.AddJsonFile(_configName, false, true);
|
|
|
|
|
|
var config = configBuilder.Build();
|
|
|
|
|
|
//Act(执行阶段)
|
|
|
var serverUrl = config.GetValue<string>("OllamaServer:OllamaServerUrl");
|
|
|
var modelId = config.GetValue<string>("OllamaServer:Model");
|
|
|
|
|
|
var option = new OllamaServerOption()
|
|
|
{
|
|
|
OllamaServerUrl = serverUrl!,
|
|
|
Model = modelId!
|
|
|
};
|
|
|
|
|
|
|
|
|
//Assert(断言阶段)
|
|
|
Assert.NotNull(serverUrl);
|
|
|
Assert.NotNull(modelId);
|
|
|
Assert.Equal("http://localhost:11434", serverUrl);
|
|
|
Assert.Equal("qwen3:0.6b", modelId);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void Get_IOption_Test()
|
|
|
{
|
|
|
//Arrange(准备阶段)
|
|
|
var configBuilder = new ConfigurationBuilder()
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
.AddJsonFile(_configName, false, true);
|
|
|
var config = configBuilder.Build();
|
|
|
|
|
|
var services = new ServiceCollection();
|
|
|
services
|
|
|
.AddOptions()
|
|
|
.Configure<OllamaServerOption>(config.GetRequiredSection("OllamaServer"));
|
|
|
|
|
|
//Act(执行阶段)
|
|
|
var o = services.BuildServiceProvider().GetRequiredService<IOptions<OllamaServerOption>>();
|
|
|
|
|
|
var ollamaServerOption = o.Value;
|
|
|
|
|
|
//Assert(断言阶段)
|
|
|
Assert.NotNull(ollamaServerOption);
|
|
|
Assert.Equal("http://localhost:11434", ollamaServerOption.OllamaServerUrl);
|
|
|
Assert.Equal("qwen3:0.6b", ollamaServerOption.Model);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void Get_IOptionsSnapshot_Test()
|
|
|
{
|
|
|
//Arrange(准备阶段)
|
|
|
var configBuilder = new ConfigurationBuilder()
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
.AddJsonFile(_configName, false, true);
|
|
|
var config = configBuilder.Build();
|
|
|
|
|
|
var services = new ServiceCollection();
|
|
|
services
|
|
|
.AddOptions()
|
|
|
.Configure<OllamaServerOption>(config.GetRequiredSection("OllamaServer"));
|
|
|
|
|
|
//Act(执行阶段)
|
|
|
var o = services.BuildServiceProvider().GetRequiredService<IOptionsSnapshot<OllamaServerOption>>();
|
|
|
|
|
|
var ollamaServerOption = o.Value;
|
|
|
|
|
|
//Assert(断言阶段)
|
|
|
Assert.NotNull(ollamaServerOption);
|
|
|
Assert.Equal("http://localhost:11434", ollamaServerOption.OllamaServerUrl);
|
|
|
Assert.Equal("qwen3:0.6b", ollamaServerOption.Model);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void Get_IOptionsMonitor_Test()
|
|
|
{
|
|
|
//Arrange(准备阶段)
|
|
|
var configBuilder = new ConfigurationBuilder()
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
.AddJsonFile(_configName,false,true);
|
|
|
var config = configBuilder.Build();
|
|
|
|
|
|
var services = new ServiceCollection();
|
|
|
services
|
|
|
.AddOptions()
|
|
|
.Configure<OllamaServerOption>(config.GetRequiredSection("OllamaServer"));
|
|
|
|
|
|
//Act(执行阶段)
|
|
|
var provider = services.BuildServiceProvider();
|
|
|
var optionMonitor = provider.GetRequiredService<IOptionsMonitor<OllamaServerOption>>();
|
|
|
|
|
|
var ollamaServerOption = optionMonitor.CurrentValue;
|
|
|
|
|
|
//Assert(断言阶段)
|
|
|
Assert.NotNull(ollamaServerOption);
|
|
|
Assert.Equal("http://localhost:11434", ollamaServerOption.OllamaServerUrl);
|
|
|
Assert.Equal("qwen3:0.6b", ollamaServerOption.Model);
|
|
|
|
|
|
//修改配置文件后,再次获取值,否会更新?
|
|
|
ChangeConfig();
|
|
|
ollamaServerOption = optionMonitor.CurrentValue;
|
|
|
|
|
|
//Assert(断言阶段)
|
|
|
Assert.Equal("http://localhost:11434", ollamaServerOption.OllamaServerUrl);
|
|
|
Assert.NotEqual("qwen3:0.6b", ollamaServerOption.Model);
|
|
|
|
|
|
ResetConfig();
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void Get_IOptionsMonitorCache_Test()
|
|
|
{
|
|
|
//Arrange(准备阶段)
|
|
|
var configBuilder = new ConfigurationBuilder()
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
.AddJsonFile(_configName, false, true);
|
|
|
var config = configBuilder.Build();
|
|
|
|
|
|
var services = new ServiceCollection();
|
|
|
services
|
|
|
.AddOptions()
|
|
|
.Configure<OllamaServerOption>(config.GetRequiredSection("OllamaServer"));
|
|
|
|
|
|
//Act(执行阶段)
|
|
|
var o = services.BuildServiceProvider().GetRequiredService<IOptionsMonitorCache<OllamaServerOption>>();
|
|
|
|
|
|
var ollamaServerOption = o.GetOrAdd(Options.DefaultName, ()=>new OllamaServerOption());
|
|
|
|
|
|
//Assert(断言阶段)
|
|
|
Assert.NotNull(ollamaServerOption);
|
|
|
Assert.Equal("http://localhost:11434", ollamaServerOption.OllamaServerUrl);
|
|
|
Assert.Equal("qwen3:0.6b", ollamaServerOption.Model);
|
|
|
}
|
|
|
|
|
|
//改变配置文件
|
|
|
private void ChangeConfig()
|
|
|
{
|
|
|
ConfigBox? configBox = new ConfigBox();
|
|
|
|
|
|
var filePath = Path.Combine(Directory.GetCurrentDirectory(), _configName);
|
|
|
using (var file = System.IO.File.OpenText(filePath))
|
|
|
{
|
|
|
var configText = file.ReadToEnd();
|
|
|
configBox = System.Text.Json.JsonSerializer.Deserialize<ConfigBox>(configText);
|
|
|
|
|
|
//保存默认配置
|
|
|
this._defaultConfigText = configText;
|
|
|
}
|
|
|
|
|
|
if (configBox == null || configBox.OllamaServer == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
//修改内容
|
|
|
configBox.OllamaServer.Model = Guid.NewGuid().ToString() + "_Model";
|
|
|
|
|
|
System.Text.Json.JsonSerializerOptions serializerOptions = new System.Text.Json.JsonSerializerOptions()
|
|
|
{
|
|
|
WriteIndented = true,
|
|
|
};
|
|
|
|
|
|
var newConfigText = System.Text.Json.JsonSerializer.Serialize(configBox, serializerOptions);
|
|
|
|
|
|
File.WriteAllText(filePath, newConfigText);
|
|
|
|
|
|
//暂停几秒,让配置文件生效
|
|
|
Thread.Sleep(3000);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 恢复配置文件
|
|
|
/// </summary>
|
|
|
private void ResetConfig()
|
|
|
{
|
|
|
var filePath = Path.Combine(Directory.GetCurrentDirectory(), _configName);
|
|
|
File.WriteAllText(filePath, this._defaultConfigText);
|
|
|
}
|
|
|
|
|
|
private class ConfigBox
|
|
|
{
|
|
|
public OllamaServerOption? OllamaServer{ get; set; }
|
|
|
}
|
|
|
}
|