|
| 1 | +@TestOn('browser') |
| 2 | +library; |
| 3 | + |
| 4 | +import 'dart:async'; |
| 5 | +import 'dart:math'; |
| 6 | +import 'dart:typed_data'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +import '../utils/test_utils_impl.dart'; |
| 10 | + |
| 11 | +const _isDart2Wasm = bool.fromEnvironment('dart.tool.dart2wasm'); |
| 12 | +final testUtils = TestUtils(); |
| 13 | + |
| 14 | +void main() { |
| 15 | + group('Web database tests', () { |
| 16 | + late String path; |
| 17 | + |
| 18 | + setUp(() async { |
| 19 | + path = testUtils.dbPath(); |
| 20 | + await testUtils.cleanDb(path: path); |
| 21 | + }); |
| 22 | + |
| 23 | + tearDown(() async { |
| 24 | + await testUtils.cleanDb(path: path); |
| 25 | + }); |
| 26 | + |
| 27 | + test('can bind all types', () async { |
| 28 | + final db = await testUtils.setupDatabase(path: path); |
| 29 | + addTearDown(db.close); |
| 30 | + |
| 31 | + Future<void> assertTypeOf(Object? value, String typeName) async { |
| 32 | + final row = await db.get('SELECT typeof(?)', [value]); |
| 33 | + expect(row.values[0], typeName); |
| 34 | + } |
| 35 | + |
| 36 | + await assertTypeOf(null, 'null'); |
| 37 | + await assertTypeOf('hello web', 'text'); |
| 38 | + await assertTypeOf(Uint8List(12), 'blob'); |
| 39 | + await assertTypeOf(2, 'integer'); |
| 40 | + await assertTypeOf(BigInt.two, 'integer'); |
| 41 | + await assertTypeOf(BigInt.one << 62, 'integer'); |
| 42 | + await assertTypeOf(pi, 'real'); |
| 43 | + }); |
| 44 | + |
| 45 | + test('large integers are read as bigints on JS', () async { |
| 46 | + final db = await testUtils.setupDatabase(path: path); |
| 47 | + addTearDown(db.close); |
| 48 | + |
| 49 | + final value = (await db.get('SELECT 1 << 62')).values[0]; |
| 50 | + if (_isDart2Wasm) { |
| 51 | + // We have 64bit ints on dart2wasm, which should be used here. |
| 52 | + expect(value, isA<int>()); |
| 53 | + expect(value, 1 << 62); |
| 54 | + } else { |
| 55 | + expect(value, isA<BigInt>()); |
| 56 | + expect(value, BigInt.parse('4611686018427387904')); |
| 57 | + } |
| 58 | + }); |
| 59 | + }); |
| 60 | +} |
0 commit comments