Skip to content

Commit da536f3

Browse files
committed
test: Modify unit tests
1 parent 3c7e2c4 commit da536f3

File tree

12 files changed

+51
-93
lines changed

12 files changed

+51
-93
lines changed

src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/TypeAlias/DefaultTypeAliasFactory.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public class DefaultTypeAliasFactory : MasaFactoryBase<ITypeAliasProvider, TypeA
1010

1111
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] TypeAlias, it was not found";
1212

13-
protected override MasaFactoryOptions<TypeAliasRelationOptions> FactoryOptions => _options.Value;
13+
protected override MasaFactoryOptions<TypeAliasRelationOptions> FactoryOptions => _options.CurrentValue;
1414

15-
private readonly IOptionsSnapshot<TypeAliasFactoryOptions> _options;
15+
private readonly IOptionsMonitor<TypeAliasFactoryOptions> _options;
1616

1717
public DefaultTypeAliasFactory(IServiceProvider serviceProvider) : base(serviceProvider)
1818
{
19-
_options = serviceProvider.GetRequiredService<IOptionsSnapshot<TypeAliasFactoryOptions>>();
19+
_options = serviceProvider.GetRequiredService<IOptionsMonitor<TypeAliasFactoryOptions>>();
2020
}
2121
}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/IdGenerator/DefaultIdGeneratorFactory.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public class DefaultIdGeneratorFactory : MasaFactoryBase<IIdGenerator, IdGenerat
2525
protected override string SpecifyServiceNotFoundMessage { get; } =
2626
"Please make sure you have used [{0}] IdGenerator, it was not found";
2727

28-
protected override MasaFactoryOptions<IdGeneratorRelationOptions> FactoryOptions => _options.Value;
28+
protected override MasaFactoryOptions<IdGeneratorRelationOptions> FactoryOptions => _options.CurrentValue;
2929

30-
private readonly IOptionsSnapshot<IdGeneratorFactoryOptions> _options;
30+
private readonly IOptionsMonitor<IdGeneratorFactoryOptions> _options;
3131

3232
public DefaultIdGeneratorFactory(IServiceProvider serviceProvider) : base(serviceProvider)
3333
{
34-
_options = serviceProvider.GetRequiredService<IOptionsSnapshot<IdGeneratorFactoryOptions>>();
34+
_options = serviceProvider.GetRequiredService<IOptionsMonitor<IdGeneratorFactoryOptions>>();
3535
}
3636

3737
public IIdGenerator<TOut> Create<TOut>() where TOut : notnull

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/Serialization/DefaultDeserializerFactory.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public class DefaultDeserializerFactory : MasaFactoryBase<IDeserializer, Deseria
99
protected override string DefaultServiceNotFoundMessage => "Default deserializer not found, you need to add it, like services.AddJson()";
1010

1111
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] deserializer, it was not found";
12-
protected override MasaFactoryOptions<DeserializerRelationOptions> FactoryOptions => _options.Value;
12+
protected override MasaFactoryOptions<DeserializerRelationOptions> FactoryOptions => _options.CurrentValue;
1313

14-
private readonly IOptionsSnapshot<DeserializerFactoryOptions> _options;
14+
private readonly IOptionsMonitor<DeserializerFactoryOptions> _options;
1515

1616
public DefaultDeserializerFactory(IServiceProvider serviceProvider) : base(serviceProvider)
1717
{
18-
_options = serviceProvider.GetRequiredService<IOptionsSnapshot<DeserializerFactoryOptions>>();
18+
_options = serviceProvider.GetRequiredService<IOptionsMonitor<DeserializerFactoryOptions>>();
1919
}
2020
}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/Serialization/DefaultSerializerFactory.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public class DefaultSerializerFactory : MasaFactoryBase<ISerializer, SerializerR
99
protected override string DefaultServiceNotFoundMessage => "Default serializer not found, you need to add it, like services.AddJson()";
1010

1111
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] serializer, it was not found";
12-
protected override MasaFactoryOptions<SerializerRelationOptions> FactoryOptions => _options.Value;
12+
protected override MasaFactoryOptions<SerializerRelationOptions> FactoryOptions => _options.CurrentValue;
1313

14-
private readonly IOptionsSnapshot<SerializerFactoryOptions> _options;
14+
private readonly IOptionsMonitor<SerializerFactoryOptions> _options;
1515

1616
public DefaultSerializerFactory(IServiceProvider serviceProvider) : base(serviceProvider)
1717
{
18-
_options = serviceProvider.GetRequiredService<IOptionsSnapshot<SerializerFactoryOptions>>();
18+
_options = serviceProvider.GetRequiredService<IOptionsMonitor<SerializerFactoryOptions>>();
1919
}
2020
}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/TypeConverts/DefaultTypeConvertFactory.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ protected override string DefaultServiceNotFoundMessage
1111

