LCOV - code coverage report
Current view: top level - src - fake.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 25 27 92.6 %
Date: 2023-05-22 12:02:26 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:event_bloc/event_bloc.dart';
       2             : import 'package:event_db/event_db.dart';
       3             : 
       4             : /// Represents a constructor specifically for [GenericModel]
       5             : typedef ModelConstructor<T extends GenericModel> = T Function();
       6             : 
       7             : /// A [DatabaseRepository] that stores everything in memory
       8             : ///
       9             : /// This is best used in unit tests
      10             : class FakeDatabaseRepository extends DatabaseRepository {
      11             :   /// [constructors] holds all of the [ModelConstructor]s that can be found in
      12             :   /// this repository.
      13           3 :   FakeDatabaseRepository({required this.constructors});
      14             : 
      15             :   /// [constructors] holds all of the [ModelConstructor]s that can be found in
      16             :   /// this repository.
      17             :   final Map<Type, ModelConstructor> constructors;
      18             : 
      19             :   /// The "Storage" of this database
      20             :   Map<String, Map<String, GenericModel>> fakeDatabaseMap = {};
      21             : 
      22           2 :   @override
      23             :   bool deleteModel<T extends GenericModel>(String database, T model) {
      24           2 :     if (model.id == null) {
      25             :       return false;
      26             :     }
      27             : 
      28           6 :     return getMap(database).remove(model.id) != null;
      29             :   }
      30             : 
      31           3 :   @override
      32             :   Iterable<T> findAllModelsOfType<T extends GenericModel>(
      33             :     String database,
      34             :     T Function() supplier,
      35             :   ) {
      36           3 :     final modelBase = supplier();
      37           3 :     final keyPrefix = modelBase.prefixTypeForId('');
      38             : 
      39           3 :     return getMap(database)
      40           3 :         .entries
      41          15 :         .where((element) => element.value.id!.startsWith(keyPrefix))
      42          15 :         .map((e) => supplier()..copy(e.value));
      43             :   }
      44             : 
      45           2 :   @override
      46             :   T? findModel<T extends GenericModel>(String database, String key) {
      47           4 :     final baseModel = getMap(database)[key];
      48             :     if (baseModel == null) {
      49             :       return null;
      50             :     }
      51             : 
      52           4 :     return getInstance<T>()..copy(baseModel);
      53             :   }
      54             : 
      55           0 :   @override
      56             :   List<BlocEventListener<dynamic>> generateListeners(BlocEventChannel channel) {
      57             :     // DO NOTHING
      58           0 :     return [];
      59             :   }
      60             : 
      61           3 :   @override
      62             :   T saveModel<T extends GenericModel>(String database, T model) {
      63           3 :     final map = getMap(database);
      64             : 
      65          12 :     map[model.autoGenId] = getInstance<T>()..copy(model);
      66             : 
      67             :     return model;
      68             :   }
      69             : 
      70             :   /// Creates an instance using one of the [constructors]
      71             :   ///
      72             :   /// Will throw an [ArgumentError] if one isn't present in [constructors]
      73             :   /// for [T]
      74           3 :   T getInstance<T>() {
      75           6 :     if (!constructors.containsKey(T)) {
      76           2 :       throw ArgumentError(
      77             :           '$T was not added to the constructors of this FakeDatabaseRepository.'
      78             :           ' Please ensure you add all constructors you want to use for each '
      79             :           'type you will use.');
      80             :     }
      81           9 :     return constructors[T]!() as T;
      82             :   }
      83             : 
      84             :   /// Gets the "Storage" map for the given [database]
      85           3 :   Map<String, GenericModel> getMap(String database) {
      86           6 :     if (fakeDatabaseMap[database] == null) {
      87           9 :       fakeDatabaseMap[database] = {};
      88             :     }
      89             : 
      90           6 :     return fakeDatabaseMap[database]!;
      91             :   }
      92             : }

Generated by: LCOV version 1.14