gpii-universal
Version:
Cross platform, core components of the GPII personalization infrastructure.
1,295 lines (1,269 loc) • 43 kB
JavaScript
/*
Ontology Handler Tests
Copyright 2014 Raising the Floor - International
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
You may obtain a copy of the License at
https://github.com/gpii/universal/LICENSE.txt
*/
// TODO: Convert these to a set of browser tests
/* eslint strict: ["error", "function"] */
(function () {
"use strict";
var fluid = require("infusion"),
gpii = fluid.registerNamespace("gpii"),
jqUnit = fluid.registerNamespace("jqUnit"),
kettle = require("kettle");
kettle.loadTestingSupport();
require("ontologyHandler");
fluid.defaults("gpii.ontologyHandler.tests", {
gradeNames: ["fluid.test.testEnvironment"],
components: {
tester: {
type: "gpii.ontologyHandler.tests.testCaseHolder"
}
}
});
var getTransformSpecTests = {
specs: {
"A-B": {
"a1": "b1",
"a2": "b2"
},
"C-B": {
"c1": "b1",
"c2": "b2"
},
"B-D": {
"b1": "d1",
"b2": "d2"
}
},
tests: [
{
from: "B",
to: "D",
expect: {
"b1": "d1",
"b2": "d2"
}
}, {
from: "B",
to: "A",
expect: {
transform: [
{
type: "fluid.transforms.value",
inputPath: "a1",
outputPath: "b1"
}, {
type: "fluid.transforms.value",
inputPath: "a2",
outputPath: "b2"
}
]
}
}
]
};
var floydWarshallTestsLength = 19;
var floydWarshallTests = [
{
name: "Initial 4 vertexes, 3 edges",
edges: [["A","B"], ["B","A"], ["C","B"], ["B", "C"], ["B","D"], ["D", "B"]],
tests: [
{
desc: "Testing path from A to D through B.",
start: "A",
end: "D",
path: ["A", "B", "D"]
}, {
desc: "Testing path from D to A through B.",
start: "D",
end: "A",
path: ["D", "B", "A"]
}, {
desc: "Testing direct path from A to B.",
start: "A",
end: "B",
path: ["A", "B"]
}, {
desc: "Testing direct path from B to A.",
start: "B",
end: "A",
path: ["B", "A"]
}
]
}, {
name: "3 vertexes, 3 edges",
edges: [["A", "B"], ["B", "A"], ["A", "C"], ["C", "A"],
["B", "C"], ["C", "B"]],
tests: [
{
desc: "3v3e A to B",
start: "A",
end: "B",
path: ["A", "B"]
}, {
desc: "3v3e A to C",
start: "A",
end: "C",
path: ["A", "C"]
}
]
}, {
name: "3 vertexes, 2 edges",
edges: [["A", "B"], ["B", "A"], ["B", "C"], ["C", "B"]],
tests: [
{
desc: "3v2e A to B",
start: "A",
end: "B",
path: ["A", "B"]
}, {
desc: "3v2e A to C",
start: "A",
end: "C",
path: ["A", "B", "C"]
}
]
}, {
name: "Square",
edges: [["A", "B"], ["B", "A"], ["A", "C"], ["C", "A"],
["C", "D"], ["D", "C"], ["D", "B"], ["B", "D"]],
tests: [
{
desc: "Square A to B",
start: "A",
end: "B",
path: ["A", "B"]
}, {
desc: "Square A to D",
start: "A",
end: "D",
path: ["A", "B", "D"]
}, {
desc: "Square A to C",
start: "A",
end: "C",
path: ["A", "C"]
}
]
}, {
name: "Tetrahedron",
edges: [["A", "B"], ["B", "A"], ["A", "C"], ["C", "A"],
["C", "D"], ["D", "C"], ["D", "B"], ["B", "D"],
["A", "D"], ["D", "A"], ["B", "C"], ["C", "B"]],
tests: [
{
desc: "Tetrahedron A to B",
start: "A",
end: "B",
path: ["A", "B"]
}, {
desc: "Tetrahedron A to D",
start: "A",
end: "D",
path: ["A", "D"]
}, {
desc: "Tetrahedron A to C",
start: "A",
end: "C",
path: ["A", "C"]
}
]
}, {
name: "4 vertexes, 2 disconnected edges",
edges: [["A", "B"], ["B", "A"], ["A", "C"], ["C", "A"],
["C", "D"], ["D", "C"], ["A", "D"], ["D", "A"]],
tests: [
{
desc: "4vert2dis A to B",
start: "A",
end: "B",
path: ["A", "B"]
}, {
desc: "4vert2dis A to D",
start: "A",
end: "D",
path: ["A", "D"]
}, {
desc: "4vert2dis A to C",
start: "A",
end: "C",
path: ["A", "C"]
}, {
desc: "4vert2dis B to D",
start: "B",
end: "D",
path: ["B", "A", "D"]
}
]
}, {
name: "Disconnected test",
edges: [["A", "B"], ["B", "A"], ["C", "D"], ["D", "C"]],
tests: [
{
desc: "Non-existant path, should be empty.",
start: "A",
end: "D",
path: []
}
]
}
];
gpii.ontologyHandler.tests.floydWarshallAlgorithmWithReconstruction = function () {
fluid.each(floydWarshallTests, function (graph) {
var mapped_edges = [];
fluid.each(graph.edges, function (edge) {
mapped_edges.push({
source: edge[0],
target: edge[1]
});
});
var data = gpii.ontologyHandler.floydWarshall.fullAlgorithmWithReconstruction(mapped_edges);
fluid.each(graph.tests, function (test) {
jqUnit.assertDeepEq(test.desc, test.path,
gpii.ontologyHandler.floydWarshallPath(data, test.start, test.end));
});
});
};
gpii.ontologyHandler.tests.getTransformSpec = function () {
fluid.each(getTransformSpecTests.tests, function (test) {
var res = gpii.ontologyHandler.getTransformSpec(getTransformSpecTests.specs, test.from, test.to);
jqUnit.assertDeepEq("Testing getTransformSpec from " + test.from + " to " + test.to, test.expect, res);
});
};
var prefsToOntologyTests = [
{
description: "Basic transform, no inversion",
prefs: {
name: "My simple prefs #1",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
}
},
from: "capitals",
to: "minuscule",
expect: {
name: "My simple prefs #1",
contexts: {
"gpii-default": {
name: "default",
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
}
}
}, {
description: "Basic single transform with inversion",
prefs: {
name: "My simple prefs #2",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
}
},
from: "minuscule",
to: "capitals",
expect: {
name: "My simple prefs #2",
contexts: {
"gpii-default": {
name: "default",
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
}
}
}, {
description: "no matching ontology transform specs",
prefs: {
name: "My simple prefs #3",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
cats: "meow",
dogs: "wruf"
}
}
}
},
from: "bogus",
to: "minuscule",
expect: {}
}, {
description: "Same ontology requested",
prefs: {
name: "My simple prefs #4",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
}
},
from: "minuscule",
to: "minuscule",
expect: {
name: "My simple prefs #4",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
}
}
}, {
description: "testing the metadata translations",
prefs: {
name: "My simple prefs #5",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "a", "b" ]
}],
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
},
metadata: [{
type: "outer",
scope: [ "c" ]
}]
},
from: "minuscule",
to: "capitals",
expect: {
name: "My simple prefs #5",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "A", "B" ]
}],
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
},
metadata: [{
type: "outer",
scope: [ "C" ]
}]
}
}, {
description: "testing the metadata translations",
prefs: {
name: "My simple prefs #6",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "a", "b" ]
}],
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
},
metadata: [{
type: "outer",
scope: [ "c" ]
}]
},
from: "minuscule",
to: "capitals",
expect: {
name: "My simple prefs #6",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "A", "B" ]
}],
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
},
metadata: [{
type: "outer",
scope: [ "C" ]
}]
}
}, {
description: "testing the metadata wildcard explosions",
prefs: {
name: "My simple prefs #7",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "x.*" ]
}],
preferences: {
x: {
a: "I was x.a"
}
}
}
},
metadata: [{
type: "outer",
scope: [ "y.*" ]
}]
},
from: "deep",
to: "capitals",
expect: {
name: "My simple prefs #7",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "A", "B" ]
}],
preferences: {
A: "I was x.a"
}
}
},
metadata: [{
type: "outer",
scope: [ "C" ]
}]
}
}, {
description: "testing multiple contexts and non-transformable settings",
prefs: {
name: "My simple prefs #8",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "x.*" ]
}],
preferences: {
x: {
a: "I was x.a"
}
}
},
"other": {
name: "the other",
metadata: [],
preferences: {
y: {
q: "I won't survive"
}
}
}
},
metadata: [{
type: "outer",
scope: [ "*" ]
}]
},
from: "deep",
to: "capitals",
expect: {
name: "My simple prefs #8",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "A", "B" ]
}],
preferences: {
A: "I was x.a"
}
},
"other": {
name: "the other",
preferences: {}
}
},
metadata: [{
type: "outer",
scope: [ "*" ]
}]
}
},
{
description: "simple test lookup across 3 ontologies",
prefs: {
name: "My simple prefs #9",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
}
},
from: "capitals",
to: "names",
expect: {
name: "My simple prefs #9",
contexts: {
"gpii-default": {
name: "default",
preferences: {
adam: "I am the a-man",
bassman: "I am the b-man"
}
}
}
}
},
{
description: "simple test lookup across 3 ontologies, reversed",
prefs: {
name: "My simple prefs #10",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
adam: "I am the a-man",
bassman: "I am the b-man"
}
}
}
},
from: "names",
to: "capitals",
expect: {
name: "My simple prefs #10",
contexts: {
"gpii-default": {
name: "default",
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
}
}
},
{
description: "3-ontologies: testing multiple contexts and non-transformable settings",
prefs: {
name: "My simple prefs #11",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "x.*" ]
}],
preferences: {
x: {
a: "I was x.a"
}
}
},
"other": {
name: "the other",
metadata: [],
preferences: {
y: {
q: "I won't survive"
}
}
}
},
metadata: [{
type: "outer",
scope: [ "*" ]
}]
},
from: "deep",
to: "minuscule",
expect: {
name: "My simple prefs #11",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "a", "b" ]
}],
preferences: {
a: "I was x.a"
}
},
"other": {
name: "the other",
preferences: {}
}
},
metadata: [{
type: "outer",
scope: [ "*" ]
}]
}
},
{
description: "3-ontologies: Basic single transform with inversion",
prefs: {
name: "My simple prefs #12",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
}
},
from: "minuscule",
to: "names",
expect: {
name: "My simple prefs #12",
contexts: {
"gpii-default": {
name: "default",
preferences: {
adam: "I am the a-man",
bassman: "I am the b-man"
}
}
}
}
},
{
description: "3-ontologies: Basic deep transform with inversion",
prefs: {
name: "My simple prefs #13",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
}
},
from: "minuscule",
to: "deep",
expect: {
name: "My simple prefs #13",
contexts: {
"gpii-default": {
name: "default",
preferences: {
x: {
a: "I am the a-man",
b: "I am the b-man"
}
}
}
}
}
}
];
gpii.ontologyHandler.tests.prefsToOntology = function (ontologyHandler) {
fluid.each(prefsToOntologyTests, function (test) {
var res = ontologyHandler.prefsToOntology(test.prefs, test.from, test.to);
jqUnit.assertDeepEq("gpii.ontologyHandler.prefsToOntology: " + test.description,
test.expect, res);
});
};
var rawPrefsToOntologyTests = [
{
description: "Basic transform, no inversion",
rawPrefs: {
capitals: {
name: "From caps #1",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
}
}
},
to: "minuscule",
expect: {
name: "From caps #1",
contexts: {
"gpii-default": {
name: "default",
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
}
}
}, {
description: "Basic single transform with inversion",
rawPrefs: {
minuscule: {
name: "From mini #1",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
a: "I am the a-man",
b: "I am the b-man"
}
}
}
}
},
to: "capitals",
expect: {
name: "From mini #1",
contexts: {
"gpii-default": {
name: "default",
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
}
}
}, {
description: "Two transform, normal and with inversion",
rawPrefs: {
capitals: {
name: "From caps #1",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
A: "I am the a-man"
}
}
}
},
names: {
name: "From names #1",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
bassman: "Bum da da dum BASS"
}
}
}
}
},
to: "minuscule",
expect: {
name: "From caps #1",
contexts: {
"gpii-default": {
name: "default",
preferences: {
a: "I am the a-man",
b: "Bum da da dum BASS"
}
}
}
}
}, {
description: "Some of the preferences already exist in the requested ontology",
rawPrefs: {
minuscule: {
name: "From mini",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
a: "I am the a-man"
}
}
}
},
names: {
name: "From names",
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
bassman: "Bum da da dum BASS"
}
}
}
}
},
to: "minuscule",
expect: {
name: "From mini",
contexts: {
"gpii-default": {
name: "default",
preferences: {
a: "I am the a-man",
b: "Bum da da dum BASS"
}
}
}
}
}, {
description: "no matching ontology transform specs",
rawPrefs: {
bogus: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
cats: "meow",
dogs: "wruf"
}
}
}
}
},
to: "minuscule",
expect: {
name: undefined,
contexts: {}
}
}, {
description: "transforms from two ontologies (normal and with inversion) to third - " +
"with metadata",
rawPrefs: {
capitals: {
name: "From caps",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "A" ]
}],
preferences: {
A: "I am the a-man"
}
}
}
},
names: {
name: "From names",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test2",
scope: [ "adam" ]
}],
preferences: {
bassman: "Bum da da dum BASS"
}
}
}
}
},
to: "minuscule",
expect: {
name: "From caps",
contexts: {
"gpii-default": {
name: "default",
metadata: [{
type: "test",
scope: [ "a" ]
}, {
type: "test2",
scope: [ "a" ]
}],
preferences: {
a: "I am the a-man",
b: "Bum da da dum BASS"
}
}
}
}
}
];
gpii.ontologyHandler.tests.rawPrefsToOntology = function (ontologyHandler) {
fluid.each(rawPrefsToOntologyTests, function (test) {
var res = ontologyHandler.rawPrefsToOntology(test.rawPrefs, test.to);
jqUnit.assertDeepEq("gpii.ontologyHandler.rawPrefsToOntology: " + test.description,
test.expect, res);
});
};
var addPrefsToRawPrefsTests = [
{
description: "Add prefs to an ontology matching the one in the raw preference set",
prefs: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
}
},
prefsView: "capitals",
rawPrefs: {
capitals: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
A: "Who am I?"
}
}
}
}
},
expect: {
capitals: {
contexts: {
"gpii-default": {
name: "default",
preferences: {
A: "I am the a-man",
B: "I am the b-man"
}
}
}
}
}
}, {
description: "Overwriting existing preference which is stored in different view",
prefs: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
A: "I am the a-man"
}
}
}
},
prefsView: "capitals",
rawPrefs: {
minuscule: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
a: "Who am I?"
}
}
}
}
},
expect: {
minuscule: {
contexts: {
"gpii-default": {
name: "default",
preferences: {
}
}
}
},
capitals: {
contexts: {
"gpii-default": {
name: "default",
preferences: {
A: "I am the a-man"
}
}
}
}
}
}, {
description: "Modifying part of another view and overwriting the same (existing) view",
prefs: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
A: "I am the a-man"
}
}
}
},
prefsView: "capitals",
rawPrefs: { // this is actually theoretically an illegal raw prefs set
minuscule: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
a: "Who am I?",
b: "I'm still here"
}
}
}
},
capitals: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
B: "I am a bee"
}
}
}
}
},
expect: {
capitals: {
contexts: {
"gpii-default": {
name: "default",
preferences: {
A: "I am the a-man"
}
}
}
},
minuscule: {
contexts: {
"gpii-default": {
name: "default",
preferences: {
b: "I'm still here"
}
}
}
}
}
}, {
description: "All other ontologies unaffected due to lack of transformation file",
prefs: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
A: "I am the a-man"
}
}
}
},
prefsView: "capitals",
rawPrefs: {
bogus: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
cat: "I'm very decent"
}
}
}
},
capitals: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
B: "I am a bee"
}
}
}
}
},
expect: {
capitals: {
contexts: {
"gpii-default": {
name: "default",
preferences: {
A: "I am the a-man"
}
}
}
},
bogus: {
contexts: {
"gpii-default": {
name: "default",
preferences: {
cat: "I'm very decent"
}
}
}
}
}
}, {
description: "All other ontologies unaffected due to lack of specific setting transformation",
prefs: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
C: "you later"
}
}
}
},
prefsView: "capitals",
rawPrefs: {
minuscule: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
d: "secret setting"
}
}
}
},
capitals: {
contexts: {
"gpii-default": {
name: "default",
metadata: [],
preferences: {
B: "I am a bee"
}
}
}
}
},
expect: {
capitals: {
contexts: {
"gpii-default": {
name: "default",
preferences: {
C: "you later"
}
}
}
},
minuscule: {
contexts: {
"gpii-default": {
name: "default",
preferences: {
d: "secret setting"
}
}
}
}
}
}
];
gpii.ontologyHandler.tests.addPrefsToRawPrefs = function (ontologyHandler) {
fluid.each(addPrefsToRawPrefsTests, function (test) {
var res = ontologyHandler.addPrefsToRawPrefs(test.prefs, test.prefsView, test.rawPrefs);
jqUnit.assertDeepEq("gpii.ontologyHandler.addPrefsToRawPrefs: " + test.description,
test.expect, res);
});
};
fluid.defaults("gpii.ontologyHandler.tests.testCaseHolder", {
gradeNames: ["fluid.test.testCaseHolder"],
components: {
"ontologyHandler": {
type: "gpii.ontologyHandler",
options: {
ontologyTransformationListFolder: __dirname + "/data/ontologies/mappings/"
}
}
},
modules: [{
name: "OntologyHandlerTests",
tests: [{
expect: floydWarshallTestsLength,
name: "gpii.ontologyHandler.tests.floydWarshallAlgorithmWithReconstruction tests",
func: "gpii.ontologyHandler.tests.floydWarshallAlgorithmWithReconstruction"
}, {
expect: 2,
name: "gpii.ontologyHandler.tests.getTransformSpec tests",
func: "gpii.ontologyHandler.tests.getTransformSpec"
}, {
expect: prefsToOntologyTests.length,
name: "gpii.ontologyHandler.prefsToOntology tests",
func: "gpii.ontologyHandler.tests.prefsToOntology",
args: [ "{ontologyHandler}" ]
}, {
expect: rawPrefsToOntologyTests.length,
name: "gpii.ontologyHandler.rawPrefsToOntology tests",
func: "gpii.ontologyHandler.tests.rawPrefsToOntology",
args: [ "{ontologyHandler}" ]
}, {
expect: addPrefsToRawPrefsTests.length,
name: "gpii.ontologyHandler.addPrefsToRawPrefs tests",
func: "gpii.ontologyHandler.tests.addPrefsToRawPrefs",
args: [ "{ontologyHandler}" ]
}]
}]
});
module.exports = kettle.test.bootstrap("gpii.ontologyHandler.tests");
})();