OLD | NEW |
(Empty) | |
| 1 library WebDBTest; |
| 2 import 'dart:async'; |
| 3 import 'dart:html'; |
| 4 import 'dart:web_sql'; |
| 5 import 'package:unittest/unittest.dart'; |
| 6 import 'package:unittest/html_individual_config.dart'; |
| 7 |
| 8 Future<SqlTransaction> transaction(SqlDatabase db) { |
| 9 final completer = new Completer<SqlTransaction>.sync(); |
| 10 |
| 11 db.transaction((SqlTransaction transaction) { |
| 12 completer.complete(transaction); |
| 13 }, (SqlError error) { |
| 14 completer.completeError(error); |
| 15 }); |
| 16 |
| 17 return completer.future; |
| 18 } |
| 19 |
| 20 Future<SqlResultSet> createTable(SqlTransaction transaction, String tableName, |
| 21 String columnName) { |
| 22 final completer = new Completer<SqlResultSet>.sync(); |
| 23 |
| 24 final sql = 'CREATE TABLE $tableName ($columnName)'; |
| 25 transaction.executeSql(sql, [], |
| 26 (SqlTransaction tx, SqlResultSet rs) { |
| 27 completer.complete(rs); |
| 28 }, |
| 29 (SqlTransaction tx, SqlError error) { |
| 30 completer.completeError(error); |
| 31 }); |
| 32 |
| 33 return completer.future; |
| 34 } |
| 35 |
| 36 Future<SqlResultSet> insert(SqlTransaction transaction, String tableName, |
| 37 String columnName, value) { |
| 38 final completer = new Completer<SqlResultSet>.sync(); |
| 39 |
| 40 final sql = 'INSERT INTO $tableName ($columnName) VALUES (?)'; |
| 41 transaction.executeSql(sql, [value], |
| 42 (SqlTransaction tx, SqlResultSet rs) { |
| 43 completer.complete(rs); |
| 44 }, |
| 45 (SqlTransaction tx, SqlError error) { |
| 46 completer.completeError(error); |
| 47 }); |
| 48 |
| 49 return completer.future; |
| 50 } |
| 51 |
| 52 Future<SqlResultSet> queryTable(SqlTransaction transaction, String tableName) { |
| 53 final completer = new Completer<SqlResultSet>.sync(); |
| 54 |
| 55 final sql = 'SELECT * FROM $tableName'; |
| 56 transaction.executeSql(sql, [], |
| 57 (SqlTransaction tx, SqlResultSet rs) { |
| 58 completer.complete(rs); |
| 59 }, |
| 60 (SqlTransaction tx, SqlError error) { |
| 61 completer.completeError(error); |
| 62 }); |
| 63 |
| 64 return completer.future; |
| 65 } |
| 66 |
| 67 Future<SqlResultSet> dropTable(SqlTransaction transaction, String tableName, |
| 68 [bool ignoreFailure = false]) { |
| 69 final completer = new Completer<SqlResultSet>.sync(); |
| 70 |
| 71 final sql = 'DROP TABLE $tableName'; |
| 72 transaction.executeSql(sql, [], |
| 73 (SqlTransaction tx, SqlResultSet rs) { |
| 74 completer.complete(rs); |
| 75 }, |
| 76 (SqlTransaction tx, SqlError error) { |
| 77 if (ignoreFailure) { |
| 78 completer.complete(null); |
| 79 } else { |
| 80 completer.completeError(error); |
| 81 } |
| 82 }); |
| 83 |
| 84 return completer.future; |
| 85 } |
| 86 |
| 87 main() { |
| 88 useHtmlIndividualConfiguration(); |
| 89 |
| 90 group('supported', () { |
| 91 test('supported', () { |
| 92 expect(SqlDatabase.supported, true); |
| 93 }); |
| 94 }); |
| 95 |
| 96 group('functional', () { |
| 97 test('unsupported throws', () { |
| 98 var expectation = SqlDatabase.supported ? returnsNormally : throws; |
| 99 expect(() { |
| 100 window.openDatabase('test_db', '1.0', 'test_db', 1024 * 1024); |
| 101 }, expectation); |
| 102 |
| 103 }); |
| 104 test('Web Database', () { |
| 105 // Skip if not supported. |
| 106 if (!SqlDatabase.supported) { |
| 107 return new Future.value(); |
| 108 } |
| 109 |
| 110 final tableName = 'test_table'; |
| 111 final columnName = 'test_data'; |
| 112 |
| 113 final db = window.openDatabase('test_db', '1.0', 'test_db', 1024 * 1024); |
| 114 |
| 115 expect(db, isNotNull, reason: 'Unable to open database'); |
| 116 |
| 117 var tx; |
| 118 return transaction(db).then((transaction) { |
| 119 tx = transaction; |
| 120 }).then((_) { |
| 121 // Attempt to clear out any tables which may be lurking from previous |
| 122 // runs. |
| 123 return dropTable(tx, tableName, true); |
| 124 }).then((_) { |
| 125 return createTable(tx, tableName, columnName); |
| 126 }).then((_) { |
| 127 return insert(tx, tableName, columnName, 'Some text data'); |
| 128 }).then((_) { |
| 129 return queryTable(tx, tableName); |
| 130 }).then((resultSet) { |
| 131 expect(resultSet.rows.length, 1); |
| 132 var row = resultSet.rows.item(0); |
| 133 expect(row.containsKey(columnName), isTrue); |
| 134 expect(row[columnName], 'Some text data'); |
| 135 expect(resultSet.rows[0], row); |
| 136 }).then((_) { |
| 137 return dropTable(tx, tableName); |
| 138 }); |
| 139 }); |
| 140 }); |
| 141 } |
OLD | NEW |