Skip to content

Commit ea7258e

Browse files
committed
added generateMissingIdsForBlankNodes tests
1 parent 61c053f commit ea7258e

File tree

1 file changed

+339
-0
lines changed

1 file changed

+339
-0
lines changed

test/knowledge-collection-tools.test.js

Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { describe, it } from "mocha";
2+
import fs from 'fs';
3+
import N3 from 'n3';
24
import { expect } from "chai";
35
import {
46
formatDataset,
@@ -531,4 +533,341 @@ describe("generateMissingIdsForBlankNodes", () => {
531533
)
532534
);
533535
});
536+
537+
it("should replace an object blank node", () => {
538+
/*
539+
JSON-LD
540+
{
541+
"@context": {
542+
"relatedTo": "http://example.org/relatedTo"
543+
},
544+
"@id": "http://example.org/document/1",
545+
"relatedTo": {}
546+
}
547+
*/
548+
const nquadsArray = [
549+
"<http://example.org/document/1> <http://example.org/relatedTo> _:c14n0 .",
550+
];
551+
552+
const updatedQuads = generateMissingIdsForBlankNodes(nquadsArray);
553+
554+
const parser = new N3.Parser();
555+
const quads = parser.parse(updatedQuads.join('\n'));
556+
557+
expect(quads[0]._subject.id).equals('http://example.org/document/1');
558+
559+
expect(quads[0]._predicate.id).equals('http://example.org/relatedTo');
560+
561+
const uuidRegex = /^uuid:[0-9a-fA-F\-]{36}$/;
562+
expect(quads[0]._object.id.match(uuidRegex));
563+
564+
expect(quads[0]._graph.id).equals('');
565+
});
566+
567+
it("should replace an occuring object blank node", () => {
568+
/*
569+
JSON-LD
570+
{
571+
"@context": {
572+
"is": {"@id": "http://example.org/is"}
573+
},
574+
"@graph": [
575+
{
576+
"@id": "http://example.org/subject1",
577+
"is": {"@id": "_:sharedBlank"}
578+
},
579+
{
580+
"@id": "http://example.org/subject2",
581+
"is": {"@id": "_:sharedBlank"}
582+
}
583+
]
584+
}
585+
*/
586+
const nquadsArray = [
587+
"<http://example.org/subject1> <http://example.org/is> _:c14n0 .",
588+
"<http://example.org/subject2> <http://example.org/is> _:c14n0 .",
589+
];
590+
591+
const updatedQuads = generateMissingIdsForBlankNodes(nquadsArray);
592+
593+
const parser = new N3.Parser();
594+
const quads = parser.parse(updatedQuads.join('\n'));
595+
596+
expect(quads[0]._subject.id).equals('http://example.org/subject1');
597+
expect(quads[1]._subject.id).equals('http://example.org/subject2');
598+
599+
expect(quads[0]._predicate.id).equals('http://example.org/is');
600+
expect(quads[1]._predicate.id).equals('http://example.org/is');
601+
602+
const uuidRegex = /^uuid:[0-9a-fA-F\-]{36}$/;
603+
604+
expect(quads[0]._object.id.match(uuidRegex));
605+
expect(quads[1]._object.id.match(uuidRegex));
606+
607+
expect(quads[0]._graph.id).equals('');
608+
expect(quads[1]._graph.id).equals('');
609+
610+
expect(quads[0]._object.id).equals(quads[1]._object.id);
611+
});
612+
613+
it("should replace a subject blank node", () => {
614+
/*
615+
JSON-LD
616+
{
617+
"@context": {
618+
"ex": "http://example.org/"
619+
},
620+
"@graph": [
621+
{
622+
"ex:name": "John Doe"
623+
}
624+
]
625+
}
626+
*/
627+
const nquadsArray = [
628+
'_:c14n0 <http://example.org/name> "John Doe" .',
629+
];
630+
631+
const updatedQuads = generateMissingIdsForBlankNodes(nquadsArray);
632+
633+
const parser = new N3.Parser();
634+
const quads = parser.parse(updatedQuads.join('\n'));
635+
636+
const uuidRegex = /^uuid:[0-9a-fA-F\-]{36}$/;
637+
expect(quads[0]._subject.id.match(uuidRegex));
638+
639+
expect(quads[0]._predicate.id).equals('http://example.org/name');
640+
641+
expect(quads[0]._object.id).equals('"John Doe"');
642+
643+
expect(quads[0]._graph.id).equals('');
644+
});
645+
646+
it("should replace an occuring subject blank node", () => {
647+
/*
648+
JSON-LD
649+
{
650+
"@context": {
651+
"ex": "http://example.org/"
652+
},
653+
"@graph": [
654+
{
655+
"ex:name": "John Doe",
656+
"ex:sex": "male"
657+
}
658+
]
659+
}
660+
*/
661+
const nquadsArray = [
662+
'_:c14n0 <http://example.org/name> "John Doe" .',
663+
'_:c14n0 <http://example.org/sex> "male" .',
664+
];
665+
666+
const updatedQuads = generateMissingIdsForBlankNodes(nquadsArray);
667+
668+
const parser = new N3.Parser();
669+
const quads = parser.parse(updatedQuads.join('\n'));
670+
671+
const uuidRegex = /^uuid:[0-9a-fA-F\-]{36}$/;
672+
expect(quads[0]._subject.id.match(uuidRegex));
673+
expect(quads[1]._subject.id.match(uuidRegex));
674+
675+
expect(quads[0]._predicate.id).equals('http://example.org/name');
676+
expect(quads[1]._predicate.id).equals('http://example.org/sex');
677+
678+
expect(quads[0]._object.id).equals('"John Doe"');
679+
expect(quads[1]._object.id).equals('"male"');
680+
681+
expect(quads[0]._graph.id).equals('');
682+
expect(quads[1]._graph.id).equals('');
683+
684+
expect(quads[0]._subject.id).equals(quads[1]._subject.id);
685+
});
686+
687+
it("should not replace two different subject blank node with the same UUID", () => {
688+
/*
689+
JSON-LD
690+
{
691+
"@context": {
692+
"ex": "http://example.org/"
693+
},
694+
"@graph": [
695+
{
696+
"ex:hasName": "Alice",
697+
"ex:sex": "male"
698+
},
699+
{
700+
"ex:hasName": "Bob",
701+
"ex:sex": "female"
702+
}
703+
]
704+
}
705+
*/
706+
const nquadsArray = [
707+
'_:c14n0 <http://example.org/sex> "male" .',
708+
'_:c14n0 <http://example.org/hasName> "Bob" .',
709+
'_:c14n1 <http://example.org/sex> "female" .',
710+
'_:c14n1 <http://example.org/hasName> "Alice" .',
711+
];
712+
713+
const updatedQuads = generateMissingIdsForBlankNodes(nquadsArray);
714+
715+
const parser = new N3.Parser();
716+
const quads = parser.parse(updatedQuads.join('\n'));
717+
718+
const uuidRegex = /^uuid:[0-9a-fA-F\-]{36}$/;
719+
expect(quads[0]._subject.id.match(uuidRegex));
720+
expect(quads[1]._subject.id.match(uuidRegex));
721+
expect(quads[2]._subject.id.match(uuidRegex));
722+
expect(quads[3]._subject.id.match(uuidRegex));
723+
724+
expect(quads[0]._predicate.id).equals('http://example.org/sex');
725+
expect(quads[1]._predicate.id).equals('http://example.org/hasName');
726+
expect(quads[2]._predicate.id).equals('http://example.org/sex');
727+
expect(quads[3]._predicate.id).equals('http://example.org/hasName');
728+
729+
expect(quads[0]._object.id).equals('"male"');
730+
expect(quads[1]._object.id).equals('"Bob"');
731+
expect(quads[2]._object.id).equals('"female"');
732+
expect(quads[3]._object.id).equals('"Alice"');
733+
734+
expect(quads[0]._graph.id).equals('');
735+
expect(quads[1]._graph.id).equals('');
736+
expect(quads[2]._graph.id).equals('');
737+
expect(quads[3]._graph.id).equals('');
738+
739+
expect(quads[0]._subject.id).equals(quads[1]._subject.id);
740+
expect(quads[2]._subject.id).equals(quads[3]._subject.id);
741+
expect(quads[0]._subject.id).not.equals(quads[2]._subject.id);
742+
});
743+
744+
it("should replace an object blank node, that occurs as a subject, with the same UUID", () => {
745+
/*
746+
JSON-LD
747+
{
748+
"@context": "http://schema.org",
749+
"review": {
750+
"reviewBody": "Excellent book!"
751+
}
752+
}
753+
*/
754+
const nquadsArray = [
755+
'_:c14n0 <http://schema.org/reviewBody> "Excellent book!" .',
756+
"_:c14n1 <http://schema.org/review> _:c14n0 .",
757+
];
758+
759+
const updatedQuads = generateMissingIdsForBlankNodes(nquadsArray);
760+
761+
const parser = new N3.Parser();
762+
const quads = parser.parse(updatedQuads.join('\n'));
763+
764+
const uuidRegex = /^uuid:[0-9a-fA-F\-]{36}$/;
765+
expect(quads[0]._subject.id.match(uuidRegex));
766+
expect(quads[1]._subject.id.match(uuidRegex));
767+
768+
expect(quads[0]._predicate.id).equals('http://schema.org/reviewBody');
769+
expect(quads[1]._predicate.id).equals('http://schema.org/review');
770+
771+
expect(quads[0]._object.id).equals('"Excellent book!"');
772+
expect(quads[1]._object.id.match(uuidRegex));
773+
774+
expect(quads[0]._graph.id).equals('');
775+
expect(quads[1]._graph.id).equals('');
776+
777+
778+
expect(quads[0]._subject.id).not.equals(quads[1]._subject.id);
779+
expect(quads[0]._subject.id).equals(quads[1]._object.id);
780+
});
781+
782+
it("should fail since graphs aren't supported at this stage", () => {
783+
/*
784+
JSON-LD
785+
{
786+
"@context": {
787+
"@base": "https://example.org/",
788+
"name": "http://schema.org/name",
789+
"knows": {
790+
"@id": "http://schema.org/knows",
791+
"@type": "@id"
792+
},
793+
"Person": "http://schema.org/Person"
794+
},
795+
"@graph": [
796+
{
797+
"@type": "Person",
798+
"name": "Alice",
799+
"knows": [
800+
{
801+
"@id": "_:bob"
802+
},
803+
{
804+
"@id": "_:carol"
805+
}
806+
]
807+
},
808+
{
809+
"@id": "_:bob",
810+
"@graph": [
811+
{
812+
"@type": "Person",
813+
"name": "Bob"
814+
}
815+
]
816+
},
817+
{
818+
"@id": "_:carol",
819+
"@graph": [
820+
{
821+
"@type": "Person",
822+
"name": "Carol"
823+
}
824+
]
825+
}
826+
]
827+
}
828+
*/
829+
const nquadsArray = [
830+
'_:c14n2 <http://schema.org/name> "Carol" _:c14n0 .',
831+
'_:c14n2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> _:c14n0 .',
832+
'_:c14n3 <http://schema.org/knows> _:c14n0 .',
833+
'_:c14n3 <http://schema.org/knows> _:c14n1 .',
834+
'_:c14n3 <http://schema.org/name> "Alice" .',
835+
'_:c14n3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .',
836+
'_:c14n4 <http://schema.org/name> "Bob" _:c14n1 .',
837+
'_:c14n4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> _:c14n1 .'
838+
];
839+
840+
try {
841+
generateMissingIdsForBlankNodes(nquadsArray);
842+
} catch (error) {
843+
expect(error.message).equals(`
844+
------------------------------------------------------------------------------------------------
845+
Unsupported JSON-LD input detected
846+
847+
After parsing the JSON-LD input, the parser detected creation of new named graphs.
848+
The DKG does not support custom named graphs.
849+
850+
Problematic Quads:
851+
1. "Carol" <http://schema.org/name> "Carol" _:b31_c14n0 .
852+
853+
2. <http://schema.org/Person> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> _:b31_c14n0 .
854+
855+
3. "Bob" <http://schema.org/name> "Bob" _:b31_c14n1 .
856+
857+
4. <http://schema.org/Person> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> _:b31_c14n1 .
858+
859+
860+
Full Parsed N-Quads Array:
861+
_:c14n2 <http://schema.org/name> "Carol" _:c14n0 .
862+
_:c14n2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> _:c14n0 .
863+
_:c14n3 <http://schema.org/knows> _:c14n0 .
864+
_:c14n3 <http://schema.org/knows> _:c14n1 .
865+
_:c14n3 <http://schema.org/name> "Alice" .
866+
_:c14n3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
867+
_:c14n4 <http://schema.org/name> "Bob" _:c14n1 .
868+
_:c14n4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> _:c14n1 .
869+
Parsing failed due to presence of unnamed (blank node) graphs. Please ensure all graphs in the input JSON-LD have proper named IRIs.
870+
`);
871+
}
872+
});
534873
});

0 commit comments

Comments
 (0)