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