Skip to content

Commit b6d4a34

Browse files
authored
several improvements, including storing new results and testcases (#17)
* create new document base functionality * initial support for create new result and testcases * adding more resource related options * add possibility to track new results * fixed admin update api payload
1 parent d604691 commit b6d4a34

21 files changed

+603
-364
lines changed

dist/opentmi-client.js

Lines changed: 270 additions & 267 deletions
Large diffs are not rendered by default.

dist/opentmi-client.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/loans.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
const _ = require('lodash');
12
const {
2-
login, transport, print, logout
3+
login, transport, logout
34
} = require('./common');
45

56
const {Users, Loans} = require('../src');
@@ -11,8 +12,10 @@ login()
1112
.loadItems().loadResources().loadLoaner()
1213
.exec()
1314
.then((items) => {
14-
// _.each(items, item => console.log(item.toString()))
15-
console.log(JSON.stringify(items[0].toJson(), null, 2));
15+
_.each(items, item => console.log(item.toString()));
16+
if (items.length>0) {
17+
console.log(JSON.stringify(items[0].toJson(), null, 2));
18+
}
1619
});
1720
})
1821
.then(() => Users.WHOAMI(transport)

sample/resources.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ login()
1515
.then((resource) => {
1616
console.log(resource.toString());
1717
return resource
18+
.type('dut')
1819
.name(`${resource.name()}abc`)
1920
.location.site('oulu')
2021
.location.country('finland')
22+
.hw.sn('12')
23+
.hw.firmware.version('12')
24+
.hw.firmware.name('test')
2125
.save()
22-
.then(data => console.log(data.toString()));
26+
.then(data => console.log(data));
2327
});
2428
})
2529
.then(logout)

sample/results.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
const _ = require('lodash');
22

3-
const {
4-
login, transport, print, logout
5-
} = require('./common');
6-
3+
const {login, transport, logout} = require('./common');
74
const {Results} = require('../src');
85

6+
const results = new Results(transport);
7+
98
login()
10-
.then(() => {
11-
const results = new Results(transport);
12-
return results.find()
9+
.then(() =>
10+
results.find()
1311
.isHW()
1412
.limit(5)
1513
.skip(4)
1614
.isPass()
1715
.exec()
18-
.then((results) => {
19-
_.each(results, r => console.log(r.toString()));
16+
.then((docs) => {
17+
_.each(docs, r => console.log(r.toString()));
2018
// console.log(results[0].toJson());
21-
});
22-
})
19+
})
20+
)
21+
.then(() =>
22+
// start listening new result
23+
results.connect()
24+
.then(() => {
25+
results.on('new', (result) => {
26+
console.log(result.toString());
27+
});
28+
})
29+
)
30+
.then(() =>
31+
// upload new result
32+
results
33+
.create()
34+
.tcid('123').verdict('pass')
35+
.save()
36+
.then(doc => console.log(doc.toString()))
37+
.catch(error => console.error(error.message))
38+
)
2339
.then(logout)
2440
.catch(error => console.error(error.message));

