-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b97c724
LocalStore | Set up tests
Minyewoo 49dcbfd
Added remove function test for LocalStore
Minyewoo 9df5d4a
Removed obsolete code from LocalStore clear function test
Minyewoo e176494
Added clean project script
Minyewoo 52c0ceb
Added tests for LocalStore
Minyewoo f718c4a
LocalStore.remove test | check on empty string on deleted entry
Minyewoo 3814cfe
LocalStore | Switched off debug
Minyewoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
test/unit/local_store/local_store_read_string_decoded_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ''); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ''); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
for (final key in initialKeys) { | ||
expect(currentLocalStoreValues[key], preferences.get(key)); | ||
} | ||
} | ||
}); | ||
} |
58 changes: 58 additions & 0 deletions
58
test/unit/local_store/local_store_write_string_encoded_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), ''); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done