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.

253 lines
7.6 KiB

  1. define(["sugar-web/bus", "sugar-web/env", "sugar-web/datastore"], function (bus, env, datastore) {
  2. 'use strict';
  3. var defaultTimeoutInterval = jasmine.getEnv().defaultTimeoutInterval;
  4. describe("datastore object", function () {
  5. beforeEach(function () {
  6. // FIXME: due to db initialization,
  7. // the very first save() call may take a while
  8. jasmine.getEnv().defaultTimeoutInterval = 10000;
  9. spyOn(env, 'isStandalone').andReturn(false);
  10. bus.listen();
  11. });
  12. afterEach(function () {
  13. jasmine.getEnv().defaultTimeoutInterval = defaultTimeoutInterval;
  14. bus.close();
  15. });
  16. it("should be able to set and get metadata", function () {
  17. var saved;
  18. var gotMetadata;
  19. var datastoreObject;
  20. var objectId;
  21. var testTitle = "hello";
  22. runs(function () {
  23. saved = false;
  24. datastoreObject = new datastore.DatastoreObject();
  25. datastoreObject.setMetadata({
  26. title: testTitle
  27. });
  28. datastoreObject.save(function () {
  29. saved = true;
  30. objectId = datastoreObject.objectId;
  31. });
  32. });
  33. waitsFor(function () {
  34. return saved;
  35. }, "should have saved the object");
  36. runs(function () {
  37. gotMetadata = false;
  38. datastoreObject = new datastore.DatastoreObject(objectId);
  39. datastoreObject.getMetadata(function (error, metadata) {
  40. expect(metadata.title).toEqual(testTitle);
  41. gotMetadata = true;
  42. });
  43. });
  44. waitsFor(function () {
  45. return gotMetadata;
  46. }, "should have got the object metadata");
  47. });
  48. it("should be able to save and load text", function () {
  49. var saved;
  50. var gotMetadata;
  51. var datastoreObject;
  52. var objectId;
  53. var testText = "hello";
  54. runs(function () {
  55. saved = false;
  56. datastoreObject = new datastore.DatastoreObject();
  57. datastoreObject.setDataAsText(testText);
  58. datastoreObject.save(function () {
  59. saved = true;
  60. objectId = datastoreObject.objectId;
  61. });
  62. });
  63. waitsFor(function () {
  64. return saved;
  65. }, "should have saved the object");
  66. runs(function () {
  67. gotMetadata = false;
  68. function onLoaded(error, metadata, text) {
  69. expect(text).toEqual(testText);
  70. gotMetadata = true;
  71. }
  72. datastoreObject = new datastore.DatastoreObject(objectId);
  73. datastoreObject.loadAsText(onLoaded);
  74. });
  75. waitsFor(function () {
  76. return gotMetadata;
  77. }, "should have got the object metadata");
  78. });
  79. });
  80. describe("datastore", function () {
  81. beforeEach(function () {
  82. // FIXME: due to db initialization,
  83. // the very first save() call may take a while
  84. jasmine.getEnv().defaultTimeoutInterval = 10000;
  85. spyOn(env, 'isStandalone').andReturn(false);
  86. bus.listen();
  87. });
  88. afterEach(function () {
  89. jasmine.getEnv().defaultTimeoutInterval = defaultTimeoutInterval;
  90. bus.close();
  91. });
  92. it("should be able to create an object", function () {
  93. var wasCreated;
  94. runs(function () {
  95. wasCreated = false;
  96. function onCreated(error, objectId) {
  97. expect(objectId).toEqual(jasmine.any(String));
  98. wasCreated = true;
  99. }
  100. datastore.create({}, onCreated);
  101. });
  102. waitsFor(function () {
  103. return wasCreated;
  104. }, "the object should be created");
  105. });
  106. it("should be able to set object metadata", function () {
  107. var metadataSet;
  108. var gotMetadata;
  109. var objectId;
  110. var testTitle = "hello";
  111. runs(function () {
  112. function onMetadataSet(error) {
  113. expect(error).toBeNull();
  114. metadataSet = true;
  115. }
  116. function onCreated(error, createdObjectId) {
  117. objectId = createdObjectId;
  118. var metadata = {
  119. title: testTitle
  120. };
  121. datastore.setMetadata(objectId, metadata, onMetadataSet);
  122. }
  123. metadataSet = false;
  124. datastore.create({}, onCreated);
  125. });
  126. waitsFor(function () {
  127. return metadataSet;
  128. }, "metadata should be set");
  129. runs(function () {
  130. function onGotMetadata(error, metadata) {
  131. expect(metadata.title).toEqual(testTitle);
  132. gotMetadata = true;
  133. }
  134. gotMetadata = false;
  135. datastore.getMetadata(objectId, onGotMetadata);
  136. });
  137. waitsFor(function () {
  138. return gotMetadata;
  139. }, "should have got object metadata");
  140. });
  141. it("should be able to get object metadata", function () {
  142. var gotMetadata = false;
  143. var testTitle = "hello";
  144. runs(function () {
  145. function onGotMetadata(error, metadata) {
  146. expect(metadata.title).toEqual(testTitle);
  147. gotMetadata = true;
  148. }
  149. function onCreated(error, objectId) {
  150. datastore.getMetadata(objectId, onGotMetadata);
  151. }
  152. datastore.create({
  153. title: testTitle
  154. }, onCreated);
  155. });
  156. waitsFor(function () {
  157. return gotMetadata;
  158. }, "should have got object metadata");
  159. });
  160. it("should be able to load an object", function () {
  161. var wasLoaded = false;
  162. var objectId = null;
  163. var inputStream = null;
  164. var objectData = null;
  165. var testData = new Uint8Array([1, 2, 3, 4]);
  166. runs(function () {
  167. function onStreamClose(error) {
  168. expect(objectData).toEqual(testData.buffer);
  169. wasLoaded = true;
  170. }
  171. function onStreamRead(error, data) {
  172. objectData = data;
  173. }
  174. function onLoaded(error, metadata, loadedInputStream) {
  175. inputStream = loadedInputStream;
  176. inputStream.read(8192, onStreamRead);
  177. inputStream.close(onStreamClose);
  178. }
  179. function onClosed(error) {
  180. datastore.load(objectId, onLoaded);
  181. }
  182. function onSaved(error, outputStream) {
  183. outputStream.write(testData);
  184. outputStream.close(onClosed);
  185. }
  186. function onCreated(error, createdObjectId) {
  187. objectId = createdObjectId;
  188. datastore.save(objectId, {}, onSaved);
  189. }
  190. datastore.create({}, onCreated);
  191. });
  192. waitsFor(function () {
  193. return wasLoaded;
  194. }, "the object should be loaded");
  195. });
  196. });
  197. });