diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 184740cb..dbf1d5be 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -40,6 +40,8 @@ jobs: - run: flutter analyze - name: Run Unit Tests run: | + apt-get install lcov pip install -Ur requirements.txt flutter test --coverage + genhtml coverage/lcov.info -o coverage/html coveralls \ No newline at end of file diff --git a/lib/app_settings.dart b/lib/app_settings.dart deleted file mode 100644 index e009b784..00000000 --- a/lib/app_settings.dart +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Class for managing app-level configuration options - */ - -import "package:sembast/sembast.dart"; -import "package:inventree/preferences.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 InvenTreeSettingsManager { - - factory InvenTreeSettingsManager() { - return _manager; - } - - InvenTreeSettingsManager._internal(); - - final store = StoreRef("settings"); - - Future get _db async => InvenTreePreferencesDB.instance.database; - - Future getValue(String key, dynamic backup) async { - - final value = await store.record(key).get(await _db); - - if (value == null) { - return backup; - } - - return value; - } - - // Load a boolean setting - Future getBool(String key, bool backup) async { - final dynamic value = await getValue(key, backup); - - if (value is bool) { - return value; - } else { - return backup; - } - } - - Future 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(); -} diff --git a/lib/preferences.dart b/lib/preferences.dart index 2c52a88d..961a16bc 100644 --- a/lib/preferences.dart +++ b/lib/preferences.dart @@ -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 */ @@ -54,32 +74,50 @@ class InvenTreePreferencesDB { } } -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 get _db async => InvenTreePreferencesDB.instance.database; - // Expand part list in PartCategory view - bool expandPartList = true; + Future 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; + } -} \ No newline at end of file + // Load a boolean setting + Future getBool(String key, bool backup) async { + final dynamic value = await getValue(key, backup); + + if (value is bool) { + return value; + } else { + return backup; + } + } + + Future 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(); +} diff --git a/test/preferences_test.dart b/test/preferences_test.dart new file mode 100644 index 00000000..cca80185 --- /dev/null +++ b/test/preferences_test.dart @@ -0,0 +1,21 @@ +/* + * Unit tests for the preferences manager + */ + +import "package:test/test.dart"; +import "package:inventree/preferences.dart"; + +void main() { + + setUp(() async { + + }); + + group("Settings Tests:", () { + test("Default Values", () async { + // Boolean values + expect(await InvenTreeSettingsManager().getBool("test", false), equals(false)); + expect(await InvenTreeSettingsManager().getBool("test", true), equals(true)); + }); + }); +} \ No newline at end of file