2024-07-18 21:35:17 +03:00
import becca_mocking from "./becca_mocking.js" ;
import ValueExtractor from "../../src/services/search/value_extractor.js" ;
import becca from "../../src/becca/becca.js" ;
import SearchContext from "../../src/services/search/search_context.js" ;
2024-05-08 23:59:11 +02:00
const dsc = new SearchContext ( ) ;
2025-01-09 18:07:02 +02:00
describe ( "Value extractor" , ( ) = > {
2024-05-08 23:59:11 +02:00
beforeEach ( ( ) = > {
becca . reset ( ) ;
} ) ;
2025-01-09 18:07:02 +02:00
it ( "simple title extraction" , async ( ) = > {
const europe = becca_mocking . note ( "Europe" ) . note ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
const valueExtractor = new ValueExtractor ( dsc , [ "note" , "title" ] ) ;
2024-05-08 23:59:11 +02:00
expect ( valueExtractor . validate ( ) ) . toBeFalsy ( ) ;
2025-01-09 18:07:02 +02:00
expect ( valueExtractor . extract ( europe ) ) . toEqual ( "Europe" ) ;
2024-05-08 23:59:11 +02:00
} ) ;
2025-01-09 18:07:02 +02:00
it ( "label extraction" , async ( ) = > {
const austria = becca_mocking . note ( "Austria" ) . label ( "Capital" , "Vienna" ) . note ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
let valueExtractor = new ValueExtractor ( dsc , [ "note" , "labels" , "capital" ] ) ;
2024-05-08 23:59:11 +02:00
expect ( valueExtractor . validate ( ) ) . toBeFalsy ( ) ;
2025-01-09 18:07:02 +02:00
expect ( valueExtractor . extract ( austria ) ) . toEqual ( "Vienna" ) ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
valueExtractor = new ValueExtractor ( dsc , [ "#capital" ] ) ;
2024-05-08 23:59:11 +02:00
expect ( valueExtractor . validate ( ) ) . toBeFalsy ( ) ;
2025-01-09 18:07:02 +02:00
expect ( valueExtractor . extract ( austria ) ) . toEqual ( "Vienna" ) ;
2024-05-08 23:59:11 +02:00
} ) ;
2025-01-09 18:07:02 +02:00
it ( "parent/child property extraction" , async ( ) = > {
const vienna = becca_mocking . note ( "Vienna" ) ;
const europe = becca_mocking . note ( "Europe" ) . child ( becca_mocking . note ( "Austria" ) . child ( vienna ) ) ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
let valueExtractor = new ValueExtractor ( dsc , [ "note" , "children" , "children" , "title" ] ) ;
2024-05-08 23:59:11 +02:00
expect ( valueExtractor . validate ( ) ) . toBeFalsy ( ) ;
2025-01-09 18:07:02 +02:00
expect ( valueExtractor . extract ( europe . note ) ) . toEqual ( "Vienna" ) ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
valueExtractor = new ValueExtractor ( dsc , [ "note" , "parents" , "parents" , "title" ] ) ;
2024-05-08 23:59:11 +02:00
expect ( valueExtractor . validate ( ) ) . toBeFalsy ( ) ;
2025-01-09 18:07:02 +02:00
expect ( valueExtractor . extract ( vienna . note ) ) . toEqual ( "Europe" ) ;
2024-05-08 23:59:11 +02:00
} ) ;
2025-01-09 18:07:02 +02:00
it ( "extract through relation" , async ( ) = > {
const czechRepublic = becca_mocking . note ( "Czech Republic" ) . label ( "capital" , "Prague" ) ;
const slovakia = becca_mocking . note ( "Slovakia" ) . label ( "capital" , "Bratislava" ) ;
const austria = becca_mocking . note ( "Austria" ) . relation ( "neighbor" , czechRepublic . note ) . relation ( "neighbor" , slovakia . note ) ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
let valueExtractor = new ValueExtractor ( dsc , [ "note" , "relations" , "neighbor" , "labels" , "capital" ] ) ;
2024-05-08 23:59:11 +02:00
expect ( valueExtractor . validate ( ) ) . toBeFalsy ( ) ;
2025-01-09 18:07:02 +02:00
expect ( valueExtractor . extract ( austria . note ) ) . toEqual ( "Prague" ) ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
valueExtractor = new ValueExtractor ( dsc , [ "~neighbor" , "labels" , "capital" ] ) ;
2024-05-08 23:59:11 +02:00
expect ( valueExtractor . validate ( ) ) . toBeFalsy ( ) ;
2025-01-09 18:07:02 +02:00
expect ( valueExtractor . extract ( austria . note ) ) . toEqual ( "Prague" ) ;
2024-05-08 23:59:11 +02:00
} ) ;
} ) ;
2025-01-09 18:07:02 +02:00
describe ( "Invalid value extractor property path" , ( ) = > {
it ( 'each path must start with "note" (or label/relation)' , ( ) = > expect ( new ValueExtractor ( dsc , [ "neighbor" ] ) . validate ( ) ) . toBeTruthy ( ) ) ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
it ( "extra path element after terminal label" , ( ) = > expect ( new ValueExtractor ( dsc , [ "~neighbor" , "labels" , "capital" , "noteId" ] ) . validate ( ) ) . toBeTruthy ( ) ) ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
it ( "extra path element after terminal title" , ( ) = > expect ( new ValueExtractor ( dsc , [ "note" , "title" , "isProtected" ] ) . validate ( ) ) . toBeTruthy ( ) ) ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
it ( "relation name and note property is missing" , ( ) = > expect ( new ValueExtractor ( dsc , [ "note" , "relations" ] ) . validate ( ) ) . toBeTruthy ( ) ) ;
2024-05-08 23:59:11 +02:00
2025-01-09 18:07:02 +02:00
it ( "relation is specified but target note property is not specified" , ( ) = > expect ( new ValueExtractor ( dsc , [ "note" , "relations" , "myrel" ] ) . validate ( ) ) . toBeTruthy ( ) ) ;
2024-05-08 23:59:11 +02:00
} ) ;