summaryrefslogtreecommitdiff
path: root/test/javascript/tests/coffee.js
blob: 44c920693e58e4ba393d3bd291d8bb73773601a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// test basic coffeescript functionality
couchTests.coffee = function(debug) {
  return console.log('done in test/elixir/test/coffee_test.exs');
  var db_name = get_random_db_name();
  var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"false"});
  db.createDb();
  if (debug) debugger;

  var ddoc = {
    _id: "_design/coffee",
    language: "coffeescript",
    views: {
      myview: {
        map: '(doc) -> if doc.foo\n  emit(doc.foo, 1)',
        reduce: '(keys, values, rereduce) ->\n  sum = 0\n  for x in values\n    sum = sum + x\n  sum'
      }
    },
    shows: {
      myshow: '(doc) ->\n  "Foo #{doc.foo}"'
    },
    lists: {
      mylist: '(head, req) ->\n  while row = getRow()\n    send("Foo #{row.value}")\n  return "Foo"'
    },
    filters: {
      filter: "(doc) ->\n  doc.foo"
    }
  };

  db.save(ddoc);

  var docs = [
    {_id:"a", foo: 100},
    {foo:1},
    {foo:1},
    {foo:2},
    {foo:2},
    {bar:1},
    {bar:1},
    {bar:2},
    {bar:2}
  ];

  db.bulkSave(docs);

  var res = db.view("coffee/myview");
  TEquals(5, res.rows[0].value, "should sum up values");

  var res = CouchDB.request("GET", "/" + db.name + "/_design/coffee/_show/myshow/a");
  TEquals("Foo 100", res.responseText, "should show 100");

  var res = CouchDB.request("GET", "/" + db.name + "/_design/coffee/_list/mylist/myview");
  TEquals("Foo 5Foo", res.responseText, "should list");

  var changes = db.changes({filter: "coffee/filter"});
  TEquals(5, changes.results.length, "should have changes");

  // cleanup
  db.deleteDb();
};