Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local store tests #71

Merged
merged 7 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/clean_project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
#
# to run it use:
# ./.github/workflows/clean_project.sh
#
# Script cleans current flutter project:
# - flutter clean
# - removes pubspec.lock,
# - flutter pub get

flutterPubUpgrade() {
echo -e "\nUpgrading packages..."
flutter pub upgrade
}

echo -e "\nCleaning project..."
flutter clean

echo -e "\nDeleting unnecessary files in \"$PWD\""
find . -name "pubspec.lock" -type f -exec rm -vf {} \;

echo -e "\nInstalling packages..."
flutter pub get

# flutter pub outdated
# echo
# while true; do
# read -p 'Would you like to upgrade resolvable dependencies: ' answer
# answer=$(echo "$answer" | tr '[:upper:]' '[:lower:]')
# case ${answer:0:1} in
# y|yes )
# flutterPubUpgrade;
# break;;
# n|no )
# echo -e "\nexit..."
# exit;;
# * ) echo "Please answer yes or no.";;
# esac
# done

echo -e "\nall done."
2 changes: 1 addition & 1 deletion lib/src/local_store/local_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:shared_preferences/shared_preferences.dart';
/// так же поддерживает шифрование данных
/// в юникод utf8 и затем в base64 string
class LocalStore {
static const _debug = false;
static const _debug = true;
LocalStore();
Future<SharedPreferences> _getPrefs() {
return SharedPreferences
Expand Down
23 changes: 23 additions & 0 deletions test/unit/local_store/local_store_clear_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/src/local_store/local_store.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('LocalStore clear removes all entries from SharedPreferences', () async {
// Adding prefixes to keys to not rewrite or read existing values
// in SharedPreferences (or values from another tests).
const keyPrefix = 'test.local_store_clear.';
final initialValues = {
'${keyPrefix}test1': 'value1',
'${keyPrefix}test2': 2,
'${keyPrefix}test3': true,
};
SharedPreferences.setMockInitialValues(initialValues);
final store = LocalStore();
final result = await store.clear();
expect(result, isTrue);
final preferences = await SharedPreferences.getInstance();
expect(preferences.getKeys(), isEmpty);
});
}
17 changes: 17 additions & 0 deletions test/unit/local_store/local_store_decode_str_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/src/local_store/local_store.dart';

void main() {
test('LocalStore decodeStr decodes string correctly', () async {
const stringsEncodings = {
'': '',
'dmFsdWUx': 'value1',
'NDQ0NCBzZGYgc2gzNA==': '4444 sdf sh34',
'fmAhQCLihJYjJDslXjo/JiooKV8tKz1be31dfFwvLiwn': '~`!@"№#\$;%^:?&*()_-+=[{}]|\\/.,\'',
};
final store = LocalStore();
for (final entry in stringsEncodings.entries) {
expect(store.decodeStr(entry.key), equals(entry.value));
}
});
}
16 changes: 16 additions & 0 deletions test/unit/local_store/local_store_encode_str_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/src/local_store/local_store.dart';

void main() {
test('LocalStore encodeStr encodes string correctly', () async {
const stringsEncodings = {
'value1': 'dmFsdWUx',
'4444 sdf sh34': 'NDQ0NCBzZGYgc2gzNA==',
'~`!@"№#\$;%^:?&*()_-+=[{}]|\\/.,\'': 'fmAhQCLihJYjJDslXjo/JiooKV8tKz1be31dfFwvLiwn',
};
final store = LocalStore();
for (final entry in stringsEncodings.entries) {
expect(store.encodeStr(entry.key), equals(entry.value));
}
});
}
57 changes: 57 additions & 0 deletions test/unit/local_store/local_store_read_string_decoded_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/src/local_store/local_store.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('LocalStore read string decoded', () {
// Adding prefixes to keys to not rewrite or read existing values
// in SharedPreferences (or values from another tests).
const keyPrefix = 'test.local_store_read_string_decoded.';
const initialPairs = {
'${keyPrefix}test1': {
'decoded': 'value1',
'encoded': 'dmFsdWUx',
},
'${keyPrefix}key2': {
'decoded': '',
'encoded': '',
},
'${keyPrefix}3key': {
'decoded': '4444 sdf sh34',
'encoded': 'NDQ0NCBzZGYgc2gzNA==',
},
'${keyPrefix}4test': {
'decoded': '~`!@"№#\$;%^:?&*()_-+=[{}]|\\/.,\'',
'encoded': 'fmAhQCLihJYjJDslXjo/JiooKV8tKz1be31dfFwvLiwn',
},
};
final decodedPairs = initialPairs.map(
(key, value) => MapEntry(key, value['decoded']!),
);
final encodedPairs = initialPairs.map(
(key, value) => MapEntry(key, value['encoded']!),
);
test('returns decoded value if key exist in SharedPreferences', () async {
SharedPreferences.setMockInitialValues(encodedPairs);
final store = LocalStore();
final preferences = await SharedPreferences.getInstance();
for (final entry in decodedPairs.entries) {
final receivedValue = await store.readStringDecoded(entry.key);
expect(receivedValue, entry.value);
expect(preferences.get(entry.key), encodedPairs[entry.key]);
}
});
test('returns empty string if key does not exist in SharedPreferences', () async {
SharedPreferences.setMockInitialValues({});
final readKeys = initialPairs.keys;
final store = LocalStore();
final preferences = await SharedPreferences.getInstance();
for (final key in readKeys) {
expect(preferences.get(key), null);
final receivedValue = await store.readStringDecoded(key);
expect(receivedValue, '');
}
});
});
}
40 changes: 40 additions & 0 deletions test/unit/local_store/local_store_read_string_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/src/local_store/local_store.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('LocalStore read string', () {
// Adding prefixes to keys to not rewrite or read existing values
// in SharedPreferences (or values from another tests).
const keyPrefix = 'test.local_store_read_string.';
const initialPairs = {
'${keyPrefix}test1': 'value1',
'${keyPrefix}key2': '2',
'${keyPrefix}3': 'true',
'${keyPrefix}4key': '4444',
'${keyPrefix}5test': '54321',
};
test('returns value if key exist in SharedPreferences', () async {
SharedPreferences.setMockInitialValues(initialPairs);
final store = LocalStore();
final preferences = await SharedPreferences.getInstance();
for (final entry in initialPairs.entries) {
final receivedValue = await store.readString(entry.key);
expect(receivedValue, entry.value);
expect(preferences.get(entry.key), entry.value);
}
});
test('returns empty string if key does not exist in SharedPreferences', () async {
SharedPreferences.setMockInitialValues({});
final readKeys = initialPairs.keys;
final store = LocalStore();
final preferences = await SharedPreferences.getInstance();
for (final key in readKeys) {
expect(preferences.get(key), null);
final receivedValue = await store.readString(key);
expect(receivedValue, '');
}
});
});
}
30 changes: 30 additions & 0 deletions test/unit/local_store/local_store_remove_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/src/local_store/local_store.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('LocalStore remove removes single entry from SharedPreferences', () async {
// Adding prefixes to keys to not rewrite existing values
// in SharedPreferences (or values from another tests).
const keyPrefix = 'test.local_store_remove.';
const initialLocalStoreValues = {
'${keyPrefix}test1': 'value1',
'${keyPrefix}test2': 2,
'${keyPrefix}test3': true,
};
SharedPreferences.setMockInitialValues(initialLocalStoreValues);
final currentLocalStoreValues = Map.from(initialLocalStoreValues);
final store = LocalStore();
final preferences = await SharedPreferences.getInstance();
final initialKeys = initialLocalStoreValues.keys;
for (final key in initialKeys) {
final result = await store.remove(key);
expect(result, isTrue);
currentLocalStoreValues.remove(key);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verify that store returns empty string on removed key

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

for (final key in initialKeys) {
expect(currentLocalStoreValues[key], preferences.get(key));
}
}
});
}
58 changes: 58 additions & 0 deletions test/unit/local_store/local_store_write_string_encoded_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/src/local_store/local_store.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('LocalStore write string encoded', () {
// Adding prefixes to keys to not rewrite or read existing values
// in SharedPreferences (or values from another tests).
const keyPrefix = 'test.local_store_write_decoded_string.';
const initialPairs = {
'${keyPrefix}test1': {
'decoded': 'value1',
'encoded': 'dmFsdWUx',
},
'${keyPrefix}key2': {
'decoded': '',
'encoded': '',
},
'${keyPrefix}3key': {
'decoded': '4444 sdf sh34',
'encoded': 'NDQ0NCBzZGYgc2gzNA==',
},
'${keyPrefix}4test': {
'decoded': '~`!@"№#\$;%^:?&*()_-+=[{}]|\\/.,\'',
'encoded': 'fmAhQCLihJYjJDslXjo/JiooKV8tKz1be31dfFwvLiwn',
},
};
final decodedPairs = initialPairs.map(
(key, value) => MapEntry(key, value['decoded']!),
);
final encodedPairs = initialPairs.map(
(key, value) => MapEntry(key, value['encoded']!),
);
test('persists entry in SharedPreferences', () async {
SharedPreferences.setMockInitialValues({});
final store = LocalStore();
final preferences = await SharedPreferences.getInstance();
for (final writePair in decodedPairs.entries) {
expect(preferences.get(writePair.key), isNull);
final result = await store.writeStringEncoded(writePair.key, writePair.value);
expect(result, isTrue);
expect(preferences.get(writePair.key), encodedPairs[writePair.key]);
}
});
test('persists empty string value as is (without encryption)', () async {
SharedPreferences.setMockInitialValues({});
final store = LocalStore();
final preferences = await SharedPreferences.getInstance();
for (final writeKey in initialPairs.keys) {
expect(preferences.get(writeKey), isNull);
final result = await store.writeStringEncoded(writeKey, '');
expect(result, isTrue);
expect(preferences.get(writeKey), '');
}
});
});
}
26 changes: 26 additions & 0 deletions test/unit/local_store/local_store_write_string_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/src/local_store/local_store.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('LocalStore write string persists entry in SharedPreferences', () async {
SharedPreferences.setMockInitialValues({});
// Adding prefixes to keys to not rewrite or read existing values
// in SharedPreferences (or values from another tests).
const keyPrefix = 'test.local_store_write_string.';
const writePairs = {
'${keyPrefix}test1': 'value1',
'${keyPrefix}test2': '2',
'${keyPrefix}test3': 'true',
};
final store = LocalStore();
final preferences = await SharedPreferences.getInstance();
for (final writePair in writePairs.entries) {
expect(preferences.get(writePair.key), isNull);
final result = await store.writeString(writePair.key, writePair.value);
expect(result, isTrue);
expect(preferences.get(writePair.key), writePair.value);
}
});
}