services/sync/tests/unit/test_records_wbo.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial