Skip to content

PgAssert

Namespace: Ankus

Assembly: Ankus.Runtime.dll

Checks conditions inside backend tests, as pgrx tests use assert! and assert_eq!.

public static class PgAssert

A failed check throws PgAssertException, which fails the PgTestAttribute method with a message naming the expected and actual values. Test frameworks such as MSTest do not load inside an extension, so use these checks in backend code and the framework’s own assertions in managed tests.

Inheritance: object

Checks that two floating-point values differ by no more than a tolerance, as MSTest’s Assert.AreEqual with a delta does.

public static void AreEqual(double expected, double actual, double delta, string? message = null)

Parameters:

expected — double

The expected value.

actual — double

The actual value.

delta — double

The largest allowed difference; NaN equals NaN.

message — string

An optional explanation.

Exceptions:

Checks that two values are equal by Default.

public static void AreEqual<T>(T expected, T actual, string? message = null)

Type parameters:

T

The value type.

Parameters:

expected — T

The expected value.

actual — T

The actual value.

message — string

An optional explanation.

Exceptions:

Checks that two values differ by Default.

public static void AreNotEqual<T>(T notExpected, T actual, string? message = null)

Type parameters:

T

The value type.

Parameters:

notExpected — T

The value that must not appear.

actual — T

The actual value.

message — string

An optional explanation.

Exceptions:

AreSequenceEqual<T>(IEnumerable<T>, IEnumerable<T>, string?)

Section titled “AreSequenceEqual<T>(IEnumerable<T>, IEnumerable<T>, string?)”

Checks that two sequences hold equal elements in the same order.

public static void AreSequenceEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, string? message = null)

Type parameters:

T

The element type.

Parameters:

expected — IEnumerable<T>

The expected elements.

actual — IEnumerable<T>

The actual elements.

message — string

An optional explanation.

Exceptions:

Checks that a string contains a substring, compared ordinally.

public static void Contains(string substring, string? value, string? message = null)

Parameters:

substring — string

The text that must appear.

value — string

The string to search.

message — string

An optional explanation.

Exceptions:

Checks that a string does not contain a substring, compared ordinally.

public static void DoesNotContain(string substring, string? value, string? message = null)

Parameters:

substring — string

The text that must not appear.

value — string

The string to search.

message — string

An optional explanation.

Exceptions:

Checks that a string ends with a suffix, compared ordinally.

public static void EndsWith(string suffix, string? value, string? message = null)

Parameters:

suffix — string

The expected end.

value — string

The string.

message — string

An optional explanation.

Exceptions:

Fails the test.

public static void Fail(string message)

Parameters:

message — string

The explanation.

Exceptions:

Checks that a sequence has exactly the given number of elements.

public static void HasCount<T>(int expected, IEnumerable<T> values, string? message = null)

Type parameters:

T

The element type.

Parameters:

expected — int

The expected count.

values — IEnumerable<T>

The sequence.

message — string

An optional explanation.

Exceptions:

Checks that a sequence has no elements.

public static void IsEmpty<T>(IEnumerable<T> values, string? message = null)

Type parameters:

T

The element type.

Parameters:

values — IEnumerable<T>

The sequence.

message — string

An optional explanation.

Exceptions:

Checks that a condition is false.

public static void IsFalse(bool condition, string? message = null, string? expression = null)

Parameters:

condition — bool

The condition.

message — string

An optional explanation.

expression — string

The condition’s source text, supplied by the compiler.

Exceptions:

Checks that a value is not null.

public static void IsNotNull(object? value, string? message = null)

Parameters:

value — object

The value.

message — string

An optional explanation.

Exceptions:

Checks that a value is null, which is SQL NULL for values read from PostgreSQL.

public static void IsNull(object? value, string? message = null)

Parameters:

value — object

The value.

message — string

An optional explanation.

Exceptions:

Checks that a condition is true.

public static void IsTrue(bool condition, string? message = null, string? expression = null)

Parameters:

condition — bool

The condition.

message — string

An optional explanation.

expression — string

The condition’s source text, supplied by the compiler.

Exceptions:

Checks that a string starts with a prefix, compared ordinally.

public static void StartsWith(string prefix, string? value, string? message = null)

Parameters:

prefix — string

The expected beginning.

value — string

The string.

message — string

An optional explanation.

Exceptions:

ThrowsExactly<TException>(Action, string?)

Section titled “ThrowsExactly<TException>(Action, string?)”

Checks that work throws an exception of exactly the given type.

public static TException ThrowsExactly<TException>(Action action, string? message = null) where TException : Exception

Type parameters:

TException

The expected exception type.

Parameters:

action — Action

The work, which runs in its own subtransaction.

message — string

An optional explanation.

Returns: TException

The exception.

Exceptions:

Running the work in a subtransaction rolls back its changes and any PostgreSQL error, so the test continues in the same transaction.

Checks that work raises a PostgreSQL error with the given SQLSTATE, as a pgrx test’s #[pg_test(error = “…”)] expects an error.

public static PgException ThrowsSqlState(string sqlState, Action action, string? message = null)

Parameters:

sqlState — string

The expected five-character SQLSTATE, such as DivisionByZero.

action — Action

The work, which runs in its own subtransaction.

message — string

An optional explanation.

Returns: PgException

The error, for checking its message and other fields.

Exceptions:

  • PgAssertException: The work completed, raised another SQLSTATE or threw another exception.