Skip to content

Commit

Permalink
Added more tests for stats
Browse files Browse the repository at this point in the history
  • Loading branch information
prijindal committed May 6, 2024
1 parent 547ec12 commit bfc016d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
50 changes: 48 additions & 2 deletions lib/helpers/stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,55 @@ class DurationData {
final Duration duration;
final DateTime start;
final DateTime end;

@override
String toString() {
return "${duration.toString()} ${start.toString()} ${end.toString()}";
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is DurationData &&
duration == other.duration &&
start == other.start &&
end == other.end;
}

@override
int get hashCode =>
Object.hash(duration.hashCode, start.hashCode, end.hashCode);
}

class CountsDayData {
final DateTime date;
final int count;

CountsDayData({required this.date, required this.count});

@override
String toString() {
return "${date.toString()} ${count.toString()}";
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is CountsDayData && date == other.date && count == other.count;
}

@override
int get hashCode => Object.hash(date.hashCode, count.hashCode);
}

String durationToStreak(Duration? streak) {
Expand Down Expand Up @@ -136,11 +178,15 @@ List<CountsDayData> countPerDaysData(List<HabbitEntryData>? entries,
}

List<DurationData> allDurationsData(
List<HabbitEntryData>? entries, {
List<HabbitEntryData>? unsortedEntries, {
bool includeCurrent = false,
}) {
final List<DurationData> allDurations = [];
if (entries != null && entries.isNotEmpty) {
if (unsortedEntries != null && unsortedEntries.isNotEmpty) {
final entries = (List<HabbitEntryData>.from(unsortedEntries)
..sort((a, b) => a.creationTime.compareTo(b.creationTime)))
.reversed
.toList();
if (includeCurrent) {
final now = DateTime.now();
allDurations.add(
Expand Down
58 changes: 58 additions & 0 deletions test/stats_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:habbit_tracker/helpers/stats.dart';
import 'package:habbit_tracker/models/core.dart';
import 'package:test/test.dart';

void main() {
Expand All @@ -10,5 +11,62 @@ void main() {
expect(
formatDate(date, HabbitDateFormat.long), "Sat, Jan 1, 2000 12:00 AM");
});
test('Convert entries into durations', () {
final entries = [
HabbitEntryData(
id: "1",
creationTime: DateTime(2000, 1, 1, 0, 0, 0, 0, 0),
habbit: "habbit-1",
),
HabbitEntryData(
id: "1",
creationTime: DateTime(2000, 1, 1, 2, 0, 0, 0, 0),
habbit: "habbit-1",
)
];
final durations = allDurationsData(entries);
expect(durations.length, 1);
expect(
durations[0] ==
DurationData(
duration: Duration(hours: 2),
start: DateTime(2000, 1, 1, 0, 0, 0, 0, 0),
end: DateTime(2000, 1, 1, 2, 0, 0, 0, 0),
),
true,
);
});

test('Convert entries into Counts per days', () {
final entries = [
HabbitEntryData(
id: "1",
creationTime: DateTime(2000, 1, 1, 0, 0, 0, 0, 0),
habbit: "habbit-1",
),
HabbitEntryData(
id: "1",
creationTime: DateTime(2000, 1, 1, 2, 0, 0, 0, 0),
habbit: "habbit-1",
),
HabbitEntryData(
id: "1",
creationTime: DateTime(2000, 1, 2, 0, 0, 0, 0, 0),
habbit: "habbit-1",
)
];
final counts = countPerDaysData(entries);
expect(counts.length, 2);
expect(
counts[0] ==
CountsDayData(date: DateTime(2000, 1, 1, 0, 0, 0, 0, 0), count: 2),
true,
);
expect(
counts[1] ==
CountsDayData(date: DateTime(2000, 1, 2, 0, 0, 0, 0, 0), count: 1),
true,
);
});
});
}

0 comments on commit bfc016d

Please sign in to comment.