mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
start on search tests
This commit is contained in:
parent
bca5f2ddff
commit
1f9e8e388a
2 changed files with 50 additions and 18 deletions
|
@ -498,7 +498,10 @@ fn extract_latex_refs(note: &Note, seen_files: &mut HashSet<String>, svg: bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
pub(crate) mod test {
|
||||||
|
pub(crate) const MEDIACHECK_ANKI2: &'static [u8] =
|
||||||
|
include_bytes!("../../tests/support/mediacheck.anki2");
|
||||||
|
|
||||||
use crate::collection::{open_collection, Collection};
|
use crate::collection::{open_collection, Collection};
|
||||||
use crate::err::Result;
|
use crate::err::Result;
|
||||||
use crate::i18n::I18n;
|
use crate::i18n::I18n;
|
||||||
|
@ -516,10 +519,7 @@ mod test {
|
||||||
fs::create_dir(&media_dir)?;
|
fs::create_dir(&media_dir)?;
|
||||||
let media_db = dir.path().join("media.db");
|
let media_db = dir.path().join("media.db");
|
||||||
let col_path = dir.path().join("col.anki2");
|
let col_path = dir.path().join("col.anki2");
|
||||||
fs::write(
|
fs::write(&col_path, MEDIACHECK_ANKI2)?;
|
||||||
&col_path,
|
|
||||||
&include_bytes!("../../tests/support/mediacheck.anki2")[..],
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let mgr = MediaManager::new(&media_dir, media_db.clone())?;
|
let mgr = MediaManager::new(&media_dir, media_db.clone())?;
|
||||||
|
|
||||||
|
|
|
@ -321,6 +321,9 @@ where
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::ids_to_string;
|
use super::ids_to_string;
|
||||||
|
use crate::{collection::open_collection, i18n::I18n, log};
|
||||||
|
use std::{fs, path::PathBuf};
|
||||||
|
use tempfile::tempdir;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ids_string() {
|
fn ids_string() {
|
||||||
|
@ -339,22 +342,51 @@ mod test {
|
||||||
s.clear();
|
s.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// use super::super::parser::parse;
|
use super::super::parser::parse;
|
||||||
// use super::*;
|
use super::*;
|
||||||
|
|
||||||
// parse
|
// shortcut
|
||||||
// fn p(search: &str) -> Node {
|
fn s(req: &mut RequestContext, search: &str) -> (String, Vec<String>) {
|
||||||
// Node::Group(parse(search).unwrap())
|
let node = Node::Group(parse(search).unwrap());
|
||||||
// }
|
node_to_sql(req, &node).unwrap()
|
||||||
|
}
|
||||||
// get sql
|
|
||||||
// fn s<'a>(n: &'a Node) -> (String, Vec<ToSqlOutput<'a>>) {
|
|
||||||
// node_to_sql(n)
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tosql() -> Result<(), String> {
|
fn sql() -> Result<()> {
|
||||||
// assert_eq!(s(&p("added:1")), ("(c.id > 1)".into(), vec![]));
|
// re-use the mediacheck .anki2 file for now
|
||||||
|
use crate::media::check::test::MEDIACHECK_ANKI2;
|
||||||
|
let dir = tempdir().unwrap();
|
||||||
|
let col_path = dir.path().join("col.anki2");
|
||||||
|
fs::write(&col_path, MEDIACHECK_ANKI2).unwrap();
|
||||||
|
|
||||||
|
let i18n = I18n::new(&[""], "", log::terminal());
|
||||||
|
let col = open_collection(
|
||||||
|
&col_path,
|
||||||
|
&PathBuf::new(),
|
||||||
|
&PathBuf::new(),
|
||||||
|
false,
|
||||||
|
i18n,
|
||||||
|
log::terminal(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
col.with_ctx(|ctx| {
|
||||||
|
// unqualified search
|
||||||
|
assert_eq!(
|
||||||
|
s(ctx, "test"),
|
||||||
|
(
|
||||||
|
"((n.sfld like ?1 escape '\\' or n.flds like ?1 escape '\\'))".into(),
|
||||||
|
vec!["%test%".into()]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
assert_eq!(s(ctx, "te%st").1, vec!["%te%st%".to_string()]);
|
||||||
|
// user should be able to escape sql wildcards
|
||||||
|
assert_eq!(s(ctx, r#"te\%s\_t"#).1, vec!["%te\\%s\\_t%".to_string()]);
|
||||||
|
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue