feat(module,share): cross-app resource sharing module
All checks were successful
arcad/edge/pipeline/head This commit looks good
All checks were successful
arcad/edge/pipeline/head This commit looks good
This commit is contained in:
11
pkg/module/share/testsuite/testdata/delete_attributes.json
vendored
Normal file
11
pkg/module/share/testsuite/testdata/delete_attributes.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
[
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app1.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_text", "type": "text", "value": "bar" },
|
||||
{ "name":"my_bool", "type": "bool", "value": true },
|
||||
{ "name":"my_number", "type": "number", "value": 5 }
|
||||
]
|
||||
}
|
||||
]
|
16
pkg/module/share/testsuite/testdata/delete_resource.json
vendored
Normal file
16
pkg/module/share/testsuite/testdata/delete_resource.json
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
[
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app1.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_text", "type": "text", "value": "bar" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app2.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_text", "type": "text", "value": "foo" }
|
||||
]
|
||||
}
|
||||
]
|
29
pkg/module/share/testsuite/testdata/find_resources_by_attribute_name.json
vendored
Normal file
29
pkg/module/share/testsuite/testdata/find_resources_by_attribute_name.json
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
[
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app1.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_text", "type": "text", "value": "bar" },
|
||||
{ "name":"my_number", "type": "number", "value": 5 },
|
||||
{ "name":"my_bool", "type": "bool", "value": true },
|
||||
{ "name":"my_path", "type": "path", "value": "/my/icon.png" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "res-2",
|
||||
"origin": "app1.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"other_text", "type": "text", "value": "foo" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app2.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_text", "type": "text", "value": "bar" },
|
||||
{ "name":"my_number", "type": "number", "value": 5 },
|
||||
{ "name":"my_bool", "type": "bool", "value": true },
|
||||
{ "name":"my_path", "type": "path", "value": "/my/icon.png" }
|
||||
]
|
||||
}
|
||||
]
|
28
pkg/module/share/testsuite/testdata/find_resources_by_attribute_type.json
vendored
Normal file
28
pkg/module/share/testsuite/testdata/find_resources_by_attribute_type.json
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
[
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app1.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_text", "type": "text", "value": "bar" },
|
||||
{ "name":"my_number", "type": "number", "value": 5 },
|
||||
{ "name":"my_bool", "type": "bool", "value": true }
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "res-2",
|
||||
"origin": "app1.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"other_text", "type": "text", "value": "foo" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app2.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_text", "type": "text", "value": "bar" },
|
||||
{ "name":"my_number", "type": "number", "value": 5 },
|
||||
{ "name":"my_bool", "type": "bool", "value": true },
|
||||
{ "name":"my_path", "type": "path", "value": "/my/icon.png" }
|
||||
]
|
||||
}
|
||||
]
|
23
pkg/module/share/testsuite/testdata/find_resources_by_attribute_type_and_name.json
vendored
Normal file
23
pkg/module/share/testsuite/testdata/find_resources_by_attribute_type_and_name.json
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
[
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app1.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_attr", "type": "text", "value": "bar" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "res-2",
|
||||
"origin": "app1.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_attr", "type": "bool", "value": true }
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app2.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_attr", "type": "number", "value": 5 }
|
||||
]
|
||||
}
|
||||
]
|
16
pkg/module/share/testsuite/testdata/get_resource.json
vendored
Normal file
16
pkg/module/share/testsuite/testdata/get_resource.json
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
[
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app1.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_text", "type": "text", "value": "bar" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "res-1",
|
||||
"origin": "app2.edge.app",
|
||||
"attributes": [
|
||||
{ "name":"my_text", "type": "text", "value": "foo" }
|
||||
]
|
||||
}
|
||||
]
|
82
pkg/module/share/testsuite/testdata/share.js
vendored
Normal file
82
pkg/module/share/testsuite/testdata/share.js
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
|
||||
function testModule() {
|
||||
var ctx = context.new();
|
||||
var resourceId = "my-first-res";
|
||||
var attributes = [
|
||||
{ name: "my_text", type: share.TYPE_TEXT, value: "my_text" },
|
||||
{ name: "my_number", type: share.TYPE_NUMBER, value: 5 },
|
||||
{ name: "my_path", type: share.TYPE_PATH, value: "/my/path" },
|
||||
{ name: "my_bool", type: share.TYPE_BOOL, value: true },
|
||||
]
|
||||
|
||||
// Create resource with attributes
|
||||
|
||||
var resource = share.upsertResource(
|
||||
ctx, resourceId,
|
||||
attributes[0],
|
||||
attributes[1],
|
||||
attributes[2],
|
||||
attributes[3]
|
||||
);
|
||||
|
||||
if (resource.id != resourceId) {
|
||||
throw new Error("resource.id: expected '"+resourceId+"', got '"+resource.id+"'")
|
||||
}
|
||||
|
||||
if (resource.origin != "test.app.edge") {
|
||||
throw new Error("resource.origin: expected 'test.app.edge', got '"+resource.origin+"'")
|
||||
}
|
||||
|
||||
if (resource.attributes.length != 4) {
|
||||
throw new Error("resource.attributes.length: expected '1', got '"+resource.attributes.length+"'")
|
||||
}
|
||||
|
||||
for(var attr, i = 0;( attr = attributes[i] ); i++) {
|
||||
var exists = resource.has(attr.name, attr.type);
|
||||
if (!exists) {
|
||||
throw new Error("resource.has('"+attr.name+"'): expected 'true', got '"+hasAttr+"'")
|
||||
}
|
||||
|
||||
var value = resource.get(attr.name, attr.type);
|
||||
if (value != attr.value) {
|
||||
throw new Error("value: expected '"+attr.value+"', got '"+value+"'")
|
||||
}
|
||||
}
|
||||
|
||||
// Test acces of unexistant attribute
|
||||
|
||||
var unexistantAttr = "unexistant_attr"
|
||||
|
||||
var exists = resource.has(unexistantAttr, share.TYPE_TEXT);
|
||||
if (exists) {
|
||||
throw new Error("attr '"+unexistantAttr+"' should not exist")
|
||||
}
|
||||
|
||||
var expected = "foo"
|
||||
var value = resource.get(unexistantAttr, share.TYPE_TEXT, expected);
|
||||
if (value != expected) {
|
||||
throw new Error("resource.get('"+attr.name+"', share.TYPE_TEXT, '"+expected+"'): expected '"+expected+"', got '"+value+"'")
|
||||
}
|
||||
|
||||
// Search resources
|
||||
|
||||
// With any attribute
|
||||
var results = share.findResources(ctx, share.ANY_NAME, share.ANY_TYPE);
|
||||
if (results.length != 1) {
|
||||
throw new Error("results.length: expected '1', got '"+results.length+"'")
|
||||
}
|
||||
|
||||
// With an unexistant attribute
|
||||
var results = share.findResources(ctx, unexistantAttr, share.ANY_TYPE);
|
||||
if (results.length != 0) {
|
||||
throw new Error("results.length: expected '0', got '"+results.length+"'")
|
||||
}
|
||||
|
||||
// With a wrong type
|
||||
var results = share.findResources(ctx, "my_text", share.TYPE_NUMBER);
|
||||
if (results.length != 0) {
|
||||
throw new Error("results.length: expected '0', got '"+results.length+"'")
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user