Skip to content

Commit

Permalink
Remake campaign manager test
Browse files Browse the repository at this point in the history
  • Loading branch information
maniman303 committed Aug 2, 2022
1 parent a741f05 commit e8024cd
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DatabaseHelper {
WidgetsFlutterBinding.ensureInitialized();

_database = await DependenciesHelper().openDatabase(
join(await getDatabasesPath(), 'campaign_database.db'),
join(await DependenciesHelper().databasePath, 'campaign_database.db'),
onCreate: ((db, version) async {
await db
.execute('CREATE TABLE strings(entityTable TEXT, entityType Text, entityId INTEGER, value TEXT)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'package:sqflite/sqflite.dart' as sqflite;
import 'package:sqflite/sqflite.dart';

// Helper used as an dependency injection tool.
class DependenciesHelper {
Expand Down Expand Up @@ -33,6 +34,7 @@ class DependenciesHelper {
bool readOnly,
bool singleInstance,
}) _databaseFun = sqflite.openDatabase;
String? _databasePath;

void useMocks({
FlutterSecureStorage? secureStorage,
Expand All @@ -51,6 +53,7 @@ class DependenciesHelper {
bool singleInstance,
})?
databaseFun,
String? databasePath,
}) {
if (secureStorage != null) {
_secureStorage = secureStorage;
Expand All @@ -71,6 +74,10 @@ class DependenciesHelper {
if (databaseFun != null) {
_databaseFun = databaseFun;
}

if (databasePath != null) {
_databasePath = databasePath;
}
}

FlutterSecureStorage get secureStorage {
Expand Down Expand Up @@ -106,4 +113,8 @@ class DependenciesHelper {
}) get openDatabase {
return _databaseFun;
}

Future<String> get databasePath async {
return _databasePath ?? await getDatabasesPath();
}
}
181 changes: 181 additions & 0 deletions mobile/CampaignKeeper_mobile/test/mocks/database_mock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import 'package:campaign_keeper_mobile/types/entity_types.dart';
import 'package:sqflite/sqflite.dart';

class DatabaseMock implements Database {
static DatabaseMock? _db;
static Future<Database> openDatabase(
String path, {
int? version,
OnDatabaseConfigureFn? onConfigure,
OnDatabaseCreateFn? onCreate,
OnDatabaseVersionChangeFn? onUpgrade,
OnDatabaseVersionChangeFn? onDowngrade,
OnDatabaseOpenFn? onOpen,
bool readOnly = false,
bool singleInstance = true,
}) async {
_db = DatabaseMock(version: version ?? 0);
return _db!;
}

static DatabaseMock get db => _db!;

DatabaseMock({required this.version});

Map<String, List<Map<String, Object?>>> map = {};
int version;

@override
Batch batch() {
return BatchMock(db: this);
}

@override
Future<void> close() async {}

@override
Future<int> delete(String table, {String? where, List<Object?>? whereArgs}) async {
map.remove(table);
return 0;
}

@override
Future<T> devInvokeMethod<T>(String method, [arguments]) {
throw UnimplementedError();
}

@override
Future<T> devInvokeSqlMethod<T>(String method, String sql, [List<Object?>? arguments]) {
throw UnimplementedError();
}

@override
Future<void> execute(String sql, [List<Object?>? arguments]) async {}

@override
Future<int> getVersion() async {
return version;
}

@override
Future<int> insert(String table, Map<String, Object?> values,
{String? nullColumnHack, ConflictAlgorithm? conflictAlgorithm}) async {
if (!map.containsKey(table)) {
map[table] = [];
}

map[table]!.add(values);

return 0;
}

@override
bool get isOpen => true;

@override
String get path => '';

@override
Future<List<Map<String, Object?>>> query(String table,
{bool? distinct,
List<String>? columns,
String? where,
List<Object?>? whereArgs,
String? groupBy,
String? having,
String? orderBy,
int? limit,
int? offset}) async {
return map[table] ?? [];
}

@override
Future<int> rawDelete(String sql, [List<Object?>? arguments]) async {
return 0;
}

@override
Future<int> rawInsert(String sql, [List<Object?>? arguments]) async {
return 0;
}

@override
Future<List<Map<String, Object?>>> rawQuery(String sql, [List<Object?>? arguments]) async {
return [];
}

@override
Future<int> rawUpdate(String sql, [List<Object?>? arguments]) async {
return 0;
}

@override
Future<void> setVersion(int version) async {}

@override
Future<T> transaction<T>(Future<T> Function(Transaction txn) action, {bool? exclusive}) {
throw UnimplementedError();
}

@override
Future<int> update(String table, Map<String, Object?> values,
{String? where, List<Object?>? whereArgs, ConflictAlgorithm? conflictAlgorithm}) {
throw UnimplementedError();
}
}

class BatchMock implements Batch {
BatchMock({required this.db});

DatabaseMock db;
List<Tuple<String, Map<String, Object?>>> _queries = [];

@override
Future<List<Object?>> commit({bool? exclusive, bool? noResult, bool? continueOnError}) async {
_queries.forEach((t) async {
await db.insert(t.first, t.second);
});

return [];
}

@override
void delete(String table, {String? where, List<Object?>? whereArgs}) {}

@override
void execute(String sql, [List<Object?>? arguments]) {}

@override
void insert(String table, Map<String, Object?> values,
{String? nullColumnHack, ConflictAlgorithm? conflictAlgorithm}) {
_queries.add(Tuple(first: table, second: values));
}

@override
void query(String table,
{bool? distinct,
List<String>? columns,
String? where,
List<Object?>? whereArgs,
String? groupBy,
String? having,
String? orderBy,
int? limit,
int? offset}) {}

@override
void rawDelete(String sql, [List<Object?>? arguments]) {}

@override
void rawInsert(String sql, [List<Object?>? arguments]) {}

@override
void rawQuery(String sql, [List<Object?>? arguments]) {}

@override
void rawUpdate(String sql, [List<Object?>? arguments]) {}

@override
void update(String table, Map<String, Object?> values,
{String? where, List<Object?>? whereArgs, ConflictAlgorithm? conflictAlgorithm}) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ import 'dart:convert';

import 'package:campaign_keeper_mobile/entities/campaign_ent.dart';
import 'package:campaign_keeper_mobile/services/data_carrier.dart';
import 'package:campaign_keeper_mobile/services/helpers/database_helper.dart';
import 'package:campaign_keeper_mobile/services/helpers/dependencies_helper.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;

import '../../mocks/database_mock.dart';
import '../../mocks/http_client_mock.dart';
import '../../mocks/secure_storage_mock.dart';
import '../../mocks/shared_storage_mock.dart';

void main() {
void main() async {
final dc = DataCarrier();
final client = HttpClientMock();
final storage = SharedStorageMock();
final secureStorage = SecureStorageMock();
DependenciesHelper().useMocks(
client: client,
storage: storage,
secureStorage: secureStorage,
databaseFun: DatabaseMock.openDatabase,
databasePath: '',
);
await DatabaseHelper().initialize();
group("Campaign manager test", () {
final dc = DataCarrier();
final client = HttpClientMock();
final storage = SharedStorageMock();
final secureStorage = SecureStorageMock();
DependenciesHelper().useMocks(
client: client,
storage: storage,
secureStorage: secureStorage,
);

test("Get entity", () async {
var ent = CampaignEntity(id: 1, name: "Test", createdAt: DateTime.now());

Expand Down Expand Up @@ -64,14 +68,17 @@ void main() {

expect(ent.equals(newEnt), true);

String storageValue = storage.value as String;
List<Map>? storageValues = DatabaseMock.db.map[CampaignEntity.tableName];

expect(storageValues == null, false);

expect(storageValues!.length, 1);

List storageData = json.decode(storageValue);
var storageEntity = CampaignEntity.fromMap(storageData[0]);
Map storageValue = storageValues[0];

await Future.delayed(Duration(milliseconds: 500));
var storageEnt = CampaignEntity.fromMap(storageValue);

expect(ent.equals(storageEntity), true);
expect(ent.equals(storageEnt), true);
});
});
}

0 comments on commit e8024cd

Please sign in to comment.