-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from hypermedia-app/dash-uri-starts
feat: pattern for new URIs
- Loading branch information
Showing
13 changed files
with
495 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@hydrofoil/shaperone-core": patch | ||
--- | ||
|
||
When adding a new value to a property which was `sh:nodeKind sh:IRI`, an empty IRI `<>` was always created, even if it clashed without existing nodes. Now random IRI references will be generated. | ||
Also, if a property is annotated with `sh1:iriPrefix`, it will be used as base for the created URIs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 0 additions & 64 deletions
64
packages/core-tests/models/resources/lib/defaultValue.test.ts
This file was deleted.
Oops, something went wrong.
173 changes: 173 additions & 0 deletions
173
packages/core-tests/models/resources/lib/objectValue.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { describe, it } from 'mocha' | ||
import { expect } from 'chai' | ||
import cf from 'clownface' | ||
import $rdf from 'rdf-ext' | ||
import { literal } from '@rdf-esm/data-model' | ||
import { xsd, rdf, foaf, dash, sh } from '@tpluscode/rdf-ns-builders' | ||
import { NodeKind, NodeKindEnum } from '@rdfine/shacl' | ||
import { defaultValue } from '@hydrofoil/shaperone-core/models/resources/lib/objectValue.js' | ||
import { propertyShape } from '@shaperone/testing/util.js' | ||
import { Term } from 'rdf-js' | ||
import sh1 from '@hydrofoil/shaperone-core/ns.js' | ||
|
||
describe('core/models/resources/lib/defaultValue', () => { | ||
it('returns default value from property', () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
defaultValue: literal('foo', xsd.anySimpleType), | ||
}) | ||
|
||
// when | ||
const pointer = defaultValue(property, graph.blankNode()) | ||
|
||
// then | ||
expect(pointer?.term).to.deep.eq(literal('foo', xsd.anySimpleType)) | ||
}) | ||
|
||
it('returns null when there is no nodeKind', () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
}) | ||
|
||
// when | ||
const pointer = defaultValue(property, graph.blankNode()) | ||
|
||
// then | ||
expect(pointer).to.be.null | ||
}) | ||
|
||
it('returns null when there is nodeKind is sh:IRIOrLiteral ad there is no sh1:iriPrefix', () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
nodeKind: sh.IRIOrLiteral, | ||
}) | ||
|
||
// when | ||
const pointer = defaultValue(property, graph.blankNode()) | ||
|
||
// then | ||
expect(pointer).to.be.null | ||
}) | ||
|
||
it('creates a random IRI when sh:nodeKind sh:IRI', () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
nodeKind: sh.IRI, | ||
}) | ||
|
||
// when | ||
const first = defaultValue(property, graph.blankNode()) | ||
const second = defaultValue(property, graph.blankNode()) | ||
|
||
// then | ||
expect(first?.term).not.to.deep.eq(second) | ||
}) | ||
|
||
it('uses base from sh1:iriPrefix when sh:nodeKind sh:IRI', () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
nodeKind: sh.IRI, | ||
[sh1.iriPrefix.value]: 'http://example.com/foo/', | ||
}) | ||
|
||
// when | ||
const term = defaultValue(property, graph.blankNode())?.term | ||
|
||
// then | ||
expect(term?.termType).to.eq('NamedNode') | ||
expect(term?.value).to.match(/^http:\/\/example.com\/foo\/.+$/) | ||
}) | ||
|
||
it('creates a URI node when node kind is sh:BlankNodeOrIRI and property has sh1:iriPrefix', () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
nodeKind: sh.BlankNodeOrIRI, | ||
[sh1.iriPrefix.value]: 'http://example.com/foo/', | ||
}) | ||
|
||
// when | ||
const term = defaultValue(property, graph.blankNode())?.term | ||
|
||
// then | ||
expect(term?.termType).to.eq('NamedNode') | ||
expect(term?.value).to.match(/^http:\/\/example.com\/foo\/.+$/) | ||
}) | ||
|
||
it('creates a URI node when node kind is sh:IRIOrLiteral and property has sh1:iriPrefix', () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
nodeKind: sh.IRIOrLiteral, | ||
[sh1.iriPrefix.value]: 'http://example.com/foo/', | ||
}) | ||
|
||
// when | ||
const term = defaultValue(property, graph.blankNode())?.term | ||
|
||
// then | ||
expect(term?.termType).to.eq('NamedNode') | ||
expect(term?.value).to.match(/^http:\/\/example.com\/foo\/.+$/) | ||
}) | ||
|
||
const resourceNodeKinds: [NodeKind, Term['termType']][] = [ | ||
[NodeKindEnum.BlankNode, 'BlankNode'], | ||
[NodeKindEnum.BlankNodeOrIRI, 'BlankNode'], | ||
[NodeKindEnum.BlankNodeOrLiteral, 'BlankNode'], | ||
[NodeKindEnum.IRI, 'NamedNode'], | ||
] | ||
|
||
resourceNodeKinds.forEach(([nodeKind, termType]) => { | ||
it(`creates a node of type ${termType} when nodeKind is ${nodeKind.value}`, () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
nodeKind, | ||
}) | ||
|
||
// when | ||
const pointer = defaultValue(property, graph.blankNode()) | ||
|
||
// then | ||
expect(pointer?.term?.termType).to.eq(termType) | ||
}) | ||
|
||
it(`adds sh:class as rdf:type to node kind ${nodeKind.value}`, () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
nodeKind, | ||
class: foaf.Agent, | ||
[sh1.iriPrefix.value]: 'http://example.com/foo/', | ||
}) | ||
|
||
// when | ||
const pointer = defaultValue(property, graph.blankNode()) | ||
|
||
// then | ||
expect(pointer?.out(rdf.type).term).to.deep.eq(foaf.Agent) | ||
}) | ||
|
||
it(`does not add rdf:type when node kind is ${nodeKind.value} but editor is ${dash.InstancesSelectEditor.value}`, () => { | ||
// given | ||
const graph = cf({ dataset: $rdf.dataset() }) | ||
const property = propertyShape(graph.blankNode(), { | ||
nodeKind, | ||
class: foaf.Agent, | ||
[dash.editor.value]: dash.InstancesSelectEditor, | ||
[sh1.iriPrefix.value]: 'http://example.com/foo/', | ||
}) | ||
|
||
// when | ||
const pointer = defaultValue(property, graph.blankNode()) | ||
|
||
// then | ||
expect(pointer?.out(rdf.type).term).to.be.undefined | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/core/models/resources/effects/forms/createFocusNodeState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.