PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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