Skip to content

Commit 16bb6ec

Browse files
committed
document metadata unit tests
1 parent 5e582ca commit 16bb6ec

File tree

3 files changed

+136
-14
lines changed

3 files changed

+136
-14
lines changed

lib/mlutil.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ function asArray() {
2525
var arg = arguments[0];
2626
if (valcheck.isArray(arg)) {
2727
return arg;
28-
} else {
29-
return [arg];
3028
}
29+
return [arg];
3130
default:
3231
var args = new Array(argLen);
3332
for(var i=0; i < argLen; ++i) {

lib/query-builder.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ function qname() {
998998
return {qname: nsName.apply(null, arguments)};
999999
}
10001000
/** @ignore */
1001-
function nsName(args) {
1001+
function nsName() {
10021002
var args = mlutil.asArray.apply(null, arguments);
10031003
switch(args.length) {
10041004
case 0:
@@ -1522,7 +1522,7 @@ function byExample() {
15221522
return {$query: []};
15231523
case 1:
15241524
var query = args[0];
1525-
if (valcheck.isNullOrUndefined(query['$query'])) {
1525+
if (valcheck.isNullOrUndefined(query.$query)) {
15261526
return {$query: query};
15271527
}
15281528
return query;
@@ -1612,7 +1612,7 @@ function sort() {
16121612

16131613
var firstIndex = asIndex(args[0]);
16141614
// null is a legitimate value of score
1615-
var isScore = (!valcheck.isUndefined(firstIndex['score']));
1615+
var isScore = (!valcheck.isUndefined(firstIndex.score));
16161616

16171617
var sorter = {};
16181618
if (isScore) {
@@ -1636,12 +1636,12 @@ function sort() {
16361636
break;
16371637
}
16381638
} else if (isScore) {
1639-
} else if (!valcheck.isNullOrUndefined(arg['datatype'])) {
1639+
} else if (!valcheck.isNullOrUndefined(arg.datatype)) {
16401640
sorter.type = arg.datatype;
1641-
if (!valcheck.isNullOrUndefined(arg['collation'])) {
1641+
if (!valcheck.isNullOrUndefined(arg.collation)) {
16421642
sorter.collation = arg.collation;
16431643
}
1644-
} else if (!valcheck.isNullOrUndefined(arg['collation'])) {
1644+
} else if (!valcheck.isNullOrUndefined(arg.collation)) {
16451645
sorter.type = 'xs:string';
16461646
sorter.collation = arg.collation;
16471647
}
@@ -1848,8 +1848,8 @@ function facet() {
18481848
constraint.type = ((datatype !== undefined) ? datatype : 'xs:string');
18491849
var constraintKeys = Object.keys(constraintIndex);
18501850
var constraintKeyLen = constraintKeys.length;
1851-
for (var i=0; i < constraintKeyLen; i++) {
1852-
var key = constraintKeys[i];
1851+
for (var j=0; j < constraintKeyLen; j++) {
1852+
var key = constraintKeys[j];
18531853
constraint[key] = constraintIndex[key];
18541854
}
18551855
if (buckets !== null) {

test-basic/documents-core.js

Lines changed: 127 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ describe('document content', function(){
256256
done();
257257
}));
258258
});
259-
it('should write a binary descriptor with a ReadableStream', function(done){
259+
it('should write a binary descriptor with a Readable stream', function(done){
260260
this.timeout(3000);
261261
var uri = '/test/write/stream1.png';
262262
var readableBinary = new ValueStream(binaryValue);
@@ -440,6 +440,22 @@ describe('document metadata', function(){
440440
},
441441
quality: 1,
442442
content: {key1: 'value 1'}
443+
},
444+
{contentType: 'application/json',
445+
collections: ['collectionDefault/0', 'collectionDefault/1'],
446+
permissions: [
447+
{'role-name':'app-user', capabilities:['read']},
448+
{'role-name':'app-builder', capabilities:['read', 'update']}
449+
],
450+
properties: {
451+
propertyDefault1: 'default property value 1',
452+
propertyDefault2: 'default property value 2'
453+
},
454+
quality: 2
455+
},
456+
{uri: '/test/write/metaContent2.json',
457+
contentType: 'application/json',
458+
content: {key2: 'value 2'}
443459
}).
444460
result(function(response){done();}, done);
445461
});
@@ -480,6 +496,43 @@ describe('document metadata', function(){
480496
done();
481497
}, done);
482498
});
499+
it('should read back default metadata and content', function(done){
500+
db.read({uris:'/test/write/metaContent2.json', categories:['metadata', 'content']}).
501+
result(function(documents) {
502+
valcheck.isUndefined(documents).should.equal(false);
503+
documents.length.should.equal(1);
504+
var document = documents[0];
505+
document.collections.length.should.equal(2);
506+
for (var i=0; i < 2; i++) {
507+
document.collections[i].should.equal('collectionDefault/'+i);
508+
}
509+
var permissionsFound = 0;
510+
document.permissions.forEach(function(permission){
511+
switch (permission['role-name']) {
512+
case 'app-user':
513+
permissionsFound++;
514+
permission.capabilities.length.should.equal(1);
515+
permission.capabilities[0].should.equal('read');
516+
break;
517+
case 'app-builder':
518+
permissionsFound++;
519+
permission.capabilities.length.should.equal(2);
520+
permission.capabilities.should.containEql('read');
521+
permission.capabilities.should.containEql('update');
522+
break;
523+
}
524+
});
525+
permissionsFound.should.equal(2);
526+
document.should.have.property('properties');
527+
document.properties.should.have.property('propertyDefault1');
528+
document.properties.propertyDefault1.should.equal('default property value 1');
529+
document.properties.should.have.property('propertyDefault2');
530+
document.properties.propertyDefault2.should.equal('default property value 2');
531+
document.quality.should.equal(2);
532+
document.content.key2.should.equal('value 2');
533+
done();
534+
}, done);
535+
});
483536
});
484537
describe('without content', function(){
485538
before(function(done){
@@ -498,19 +551,19 @@ describe('document metadata', function(){
498551
}).
499552
result(function(response){done();}, done);
500553
});
501-
it('should read back the metadata', function(done){
554+
it('should read back the all of the metadata', function(done){
502555
db.read({uris:'/test/write/metaContent1.json', categories:'metadata'}).
503556
result(function(documents) {
504557
valcheck.isUndefined(documents).should.equal(false);
505558
documents.length.should.equal(1);
506559
var document = documents[0];
507560
valcheck.isUndefined(document).should.equal(false);
508-
valcheck.isUndefined(document.collections).should.equal(false);
561+
document.should.have.property('collections');
509562
document.collections.length.should.equal(2);
510563
for (var i=0; i < 2; i++) {
511564
document.collections[i].should.equal('collection2/'+i);
512565
}
513-
valcheck.isUndefined(document.permissions).should.equal(false);
566+
document.should.have.property('permissions');
514567
var permissionsFound = 0;
515568
document.permissions.forEach(function(permission){
516569
switch (permission['role-name']) {
@@ -539,6 +592,76 @@ describe('document metadata', function(){
539592
done();
540593
}, done);
541594
});
595+
it('should read back collections metadata', function(done){
596+
db.read({uris:'/test/write/metaContent1.json', categories:'collections'}).
597+
result(function(documents) {
598+
valcheck.isUndefined(documents).should.equal(false);
599+
documents.length.should.equal(1);
600+
var document = documents[0];
601+
document.should.have.property('collections');
602+
document.collections.length.should.equal(2);
603+
for (var i=0; i < 2; i++) {
604+
document.collections[i].should.equal('collection2/'+i);
605+
}
606+
document.should.not.have.property('content');
607+
done();
608+
}, done);
609+
});
610+
it('should read back permissions metadata', function(done){
611+
db.read({uris:'/test/write/metaContent1.json', categories:'permissions'}).
612+
result(function(documents) {
613+
valcheck.isUndefined(documents).should.equal(false);
614+
documents.length.should.equal(1);
615+
var document = documents[0];
616+
document.should.have.property('permissions');
617+
var permissionsFound = 0;
618+
document.permissions.forEach(function(permission){
619+
switch (permission['role-name']) {
620+
case 'app-user':
621+
permissionsFound++;
622+
permission.capabilities.length.should.equal(2);
623+
permission.capabilities.should.containEql('read');
624+
permission.capabilities.should.containEql('update');
625+
break;
626+
case 'app-builder':
627+
permissionsFound++;
628+
permission.capabilities.length.should.equal(1);
629+
permission.capabilities[0].should.equal('execute');
630+
break;
631+
}
632+
});
633+
permissionsFound.should.equal(2);
634+
document.should.not.have.property('content');
635+
done();
636+
}, done);
637+
});
638+
it('should read back the properties metadata', function(done){
639+
db.read({uris:'/test/write/metaContent1.json', categories:'properties'}).
640+
result(function(documents) {
641+
valcheck.isUndefined(documents).should.equal(false);
642+
documents.length.should.equal(1);
643+
var document = documents[0];
644+
document.should.have.property('properties');
645+
document.properties.should.have.property('property1');
646+
document.properties.property1.should.equal('property value 1');
647+
document.properties.should.have.property('property2');
648+
document.properties.property2.should.equal('property value 2');
649+
document.should.not.have.property('content');
650+
done();
651+
}, done);
652+
});
653+
it('should read back the quality metadata', function(done){
654+
db.read({uris:'/test/write/metaContent1.json', categories:'quality'}).
655+
result(function(documents) {
656+
valcheck.isUndefined(documents).should.equal(false);
657+
documents.length.should.equal(1);
658+
var document = documents[0];
659+
document.should.have.property('quality');
660+
document.quality.should.equal(2);
661+
document.should.not.have.property('content');
662+
done();
663+
}, done);
664+
});
542665
});
543666
});
544667
});

0 commit comments

Comments
 (0)