Skip to content

Commit 64a9391

Browse files
committed
update tests
1 parent 278202d commit 64a9391

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

app/lib/service/download_counts/package_trends.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const minThirtyDaysDownloadThreshold = 30000;
2323
/// downloads (10% relative growth) than for a package with 10000 average daily
2424
/// downloads (0.1% relative growth).
2525
double computeRelativeGrowthRate(List<int> totalDownloads) {
26+
if (totalDownloads.isEmpty) {
27+
return 0;
28+
}
2629
final List<int> data;
2730
if (totalDownloads.length < analysisWindowDays) {
2831
data = [

app/test/service/download_counts/package_trends_test.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ void main() {
3636
test('calculates positive relative growth rate for positive trend', () {
3737
// Input list (newest first): [1645, 1635, ..., 1355] (30 values)
3838
// Average = 1500 for the first 30 values. Slope: 10.
39-
final downloads =
40-
List<int>.generate(analysisWindowDays * 2, (i) => 1645 - (i * 10));
41-
final expectedRate = 10.0 / 1500.0;
39+
final downloads = <int>[
40+
...List<int>.generate(analysisWindowDays * 2, (i) => 1645 - (i * 10)),
41+
...List.filled(300, 0)
42+
];
43+
final avg = downloads.reduce((prev, element) => prev + element) / 330;
44+
final expectedRate = 10.0 / avg;
4245
expect(computeRelativeGrowthRate(downloads), expectedRate);
4346
});
4447

@@ -107,10 +110,10 @@ void main() {
107110
final downloads = [100, 50];
108111
// For relativeGrowth:
109112
// Padded data: [100, 50, 0...0] (28 zeros)
110-
// avg = 150/30 = 5
113+
// avg = (100 + 50) / 2 = 75.
111114
// growthRate = 63750 / 67425
112115
final expectedDampening = min(1.0, 150 / 30000);
113-
final expectedRelativeGrowth = 63750 / 67425 / 5;
116+
final expectedRelativeGrowth = (63750 / 67425) / 75;
114117
final expectedScore =
115118
expectedRelativeGrowth * expectedDampening * expectedDampening;
116119
expect(computeTrendScore(downloads), expectedScore);
@@ -178,7 +181,7 @@ void main() {
178181
test('Short history, high sum meets threshold -> no dampening', () {
179182
final downloads = List<int>.filled(15, 2000);
180183
final expectedDampening = min(1.0, 30000 / 30000);
181-
final expectedRelativeGrowth = 6750000 / 67425 / 1000;
184+
final expectedRelativeGrowth = (6750000 / 67425) / 2000;
182185
final expectedScore =
183186
expectedRelativeGrowth * expectedDampening * expectedDampening;
184187

0 commit comments

Comments
 (0)