services/sync/tests/unit/test_records_wbo.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_records_wbo.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://services-sync/record.js");
     1.8 +Cu.import("resource://services-sync/identity.js");
     1.9 +Cu.import("resource://services-sync/resource.js");
    1.10 +Cu.import("resource://services-sync/service.js");
    1.11 +Cu.import("resource://services-sync/util.js");
    1.12 +Cu.import("resource://testing-common/services/sync/utils.js");
    1.13 +
    1.14 +
    1.15 +function test_toJSON() {
    1.16 +  _("Create a record, for now without a TTL.");
    1.17 +  let wbo = new WBORecord("coll", "a_record");
    1.18 +  wbo.modified = 12345;
    1.19 +  wbo.sortindex = 42;
    1.20 +  wbo.payload = {};
    1.21 +
    1.22 +  _("Verify that the JSON representation contains the WBO properties, but not TTL.");
    1.23 +  let json = JSON.parse(JSON.stringify(wbo));
    1.24 +  do_check_eq(json.modified, 12345);
    1.25 +  do_check_eq(json.sortindex, 42);
    1.26 +  do_check_eq(json.payload, "{}");
    1.27 +  do_check_false("ttl" in json);
    1.28 +
    1.29 +  _("Set a TTL, make sure it's present in the JSON representation.");
    1.30 +  wbo.ttl = 30*60;
    1.31 +  json = JSON.parse(JSON.stringify(wbo));
    1.32 +  do_check_eq(json.ttl, 30*60);
    1.33 +}
    1.34 +
    1.35 +
    1.36 +function test_fetch() {
    1.37 +  let record = {id: "asdf-1234-asdf-1234",
    1.38 +                modified: 2454725.98283,
    1.39 +                payload: JSON.stringify({cheese: "roquefort"})};
    1.40 +  let record2 = {id: "record2",
    1.41 +                 modified: 2454725.98284,
    1.42 +                 payload: JSON.stringify({cheese: "gruyere"})};
    1.43 +  let coll = [{id: "record2",
    1.44 +               modified: 2454725.98284,
    1.45 +               payload: JSON.stringify({cheese: "gruyere"})}];
    1.46 +
    1.47 +  _("Setting up server.");
    1.48 +  let server = httpd_setup({
    1.49 +    "/record":  httpd_handler(200, "OK", JSON.stringify(record)),
    1.50 +    "/record2": httpd_handler(200, "OK", JSON.stringify(record2)),
    1.51 +    "/coll":    httpd_handler(200, "OK", JSON.stringify(coll))
    1.52 +  });
    1.53 +  do_test_pending();
    1.54 +
    1.55 +  try {
    1.56 +    _("Fetching a WBO record");
    1.57 +    let rec = new WBORecord("coll", "record");
    1.58 +    rec.fetch(Service.resource(server.baseURI + "/record"));
    1.59 +    do_check_eq(rec.id, "asdf-1234-asdf-1234"); // NOT "record"!
    1.60 +
    1.61 +    do_check_eq(rec.modified, 2454725.98283);
    1.62 +    do_check_eq(typeof(rec.payload), "object");
    1.63 +    do_check_eq(rec.payload.cheese, "roquefort");
    1.64 +
    1.65 +    _("Fetching a WBO record using the record manager");
    1.66 +    let rec2 = Service.recordManager.get(server.baseURI + "/record2");
    1.67 +    do_check_eq(rec2.id, "record2");
    1.68 +    do_check_eq(rec2.modified, 2454725.98284);
    1.69 +    do_check_eq(typeof(rec2.payload), "object");
    1.70 +    do_check_eq(rec2.payload.cheese, "gruyere");
    1.71 +    do_check_eq(Service.recordManager.response.status, 200);
    1.72 +
    1.73 +    // Testing collection extraction.
    1.74 +    _("Extracting collection.");
    1.75 +    let rec3 = new WBORecord("tabs", "foo");   // Create through constructor.
    1.76 +    do_check_eq(rec3.collection, "tabs");
    1.77 +
    1.78 +  } finally {
    1.79 +    server.stop(do_test_finished);
    1.80 +  }
    1.81 +}
    1.82 +
    1.83 +function run_test() {
    1.84 +  initTestLogging("Trace");
    1.85 +  ensureLegacyIdentityManager();
    1.86 +
    1.87 +  test_toJSON();
    1.88 +  test_fetch();
    1.89 +}

mercurial