not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
9.4 KiB

  1. // Unit testing on datastore
  2. describe('Datastore', function() {
  3. before(function() {
  4. initSugarizer();
  5. });
  6. var objectIds = [];
  7. describe('#create()', function() {
  8. it('should create one entry', function(done) {
  9. var results = datastore.create({name: "entry", activity: "test"}, function(err, oid) {
  10. objectIds.push(oid);
  11. chai.assert.equal(null, err);
  12. chai.assert.notEqual(null, oid);
  13. chai.assert.equal(36, oid.length);
  14. }, {value: "100"});
  15. var results = datastore.find();
  16. chai.assert.equal(1, results.length);
  17. chai.assert.equal("entry", results[0].metadata.name);
  18. chai.assert.equal("test", results[0].metadata.activity);
  19. chai.assert.isNotNull(results[0].text);
  20. var results = datastore.find("test");
  21. chai.assert.equal(1, results.length);
  22. chai.assert.equal("entry", results[0].metadata.name);
  23. chai.assert.equal("test", results[0].metadata.activity);
  24. chai.assert.isNotNull(results[0].text);
  25. var entry = new datastore.DatastoreObject(objectIds[0]);
  26. entry.loadAsText(function(err, metadata, text) {
  27. chai.assert.equal("100", text.value);
  28. done();
  29. });
  30. });
  31. it('should set text to null if undefined', function() {
  32. var results = datastore.create({name: "entry2", activity: "test2"}, function(err, oid) {
  33. objectIds.push(oid);
  34. chai.assert.equal(null, err);
  35. chai.assert.notEqual(null, oid);
  36. chai.assert.equal(36, oid.length);
  37. });
  38. var results = datastore.find("test2");
  39. chai.assert.equal(1, results.length);
  40. chai.assert.equal("entry2", results[0].metadata.name);
  41. chai.assert.equal("test2", results[0].metadata.activity);
  42. chai.assert.isNull(results[0].text);
  43. });
  44. });
  45. describe('#find()', function() {
  46. it('should return all object created', function() {
  47. var results = datastore.find();
  48. chai.assert.equal(objectIds.length, results.length);
  49. });
  50. it('should allow find specific activity', function() {
  51. var results = datastore.find("test");
  52. chai.assert.equal(1, results.length);
  53. });
  54. it('should return empty on inexisting activity', function() {
  55. var results = datastore.find("xxxx");
  56. chai.assert.equal(0, results.length);
  57. });
  58. });
  59. describe('#getMetadata()', function() {
  60. it('should get entry', function(done) {
  61. var entry = new datastore.DatastoreObject(objectIds[0]);
  62. entry.getMetadata(function(err, metadata) {
  63. chai.assert.equal("entry", metadata.name);
  64. chai.assert.equal("test", metadata.activity);
  65. done();
  66. });
  67. });
  68. it('should get empty element on inexisting entry', function(done) {
  69. var entry = new datastore.DatastoreObject("ffffffff-ffff-ffff-ffff-ffffffffffff");
  70. entry.getMetadata(function() {
  71. chai.assert.fail();
  72. });
  73. chai.assert.equal("ffffffff-ffff-ffff-ffff-ffffffffffff", entry.objectId);
  74. chai.assert.deepEqual({}, entry.newMetadata);
  75. done();
  76. });
  77. it('should get empty element on undefined id', function(done) {
  78. var entry = new datastore.DatastoreObject();
  79. entry.getMetadata(function() {
  80. chai.assert.fail();
  81. });
  82. chai.assert.equal(undefined, entry.objectId);
  83. chai.assert.deepEqual({}, entry.newMetadata);
  84. chai.assert.isNull(entry.newDataAsText);
  85. done();
  86. });
  87. });
  88. describe('#save()', function() {
  89. it('should update metadata on existing object', function(done) {
  90. var entry = new datastore.DatastoreObject(objectIds[0]);
  91. entry.getMetadata(function(err, metadata) {
  92. chai.assert.equal(objectIds[0], entry.objectId);
  93. chai.assert.equal("entry", metadata.name);
  94. chai.assert.equal("test", metadata.activity);
  95. var timeBefore = new Date().getTime();
  96. entry.save(function(err, metasaved) {
  97. var timeAfter = new Date().getTime();
  98. chai.assert.equal("entry", metasaved.name);
  99. chai.assert.equal("test", metasaved.activity);
  100. chai.assert.ok(metasaved.timestamp >= timeBefore);
  101. chai.assert.ok(metasaved.timestamp <= timeAfter);
  102. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, metasaved.buddy_color);
  103. chai.assert.equal("Mocha", metasaved.buddy_name);
  104. done();
  105. });
  106. });
  107. });
  108. it('should update text on existing object', function(done) {
  109. var entry = new datastore.DatastoreObject(objectIds[0]);
  110. entry.loadAsText(function(err, metadata, text) {
  111. chai.assert.equal(objectIds[0], entry.objectId);
  112. chai.assert.equal("entry", metadata.name);
  113. chai.assert.equal("test", metadata.activity);
  114. chai.assert.equal("100", text.value);
  115. entry.setDataAsText({value: "200"});
  116. var timeBefore = new Date().getTime();
  117. entry.save(function(err, metasaved, textsaved) {
  118. var timeAfter = new Date().getTime();
  119. chai.assert.equal("entry", metasaved.name);
  120. chai.assert.equal("test", metasaved.activity);
  121. chai.assert.ok(metasaved.timestamp >= timeBefore);
  122. chai.assert.ok(metasaved.timestamp <= timeAfter);
  123. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, metasaved.buddy_color);
  124. chai.assert.equal("Mocha", metasaved.buddy_name);
  125. chai.assert.equal("200", textsaved.value);
  126. done();
  127. });
  128. });
  129. });
  130. it('should create metadata for new object', function(done) {
  131. var entry = new datastore.DatastoreObject();
  132. var timeBefore = new Date().getTime();
  133. entry.save(function(err, metadata, text) {
  134. var timeAfter = new Date().getTime();
  135. chai.assert.isNotNull(entry.objectId);
  136. chai.assert.equal(36, entry.objectId.length);
  137. chai.assert.ok(metadata.timestamp >= timeBefore);
  138. chai.assert.ok(metadata.timestamp <= timeAfter);
  139. chai.assert.ok(metadata.creation_time >= timeBefore);
  140. chai.assert.ok(metadata.creation_time <= timeAfter);
  141. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, metadata.buddy_color);
  142. chai.assert.equal("Mocha", metadata.buddy_name);
  143. chai.assert.equal("0", metadata.file_size);
  144. chai.assert.isNull(text);
  145. objectIds.push(entry.objectId);
  146. done();
  147. });
  148. });
  149. });
  150. describe('#loadAsText()', function() {
  151. it('should get text entry', function(done) {
  152. var entry = new datastore.DatastoreObject(objectIds[0]);
  153. entry.loadAsText(function(err, metadata, text) {
  154. chai.assert.equal("200", text.value);
  155. done();
  156. });
  157. });
  158. it('should get null on free text entry', function(done) {
  159. var entry = new datastore.DatastoreObject(objectIds[1]);
  160. entry.loadAsText(function(err, metadata, text) {
  161. chai.assert.equal(objectIds[1], entry.objectId);
  162. chai.assert.isNull(text);
  163. done();
  164. });
  165. });
  166. it('should do nothing on undefined id', function(done) {
  167. var entry = new datastore.DatastoreObject();
  168. entry.loadAsText(function() {
  169. chai.assert.fail();
  170. });
  171. chai.assert.equal(undefined, entry.objectId);
  172. chai.assert.deepEqual({}, entry.newMetadata);
  173. chai.assert.isNull(entry.newDataAsText);
  174. done();
  175. });
  176. });
  177. describe('#setMetadata()', function() {
  178. it('should not change data when nothing change', function(done) {
  179. var entry = new datastore.DatastoreObject(objectIds[0]);
  180. entry.getMetadata(function() {
  181. entry.setMetadata();
  182. entry.save(function(err, metadata, text) {
  183. chai.assert.equal(objectIds[0], entry.objectId);
  184. chai.assert.equal("entry", metadata.name);
  185. chai.assert.equal("test", metadata.activity);
  186. done();
  187. });
  188. });
  189. });
  190. it('should change/update metadata', function(done) {
  191. var entry = new datastore.DatastoreObject(objectIds[0]);
  192. entry.getMetadata(function() {
  193. entry.setMetadata({
  194. name: "updatedentry",
  195. width: "100px"
  196. });
  197. entry.save(function(err, metadata) {
  198. chai.assert.equal(objectIds[0], entry.objectId);
  199. chai.assert.equal("updatedentry", metadata.name);
  200. chai.assert.equal("test", metadata.activity);
  201. chai.assert.equal("100px", metadata.width);
  202. done();
  203. });
  204. });
  205. });
  206. it('should update entry - double check', function(done) {
  207. var entry = new datastore.DatastoreObject(objectIds[0]);
  208. entry.getMetadata(function(err, metadata) {
  209. chai.assert.equal("updatedentry", metadata.name);
  210. chai.assert.equal("test", metadata.activity);
  211. chai.assert.equal("100px", metadata.width);
  212. done();
  213. });
  214. });
  215. });
  216. describe('#setDataAsText()', function() {
  217. it('should remove text when undefined', function(done) {
  218. var entry = new datastore.DatastoreObject(objectIds[0]);
  219. entry.loadAsText(function() {
  220. entry.setDataAsText();
  221. entry.save(function(err, metadata, text) {
  222. chai.assert.equal(objectIds[0], entry.objectId);
  223. chai.assert.equal(undefined, text);
  224. done();
  225. });
  226. });
  227. });
  228. it('should update text', function(done) {
  229. var entry = new datastore.DatastoreObject(objectIds[0]);
  230. entry.loadAsText(function() {
  231. entry.setDataAsText({newvalue: "200"});
  232. entry.save(function(err, metadata, text) {
  233. chai.assert.equal(objectIds[0], entry.objectId);
  234. chai.assert.equal("200",text.newvalue);
  235. done();
  236. });
  237. });
  238. });
  239. it('should update entry - double check', function(done) {
  240. var entry = new datastore.DatastoreObject(objectIds[0]);
  241. entry.loadAsText(function(err, metadata, text) {
  242. chai.assert.equal("200", text.newvalue);
  243. done();
  244. });
  245. });
  246. });
  247. describe('#remove()', function() {
  248. it('should do nothing on inexisting entry', function() {
  249. datastore.remove("ffffffff-ffff-ffff-ffff-ffffffffffff");
  250. var results = datastore.find();
  251. chai.assert.equal(objectIds.length, results.length);
  252. });
  253. it('should remove entry', function() {
  254. for (var i = 0 ; i < objectIds.length ; i++)
  255. datastore.remove(objectIds[i]);
  256. var results = datastore.find();
  257. chai.assert.equal(0, results.length);
  258. });
  259. });
  260. });