/services/sync/tests/unit/test_records_wbo.js

http://github.com/zpao/v8monkey · JavaScript · 80 lines · 65 code · 14 blank · 1 comment · 0 complexity · 7a960806655a3faece0ab432454776ea MD5 · raw file

  1. Cu.import("resource://services-sync/record.js");
  2. Cu.import("resource://services-sync/identity.js");
  3. Cu.import("resource://services-sync/resource.js");
  4. Cu.import("resource://services-sync/util.js");
  5. function test_toJSON() {
  6. _("Create a record, for now without a TTL.");
  7. let wbo = new WBORecord("coll", "a_record");
  8. wbo.modified = 12345;
  9. wbo.sortindex = 42;
  10. wbo.payload = {};
  11. _("Verify that the JSON representation contains the WBO properties, but not TTL.");
  12. let json = JSON.parse(JSON.stringify(wbo));
  13. do_check_eq(json.modified, 12345);
  14. do_check_eq(json.sortindex, 42);
  15. do_check_eq(json.payload, "{}");
  16. do_check_false("ttl" in json);
  17. _("Set a TTL, make sure it's present in the JSON representation.");
  18. wbo.ttl = 30*60;
  19. json = JSON.parse(JSON.stringify(wbo));
  20. do_check_eq(json.ttl, 30*60);
  21. }
  22. function test_fetch() {
  23. let record = {id: "asdf-1234-asdf-1234",
  24. modified: 2454725.98283,
  25. payload: JSON.stringify({cheese: "roquefort"})};
  26. let record2 = {id: "record2",
  27. modified: 2454725.98284,
  28. payload: JSON.stringify({cheese: "gruyere"})};
  29. let coll = [{id: "record2",
  30. modified: 2454725.98284,
  31. payload: JSON.stringify({cheese: "gruyere"})}];
  32. _("Setting up server.");
  33. let server = httpd_setup({
  34. "/record": httpd_handler(200, "OK", JSON.stringify(record)),
  35. "/record2": httpd_handler(200, "OK", JSON.stringify(record2)),
  36. "/coll": httpd_handler(200, "OK", JSON.stringify(coll))
  37. });
  38. do_test_pending();
  39. try {
  40. _("Fetching a WBO record");
  41. let rec = new WBORecord("coll", "record");
  42. rec.fetch("http://localhost:8080/record");
  43. do_check_eq(rec.id, "asdf-1234-asdf-1234"); // NOT "record"!
  44. do_check_eq(rec.modified, 2454725.98283);
  45. do_check_eq(typeof(rec.payload), "object");
  46. do_check_eq(rec.payload.cheese, "roquefort");
  47. _("Fetching a WBO record using the record manager");
  48. let rec2 = Records.get("http://localhost:8080/record2");
  49. do_check_eq(rec2.id, "record2");
  50. do_check_eq(rec2.modified, 2454725.98284);
  51. do_check_eq(typeof(rec2.payload), "object");
  52. do_check_eq(rec2.payload.cheese, "gruyere");
  53. do_check_eq(Records.response.status, 200);
  54. // Testing collection extraction.
  55. _("Extracting collection.");
  56. let rec3 = new WBORecord("tabs", "foo"); // Create through constructor.
  57. do_check_eq(rec3.collection, "tabs");
  58. } finally {
  59. server.stop(do_test_finished);
  60. }
  61. }
  62. function run_test() {
  63. initTestLogging("Trace");
  64. test_toJSON();
  65. test_fetch();
  66. }