1212
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] typeConvert, it was not found";
1313

14-
protected override MasaFactoryOptions<TypeConvertRelationOptions> FactoryOptions => _options.Value;
14+
protected override MasaFactoryOptions<TypeConvertRelationOptions> FactoryOptions => _options.CurrentValue;
1515

16-
private readonly IOptionsSnapshot<TypeConvertFactoryOptions> _options;
16+
private readonly IOptionsMonitor<TypeConvertFactoryOptions> _options;
1717

1818
public DefaultTypeConvertFactory(IServiceProvider serviceProvider) : base(serviceProvider)
1919
{
20-
_options = serviceProvider.GetRequiredService<IOptionsSnapshot<TypeConvertFactoryOptions>>();
20+
_options = serviceProvider.GetRequiredService<IOptionsMonitor<TypeConvertFactoryOptions>>();
2121
}
2222
}

src/BuildingBlocks/RulesEngine/Masa.BuildingBlocks.RulesEngine/DefaultRulesEngineFactory.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ protected override string DefaultServiceNotFoundMessage
1212
protected override string SpecifyServiceNotFoundMessage
1313
=> $"Please make sure you have used [{0}] {nameof(IRulesEngineClient)}, it was not found";
1414

15-
protected override MasaFactoryOptions<RulesEngineRelationOptions> FactoryOptions => _options.Value;
15+
protected override MasaFactoryOptions<RulesEngineRelationOptions> FactoryOptions => _options.CurrentValue;
1616

17-
private readonly IOptionsSnapshot<RulesEngineFactoryOptions> _options;
17+
private readonly IOptionsMonitor<RulesEngineFactoryOptions> _options;
1818

1919
public DefaultRulesEngineFactory(IServiceProvider serviceProvider) : base(serviceProvider)
2020
{
21-
_options = serviceProvider.GetRequiredService<IOptionsSnapshot<RulesEngineFactoryOptions>>();
21+
_options = serviceProvider.GetRequiredService<IOptionsMonitor<RulesEngineFactoryOptions>>();
2222
}
2323
}

src/BuildingBlocks/SearchEngine/Masa.BuildingBlocks.SearchEngine.AutoComplete/AutoCompleteFactory.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ public class AutoCompleteFactory : MasaFactoryBase<IAutoCompleteClient, AutoComp
99
{
1010
protected override string DefaultServiceNotFoundMessage => "No default AutoComplete found";
1111
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] AutoComplete, it was not found";
12-
protected override MasaFactoryOptions<AutoCompleteRelationsOptions> FactoryOptions => _options.Value;
12+
protected override MasaFactoryOptions<AutoCompleteRelationsOptions> FactoryOptions => _options.CurrentValue;
1313

14-
private readonly IOptionsSnapshot<AutoCompleteFactoryOptions> _options;
14+
private readonly IOptionsMonitor<AutoCompleteFactoryOptions> _options;
1515

1616
public AutoCompleteFactory(IServiceProvider serviceProvider) : base(serviceProvider)
1717
{
18-
_options = serviceProvider.GetRequiredService<IOptionsSnapshot<AutoCompleteFactoryOptions>>();
18+
_options = serviceProvider.GetRequiredService<IOptionsMonitor<AutoCompleteFactoryOptions>>();
1919
}
2020

2121
public IAutoCompleteClient CreateClient() => base.Create();

src/Contrib/SearchEngine/AutoComplete/Masa.Contrib.SearchEngine.AutoComplete.ElasticSearch/AutoCompleteClient.cs

+11-19
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,21 @@ public AutoCompleteClient(
2424
IElasticClient elasticClient,
2525
IMasaElasticClient client,
2626
ILogger<AutoCompleteClient<TDocument>>? logger,
27-
string indexName,
28-
string? alias,
29-
Operator defaultOperator,
30-
SearchType defaultSearchType,
31-
bool enableMultipleCondition,
32-
string queryAnalyzer,
33-
Action<IIndexSettings>? indexSettingAction,
34-
Action<TypeMappingDescriptor<TDocument>>? action)
27+
AutoCompleteOptions<TDocument> options)
3528
{
3629
_elasticClient = elasticClient;
3730
_client = client;
3831
_logger = logger;
39-
_indexName = indexName;
40-
41-
MasaArgumentException.ThrowIfNullOrWhiteSpace(indexName);
42-
43-
_alias = alias;
44-
_defaultOperator = defaultOperator;
45-
_defaultSearchType = defaultSearchType;
46-
_enableMultipleCondition = enableMultipleCondition;
47-
_queryAnalyzer = queryAnalyzer;
48-
_indexSettingAction = indexSettingAction;
49-
_action = action;
32+
_indexName = options.IndexName;
33+
34+
MasaArgumentException.ThrowIfNullOrWhiteSpace(options.IndexName);
35+
_alias = options.Alias;
36+
_defaultOperator = options.DefaultOperator;
37+
_defaultSearchType = options.DefaultSearchType;
38+
_enableMultipleCondition = options.EnableMultipleCondition;
39+
_queryAnalyzer = options.QueryAnalyzer;
40+
_indexSettingAction = options.IndexSettingAction;
41+
_action = options.Action;
5042
}
5143

