627e96c9c1
All of DDC's supported platforms have Promises, so we can use them instead of MutationObservers (web) and timers (node.js). See issue #20055 (same issue, but for dart2js). Change-Id: Id635a4a9fa104a2ab19dd20824d209f682f831f9 Reviewed-on: https://dart-review.googlesource.com/c/91765 Reviewed-by: Mark Zhou <markzipan@google.com> Commit-Queue: Jenny Messerly <jmesserly@google.com>
119 lines
3.4 KiB
Dart
119 lines
3.4 KiB
Dart
library WebDBTest;
|
|
|
|
import 'dart:async';
|
|
import 'dart:html';
|
|
import 'dart:web_sql';
|
|
|
|
import 'package:expect/async_minitest.dart';
|
|
|
|
Future<SqlResultSet> createTable(
|
|
SqlTransaction transaction, String tableName, String columnName) async {
|
|
return transaction.executeSql('CREATE TABLE $tableName ($columnName)', []);
|
|
}
|
|
|
|
Future<SqlResultSet> insertTable(SqlTransaction transaction, String tableName,
|
|
String columnName, value) async {
|
|
final sql = 'INSERT INTO $tableName ($columnName) VALUES (?)';
|
|
return transaction.executeSql(sql, [value]);
|
|
}
|
|
|
|
Future<SqlResultSet> queryTable(
|
|
SqlTransaction transaction, String tableName) async {
|
|
final sql = 'SELECT * FROM $tableName';
|
|
return transaction.executeSql(sql, []);
|
|
}
|
|
|
|
Future<SqlResultSet> dropTable(SqlTransaction transaction, String tableName,
|
|
[bool ignoreFailure = false]) async {
|
|
try {
|
|
var result = await transaction.executeSql('DROP TABLE $tableName', []);
|
|
return result;
|
|
} catch (error) {
|
|
if (!ignoreFailure) throw error;
|
|
}
|
|
}
|
|
|
|
final tableName = 'test_table';
|
|
final columnName = 'test_data';
|
|
|
|
SqlDatabase db;
|
|
SqlTransaction tx;
|
|
|
|
Future setup() async {
|
|
if (SqlDatabase.supported) {
|
|
db = await window.openDatabase('test_db', '1.0', 'test_db', 1024 * 1024);
|
|
expect(db, isNotNull, reason: 'Unable to open database');
|
|
|
|
tx = await db.transaction_future();
|
|
expect(tx, isNotNull, reason: "Transaction not ready");
|
|
}
|
|
}
|
|
|
|
main() async {
|
|
await setup();
|
|
|
|
group('Database', () {
|
|
test('Open/Transaction', () async {
|
|
if (!SqlDatabase.supported) return;
|
|
|
|
expect(tx, isNotNull, reason: "Transaction not ready");
|
|
|
|
// Should not succeed table doesn't exist to be dropped.
|
|
try {
|
|
await dropTable(tx, tableName);
|
|
expect(false, true, reason: "dropTable should fail");
|
|
} catch (error) {
|
|
expect(error.message,
|
|
"could not prepare statement (1 no such table: test_table)");
|
|
}
|
|
});
|
|
|
|
test('create', () async {
|
|
if (!SqlDatabase.supported) return;
|
|
|
|
expect(tx, isNotNull, reason: "Transaction not ready");
|
|
try {
|
|
SqlResultSet createResult =
|
|
await createTable(tx, tableName, columnName);
|
|
expect(createResult.insertId, 0);
|
|
} catch (error) {
|
|
expect(false, true, reason: "createTable failed - ${error.message}");
|
|
}
|
|
});
|
|
|
|
test('insert', () async {
|
|
if (!SqlDatabase.supported) return;
|
|
|
|
expect(tx, isNotNull, reason: "Transaction not ready");
|
|
try {
|
|
SqlResultSet insertResult =
|
|
await insertTable(tx, tableName, columnName, 'Some text data');
|
|
expect(insertResult.insertId, 1);
|
|
expect(insertResult.rowsAffected, 1);
|
|
} catch (error) {
|
|
expect(false, true, reason: "insert failed - ${error.message}");
|
|
}
|
|
});
|
|
|
|
test('query', () async {
|
|
if (!SqlDatabase.supported) return;
|
|
|
|
expect(tx, isNotNull, reason: "Transaction not ready");
|
|
try {
|
|
SqlResultSet queryResult = await queryTable(tx, tableName);
|
|
expect(queryResult.rows.length, 1);
|
|
expect(queryResult.rows[0]['test_data'], "Some text data");
|
|
} catch (error) {
|
|
expect(false, true, reason: "queryTable failed - ${error.message}");
|
|
}
|
|
});
|
|
|
|
test('cleanup', () async {
|
|
if (!SqlDatabase.supported) return;
|
|
|
|
expect(tx, isNotNull, reason: "Transaction not ready");
|
|
await dropTable(tx, tableName, true);
|
|
});
|
|
});
|
|
}
|