Merge pull request #135 from inventree/unit-testing

Unit testing

(cherry picked from commit d55f594342)
This commit is contained in:
Oliver 2022-05-22 10:11:36 +10:00 committed by Oliver Walters
parent 10b435f4fa
commit cfc9f09b80
35 changed files with 893 additions and 317 deletions

View file

@ -6,6 +6,26 @@ import "package:sembast/sembast_io.dart";
import "package:path/path.dart";
// Settings key values
const String INV_HOME_SHOW_SUBSCRIBED = "homeShowSubscribed";
const String INV_HOME_SHOW_PO = "homeShowPo";
const String INV_HOME_SHOW_MANUFACTURERS = "homeShowManufacturers";
const String INV_HOME_SHOW_CUSTOMERS = "homeShowCustomers";
const String INV_HOME_SHOW_SUPPLIERS = "homeShowSuppliers";
const String INV_SOUNDS_BARCODE = "barcodeSounds";
const String INV_SOUNDS_SERVER = "serverSounds";
const String INV_PART_SUBCATEGORY = "partSubcategory";
const String INV_STOCK_SUBLOCATION = "stockSublocation";
const String INV_STOCK_SHOW_HISTORY = "stockShowHistory";
const String INV_REPORT_ERRORS = "reportErrors";
const String INV_STRICT_HTTPS = "strictHttps";
/*
* Class for storing InvenTree preferences in a NoSql DB
*/
@ -40,46 +60,59 @@ class InvenTreePreferencesDB {
// Get a platform-specific directory where persistent app data can be stored
final appDocumentDir = await getApplicationDocumentsDirectory();
print("Documents Dir: ${appDocumentDir.toString()}");
print("Path: ${appDocumentDir.path}");
// Path with the form: /platform-specific-directory/demo.db
final dbPath = join(appDocumentDir.path, "InvenTreeSettings.db");
final database = await databaseFactoryIo.openDatabase(dbPath);
// Any code awaiting the Completer's future will now start executing
_dbOpenCompleter.complete(database);
}
}
class InvenTreePreferences {
factory InvenTreePreferences() {
return _api;
/*
* InvenTree setings manager class.
* Provides functions for loading and saving settings, with provision for default values
*/
class InvenTreeSettingsManager {
factory InvenTreeSettingsManager() {
return _manager;
}
InvenTreePreferences._internal();
InvenTreeSettingsManager._internal();
/* The following settings are not stored to persistent storage,
* instead they are only used as "session preferences".
* They are kept here as a convenience only.
*/
final store = StoreRef("settings");
// Expand subcategory list in PartCategory view
bool expandCategoryList = false;
Future<Database> get _db async => InvenTreePreferencesDB.instance.database;
// Expand part list in PartCategory view
bool expandPartList = true;
Future<dynamic> getValue(String key, dynamic backup) async {
// Expand sublocation list in StockLocation view
bool expandLocationList = false;
final value = await store.record(key).get(await _db);
// Expand item list in StockLocation view
bool expandStockList = true;
if (value == null) {
return backup;
}
// Ensure we only ever create a single instance of the preferences class
static final InvenTreePreferences _api = InvenTreePreferences._internal();
return value;
}
}
// Load a boolean setting
Future<bool> getBool(String key, bool backup) async {
final dynamic value = await getValue(key, backup);
if (value is bool) {
return value;
} else {
return backup;
}
}
Future<void> setValue(String key, dynamic value) async {
await store.record(key).put(await _db, value);
}
// Ensure we only ever create a single instance of this class
static final InvenTreeSettingsManager _manager = InvenTreeSettingsManager._internal();
}