Skip to content

Commit

Permalink
marklogic-community#640 updating unit tests for SJS controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcassel committed Oct 20, 2016
1 parent 6889b69 commit 877f2fb
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 31 deletions.
99 changes: 71 additions & 28 deletions src/test/suites/Framework Tests/routing.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ declare variable $options-non-xml :=
</authentication>
</options>;

(:
: Each of the URLs being tested here includes the language parameter, because
: the controllers can be implemented in either XQuery or SJS. The default is
: controlled by a property. In order to avoid false negatives, these tests
: make that choice explicit.
:)
declare variable $LANG-XQY := "language=xqy";
declare variable $LANG-SJS := "language=sjs";

(: Verify that /tester will call tester:main and return the html view :)
let $response := xdmp:http-get(test:easy-url("/tester"), $options)
let $response := xdmp:http-get(test:easy-url("/tester?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
Expand All @@ -42,7 +50,7 @@ return
),

(: Verify that the .html will return the html view :)
let $response := xdmp:http-get(test:easy-url("/tester.html"), $options)
let $response := xdmp:http-get(test:easy-url("/tester.html?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
Expand All @@ -51,39 +59,39 @@ return
),

(: Verify that the .xml will return the xml view :)
let $response := xdmp:http-get(test:easy-url("/tester.xml"), $options)
let $response := xdmp:http-get(test:easy-url("/tester.xml?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
test:assert-equal("test message: main", fn:string($response[2]/xml))
),

(: JSON view is not defined. Should throw a 500 error with handy error message :)
let $response := xdmp:http-get(test:easy-url("/tester.json"), $options)
let $response := xdmp:http-get(test:easy-url("/tester.json?" || $LANG-XQY), $options)
return
(
test:assert-equal(500, fn:data($response[1]/*:code)),
test:assert-true(fn:contains(fn:string($response[2]//html:div[@class="error-message"]), "Missing the view:tester/main.json"))
),

(: verify that a handy error message is displayed when a layout is missing. :)
let $response := xdmp:http-get(test:easy-url("/tester/missing-layout"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/missing-layout?" || $LANG-XQY), $options)
return
(
test:assert-equal(500, fn:data($response[1]/*:code)),
test:assert-true(fn:contains(fn:string($response[2]//html:div[@class="error-message"]), "Layout:i-dont-exist"))
),

(: verify that a handy error message is displayed when a view is missing. :)
let $response := xdmp:http-get(test:easy-url("/tester/missing-view"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/missing-view?" || $LANG-XQY), $options)
return
(
test:assert-equal(500, fn:data($response[1]/*:code)),
test:assert-true(fn:contains(fn:string($response[2]//html:div[@class="error-message"]), "Missing the view:tester/missing-view.html"))
),

(: verify that turning off the layout works :)
let $response := xdmp:http-get(test:easy-url("/tester/no-layout"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/no-layout?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
Expand All @@ -92,7 +100,7 @@ return
),

(: verify that turning off the view works :)
let $response := xdmp:http-get(test:easy-url("/tester/no-view"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/no-view?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
Expand All @@ -101,15 +109,15 @@ return
),

(: verify that turning off the view and layout works :)
let $response := xdmp:http-get(test:easy-url("/tester/no-view-or-layout"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/no-view-or-layout?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
test:assert-equal("test message: no-view-or-layout", fn:string($response[2]/x))
),

(: verify that specifying a different view works :)
let $response := xdmp:http-get(test:easy-url("/tester/different-view"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/different-view?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
Expand All @@ -118,7 +126,7 @@ return
),

(: verify that specifying a different layout works :)
let $response := xdmp:http-get(test:easy-url("/tester/different-layout"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/different-layout?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
Expand All @@ -127,7 +135,7 @@ return
),

(: verify that specifying a different view for only xml works - first get html :)
let $response := xdmp:http-get(test:easy-url("/tester/different-view-xml-only"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/different-view-xml-only?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
Expand All @@ -136,15 +144,15 @@ return
),

(: verify that specifying a different view for only xml works - now get xml :)
let $response := xdmp:http-get(test:easy-url("/tester/different-view-xml-only.xml"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/different-view-xml-only.xml?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
test:assert-equal("test message: different-view", fn:string($response[2]/xml))
),

(: verify that returning the input from the view doesn't break anything :)
let $response := xdmp:http-get(test:easy-url("/tester/view-that-returns-the-input"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/view-that-returns-the-input?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
Expand All @@ -153,23 +161,23 @@ return
),

(: verify that a missing variable returns a handy error message :)
let $response := xdmp:http-get(test:easy-url("/tester/missing-variable"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/missing-variable?" || $LANG-XQY), $options)
return
(
test:assert-equal(500, fn:data($response[1]/*:code)),
test:assert-true(fn:contains(fn:string($response[2]//html:div[@class="error-message"]), "is expecting the parameter message"))
),

(: verify that a bad import propagates the correct error :)
let $response := xdmp:http-get(test:easy-url("/tester/layout-with-bad-import"), $options-non-xml)
let $response := xdmp:http-get(test:easy-url("/tester/layout-with-bad-import?" || $LANG-XQY), $options-non-xml)
return
(
test:assert-equal(500, fn:data($response[1]/*:code)),
test:assert-true(fn:contains($response[2], "SVC-FILOPN") or fn:contains($response[2], "XDMP-MODNOTFOUND"))
),

(: verify that a bad import propagates the correct error :)
let $response := xdmp:http-get(test:easy-url("/tester/view-with-bad-import"), $options-non-xml)
let $response := xdmp:http-get(test:easy-url("/tester/view-with-bad-import?" || $LANG-XQY), $options-non-xml)
return
(
test:assert-equal(500, fn:data($response[1]/*:code)),
Expand Down Expand Up @@ -198,52 +206,87 @@ return
test:assert-equal(200, fn:data($response[1]/*:code))
),

(: verify that a non-existent route returns 404 :)
(: verify that a non-existent route returns 404
: Since it's not a real controller, doesn't matter what language we ask for
:)
let $response := xdmp:http-get(test:easy-url("/not-real"), $options-non-xml)
return
(
test:assert-equal(404, fn:data($response[1]/*:code))
),

(: verify that a non-existent route returns 404 :)
(: verify that a non-existent route returns 404
: Since it's not a real controller, doesn't matter what language we ask for
:)
let $response := xdmp:http-get(test:easy-url("/not-real.xml"), $options-non-xml)
return
(
test:assert-equal(404, fn:data($response[1]/*:code))
),

(: verify that a non-existent route returns 404 :)
(: verify that a non-existent route returns 404
: Since it's not a real controller, doesn't matter what language we ask for
:)
let $response := xdmp:http-get(test:easy-url("/not-real/at-all"), $options-non-xml)
return
(
test:assert-equal(404, fn:data($response[1]/*:code))
),

let $response := xdmp:http-get(test:easy-url("/tester/update"), $options)
let $response := xdmp:http-get(test:easy-url("/tester/update?" || $LANG-XQY), $options)
return
(
test:assert-equal(500, fn:data($response[1]/*:code)),
test:assert-equal("XDMP-UPDATEFUNCTIONFROMQUERY", fn:string($response[2]//*:code))
),

let $response := xdmp:http-head(test:easy-url("/tester/update"), $options)
let $response := xdmp:http-head(test:easy-url("/tester/update?" || $LANG-XQY), $options)
return
(
test:assert-equal(500, fn:data($response[1]/*:code))
(: A HEAD request doesn't get the body, so we can't check the detailed message :)
),

let $response := xdmp:http-delete(test:easy-url("/tester/delete?uri=/delete-me.xml"), $options-non-xml)
return
let $response := xdmp:http-delete(test:easy-url("/tester/delete?uri=/delete-me.xml&amp;" || $LANG-XQY), $options-non-xml)
return
test:assert-equal(200, fn:data($response[1]/*:code)),

let $response := xdmp:http-post(test:easy-url("/tester/update"), $options-non-xml)
let $response := xdmp:http-post(test:easy-url("/tester/update?" || $LANG-XQY), $options-non-xml)
return
test:assert-equal(200, fn:data($response[1]/*:code)),

let $response := xdmp:http-put(test:easy-url("/tester/update2"), $options-non-xml)
let $response := xdmp:http-put(test:easy-url("/tester/update2?" || $LANG-XQY), $options-non-xml)
return
test:assert-equal(200, fn:data($response[1]/*:code))
test:assert-equal(200, fn:data($response[1]/*:code)),

(: test the SJS controller's default function :)
let $response := xdmp:http-get(test:easy-url("/tester?" || $LANG-SJS), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
test:assert-equal("main", fn:string($response[2]//html:title)),
test:assert-equal("test message: main", fn:string($response[2]//html:div[@id="message"]))
),

(: test the SJS controller's default function :)
let $response := xdmp:http-get(test:easy-url("/tester/print?" || $LANG-SJS), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
test:assert-equal("this is print", fn:string($response[2]/div/fn:string()))
),

(: both the XQuery and SJS controllers have a main function. Test whichever is
: configured.
:)
let $response := xdmp:http-get(test:easy-url("/tester"), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
test:assert-equal("main", fn:string($response[2]//html:title)),
test:assert-equal("test message: main", fn:string($response[2]//html:div[@id="message"]))
)


;

Expand All @@ -256,4 +299,4 @@ return
test:assert-equal(<test/>, $doc),
test:assert-equal(<test/>, $doc2),
test:assert-not-exists($doc3)
)
)
12 changes: 10 additions & 2 deletions src/test/suites/Framework Tests/site-index.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/t

import module namespace c = "http://marklogic.com/roxy/test-config" at "/test/test-config.xqy";

(:
: Each of the URLs being tested here includes the language parameter, because
: the controllers can be implemented in either XQuery or SJS. The default is
: controlled by a property. In order to avoid false negatives, these tests
: make that choice explicit.
:)
declare variable $LANG-XQY := "language=xqy";

let $options :=
<options xmlns="xdmp:http">
<format xmlns="xdmp:document-get">xml</format>
Expand All @@ -12,9 +20,9 @@ let $options :=
<password>{$c:PASSWORD}</password>
</authentication>
</options>
let $response := xdmp:http-get(test:easy-url("/"), $options)
let $response := xdmp:http-get(test:easy-url("/?" || $LANG-XQY), $options)
return
(
test:assert-equal(200, fn:data($response[1]/*:code)),
test:assert-equal(1, fn:count($response[2]//*:html))
)
)
3 changes: 2 additions & 1 deletion src/test/suites/Framework Tests/suite-setup.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import module namespace test="http://marklogic.com/roxy/test-helper" at "/test/t

(: create a test controller and views :)
test:load-test-file("tester.xqy", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/controllers/tester.xqy")),
test:load-test-file("tester.sjs", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/controllers/tester.sjs")),
test:load-test-file("missing-map.xqy", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/controllers/missing-map.xqy")),
test:load-test-file("different-layout-view.html.xqy", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/views/tester/different-layout.html.xqy")),
test:load-test-file("different-view-xml-only.html.xqy", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/views/tester/different-view-xml-only.html.xqy")),
Expand All @@ -16,4 +17,4 @@ test:load-test-file("view-with-bad-import.html.xqy", xdmp:modules-database(), fn
test:load-test-file("view-that-returns-the-input.html.xqy", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/views/tester/view-that-returns-the-input.html.xqy")),
test:load-test-file("different-layout.html.xqy", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/views/layouts/different-layout.html.xqy")),
test:load-test-file("test-layout.html.xqy", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/views/layouts/test-layout.html.xqy")),
test:load-test-file("layout-with-bad-import.html.xqy", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/views/layouts/layout-with-bad-import.html.xqy"))
test:load-test-file("layout-with-bad-import.html.xqy", xdmp:modules-database(), fn:concat(xdmp:modules-root(), "app/views/layouts/layout-with-bad-import.html.xqy"))
14 changes: 14 additions & 0 deletions src/test/suites/Framework Tests/test-data/tester.sjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var req = require("/roxy/lib/request.xqy");

module.exports = {

main : function () {
return '<html xmlns="http://www.w3.org/1999/xhtml">' +
' <title>main</title>' +
' <div id="message">test message: main</div>' +
'</html>';
},
print : function () {
return "<div>this is print</div>";
}
};

0 comments on commit 877f2fb

Please sign in to comment.