Skip to content

Commit

Permalink
fixed the testcase being externally defined
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Jul 20, 2024
1 parent 2cdbb44 commit 1ee6933
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
17 changes: 14 additions & 3 deletions server/main-api/src/search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::time::Instant;
use crate::AppData;
use actix_web::{get, web, HttpResponse};
use cached::proc_macro::cached;
use meilisearch_sdk::client::Client;
use serde::{Deserialize, Serialize};
use tracing::{debug, error};
use unicode_truncate::UnicodeTruncateStr;
Expand Down Expand Up @@ -131,9 +132,19 @@ async fn cached_geoentry_search(
highlighting: Highlighting,
limits: Limits,
) -> Vec<search_executor::ResultsSection> {
search_executor::do_geoentry_search(q, highlighting, limits)
.await
.0
let ms_url = std::env::var("MIELI_URL").unwrap_or_else(|_| "http://localhost:7700".to_string());
let client = Client::new(ms_url, std::env::var("MEILI_MASTER_KEY").ok());
match client {
Ok(client) => {
search_executor::do_geoentry_search(&client, q, highlighting, limits)
.await
.0
}
Err(e) => {
error!("Cannot connect to meilisearch because {e:?}");
vec![]
}
}
}

#[cfg(test)]
Expand Down
22 changes: 17 additions & 5 deletions server/main-api/src/search/search_executor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use meilisearch_sdk::client::Client;
use serde::Serialize;
use tracing::error;

Expand Down Expand Up @@ -37,13 +38,14 @@ struct ResultEntry {
}
#[tracing::instrument]
pub async fn do_geoentry_search(
client: &Client,
q: String,
highlighting: Highlighting,
limits: Limits,
) -> LimitedVec<ResultsSection> {
let parsed_input = ParsedQuery::from(q.as_str());

match query::GeoEntryQuery::from((&parsed_input, &limits, &highlighting))
match query::GeoEntryQuery::from((client, &parsed_input, &limits, &highlighting))
.execute()
.await
{
Expand Down Expand Up @@ -76,6 +78,7 @@ pub async fn do_geoentry_search(
#[cfg(test)]
mod test {
use super::*;
use crate::setup::tests::MeiliSearchTestContainer;
use std::fmt::{Display, Formatter};

#[derive(serde::Deserialize)]
Expand All @@ -98,8 +101,9 @@ mod test {
let mut acceptable_range = actual.iter().flat_map(|r| r.entries.clone()).take(among);
acceptable_range.any(|r| r.id == self.target)
}
async fn search(&self) -> Vec<ResultsSection> {
async fn search(&self, client: &Client) -> Vec<ResultsSection> {
do_geoentry_search(
client,
self.query.clone(),
Highlighting::default(),
Limits::default(),
Expand All @@ -126,8 +130,12 @@ mod test {
#[tokio::test]
#[tracing_test::traced_test]
async fn test_good_queries() {
let ms = MeiliSearchTestContainer::new().await;
crate::setup::meilisearch::load_data(&ms.client)
.await
.unwrap();
for query in TestQuery::load_good() {
let actual = query.search().await;
let actual = query.search(&ms.client).await;
assert!(
query.actual_matches_among(&actual),
"{query}\nSince it can't, please move it to .bad list, actual={actual:?}"
Expand All @@ -145,8 +153,12 @@ mod test {
#[tokio::test]
#[tracing_test::traced_test]
async fn test_bad_queries() {
for query in TestQuery::load_bad() {
let actual = query.search().await;
let ms = MeiliSearchTestContainer::new().await;
crate::setup::meilisearch::load_data(&ms.client)
.await
.unwrap();
for query in TestQuery::load_bad() {
let actual = query.search(&ms.client).await;
assert!(
!query.actual_matches_among(&actual),
"{query}\nSince it can't, please move it to .bad list, actual={actual:?}"
Expand Down
20 changes: 13 additions & 7 deletions server/main-api/src/search/search_executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,25 @@ impl From<&Filter> for GeoEntryFilters {

#[derive(Debug)]
pub(super) struct GeoEntryQuery {
client: Client,
parsed_input: ParsedQuery,
limits: Limits,
highlighting: Highlighting,
filters: GeoEntryFilters,
sorting: Vec<String>,
}

impl From<(&ParsedQuery, &Limits, &Highlighting)> for GeoEntryQuery {
fn from((parsed_input, limits, highlighting): (&ParsedQuery, &Limits, &Highlighting)) -> Self {
impl From<(&Client, &ParsedQuery, &Limits, &Highlighting)> for GeoEntryQuery {
fn from(
(client, parsed_input, limits, highlighting): (
&Client,
&ParsedQuery,
&Limits,
&Highlighting,
),
) -> Self {
Self {
client: client.clone(),
parsed_input: parsed_input.clone(),
limits: *limits,
highlighting: highlighting.clone(),
Expand All @@ -68,10 +77,7 @@ impl GeoEntryQuery {
#[tracing::instrument(ret(level = tracing::Level::TRACE))]
pub async fn execute(self) -> Result<MultiSearchResponse<MSHit>, Error> {
let q_default = self.prompt_for_querying();
let ms_url =
std::env::var("MIELI_URL").unwrap_or_else(|_| "http://localhost:7700".to_string());
let client = Client::new(ms_url, std::env::var("MEILI_MASTER_KEY").ok())?;
let entries = client.index("entries");
let entries = self.client.index("entries");

// due to lifetime shenanigans this is added here (I can't make it move down to the other statements)
// If you can make it, please propose a PR, I know that this is really hacky ^^
Expand All @@ -86,7 +92,7 @@ impl GeoEntryQuery {
// for all entries and only rooms, search matching (and relevant) buildings can be
// expected to be at the top of the merged search. However sometimes a lot of
// buildings will be hidden (e.g. building parts), so the extra room search ....
client
self.client
.multi_search()
.with_search_query(
self.merged_query(&entries, &q_default)
Expand Down

0 comments on commit 1ee6933

Please sign in to comment.