Skip to content

Commit

Permalink
Refactor encoding test + partial verification of #22 (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Nov 28, 2023
1 parent af9c319 commit 313a20d
Showing 1 changed file with 67 additions and 56 deletions.
123 changes: 67 additions & 56 deletions test/encoding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,66 @@ import 'package:test/test.dart';

import 'docker.dart';

late Connection conn;

void main() {
withPostgresServer('Binary encoders', (server) {
withPostgresServer('Binary encoders with reversible tests', (server) {
late Connection conn;

Future expectReversible(
String typeName,
List values, {
String? expectedDartType,
bool skipNegative = false,
Object negative = 'non-value',
}) async {
await conn
.execute('CREATE TEMPORARY TABLE IF NOT EXISTS t (v $typeName)');

for (final value in values) {
final explicit = await conn.execute(
Sql(r'SELECT $1',
types: [TypeRegistry().resolveSubstitution(typeName)!]),
parameters: [value],
);
if (value is! double || !value.isNaN) {
expect(explicit.single.single, value);
} else {
expect((explicit.first.first as double).isNaN, true);
}

final named = await conn.execute(Sql.named('SELECT @v:$typeName'),
parameters: {'v': value});
if (value is! double || !value.isNaN) {
expect(named.single.single, value);
} else {
expect((named.first.first as double).isNaN, true);
}

final inserted = await conn.execute(
Sql.named('INSERT INTO t (v) VALUES (@v:$typeName) RETURNING v'),
parameters: {'v': value});
if (value is! double || !value.isNaN) {
expect(inserted.first.first, equals(value));
} else {
expect((inserted.first.first as double).isNaN, true);
}
}

if (!skipNegative) {
try {
await conn.execute(
Sql.named('INSERT INTO t (v) VALUES (@v:$typeName)'),
parameters: {'v': negative});
fail('unreachable');
} on FormatException catch (e) {
expect(
e.toString(),
contains(expectedDartType == null
? 'Expected: '
: 'Expected: $expectedDartType'));
}
}
}

setUp(() async {
conn = await server.newConnection();
});
Expand Down Expand Up @@ -430,6 +486,14 @@ void main() {
skipNegative: true,
);
});

test('issue #22', () async {
// TODO: investigate
// await conn.execute(Sql.named('SELECT TO_TIMESTAMP(@ts / 1000)'),
// parameters: {'ts': 1640556171599});
await conn.execute(Sql.named('SELECT TO_TIMESTAMP(@ts:int8 / 1000)'),
parameters: {'ts': 1640556171599});
});
});

group('Text encoders', () {
Expand Down Expand Up @@ -547,56 +611,3 @@ void main() {
});
});
}

Future expectReversible(
String typeName,
List values, {
String? expectedDartType,
bool skipNegative = false,
Object negative = 'non-value',
}) async {
await conn.execute('CREATE TEMPORARY TABLE IF NOT EXISTS t (v $typeName)');

for (final value in values) {
final explicit = await conn.execute(
Sql(r'SELECT $1', types: [TypeRegistry().resolveSubstitution(typeName)!]),
parameters: [value],
);
if (value is! double || !value.isNaN) {
expect(explicit.single.single, value);
} else {
expect((explicit.first.first as double).isNaN, true);
}

final named = await conn
.execute(Sql.named('SELECT @v:$typeName'), parameters: {'v': value});
if (value is! double || !value.isNaN) {
expect(named.single.single, value);
} else {
expect((named.first.first as double).isNaN, true);
}

final inserted = await conn.execute(
Sql.named('INSERT INTO t (v) VALUES (@v:$typeName) RETURNING v'),
parameters: {'v': value});
if (value is! double || !value.isNaN) {
expect(inserted.first.first, equals(value));
} else {
expect((inserted.first.first as double).isNaN, true);
}
}

if (!skipNegative) {
try {
await conn.execute(Sql.named('INSERT INTO t (v) VALUES (@v:$typeName)'),
parameters: {'v': negative});
fail('unreachable');
} on FormatException catch (e) {
expect(
e.toString(),
contains(expectedDartType == null
? 'Expected: '
: 'Expected: $expectedDartType'));
}
}
}

0 comments on commit 313a20d

Please sign in to comment.