5244
public override async Task<bool> BuildAsync(CancellationToken cancellationToken = default)

src/Contrib/SearchEngine/AutoComplete/Masa.Contrib.SearchEngine.AutoComplete.ElasticSearch/Extensions/MasaElasticsearchBuilderExtensions.cs

+5-39
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ public static MasaElasticsearchBuilder AddAutoComplete<TValue>(
2525
string name) where TValue : notnull
2626
{
2727
var indexName = builder.ElasticClient.ConnectionSettings.DefaultIndex;
28-
if (string.IsNullOrEmpty(indexName))
29-
throw new ArgumentNullException(
30-
nameof(builder.ElasticClient.ConnectionSettings.DefaultIndex),
31-
"The default IndexName is not set");
28+
MasaArgumentException.ThrowIfNullOrWhiteSpace(indexName);
3229

3330
return builder.AddAutoComplete<AutoCompleteDocument<TValue>>(name, option => option.UseIndexName(indexName));
3431
}
@@ -53,21 +50,6 @@ public static MasaElasticsearchBuilder AddAutoComplete<TValue>(
5350
Action<AutoCompleteOptions<AutoCompleteDocument<TValue>>>? action) where TValue : notnull
5451
=> builder.AddAutoCompleteBySpecifyDocument(name, action);
5552

56-
[Obsolete($"{nameof(AddAutoComplete)} expired, please use {nameof(AddAutoCompleteBySpecifyDocument)}")]
57-
public static MasaElasticsearchBuilder AddAutoComplete<TDocument, TValue>(
58-
this MasaElasticsearchBuilder builder)
59-
where TDocument : AutoCompleteDocument
60-
where TValue : notnull
61-
=> builder.AddAutoCompleteBySpecifyDocument<TDocument>(DefaultName);
62-
63-
[Obsolete($"{nameof(AddAutoComplete)} expired, please use {nameof(AddAutoCompleteBySpecifyDocument)}")]
64-
public static MasaElasticsearchBuilder AddAutoComplete<TDocument, TValue>(
65-
this MasaElasticsearchBuilder builder,
66-
Action<AutoCompleteOptions<TDocument>>? action)
67-
where TDocument : AutoCompleteDocument
68-
where TValue : notnull
69-
=> builder.AddAutoCompleteBySpecifyDocument(DefaultName, action);
70-
7153
public static MasaElasticsearchBuilder AddAutoCompleteBySpecifyDocument<TDocument>(
7254
this MasaElasticsearchBuilder builder)
7355
where TDocument : AutoCompleteDocument
@@ -79,10 +61,8 @@ public static MasaElasticsearchBuilder AddAutoCompleteBySpecifyDocument<TDocumen
7961
where TDocument : AutoCompleteDocument
8062
{
8163
var indexName = builder.ElasticClient.ConnectionSettings.DefaultIndex;
82-
if (string.IsNullOrEmpty(indexName))
83-
throw new ArgumentNullException(
84-
nameof(builder.ElasticClient.ConnectionSettings.DefaultIndex),
85-
"The default IndexName is not set");
64+
65+
MasaArgumentException.ThrowIfNullOrWhiteSpace(indexName);
8666

8767
return builder.AddAutoCompleteBySpecifyDocument<TDocument>(name, option => option.UseIndexName(indexName));
8868
}
@@ -117,29 +97,15 @@ public static MasaElasticsearchBuilder AddAutoCompleteBySpecifyDocument<TDocumen
11797
serviceProvider.GetRequiredService<IElasticClientFactory>().Create(relationsOptions.Name),
11898
serviceProvider.GetRequiredService<IMasaElasticClientFactory>().Create(relationsOptions.Name),
11999
serviceProvider.GetService<ILogger<AutoCompleteClient<TDocument>>>(),
120-
options.IndexName,
121-
options.Alias,
122-
options.DefaultOperator,
123-
options.DefaultSearchType,
124-
options.EnableMultipleCondition,
125-
options.QueryAnalyzer,
126-
options.IndexSettingAction,
127-
options.Action
100+
options
128101
);
129102
}
130103

