Skip to content

Commit

Permalink
Merge pull request #133 from NobodyForNothing/FIX-tests
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
derdilla authored Aug 22, 2023
2 parents 4a810ba + a9178e4 commit 9040764
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/model/blood_pressure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class BloodPressureRecord {
DateTime get creationTime => _creationTime;
/// datetime needs to be after epoch
set creationTime(DateTime value) {
if (creationTime.millisecondsSinceEpoch > 0) {
_creationTime = creationTime;
if (value.millisecondsSinceEpoch > 0) {
_creationTime = value;
} else {
assert(false, "Tried to create BloodPressureRecord at or before epoch");
_creationTime = DateTime.fromMillisecondsSinceEpoch(1);
Expand Down
9 changes: 5 additions & 4 deletions lib/model/settings_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import 'package:shared_preferences/shared_preferences.dart';

class Settings extends ChangeNotifier {
late final SharedPreferences _prefs;
final PackageInfo _packageInfo;

DateTime? _displayDataStart;
DateTime? _displayDataEnd;

Settings._create();
Settings._create(this._packageInfo);
// factory method, to allow for async constructor
static Future<Settings> create() async {
final component = Settings._create();
static Future<Settings> create([PackageInfo? packageInfo]) async {
final component = Settings._create(packageInfo ?? (await PackageInfo.fromPlatform()));
component._prefs = await SharedPreferences.getInstance();
await component._update();
return component;
Expand Down Expand Up @@ -61,7 +62,7 @@ class Settings extends ChangeNotifier {
for (var e in toAwait) {
await e;
}
await _prefs.setInt('lastAppVersion', int.parse((await PackageInfo.fromPlatform()).buildNumber));
await _prefs.setInt('lastAppVersion', int.parse(_packageInfo.buildNumber));
return;
}

Expand Down
6 changes: 2 additions & 4 deletions test/model/analyzer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ void main() {

test('should determine special days', () async {
var m = BloodPressureAnalyser([BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(100), 0, 0, 0, ''),
BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(-2200), 0, 0, 0, ''),
BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(20), 0, 0, 0, ''),
BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(9000000), 0, 0, 0, ''),
BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(3124159), 0, 0, 0, ''),
]);

expect((m.firstDay), DateTime.fromMillisecondsSinceEpoch(-2200));
expect((m.firstDay), DateTime.fromMillisecondsSinceEpoch(20));
expect((m.lastDay), DateTime.fromMillisecondsSinceEpoch(9000000));
});

// TODO null tests, test with 1 element
});
}
7 changes: 5 additions & 2 deletions test/model/bood_pressure_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import 'package:sqflite_common_ffi/sqflite_ffi.dart';
void main() {
group('BloodPressureRecord', () {
test('should initialize with all values supported by dart', () {
BloodPressureRecord record = BloodPressureRecord(DateTime.fromMicrosecondsSinceEpoch(0), 0, -50, 1000,
BloodPressureRecord record = BloodPressureRecord(DateTime.fromMicrosecondsSinceEpoch(1582991592), 0, -50, 1000,
"((V⍳V)=⍳⍴V)/V←,V ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈๏ แผ่นดินฮั่นเABCDEFGHIJKLMNOPQRSTUVWXYZ /0123456789abcdefghijklmnopqrstuvwxyz £©µÀÆÖÞßéöÿ–—‘“”„†•…‰™œŠŸž€ ΑΒΓΔΩαβγδω АБВГДабвг, \n \t д∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა");

expect(record.creationTime, DateTime.fromMicrosecondsSinceEpoch(0));
expect(record.creationTime, DateTime.fromMicrosecondsSinceEpoch(1582991592));
expect(record.systolic, 0);
expect(record.diastolic, -50);
expect(record.pulse, 1000);
expect(record.notes,
"((V⍳V)=⍳⍴V)/V←,V ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈๏ แผ่นดินฮั่นเABCDEFGHIJKLMNOPQRSTUVWXYZ /0123456789abcdefghijklmnopqrstuvwxyz £©µÀÆÖÞßéöÿ–—‘“”„†•…‰™œŠŸž€ ΑΒΓΔΩαβγδω АБВГДабвг, \n \t д∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა");
});
test('should not save times at or before epoch', () {
expect(() => BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(0), 0, 0, 0, ""), throwsAssertionError);
});
});

group('BloodPressureModel', () {
Expand Down
10 changes: 6 additions & 4 deletions test/model/settings_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ import 'package:blood_pressure_app/model/ram_only_implementations.dart';
import 'package:blood_pressure_app/model/settings_store.dart';
import 'package:file_saver/file_saver.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
SharedPreferences.setMockInitialValues({});
final testPackageInfo = PackageInfo(appName: 'blood_pressure_app', packageName: 'com.derdilla.bloodPressureApp', version: 'UnitTests', buildNumber: '999999');

group('Settings model', () {
// setup db path
databaseFactory = databaseFactoryFfi;

test('should initialize', () async {
expect(() async {
await Settings.create();
await Settings.create(testPackageInfo);
}, returnsNormally);
});

test('fields defaults should be set after initialization', () async {
var s = await Settings.create();
var s = await Settings.create(testPackageInfo);
expect(s.graphStepSize, TimeStep.day);
expect(s.followSystemDarkMode, true);
expect(s.darkMode, true);
Expand Down Expand Up @@ -52,7 +54,7 @@ void main() {
});

test('setting fields should save changes', () async {
var s = await Settings.create();
var s = await Settings.create(testPackageInfo);

int i = 0;
s.addListener(() {
Expand Down Expand Up @@ -123,7 +125,7 @@ void main() {
});

test('setting fields should notify listeners and change values', () async {
var s = await Settings.create();
var s = await Settings.create(testPackageInfo);

int i = 0;
s.addListener(() {
Expand Down
2 changes: 1 addition & 1 deletion test/ui/statistics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() {
testWidgets("should report measurement count", (widgetTester) async {
await _initStatsPage(widgetTester, _allMeasurements(), [
for (int i = 1; i<51; i++) // can't safe entries at or before epoch
BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(i), 40+i, 60+i, 30+i, 'Test comment $i'),
BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(1582991592 + i), 40+i, 60+i, 30+i, 'Test comment $i'),
]);
final measurementCountWidget = find.byKey(const Key('measurementCount'));
expect(measurementCountWidget, findsOneWidget);
Expand Down

0 comments on commit 9040764

Please sign in to comment.