arrowdbv2demo
Version:
Appcelerator ArrowDB SDK for NodeJS
222 lines (208 loc) • 8.29 kB
JavaScript
var fs = require('fs'),
assert = require('assert'),
testUtil = require('./testUtil')
config = require('../config');
var arrowDBEntryPoint = (process.env.ARROWDB_ENTRYPOINT ? process.env.ARROWDB_ENTRYPOINT : config.endpoint.test_api);
var arrowDBKey = process.env.ARROWDB_APPKEY;
if (!arrowDBKey) {
console.error('Please create an ArrowDB app and assign ARROWDB_APPKEY in environment vars.');
process.exit(1);
}
console.log('ArrowDB Entry Point: %s', arrowDBEntryPoint);
console.log('MD5 of ARROWDB_APPKEY: %s', testUtil.md5(arrowDBKey));
var ArrowDB = require('../index'),
arrowDBApp = new ArrowDB(arrowDBKey, {
apiEntryPoint: arrowDBEntryPoint
}),
arrowDBUsername = 'paul2',
arrowDBPassword = 'cocoafish2',
arrowDBObjectId = '',
arrowDBObjectId4 = '',
arrowDBObjectId5 = '',
authNew = 'Basic ' + Buffer.from(arrowDBUsername + ':' + arrowDBPassword).toString('base64');
describe('customObject Test', function () {
before(function (done) {
testUtil.generateUsername(function (username) {
arrowDBcustomObjectName = arrowDBUsername;
arrowDBcustomObjectNewName = arrowDBUsername;
done();
});
});
describe('.userLogin', function () {
it('Should create user successfully', function (done) {
arrowDBApp.userLogin({
req: {
headers: {
Authorization: authNew,
}
}
}, function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 200);
assert.equal(result.body.method_name, 'GET /v2/user/login');
arrowDBApp.sessionCookieString = result.cookieString;
done();
});
});
});
describe('.createObject', function () {
it('Should create custom Object 1 successfully', function (done) {
arrowDBApp.objectCreate({
type: "bike",
data: {
"name": "custom object is a bike",
"_acl": {
"rgs": ["#public"],
"wgs": ["#public"]
}
},
}, function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 201);
assert.equal(result.body.method_name, 'POST /v2/object/bike');
assert(result.body.response.data[0]._id);
arrowDBObjectId = result.body.response.data[0]._id
done();
});
});
it('Should batch create custom Object 2 & 3 successfully', function (done) {
this.timeout(10000);
arrowDBApp.objectCreateMany({
type : "bcobject",
data: `[{"name": "one","delete": "this","_acl": {"rgs":["#public"],"wgs":["#public"]}}, {"name": "two","delete": "this","_acl": {"rgs":["#public"],"wgs":["#public"]}}]`,
},function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 201);
assert.equal(result.body.message, 'nInserted=2');
assert.equal(result.body.method_name, 'POST /v2/object/create/bcobject');
done();
});
});
it('Should create custom Object 4 successfully', function (done) {
arrowDBApp.objectCreate({
type: "house",
data: {
"name": "custom object is a house",
"_acl": {
"rgs": ["#public"],
"wgs": ["#public"]
}
},
}, function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 201);
assert.equal(result.body.method_name, 'POST /v2/object/house');
assert(result.body.response.data[0]._id);
arrowDBObjectId4 = result.body.response.data[0]._id
done();
});
});
it('Should create custom Object 5 successfully', function (done) {
arrowDBApp.objectCreate({
type: "laptop",
data: {
"name": "custom object is a laptop",
"_acl": {
"rgs": ["#public"],
"wgs": ["#public"]
}
},
}, function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 201);
assert.equal(result.body.method_name, 'POST /v2/object/laptop');
assert(result.body.response.data[0]._id);
arrowDBObjectId5 = result.body.response.data[0]._id
done();
});
});
});
testUtil.delay(1000)
describe('.objectQuery', function () {
it('Should query Object correctly', function (done) {
this.timeout(5000);
arrowDBApp.objectQuery({
where: { "_id": [arrowDBObjectId,arrowDBObjectId4] },
}, function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 200);
done();
});
});
});
describe('.queryAllObjects', function () {
it('Should return all Objects', function (done) {
this.timeout(5000);
arrowDBApp.objectQuery({
limit: 100
}, function (err, result) {
assert.ifError(err);
assert(result);
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 200);
done();
});
});
});
describe('.showAndUpdateObject', function () {
it('Should show object successfully', function (done) {
this.timeout(5000);
arrowDBApp.get('/v2/object/' + arrowDBObjectId, function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 200);
assert.equal(result.body.response.data[0]._id, arrowDBObjectId);
assert.equal(result.body.response.data[0]._type, "bike");
done();
});
});
testUtil.delay(1000)
it('Should update object be successfull', function (done) {
this.timeout(5000);
arrowDBApp.objectUpdate({
oid: arrowDBObjectId,
data: {
"$set":{
"hello" : "bye"
}
}
}, function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 200);
arrowDBObjectId = result.body.response.data[0]._id
done();
});
});
});
describe('.deleteObject', function () {
it('Should delete single object and be successfull', function (done) {
arrowDBApp.objectDelete({
oid: arrowDBObjectId
}, function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 200);
done();
});
});
it('Should delete batch object and be successfull', function (done) {
this.timeout(10000);
arrowDBApp.delete('/v2/object'+'?where={"objects":["'+arrowDBObjectId5+'","'+ arrowDBObjectId5+'"]}',
{},function (err, result) {
assert.ifError(err);
assert(result.body);
assert.equal(result.body.status, 200);
assert.equal(result.body.method_name, 'DELETE /v2/object');
done();
});
});
});
});