Skip to content

Commit

Permalink
Adds tests for network address (cidr and inet) operators
Browse files Browse the repository at this point in the history
- Includes functional tests convering network address operators.

- Includes the network address function `ContainsOrContainedBy(...)` (previously omitted by mistake).

- Includes an inline patch for #426

- Reordering functions/operators to reflect the docs for version 10.

- TODO:
  - Add operators for `macaddr` and `macaddr8`.
  - Add some basic info to the docs.
  • Loading branch information
austindrenski committed May 26, 2018
1 parent 8b5f37a commit 1f23ca6
Show file tree
Hide file tree
Showing 3 changed files with 605 additions and 88 deletions.
140 changes: 84 additions & 56 deletions src/EFCore.PG/Extensions/NpgsqlNetworkAddressExtensions.cs
Expand Up @@ -37,62 +37,6 @@ namespace Microsoft.EntityFrameworkCore
[PublicAPI]
public static class NpgsqlNetworkAddressExtensions
{
/// <summary>
/// Determines whether an <see cref="IPAddress"/> contains another <see cref="IPAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="inet">The IP address to search.</param>
/// <param name="other">The IP address to locate.</param>
/// <returns>
/// True if the <see cref="IPAddress"/> contains the other <see cref="IPAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Contains([CanBeNull] this DbFunctions _, IPAddress inet, IPAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an (IPAddress Address, int Subnet) contains another (IPAddress Address, int Subnet).
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="cidr">The cidr to search.</param>
/// <param name="other">The cidr to locate.</param>
/// <returns>
/// True if the (IPAddress Address, int Subnet) contains the other (IPAddress Address, int Subnet); otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Contains([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> contains or is equal to another <see cref="IPAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="inet">The IP address to search.</param>
/// <param name="other">The IP address to locate.</param>
/// <returns>
/// True if the <see cref="IPAddress"/> contains or is equal to the other <see cref="IPAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool ContainsOrEqual([CanBeNull] this DbFunctions _, IPAddress inet, IPAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an (IPAddress Address, int Subnet) contains or is equal to another (IPAddress Address, int Subnet).
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="cidr">The cidr to search.</param>
/// <param name="other">The cidr to locate.</param>
/// <returns>
/// True if the (IPAddress Address, int Subnet) contains or is equal to the other (IPAddress Address, int Subnet); otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool ContainsOrEqual([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> is less than another <see cref="IPAddress"/>.
/// </summary>
Expand Down Expand Up @@ -317,6 +261,90 @@ public static class NpgsqlNetworkAddressExtensions
/// </exception>
public static bool ContainedByOrEqual([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> contains another <see cref="IPAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="inet">The IP address to search.</param>
/// <param name="other">The IP address to locate.</param>
/// <returns>
/// True if the <see cref="IPAddress"/> contains the other <see cref="IPAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Contains([CanBeNull] this DbFunctions _, IPAddress inet, IPAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an (IPAddress Address, int Subnet) contains another (IPAddress Address, int Subnet).
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="cidr">The cidr to search.</param>
/// <param name="other">The cidr to locate.</param>
/// <returns>
/// True if the (IPAddress Address, int Subnet) contains the other (IPAddress Address, int Subnet); otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool Contains([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> contains or is equal to another <see cref="IPAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="inet">The IP address to search.</param>
/// <param name="other">The IP address to locate.</param>
/// <returns>
/// True if the <see cref="IPAddress"/> contains or is equal to the other <see cref="IPAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool ContainsOrEqual([CanBeNull] this DbFunctions _, IPAddress inet, IPAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an (IPAddress Address, int Subnet) contains or is equal to another (IPAddress Address, int Subnet).
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="cidr">The cidr to search.</param>
/// <param name="other">The cidr to locate.</param>
/// <returns>
/// True if the (IPAddress Address, int Subnet) contains or is equal to the other (IPAddress Address, int Subnet); otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool ContainsOrEqual([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an <see cref="IPAddress"/> contains or is contained by another <see cref="IPAddress"/>.
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="inet">The IP address to search.</param>
/// <param name="other">The IP address to locate.</param>
/// <returns>
/// True if the <see cref="IPAddress"/> contains or is contained by the other <see cref="IPAddress"/>; otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool ContainsOrContainedBy([CanBeNull] this DbFunctions _, IPAddress inet, IPAddress other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Determines whether an (IPAddress Address, int Subnet) contains or is contained by another (IPAddress Address, int Subnet).
/// </summary>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="cidr">The cidr to search.</param>
/// <param name="other">The cidr to locate.</param>
/// <returns>
/// True if the (IPAddress Address, int Subnet) contains or is contained by the other (IPAddress Address, int Subnet); otherwise, false.
/// </returns>
/// <exception cref="ClientEvaluationNotSupportedException">
/// This method is only intended for use via SQL translation as part of an EF Core LINQ query.
/// </exception>
public static bool ContainsOrContainedBy([CanBeNull] this DbFunctions _, (IPAddress Address, int Subnet) cidr, (IPAddress Address, int Subnet) other) => throw new ClientEvaluationNotSupportedException();

/// <summary>
/// Computes the bitwise NOT operation on an <see cref="IPAddress"/>.
/// </summary>
Expand Down
Expand Up @@ -50,12 +50,6 @@ public Expression Translate(MethodCallExpression expression)

switch (expression.Method.Name)
{
case nameof(NpgsqlNetworkAddressExtensions.Contains):
return new CustomBinaryExpression(expression.Arguments[1], expression.Arguments[2], ">>", typeof(bool));

case nameof(NpgsqlNetworkAddressExtensions.ContainsOrEqual):
return new CustomBinaryExpression(expression.Arguments[1], expression.Arguments[2], ">>=", typeof(bool));

case nameof(NpgsqlNetworkAddressExtensions.LessThan):
return new CustomBinaryExpression(expression.Arguments[1], expression.Arguments[2], "<", typeof(bool));

Expand All @@ -80,6 +74,15 @@ public Expression Translate(MethodCallExpression expression)
case nameof(NpgsqlNetworkAddressExtensions.ContainedByOrEqual):
return new CustomBinaryExpression(expression.Arguments[1], expression.Arguments[2], "<<=", typeof(bool));

case nameof(NpgsqlNetworkAddressExtensions.Contains):
return new CustomBinaryExpression(expression.Arguments[1], expression.Arguments[2], ">>", typeof(bool));

case nameof(NpgsqlNetworkAddressExtensions.ContainsOrEqual):
return new CustomBinaryExpression(expression.Arguments[1], expression.Arguments[2], ">>=", typeof(bool));

case nameof(NpgsqlNetworkAddressExtensions.ContainsOrContainedBy):
return new CustomBinaryExpression(expression.Arguments[1], expression.Arguments[2], "&&", typeof(bool));

case nameof(NpgsqlNetworkAddressExtensions.Not):
return new CustomUnaryExpression(expression.Arguments[1], "~", expression.Arguments[1].Type);

Expand Down

0 comments on commit 1f23ca6

Please sign in to comment.