Skip to content

Commit

Permalink
- Change data type from decimal to double.
Browse files Browse the repository at this point in the history
- Refactor test case.
  • Loading branch information
HuyLuong committed Nov 10, 2020
1 parent 3052c98 commit 59b8c65
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public virtual SqlExpression Translate(
nullable: false,
argumentsPropagateNullability: Array.Empty<bool>(),
method.ReturnType),
_sqlExpressionFactory.Constant(9223372036854775807.0))
_sqlExpressionFactory.Constant(9223372036854780000.0))
},
nullable: false,
argumentsPropagateNullability: Array.Empty<bool>(),
Expand Down
10 changes: 4 additions & 6 deletions src/EFCore/DbFunctionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ public static bool Like(
/// </para>
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <returns>a random decimal number between 0 and 1.</returns>
public static decimal Random([CanBeNull] this DbFunctions _)
/// <returns>a random double number between 0 and 1.</returns>
public static double Random([CanBeNull] this DbFunctions _)
=> RandomCore();

private static bool LikeCore(string matchExpression, string pattern, string escapeCharacter)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Like)));
private static bool LikeCore(string matchExpression, string pattern, string escapeCharacter) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Like)));

private static decimal RandomCore()
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Random)));
private static double RandomCore() => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Random)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ public virtual Task Like_all_literals_with_escape(bool async)
c => EF.Functions.Like("%", "!%", "!"),
c => "%".Contains("%"));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Random_return_less_than_1(bool async)
=> AssertCount(
async,
ss => ss.Set<Order>(),
ss => ss.Set<Order>(),
ss => EF.Functions.Random() < 1,
c => true);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Random_return_greater_than_0(bool async)
=> AssertCount(
async,
ss => ss.Set<Order>(),
ss => ss.Set<Order>(),
ss => EF.Functions.Random() >= 0,
c => true);

protected NorthwindContext CreateContext()
=> Fixture.CreateContext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1149,38 +1149,24 @@ FROM [Orders] AS [o]
}
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Random_return_less_than_1(bool async)
public override async Task Random_return_less_than_1(bool async)
{
await AssertCount(
async,
ss => ss.Set<Order>(),
ss => ss.Set<Order>(),
ss => EF.Functions.Random() < 1,
c => true);
await base.Random_return_less_than_1(async);

AssertSql(
@"SELECT COUNT(*)
FROM [Orders] AS [o]
WHERE RAND() < 1.0");
WHERE RAND() < 1.0E0");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Random_return_greater_than_0(bool async)
public override async Task Random_return_greater_than_0(bool async)
{
await AssertCount(
async,
ss => ss.Set<Order>(),
ss => ss.Set<Order>(),
ss => EF.Functions.Random() >= 0,
c => true);
await base.Random_return_greater_than_0(async);

AssertSql(
@"SELECT COUNT(*)
FROM [Orders] AS [o]
WHERE RAND() >= 0.0");
WHERE RAND() >= 0.0E0");
}

private void AssertSql(params string[] expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,29 @@ protected override string CaseInsensitiveCollation
protected override string CaseSensitiveCollation
=> "BINARY";

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Random_return_less_than_1(bool async)
public override async Task Random_return_less_than_1(bool async)
{
await AssertCount(
async,
ss => ss.Set<Order>(),
ss => ss.Set<Order>(),
ss => EF.Functions.Random() < 1,
ss => EF.Functions.Random() <= 1,
c => true);

AssertSql(
@"SELECT COUNT(*)
FROM ""Orders"" AS ""o""
WHERE ef_compare(abs(random() / '9223372036854780000.0'), '1.0') < 0");
WHERE abs(random() / 9.2233720368547799E+18) <= 1.0");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Random_return_greater_than_0(bool async)
public override async Task Random_return_greater_than_0(bool async)
{
await AssertCount(
async,
ss => ss.Set<Order>(),
ss => ss.Set<Order>(),
ss => EF.Functions.Random() >= 0,
c => true);
await base.Random_return_greater_than_0(async);

AssertSql(
@"SELECT COUNT(*)
FROM ""Orders"" AS ""o""
WHERE ef_compare(abs(random() / '9223372036854780000.0'), '0.0') >= 0");
WHERE abs(random() / 9.2233720368547799E+18) >= 0.0");
}

private void AssertSql(params string[] expected)
Expand Down

0 comments on commit 59b8c65

Please sign in to comment.