sample/schema.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ const {
55
const {Schemas} = require('../src');
66

77
const schema = new Schemas(transport);
8+
//schema.updateSchemas = schema.updateSchemas.bind(schema);
9+
schema.collections = schema.collections.bind(schema);
810

911
login()
10-
// .then(print('updateSchemas').then(schema.updateSchemas.bind(schema)))
11-
.then(print('get collections')().then(schema.collections.bind(schema)))
12-
.then(print('get Result schema')().then(() => schema.schema('Result')))
13-
.then(print('get Result schema')().then(() => schema.schema('Result')))
12+
// .then(print('updateSchemas').then(schema.updateSchemas)
13+
.then(print('get collections')).then(schema.collections).then(cols => console.log(cols))
14+
.then(print('get Result schema')).then(() => schema.schema('Result').then(sch => console.log(sch)))
1415
.then(logout)
1516
.catch(error => console.error(error.message));

sample/testcases.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ const {
55
} = require('./common');
66

77
const {Testcases} = require('../src');
8+
const testcases = new Testcases(transport);
89

910
login()
1011
.then(() => {
11-
const testcases = new Testcases(transport);
1212
return testcases.find()
1313
.limit(5)
1414
.exec()
1515
.then((tcs) => {
1616
_.each(tcs, tc => console.log(tc.toString()));
1717
});
1818
})
19+
.then(() => {
20+
return testcases.create()
21+
.tcid('12345')
22+
.save()
23+
.then(doc => console.log(doc.toString()))
24+
.catch(error => console.log(error.message));
25+
})
1926
.then(logout)
2027
.catch(error => console.error(error.message));

src/admin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class Admin {
2626

2727
/**
2828
* Update opentmi server which are connected through Transport
29-
* @param {string} version - tag/commitId to be deployed
29+
* @param {string} revision - tag/commitId to be deployed
3030
* @return {Promise} resolves when upgrade is ready
3131
*/
32-
upgrade(version) {
32+
upgrade(revision) {
3333
invariant(this._transport.isLoggedIn, 'Transport should be connected');
3434
debug('request opentmi version');
3535
return this._transport
36-
.post('/api/v0/version', {version})
36+
.post('/api/v0/version', {revision})
3737
.then(response => response.data);
3838
}
3939
}

src/group.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Group extends Document {
1010
* @param {Object}groupJson - group as plain json
1111
*/
1212
constructor(transport, groupJson) {
13-
super(transport, `/api/v0/groups/${groupJson._id}`, groupJson);
13+
super(transport, '/api/v0/groups', groupJson);
1414
}
1515

1616
static fromId(transport, id) {

src/item.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Item extends Document {
99
* @param {object} resourceJson - plain json object
1010
*/
1111
constructor(transport, resourceJson) {
12-
super(transport, `/api/v0/items/${resourceJson.id}`, resourceJson);
12+
super(transport, '/api/v0/items', resourceJson);
1313
}
1414

1515
/**
@@ -50,9 +50,17 @@ class Item extends Document {
5050
* @return {Promise<buffer>} image buffer
5151
*/
5252
getImage() {
53-
return this._transport.get(`/api/v0/items/${this.id}/image`)
53+
return this._transport.get(`${this.path}/image`)
5454
.then(response => response.data);
5555
}
56+
57+
barcode(value) { return this.getOrSet('barcode', value); }
58+
59+
imageSrc(value) { return this.getOrSet('image_src', value); }
60+
61+
description(value) { return this.getOrSet('text_description', value); }
62+
63+
reference(value) { return this.getOrSet('external_reference', value); }
5664
}
5765

5866
module.exports = Item;

src/loan.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Loan extends Document {
5252
* @param {object} resourceJson - plain json object
5353
*/
5454
constructor(transport, resourceJson) {
55-
super(transport, `/api/v0/loans/${resourceJson.id}`, resourceJson);
55+
super(transport, '/api/v0/loans', resourceJson);
5656
}
5757

5858
/**

src/resource.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Resource extends Document {
99
* @param {object} resourceJson - plain json object
1010
*/
1111
constructor(transport, resourceJson) {
12-
super(transport, `/api/v0/resources/${resourceJson.id}`, resourceJson);
12+
super(transport, '/api/v0/resources', resourceJson);
1313
}
1414

1515
/**
@@ -27,26 +27,48 @@ class Resource extends Document {
2727
*/
2828
name(value) { return this.getOrSet('name', value); }
2929

30-
barcode(value) { return this.getOrSet('barcode', value); }
31-
32-
imageSrc(value) { return this.getOrSet('image_src', value); }
33-
34-
description(value) { return this.getOrSet('text_description', value); }
35-
36-
reference(value) { return this.getOrSet('external_reference', value); }
37-
30+
/**
31+
* Get resource type of set it
32+
* @param {String}value resource type
33+
* @return {Resource|String} resource type of Resource object
34+
*/
35+
type(value) { return this.getOrSet('type', value); }
3836

37+
/**
38+
* Manage hw informations
39+
* @example
40+
* doc
41+
* .hw.sn('123')
42+
* .hw.imei('12334')
43+
* .hw.firmware.name('aa')
44+
* .hw.firmware.version('1.0.0')
45+
* @return {Object} hardware object
46+
*/
47+
get hw() {
48+
const self = this;
49+
const hw = {
50+
get firmware() {
51+
return {
52+
name: function name(value) { return this.getOrSet('hw.firmware.name', value); }.bind(self),
53+
version: function version(value) {
54+
return this.getOrSet('hw.firmware.version', value);
55+
}.bind(self)
56+
};
57+
},
58+
sn: function sn(value) { return this.getOrSet('hw.sn', value); }.bind(this),
59+
imei: function imei(value) { return this.getOrSet('hw.imei', value); }.bind(this),
60+
id: function id(value) { return this.getOrSet('hw.id', value); }.bind(this)
61+
};
62+
return hw;
63+
}
3964
/**
4065
* Manage location information
4166
* @example
4267
* // set site and country
4368
* doc
4469
* .location.site('oulu')
4570
* .location.country('finland')
46-
* @return {{site: (function(this:Resource)), country: (function(this:Resource)),
47-
* city: (function(this:Resource)), address: (function(this:Resource)), postcode:
48-
* (function(this:Resource)), room: (function(this:Resource)), subRoom:
49-
* (function(this:Resource)), geo: (function(this:Resource))}} Location object
71+
* @return {Object} Location object
5072
*/
5173
get location() {
5274
const loc = {

src/resources.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,38 @@ const {QueryBase, Collection, notImplemented} = require('./utils');
1212
*/
1313
class ResourcesQuery extends QueryBase {
1414
/* Find resources by id
15-
* @param {string} type
16-
* @return {Query}
15+
* @param {string} id
16+
* @return {MongooseQueryClient} returns this
1717
*/
1818
id(id) {
1919
invariant(_.isString(id), 'id should be a string');
2020
return this.has({_id: id});
2121
}
2222

23+
/**
24+
* Find resources by hwid
25+
* @param {String} id hardware id as a string
26+
* @return {ResourcesQuery} returns this
27+
*/
28+
hwid(id) {
29+
invariant(_.isString(id), 'id should be a string');
30+
return this.has({'hw.id': id});
31+
}
32+
33+
/**
34+
* Find resources by serial number
35+
* @param {String}sn hardware serial number as a string
36+
* @return {ResourcesQuery} returns this
37+
*/
38+
hwsn(sn) {
39+
invariant(_.isString(sn), 'sn should be a string');
40+
return this.has({'hw.sn': sn});
41+
}
42+
2343
/**
2444
* Resource has parent
2545
* @param {String} id - optional parent resource id
26-
* @return {MongooseQueryClient} returns this
46+
* @return {ResourcesQuery} returns this
2747
*/
2848
hasParent(id = undefined) {
2949
if (_.isUndefined(id)) {
@@ -34,7 +54,7 @@ class ResourcesQuery extends QueryBase {
3454

3555
/**
3656
* Resource doesn't have parent
37-
* @return {MongooseQueryClient} returns this
57+
* @return {ResourcesQuery} returns this
3858
*/
3959
hasNoParent() {
4060
return this.has({parent: {$exists: false}});
@@ -43,16 +63,17 @@ class ResourcesQuery extends QueryBase {
4363
/**
4464
* Find resources with name
4565
* @param {string} name resource name
46-
* @return {Query} returns this
66+
* @return {ResourcesQuery} returns this
4767
*/
4868
name(name) {
4969
invariant(_.isString(name), 'type should be a string');
5070
return this.has({name});
5171
}
72+
5273
/**
5374
* Find resources by type
5475
* @param {string} type resource type
55-
* @return {Query} returns this
76+
* @return {ResourcesQuery} returns this
5677
*/
5778
type(type) {
5879
invariant(_.isString(type), 'type should be a string');
@@ -62,7 +83,7 @@ class ResourcesQuery extends QueryBase {
6283
/**
6384
* Find resources by status
6485
* @param {string} status resource status. One of 'active', 'maintenance', 'broken'
65-
* @return {Query} returns this
86+
* @return {ResourcesQuery} returns this
6687
*/
6788
status(status) {
6889
const STATUS = ['active', 'maintenance', 'broken'];
@@ -73,7 +94,7 @@ class ResourcesQuery extends QueryBase {
7394
/**
7495
* Find resources by usage type
7596
* @param {String} usageType resource usage type
76-
* @return {MongooseQueryClient} returns this
97+
* @return {ResourcesQuery} returns this
7798
*/
7899
usageType(usageType) {
79100
invariant(_.isString(usageType), 'usageType should be a string');
@@ -84,7 +105,7 @@ class ResourcesQuery extends QueryBase {
84105
* Find resources by a tag
85106
* @param {string} tag tag name
86107
* @param {bool} isTrue tag value, optional. default: true
87-
* @return {Query} returns this
108+
* @return {ResourcesQuery} returns this
88109
*/
89110
haveTag(tag, isTrue = true) {
90111
invariant(_.isBoolean(isTrue), 'isTrue should be a boolean');
@@ -96,7 +117,7 @@ class ResourcesQuery extends QueryBase {
96117
/**
97118
* Find resources by multiple tags
98119
* @param {array<String>} tags array of tag names
99-
* @return {Query} returns this
120+
* @return {ResourcesQuery} returns this
100121
*/
101122
haveTags(tags) {
102123
invariant(_.isArray(tags), 'tags should be an array');

0 commit comments

Comments
 (0)