|
| 1 | +(function($) { |
| 2 | + |
| 3 | + module("tree1"); |
| 4 | + |
| 5 | + // Test case : Tree Test #1 |
| 6 | + _asyncTest("Tree #1", function() |
| 7 | + { |
| 8 | + expect(3); |
| 9 | + |
| 10 | + var createFolder = function(branch, parentFolderPath, title, callback) |
| 11 | + { |
| 12 | + Chain(branch).createNode({ |
| 13 | + "title": title |
| 14 | + }, { |
| 15 | + "folderPath": parentFolderPath |
| 16 | + }).then(function() { |
| 17 | + callback(); |
| 18 | + }); |
| 19 | + }; |
| 20 | + |
| 21 | + var defineArticleType = function(branch, callback) |
| 22 | + { |
| 23 | + Chain(branch).createNode({ |
| 24 | + "_type": "d:type", |
| 25 | + "_qname": "my:article", |
| 26 | + "title": "My Article", |
| 27 | + "type": "object", |
| 28 | + "properties": { |
| 29 | + "title": { |
| 30 | + "type": "string" |
| 31 | + }, |
| 32 | + "category": { |
| 33 | + "type": "string" |
| 34 | + }, |
| 35 | + "published": { |
| 36 | + "type": "boolean" |
| 37 | + } |
| 38 | + } |
| 39 | + }).then(function() { |
| 40 | + callback(); |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + var createArticle = function(branch, parentFolderPath, title, category, published, text, callback) |
| 45 | + { |
| 46 | + var o = { |
| 47 | + "_type": "my:article", |
| 48 | + "title": title, |
| 49 | + "category": category |
| 50 | + }; |
| 51 | + |
| 52 | + if (published) { |
| 53 | + o.published = published; |
| 54 | + } |
| 55 | + |
| 56 | + if (text) { |
| 57 | + o.text = text; |
| 58 | + } |
| 59 | + |
| 60 | + Chain(branch).createNode(o, { |
| 61 | + "folderPath": parentFolderPath |
| 62 | + }).then(function() { |
| 63 | + callback(); |
| 64 | + }); |
| 65 | + }; |
| 66 | + |
| 67 | + var countArticles = function(obj) |
| 68 | + { |
| 69 | + var count = 0; |
| 70 | + |
| 71 | + var type = obj["typeQName"]; |
| 72 | + if ("my:article" === type) |
| 73 | + { |
| 74 | + count++; |
| 75 | + } |
| 76 | + |
| 77 | + var children = obj["children"]; |
| 78 | + if (children) |
| 79 | + { |
| 80 | + for (var i = 0; i < children.length; i++) |
| 81 | + { |
| 82 | + count += countArticles(children[i]); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return count; |
| 87 | + }; |
| 88 | + |
| 89 | + var loadTree = function(rootNode, depth, query, search, callback) { |
| 90 | + |
| 91 | + var o = { |
| 92 | + "properties": true, |
| 93 | + "depth": -1 |
| 94 | + }; |
| 95 | + if (depth) { |
| 96 | + o.depth = depth; |
| 97 | + } |
| 98 | + if (query) { |
| 99 | + o.query = query; |
| 100 | + } |
| 101 | + if (search) { |
| 102 | + o.search = search; |
| 103 | + } |
| 104 | + |
| 105 | + Chain(rootNode).loadTree(o, function(tree) { |
| 106 | + callback(tree); |
| 107 | + }); |
| 108 | + }; |
| 109 | + |
| 110 | + var runTests = function(branch, rootNode, callback) |
| 111 | + { |
| 112 | + // find all articles (should be 6) |
| 113 | + loadTree(rootNode, 3, { "_type": "my:article" }, null, function(tree) { |
| 114 | + equal(countArticles(tree), 6, "Should find 6 articles"); |
| 115 | + |
| 116 | + // find published articles (should be 2) |
| 117 | + loadTree(rootNode, 3, { "published": true, "_type": "my:article" }, null, function(tree) { |
| 118 | + equal(countArticles(tree), 2, "Should find 2 published articles"); |
| 119 | + |
| 120 | + // find published articles with text "brewers" (should be 1) |
| 121 | + loadTree(rootNode, 3, { "published": true, "_type": "my:article" }, { "query_string": { "query": "brewers" }}, function(tree) { |
| 122 | + equal(countArticles(tree), 1, "Should find 1 published article for 'brewers'"); |
| 123 | + |
| 124 | + callback(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }; |
| 129 | + |
| 130 | + var gitana = GitanaTest.authenticateFullOAuth(); |
| 131 | + gitana.then(function() { |
| 132 | + |
| 133 | + // NOTE: this = platform |
| 134 | + |
| 135 | + this.createRepository().readBranch("master").then(function() { |
| 136 | + |
| 137 | + // NOTE: this = branch |
| 138 | + var branch = this; |
| 139 | + |
| 140 | + createFolder(branch, "/", "folder1", function() { |
| 141 | + createFolder(branch, "/folder1", "folder11", function () { |
| 142 | + createFolder(branch, "/folder1", "folder12", function () { |
| 143 | + defineArticleType(branch, function() { |
| 144 | + createArticle(branch, "/folder1/folder11", "Article 1", "red", false, null, function() { |
| 145 | + createArticle(branch, "/folder1/folder11", "Article 2", "blue", false, null, function() { |
| 146 | + createArticle(branch, "/folder1/folder11", "Article 3", "green", true, null, function() { |
| 147 | + createArticle(branch, "/folder1/folder12", "Article 4", "red", false, null, function() { |
| 148 | + createArticle(branch, "/folder1/folder12", "Article 5", "red", false, null, function() { |
| 149 | + createArticle(branch, "/folder1/folder12", "Article 6", "green", true, "brewers", function() { |
| 150 | + |
| 151 | + branch.rootNode().then(function() { |
| 152 | + var rootNode = this; |
| 153 | + |
| 154 | + // wait a bit to allow indexing |
| 155 | + setTimeout(function() { |
| 156 | + |
| 157 | + // now run the actual tests |
| 158 | + runTests(branch, rootNode, function() { |
| 159 | + success(); |
| 160 | + }); |
| 161 | + |
| 162 | + }, 5000); |
| 163 | + }) |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + }); |
| 177 | + |
| 178 | + var success = function() |
| 179 | + { |
| 180 | + start(); |
| 181 | + }; |
| 182 | + |
| 183 | + }); |
| 184 | + |
| 185 | +}(jQuery) ); |
0 commit comments