lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

selection_tests.rs (3775B)


      1 use super::*;
      2 
      3 fn generation(byte: u8) -> ProjectionGeneration {
      4     ProjectionGeneration::new([byte; 32]).unwrap()
      5 }
      6 fn query() -> ProjectionDocumentQuery {
      7     ProjectionDocumentQuery::new(
      8         ProjectionId::parse("fixture").unwrap(),
      9         ProjectionDocumentGenerations::Exact(generation(1)),
     10         2,
     11     )
     12     .unwrap()
     13 }
     14 fn range(first: &str, last: &str) -> ProjectionDocumentKeyRange {
     15     ProjectionDocumentKeyRange::new(first.into(), last.into()).unwrap()
     16 }
     17 fn record(key: &str, bytes: usize) -> ProjectionDocumentRecord {
     18     ProjectionDocumentRecord::new(
     19         generation(1),
     20         ProjectionDocument::new(key.into(), vec![1; bytes]).unwrap(),
     21     )
     22     .unwrap()
     23 }
     24 
     25 #[test]
     26 fn selection_validates_key_and_payload_boundaries_before_provider_work() {
     27     for key in ["", " bad", "bad\n", &"x".repeat(513), &"é".repeat(257)] {
     28         assert!(ProjectionDocumentKeyRange::new(key.into(), key.into()).is_err());
     29         assert!(query().with_after_key(key.into()).is_err());
     30     }
     31     let maximum = "é".repeat(256);
     32     assert_eq!(range(&maximum, &maximum).first().len(), 512);
     33     assert!(query().with_after_key(maximum).is_ok());
     34     assert!(ProjectionDocumentKeyRange::new("z".into(), "a".into()).is_err());
     35     for bytes in [0, PROJECTION_DOCUMENT_PAGE_BYTES_MAX + 1, usize::MAX] {
     36         assert!(query().with_payload_budget(bytes).is_err());
     37     }
     38     for bytes in [1, PROJECTION_DOCUMENT_PAGE_BYTES_MAX] {
     39         assert_eq!(
     40             query().with_payload_budget(bytes).unwrap().payload_budget(),
     41             bytes
     42         );
     43     }
     44     let all = ProjectionDocumentQuery::new(
     45         query().projection_id().clone(),
     46         ProjectionDocumentGenerations::All,
     47         1,
     48     )
     49     .unwrap();
     50     assert!(all.clone().with_key_range(range("a", "z")).is_err());
     51     assert!(all.with_after_key("a".into()).is_err());
     52     for key in ["a", "z"] {
     53         assert!(
     54             query()
     55                 .with_key_range(range("b", "y"))
     56                 .unwrap()
     57                 .with_after_key(key.into())
     58                 .is_err()
     59         );
     60     }
     61     assert!(
     62         query()
     63             .with_after_key("c".into())
     64             .unwrap()
     65             .with_key_range(range("a", "z"))
     66             .is_err()
     67     );
     68 }
     69 
     70 #[test]
     71 fn selected_continuations_cannot_widen_range_or_cross_generation() {
     72     let selected = query().with_key_range(range("b", "é")).unwrap();
     73     let page =
     74         ProjectionDocumentPage::new(&selected, vec![record("b", 1), record("z", 1)], true).unwrap();
     75     let cursor = page.next_cursor().unwrap();
     76     let resumed = selected.clone().with_cursor(cursor).unwrap();
     77     let sought = selected.clone().with_after_key("z".into()).unwrap();
     78     assert_eq!(resumed, sought);
     79     assert!(ProjectionDocumentPage::new(&sought, vec![record("é", 1)], false).is_ok());
     80     for changed in [
     81         query(),
     82         query().with_key_range(range("a", "é")).unwrap(),
     83         ProjectionDocumentQuery::new(
     84             query().projection_id().clone(),
     85             ProjectionDocumentGenerations::Exact(generation(2)),
     86             2,
     87         )
     88         .unwrap()
     89         .with_key_range(range("b", "é"))
     90         .unwrap(),
     91     ] {
     92         assert!(changed.with_cursor(cursor).is_err());
     93     }
     94     for key in ["a", "ê"] {
     95         assert!(ProjectionDocumentPage::new(&selected, vec![record(key, 1)], false).is_err());
     96     }
     97     let exact = query().with_key_range(range("é", "é")).unwrap();
     98     assert_eq!(
     99         ProjectionDocumentPage::new(&exact, vec![record("é", 1)], false)
    100             .unwrap()
    101             .records()
    102             .len(),
    103         1
    104     );
    105     let budget = selected.with_payload_budget(7).unwrap();
    106     assert!(
    107         ProjectionDocumentPage::new(&budget, vec![record("b", 4), record("c", 4)], false).is_err()
    108     );
    109 }