Skip to content

Commit eff50c6

Browse files
authored
Example: bytes written and read back (#289)
1 parent 1598b20 commit eff50c6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/bytes_example_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'dart:typed_data';
2+
3+
import 'package:postgres/postgres.dart';
4+
import 'package:test/test.dart';
5+
6+
import 'docker.dart';
7+
8+
void main() {
9+
withPostgresServer('bytes example', (server) {
10+
test('write and read', () async {
11+
final conn = await server.newConnection();
12+
await conn.execute('''
13+
CREATE TABLE IF NOT EXISTS images (
14+
id SERIAL PRIMARY KEY,
15+
name TEXT,
16+
description TEXT,
17+
image BYTEA NOT NULL
18+
);
19+
''');
20+
21+
final rs1 = await conn.execute(Sql.named('''
22+
INSERT INTO images (name, description, image)
23+
VALUES (@name, @description, @image:bytea)
24+
RETURNING id
25+
'''), parameters: {
26+
'name': 'name-1',
27+
'description': 'descr-1',
28+
'image': Uint8List.fromList([0, 1, 2]),
29+
});
30+
final id = rs1.single.single;
31+
32+
final rs2 = await conn
33+
.execute(r'SELECT image FROM images WHERE id=$1', parameters: [id]);
34+
final bytes = rs2.single.single;
35+
expect(bytes, [0, 1, 2]);
36+
});
37+
});
38+
}

0 commit comments

Comments
 (0)