113 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Dart
		
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Dart
		
	
	
	
| import 'package:sqflite_common_ffi/sqflite_ffi.dart';
 | |
| import 'package:test/test.dart';
 | |
| 
 | |
| import 'package:matrix/src/database/indexeddb_box.dart'
 | |
|     if (dart.library.io) 'package:matrix/src/database/sqflite_box.dart';
 | |
| 
 | |
| void main() {
 | |
|   group('Box tests', () {
 | |
|     late BoxCollection collection;
 | |
|     const Set<String> boxNames = {'cats', 'dogs'};
 | |
|     const data = {'name': 'Fluffy', 'age': 2};
 | |
|     const data2 = {'name': 'Loki', 'age': 4};
 | |
|     Database? db;
 | |
|     const isWeb = bool.fromEnvironment('dart.library.js_util');
 | |
|     setUp(() async {
 | |
|       if (!isWeb) {
 | |
|         db = await databaseFactoryFfi.openDatabase(':memory:');
 | |
|       }
 | |
|       collection = await BoxCollection.open(
 | |
|         'testbox',
 | |
|         boxNames,
 | |
|         sqfliteDatabase: db,
 | |
|         sqfliteFactory: isWeb ? null : databaseFactoryFfi,
 | |
|       );
 | |
|     });
 | |
| 
 | |
|     test('Box.put and Box.get', () async {
 | |
|       final box = collection.openBox<Map>('cats');
 | |
|       await box.put('fluffy', data);
 | |
|       expect(await box.get('fluffy'), data);
 | |
|       await box.clear();
 | |
|     });
 | |
| 
 | |
|     test('Box.getAll', () async {
 | |
|       final box = collection.openBox<Map>('cats');
 | |
|       await box.put('fluffy', data);
 | |
|       await box.put('loki', data2);
 | |
|       expect(await box.getAll(['fluffy', 'loki']), [data, data2]);
 | |
|       await box.clear();
 | |
|     });
 | |
| 
 | |
|     test('Box.getAllKeys', () async {
 | |
|       final box = collection.openBox<Map>('cats');
 | |
|       await box.put('fluffy', data);
 | |
|       await box.put('loki', data2);
 | |
|       expect(await box.getAllKeys(), ['fluffy', 'loki']);
 | |
|       await box.clear();
 | |
|     });
 | |
| 
 | |
|     test('Box.getAllValues', () async {
 | |
|       final box = collection.openBox<Map>('cats');
 | |
|       await box.put('fluffy', data);
 | |
|       await box.put('loki', data2);
 | |
|       expect(await box.getAllValues(), {'fluffy': data, 'loki': data2});
 | |
|       await box.clear();
 | |
|     });
 | |
| 
 | |
|     test('Box.delete', () async {
 | |
|       final box = collection.openBox<Map>('cats');
 | |
|       await box.put('fluffy', data);
 | |
|       await box.put('loki', data2);
 | |
|       await box.delete('fluffy');
 | |
|       expect(await box.get('fluffy'), null);
 | |
|       await box.clear();
 | |
|     });
 | |
| 
 | |
|     test('Box.delete in transaction', () async {
 | |
|       final box = collection.openBox<Map>('cats');
 | |
|       await box.put('fluffy', data);
 | |
|       await box.put('loki', data2);
 | |
|       await collection.transaction(() async {
 | |
|         await box.delete('fluffy');
 | |
|         expect(await box.get('fluffy'), null);
 | |
|       });
 | |
|       expect(await box.get('fluffy'), null);
 | |
|       await box.clear();
 | |
|     });
 | |
| 
 | |
|     test('Box.deleteAll', () async {
 | |
|       final box = collection.openBox<Map>('cats');
 | |
|       await box.put('fluffy', data);
 | |
|       await box.put('loki', data2);
 | |
|       await box.deleteAll(['fluffy', 'loki']);
 | |
|       expect(await box.get('fluffy'), null);
 | |
|       expect(await box.get('loki'), null);
 | |
|       await box.clear();
 | |
|     });
 | |
| 
 | |
|     test('Box.clear', () async {
 | |
|       final box = collection.openBox<Map>('cats');
 | |
|       await box.put('fluffy', data);
 | |
|       await box.put('loki', data2);
 | |
|       await box.clear();
 | |
|       expect(await box.get('fluffy'), null);
 | |
|       expect(await box.get('loki'), null);
 | |
|     });
 | |
| 
 | |
|     test('Box.close', () async {
 | |
|       await collection.close();
 | |
|     });
 | |
| 
 | |
|     test(
 | |
|       'Collection.deleteDatabase',
 | |
|       () async {
 | |
|         await collection.deleteDatabase(
 | |
|           db?.path ?? '',
 | |
|           isWeb ? null : databaseFactoryFfi,
 | |
|         );
 | |
|       },
 | |
|     );
 | |
|   });
 | |
| }
 |