-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
SqliteVersionConditionAttribute.cs
75 lines (61 loc) · 1.93 KB
/
SqliteVersionConditionAttribute.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore.TestUtilities.Xunit;
namespace Microsoft.EntityFrameworkCore.TestUtilities;
#nullable enable
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class SqliteVersionConditionAttribute : Attribute, ITestCondition
{
private Version? _min;
private Version? _max;
private Version? _skip;
public string? Min
{
get => _min?.ToString();
set => _min = value is null ? null : new Version(value);
}
public string? Max
{
get => _max?.ToString();
set => _max = value is null ? null : new Version(value);
}
public string? Skip
{
get => _skip?.ToString();
set => _skip = value is null ? null : new Version(value);
}
private static Version? Current
{
get
{
var connection = new SqliteConnection("Data Source=:memory:;");
return connection.ServerVersion != null ? new Version(connection.ServerVersion) : null;
}
}
public ValueTask<bool> IsMetAsync()
{
if (Current == _skip)
{
return new ValueTask<bool>(false);
}
if (_min == null
&& _max == null)
{
return new ValueTask<bool>(true);
}
if (_min == null)
{
return new ValueTask<bool>(Current <= _max);
}
return new ValueTask<bool>(_max == null ? Current >= _min : Current <= _max && Current >= _min);
}
private string? _skipReason;
public string SkipReason
{
set => _skipReason = value;
get => _skipReason
?? $"Test only runs for SQLite versions >= {Min ?? "Any"} and <= {Max ?? "Any"}"
+ (Skip == null ? "" : "and skipping on " + Skip);
}
}