Skip to content

Commit

Permalink
feat: only return top 20 in charts
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-hagemann committed Nov 28, 2023
1 parent 1b2e482 commit 2307068
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion example.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
APP_ENV=dev
APP_HOST=0.0.0.0
APP_JWT_SECRET=deadbeef
APP_LOG_LEVEL=info
APP_LOG_LEVEL=error
APP_NAME=ratings
APP_PORT=8080
# Update this with some real PostgreSQL details
Expand Down
5 changes: 4 additions & 1 deletion src/features/chart/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ impl Chart {
.unwrap_or(std::cmp::Ordering::Equal)
});

// Take only the first 20 elements from the sorted chart_data
let top_20: Vec<ChartData> = chart_data.into_iter().take(20).collect();

Chart {
timeframe,
chart_data,
chart_data: top_20,
}
}
}
Expand Down
50 changes: 24 additions & 26 deletions tests/chart_tests/lifecycle_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ async fn chart_lifecycle_test() -> Result<(), Box<dyn std::error::Error>> {
// Does an app voted against once appear correctly in the chart?
async fn vote_once(mut data: TestData) -> TestData {
let vote_up = true;
let expected_raw_rating = 0.0;
let expected_rating = Rating {
snap_id: data.snap_id.clone().unwrap(),
total_votes: 1,
ratings_band: RatingsBand::InsufficientVotes.into(),
};

// Fill up chart with other votes so ours doesn't appear
for _ in 0..20 {
generate_votes(&data_faker::rnd_id(), 111, vote_up, 25, data.clone())
.await
.expect("Votes should succeed");
}

let vote_up = true;

generate_votes(
&data.snap_id.clone().unwrap(),
Expand Down Expand Up @@ -89,11 +92,8 @@ async fn vote_once(mut data: TestData) -> TestData {
}
});

let actual_rating = result.clone().unwrap().rating.unwrap();
let actual_raw_rating = result.unwrap().raw_rating;

assert_eq!(expected_rating, actual_rating);
assert_eq!(expected_raw_rating, actual_raw_rating);
// Should not appear in chart
assert_eq!(result, None);

data
}
Expand All @@ -104,15 +104,16 @@ async fn multiple_votes(mut data: TestData) -> TestData {
let expected_raw_rating = 0.8;
let expected_rating = Rating {
snap_id: data.snap_id.clone().unwrap(),
total_votes: 25,
total_votes: 101,
ratings_band: RatingsBand::VeryGood.into(),
};

// This should rank our snap_id at the top of the chart
generate_votes(
&data.snap_id.clone().unwrap(),
111,
vote_up,
24,
100,
data.clone(),
)
.await
Expand All @@ -138,19 +139,16 @@ async fn multiple_votes(mut data: TestData) -> TestData {
.into_inner()
.ordered_chart_data;

let result = chart_data_result.into_iter().find(|chart_data| {
if let Some(rating) = &chart_data.rating {
rating.snap_id == data.snap_id.clone().unwrap()
} else {
false
}
});
// Should be at the top of the chart
if let Some(chart_data) = chart_data_result.first() {
let actual_rating = chart_data.rating.clone().expect("Rating should exist");
let actual_raw_rating = chart_data.raw_rating;

let actual_rating = result.clone().unwrap().rating.unwrap();
let actual_raw_rating = result.unwrap().raw_rating;

assert_eq!(expected_rating, actual_rating);
assert!(expected_raw_rating < actual_raw_rating);
assert_eq!(expected_rating, actual_rating);
assert!(expected_raw_rating < actual_raw_rating);
} else {
panic!("No chart data available");
}

data
}
Expand Down Expand Up @@ -200,7 +198,7 @@ async fn timeframed_votes_dont_appear(mut data: TestData) -> TestData {
let expected_raw_rating = 0.8;
let expected_rating = Rating {
snap_id: data.snap_id.clone().unwrap(),
total_votes: 25,
total_votes: 101,
ratings_band: RatingsBand::VeryGood.into(),
};

Expand Down

0 comments on commit 2307068

Please sign in to comment.