131104
return new AutoCompleteClient<TDocument>(
132105
builder.ElasticClient,
133106
builder.Client,
134107
serviceProvider.GetService<ILogger<AutoCompleteClient<TDocument>>>(),
135-
options.IndexName,
136-
options.Alias,
137-
options.DefaultOperator,
138-
options.DefaultSearchType,
139-
options.EnableMultipleCondition,
140-
options.QueryAnalyzer,
141-
options.IndexSettingAction,
142-
options.Action
108+
options
143109
);
144110
};
145111
}, builder.AlwaysGetNewestElasticClient ? ServiceLifetime.Scoped : ServiceLifetime.Singleton);

src/Contrib/SearchEngine/AutoComplete/Tests/Masa.Contrib.SearchEngine.AutoComplete.ElasticSearch.Tests/AutoCompleteTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public async Task TestPreciseAsync()
348348
option => option.UseNodes(_defaultNode).UseConnectionSettings(setting => setting.EnableApiVersioningHeader(false)));
349349

350350
await builder.Client.DeleteIndexByAliasAsync(employeeAlias);
351-
builder.AddAutoComplete<Employee, int>(option => option
351+
builder.AddAutoCompleteBySpecifyDocument<Employee>(option => option
352352
.UseIndexName(employeeIndexName)
353353
.UseAlias(employeeAlias)
354354
.UseDefaultSearchType(SearchType.Precise)

src/Contrib/Service/Caller/Masa.Contrib.Service.Caller/DefaultCallerFactory.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ internal class DefaultCallerFactory : MasaFactoryBase<ICaller, CallerRelationOpt
99

1010
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] Caller, it was not found";
1111

12-
protected override MasaFactoryOptions<CallerRelationOptions> FactoryOptions => _options.Value;
12+
protected override MasaFactoryOptions<CallerRelationOptions> FactoryOptions => _options.CurrentValue;
1313

14-
private readonly IOptionsSnapshot<CallerFactoryOptions> _options;
14+
private readonly IOptionsMonitor<CallerFactoryOptions> _options;
1515

1616
public DefaultCallerFactory(IServiceProvider serviceProvider) : base(serviceProvider)
1717
{
18-
_options = serviceProvider.GetRequiredService<IOptionsSnapshot<CallerFactoryOptions>>();
18+
_options = serviceProvider.GetRequiredService<IOptionsMonitor<CallerFactoryOptions>>();
1919
}
2020
}

src/Utils/Data/Masa.Utils.Data.Elasticsearch/DefaultElasticClientFactory.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@ namespace Masa.Utils.Data.Elasticsearch;
66
public class DefaultElasticClientFactory : IElasticClientFactory
77
{
88
private readonly IServiceProvider _serviceProvider;
9-
private readonly IOptionsSnapshot<ElasticsearchFactoryOptions> _elasticsearchFactoryOptions;
9+
private readonly IOptionsMonitor<ElasticsearchFactoryOptions> _elasticsearchFactoryOptions;
1010

1111
public DefaultElasticClientFactory(IServiceProvider serviceProvider,
12-
IOptionsSnapshot<ElasticsearchFactoryOptions> elasticsearchFactoryOptions)
12+
IOptionsMonitor<ElasticsearchFactoryOptions> elasticsearchFactoryOptions)
1313
{
1414
_serviceProvider = serviceProvider;
1515
_elasticsearchFactoryOptions = elasticsearchFactoryOptions;
1616
}
1717

1818
public IElasticClient Create()
1919
{
20-
var options = _elasticsearchFactoryOptions.Value;
20+
var options = _elasticsearchFactoryOptions.CurrentValue;
2121
var defaultOptions = GetDefaultOptions(options.Options);
2222
if (defaultOptions == null)
2323
throw new NotSupportedException("No default ElasticClient found");
2424

2525
return defaultOptions.Func.Invoke(_serviceProvider);
2626
}
2727

28-
private static ElasticsearchRelationsOptions? GetDefaultOptions(List<ElasticsearchRelationsOptions> optionsList)
29-
{
30-
return optionsList.SingleOrDefault(c => c.Name == Microsoft.Extensions.Options.Options.DefaultName) ??
31-
optionsList.FirstOrDefault();
32-
}
33-
3428
public IElasticClient Create(string name)
3529
{
36-
var options = _elasticsearchFactoryOptions.Value.Options.SingleOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
30+
var options = _elasticsearchFactoryOptions.CurrentValue.Options.SingleOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
3731
if (options == null)
3832
throw new NotSupportedException($"Please make sure you have used [{name}] ElasticClient, it was not found");
3933

4034
return options.Func.Invoke(_serviceProvider);
4135
}
36+
37+
private static ElasticsearchRelationsOptions? GetDefaultOptions(List<ElasticsearchRelationsOptions> optionsList)
38+
{
39+
return optionsList.SingleOrDefault(c => c.Name == Microsoft.Extensions.Options.Options.DefaultName) ??
40+
optionsList.FirstOrDefault();
41+
}
4242
}

0 commit comments

Comments
 (0)