1 |
|
2 | const assert = require("chai").assert
|
3 | const cloneDeep = require("lodash.clonedeep")
|
4 | const Promise = require("bluebird")
|
5 | const d = require("debug")("raptorjs:test:inventory")
|
6 | const util = require("../util")
|
7 |
|
8 | describe("Inventory", function () {
|
9 |
|
10 | it("should create a new object", function () {
|
11 | return util.createDevice()
|
12 | })
|
13 |
|
14 | it("should update an object streams and actions", function () {
|
15 | return util.getRaptor()
|
16 | .then((raptor) => {
|
17 | return util.createDevice(raptor)
|
18 | .then((device) => {
|
19 |
|
20 | d("Created device %s", device.id)
|
21 | d("%j", device.toJSON())
|
22 |
|
23 | device.actions = []
|
24 | device.streams["test1"] = {
|
25 | a: "number",
|
26 | b: "string"
|
27 | }
|
28 |
|
29 | return raptor.Inventory().update(device)
|
30 | })
|
31 | })
|
32 | })
|
33 |
|
34 | it("should delete an object", function () {
|
35 | return util.getRaptor()
|
36 | .then((raptor) => {
|
37 | return util.createDevice(raptor)
|
38 | .then((device) => {
|
39 | return raptor.Inventory().delete(device)
|
40 | .then(() => this.Inventory().read(device.id))
|
41 | .catch(() => Promise.resolve())
|
42 | })
|
43 | })
|
44 | })
|
45 |
|
46 | it("should find a device by name", function () {
|
47 | return util.getRaptor()
|
48 | .then((raptor) => {
|
49 |
|
50 | let json = require("../data/device")
|
51 | json.name = "test_find"
|
52 |
|
53 | return util.createDevice(raptor, json)
|
54 | .then((device) => {
|
55 | return raptor.Inventory().search({
|
56 | name: device.name
|
57 | })
|
58 | .then((pager) => {
|
59 |
|
60 | const list = pager.getContent()
|
61 | d("Found %s records", list.length)
|
62 | assert.isTrue(list.length > 0)
|
63 |
|
64 | return Promise.all(list)
|
65 | .each((d) => raptor.Inventory().delete(d))
|
66 | })
|
67 | })
|
68 | })
|
69 | })
|
70 |
|
71 | it("should find a device by properties", function () {
|
72 | return Promise.all([ util.createUserInstance(), util.getRaptor() ])
|
73 | .then((apis) => {
|
74 |
|
75 | const usr1 = apis[0]
|
76 | const adm1 = apis[1]
|
77 |
|
78 | const u1id = usr1.Auth().getUser().id
|
79 |
|
80 | let json = require("../data/device")
|
81 |
|
82 | json.userId = u1id
|
83 |
|
84 | let json1 = cloneDeep(json)
|
85 | let json2 = cloneDeep(json)
|
86 | let json3 = cloneDeep(json)
|
87 |
|
88 | json1.name = "test_find_properties_1"
|
89 | json1.userId = u1id
|
90 | json1.properties = {
|
91 | foo: "bar",
|
92 | active: false
|
93 | }
|
94 |
|
95 | json2.name = "test_find_properties_2"
|
96 | json2.userId = u1id
|
97 | json2.properties = {
|
98 | foo: "baz",
|
99 | active: false
|
100 | }
|
101 |
|
102 | json3.name = "test_find_properties_3"
|
103 | json3.userId = u1id
|
104 | json3.properties = {
|
105 | foo: "bar",
|
106 | active: true
|
107 | }
|
108 |
|
109 | return Promise.all([
|
110 | util.createDevice(adm1, json),
|
111 | util.createDevice(adm1, json1),
|
112 | util.createDevice(adm1, json2),
|
113 | util.createDevice(adm1, json3)
|
114 | ]).then(() => {
|
115 | return usr1.Inventory().search({
|
116 | properties: {
|
117 | active: false
|
118 | }
|
119 | }).then((pager) => {
|
120 |
|
121 | const list = pager.getContent()
|
122 |
|
123 | d("Found %s records", list.length)
|
124 | list.forEach((dev) => d("- %s [userId=%s]", dev.name, dev.userId))
|
125 |
|
126 | assert.isTrue(list.length > 0)
|
127 | assert.equal(2, list.length)
|
128 |
|
129 | return Promise.resolve()
|
130 | })
|
131 | })
|
132 | })
|
133 | })
|
134 |
|
135 | })
|