~ubuntu-branches/ubuntu/oneiric/weave/oneiric

« back to all changes in this revision

Viewing changes to tests/unit/test_collection_inc_get.js

  • Committer: Bazaar Package Importer
  • Author(s): Micah Gersten
  • Date: 2010-08-11 00:35:15 UTC
  • mfrom: (3.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100811003515-o3jbh826bnd1syjv
Tags: 1.4.3-1ubuntu1
* Add -fshort-wchar to CXXFLAGS to fix FTBFS in Ubuntu
  - update debian/rules 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
_("Make sure Collection can correctly incrementally parse GET requests");
2
 
Cu.import("resource://weave/base_records/collection.js");
3
 
Cu.import("resource://weave/base_records/wbo.js");
4
 
 
5
 
function run_test() {
6
 
  let coll = new Collection("", WBORecord);
7
 
  let stream = { _data: "" };
8
 
  let called, recCount, sum;
9
 
 
10
 
  _("Not-JSON, string payloads are strings");
11
 
  called = false;
12
 
  stream._data = '{"payload":"hello"}\n';
13
 
  coll.recordHandler = function(rec) {
14
 
    called = true;
15
 
    _("Got record:", JSON.stringify(rec));
16
 
    do_check_eq(rec.payload, "hello");
17
 
  };
18
 
  coll._onProgress.call(stream);
19
 
  do_check_eq(stream._data, '');
20
 
  do_check_true(called);
21
 
  _("\n");
22
 
 
23
 
 
24
 
  _("Parse record with payload");
25
 
  called = false;
26
 
  stream._data = '{"payload":"{\\"value\\":123}"}\n';
27
 
  coll.recordHandler = function(rec) {
28
 
    called = true;
29
 
    _("Got record:", JSON.stringify(rec));
30
 
    do_check_eq(rec.payload.value, 123);
31
 
  };
32
 
  coll._onProgress.call(stream);
33
 
  do_check_eq(stream._data, '');
34
 
  do_check_true(called);
35
 
  _("\n");
36
 
 
37
 
 
38
 
  _("Parse multiple records in one go");
39
 
  called = false;
40
 
  recCount = 0;
41
 
  sum = 0;
42
 
  stream._data = '{"payload":"{\\"value\\":100}"}\n{"payload":"{\\"value\\":10}"}\n{"payload":"{\\"value\\":1}"}\n';
43
 
  coll.recordHandler = function(rec) {
44
 
    called = true;
45
 
    _("Got record:", JSON.stringify(rec));
46
 
    recCount++;
47
 
    sum += rec.payload.value;
48
 
    _("Incremental status: count", recCount, "sum", sum);
49
 
    switch (recCount) {
50
 
      case 1:
51
 
        do_check_eq(rec.payload.value, 100);
52
 
        do_check_eq(sum, 100);
53
 
        break;
54
 
      case 2:
55
 
        do_check_eq(rec.payload.value, 10);
56
 
        do_check_eq(sum, 110);
57
 
        break;
58
 
      case 3:
59
 
        do_check_eq(rec.payload.value, 1);
60
 
        do_check_eq(sum, 111);
61
 
        break;
62
 
      default:
63
 
        do_throw("unexpected number of record counts", recCount);
64
 
        break;
65
 
    }
66
 
  };
67
 
  coll._onProgress.call(stream);
68
 
  do_check_eq(recCount, 3);
69
 
  do_check_eq(sum, 111);
70
 
  do_check_eq(stream._data, '');
71
 
  do_check_true(called);
72
 
  _("\n");
73
 
 
74
 
 
75
 
  _("Handle incremental data incoming");
76
 
  called = false;
77
 
  recCount = 0;
78
 
  sum = 0;
79
 
  stream._data = '{"payl';
80
 
  coll.recordHandler = function(rec) {
81
 
    called = true;
82
 
    do_throw("shouldn't have gotten a record..");
83
 
  };
84
 
  coll._onProgress.call(stream);
85
 
  _("shouldn't have gotten anything yet");
86
 
  do_check_eq(recCount, 0);
87
 
  do_check_eq(sum, 0);
88
 
  _("leading array bracket should have been trimmed");
89
 
  do_check_eq(stream._data, '{"payl');
90
 
  do_check_false(called);
91
 
  _();
92
 
 
93
 
  _("adding more data enough for one record..");
94
 
  called = false;
95
 
  stream._data += 'oad":"{\\"value\\":100}"}\n';
96
 
  coll.recordHandler = function(rec) {
97
 
    called = true;
98
 
    _("Got record:", JSON.stringify(rec));
99
 
    recCount++;
100
 
    sum += rec.payload.value;
101
 
  };
102
 
  coll._onProgress.call(stream);
103
 
  _("should have 1 record with sum 100");
104
 
  do_check_eq(recCount, 1);
105
 
  do_check_eq(sum, 100);
106
 
  _("all data should have been consumed including trailing comma");
107
 
  do_check_eq(stream._data, '');
108
 
  do_check_true(called);
109
 
  _();
110
 
 
111
 
  _("adding more data..");
112
 
  called = false;
113
 
  stream._data += '{"payload":"{\\"value\\":10}"';
114
 
  coll.recordHandler = function(rec) {
115
 
    called = true;
116
 
    do_throw("shouldn't have gotten a record..");
117
 
  };
118
 
  coll._onProgress.call(stream);
119
 
  _("should still have 1 record with sum 100");
120
 
  do_check_eq(recCount, 1);
121
 
  do_check_eq(sum, 100);
122
 
  _("should almost have a record");
123
 
  do_check_eq(stream._data, '{"payload":"{\\"value\\":10}"');
124
 
  do_check_false(called);
125
 
  _();
126
 
 
127
 
  _("add data for two records..");
128
 
  called = false;
129
 
  stream._data += '}\n{"payload":"{\\"value\\":1}"}\n';
130
 
  coll.recordHandler = function(rec) {
131
 
    called = true;
132
 
    _("Got record:", JSON.stringify(rec));
133
 
    recCount++;
134
 
    sum += rec.payload.value;
135
 
    switch (recCount) {
136
 
      case 2:
137
 
        do_check_eq(rec.payload.value, 10);
138
 
        do_check_eq(sum, 110);
139
 
        break;
140
 
      case 3:
141
 
        do_check_eq(rec.payload.value, 1);
142
 
        do_check_eq(sum, 111);
143
 
        break;
144
 
      default:
145
 
        do_throw("unexpected number of record counts", recCount);
146
 
        break;
147
 
    }
148
 
  };
149
 
  coll._onProgress.call(stream);
150
 
  _("should have gotten all 3 records with sum 111");
151
 
  do_check_eq(recCount, 3);
152
 
  do_check_eq(sum, 111);
153
 
  _("should have consumed all data");
154
 
  do_check_eq(stream._data, '');
155
 
  do_check_true(called);
156
 
  _();
157
 
 
158
 
  _("add no extra data");
159
 
  called = false;
160
 
  stream._data += '';
161
 
  coll.recordHandler = function(rec) {
162
 
    called = true;
163
 
    do_throw("shouldn't have gotten a record..");
164
 
  };
165
 
  coll._onProgress.call(stream);
166
 
  _("should still have 3 records with sum 111");
167
 
  do_check_eq(recCount, 3);
168
 
  do_check_eq(sum, 111);
169
 
  _("should have consumed nothing but still have nothing");
170
 
  do_check_eq(stream._data, "");
171
 
  do_check_false(called);
172
 
  _("\n");
173
 
}