Unit Testing XQuery on MarkLogic
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Unit Testing XQuery on MarkLogic

on

  • 1,396 views

Unit testing is a required part of a healthy software development lifecycle. Business logic in MarkLogic Xquery needs the same insurance of superb testing as any other language. ...

Unit testing is a required part of a healthy software development lifecycle. Business logic in MarkLogic Xquery needs the same insurance of superb testing as any other language.

Principles: Come learn the motivation for unit testing and how test-driven development can increase your productivity writing solid Xquery code in an Agile-coding environment.

Skills: We'll code Xquery examples to learn general skills including the TDD workflow, how to isolate your code for unit testability, and how to test one thing at a time. In each case, we'll address how to apply these skills specifically to development in the MarkLogic environment.

Tools: We'll also introduce you to in-house-developed tooling for creating unit tests and running them. This tooling provides an all-Xquery method of creating test functions, annotating them as such so they're runnable in the test runner, isolating certain modules to test, and viewing clear test results.

With a few principles, skills, and tools for unit testing, you can go forward with increased confidence that the Xquery code you write on MarkLogic is more awesome than ever.

This slide deck is from a presentation at the MarkLogic Users Conference 2011.

More info available at RockyCode.com

Statistics

Views

Total Views
1,396
Views on SlideShare
1,233
Embed Views
163

Actions

Likes
0
Downloads
15
Comments
0

4 Embeds 163

http://rockycode.com 153
http://jaketrent.com 8
url_unknown 1
http://localhost 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Unit Testing XQuery on MarkLogic Presentation Transcript

  • 1. declare function testFeature() { let $actual := feature:doFeature() (: assert feature condition :)};declare function doFeature() { () (: todo: impl :)};
  • 2. declare function doFeature() { let $output := lotsOfLogic() return $output};
  • 3. declare namespace its = “http://www.w3.org/2005/11/its”;declare function buildXmlWithTranslateFlags() { element data { attribute its:translate { “yes” }, businessLogicToBuildXml(); }};
  • 4. declare function buildXml() { element data { businessLogicToBuildXml(); }};declare namespace its = “http://www.w3.org/2005/11/its”;declare function addTranslateFlags($xml) { element {$xml/fn:name()} { attribute its:translate { “yes” }, $xml/@*, $xml/* }};
  • 5. import module namespace build = “b” at “/build.xqy”;declare function testBuildXml() { let $actual := b:buildXml() (: assert only xml is well-built :)};import module namespace translate = “t” at “/transl8.xqy”;declare function testAddTranslateFlags() { let $actual := t:addTranslateFlags() (: assert only that translation flags added correctly :)};
  • 6. import module namespace build = “b” at “/build.xqy”;declare function buildXml_condition1() { (: ... :)};declare function buildXml_condition2() { (: ... :)};(: etc. :)
  • 7. declare function bizLogic($input as element()*) { (: ... :)};import module namespace logic = “logic” at “/logic.xqy”;declare function bizLogic_emptyInput() { (: ... :)};declare function bizLogic_oneInput() { (: ... :)};declare function bizLogic_manyInput() { (: ... :)};
  • 8. declare function bizLogic($input as element()*) { (: ... :)};import module namespace logic = “logic” at “/logic.xqy”;declare function bizLogic_insufficientInput() { (: ... :)};declare function bizLogic_malformedInput() { (: ... :)};
  • 9. declare function bizLogic($input as element()*) { if (isInsufficientInput($input)) then fn:error(xs:Qname(“ASSERT”), “Insufficient msg”) else (: normal process... :)};import module namespace logic = “logic” at “/logic.xqy”;declare function bizLogic_insufficientInput() { try { (: call w/ insufficient input :) } catch ($e) { if ($e/error:name eq “ASSERT”) then () else xdmp:rethrow() }};
  • 10. declare function businessLogic($input) { let $moreInput := /more-input[@attr eq $input] let $returnVal := for $item in $moreInput let $details := /detail[@id eq $item/id] return if ($details/thing eq xdmp:get-request-field(“thing-id”)) then $details else () return if (fn:exists($returnVal)) then let $audit := xdmp:document-insert(“/latest.xml”, element ok {}) return $returnVal else xdmp:redirect-response(“/empty.xqy”);};
  • 11. declare function businessLogic($input) { let $moreInput := /more-input[@attr eq $input] let $returnVal := for $item in $moreInput let $details := /detail[@id eq $item/id] return if ($details/thing eq xdmp:get-request-field(“thing-id”)) then $details else () return if (fn:exists($returnVal)) then let $audit := xdmp:document-insert(“/latest.xml”, element ok {}) return $returnVal else xdmp:redirect-response(“/empty.xqy”);};
  • 12. xdmp:get-request-field(“var”)xdmp:redirect-response(“/page.xqy”)
  • 13. fn:collection()/dances/rainxdmp:document-insert(“/trades.xml”, element beads {})
  • 14. declare function feature() { let $filter := xdmp:get-request-field(“filter”) let $input := fn:collection()/data[@attr eq $filter] let $result := businessLogic($input) let $persist := xdmp:document-insert(“/doc.xml”, $result) return xdmp:redirect-response(“complete.xqy”)};
  • 15. declare function feature() { let $filter := xdmp:get-request-field(“filter”) let $input := fn:collection()/data[@attr eq $filter] let $result := businessLogic($input) let $persist := xdmp:document-insert(“/doc.xml”, $result) return xdmp:redirect-response(“complete.xqy”)};
  • 16. declare function (:TEST:) isolate_noUrlNoChannels(){ (: ... :)};
  • 17. <dependency> <groupId>org.lds.marklogic.shared</groupId> <artifactId>xqtest</artifactId> <version>1.0-SNAPSHOT</version> <type>xar</type></dependency>
  • 18. import module namespace unit ="http://lds.org/code/shared/xqtest/html" at"/shared/xqtest/html.xqy";html:test()
  • 19. import module namespace unit ="http://lds.org/code/shared/xqtest/testng" at"/shared/xqtest/testng.xqy";testng:test()
  • 20. <channels> <channel> <name>Dress &amp; Grooming</name> <path>/dress-grooming/</path> <channels> <!-- ... --> <channels> </channel> <!-- ... --></channels>
  • 21. ...test - show only active subchannels - greentest - show only active subchannels - redtest - 1 child level all children - greentest - 1 child level, 2 exist - greentest - 1 child level, 2 exist - redtest - 1 child level - greentest - child level - redtest - get closest url - greentest - get closest url - redtest - match level 2 - greentest - match level 2 - redtest - active chg - match whole tree & match lvl1 partial tree - greentest - active chg - match whole tree & match lvl1 partial tree - redtest - match lvl1 partial tree - redtest - match whole tree - greentest - match whole tree – red...
  • 22. Demoable parametersstyle adjustmentadded stylesheet for sidebaradded demo page, adjusted reverse tree builder to handle queried docstestng runner addedxqtest ver .5 - update to work with new apitest - limit shallow non-hierarchical url - greentest - shallow limit 1 level, 2 exist - greentest - adjust test (only work with hierarchical urls) - shallow …test - shallow limit 1 level, 2 exist - redtest - deep limit 1 level, 2 exist - greentest - adjust test - deep limit 1 level, 2 exist - redtest - limit 1 level, 2 exist - redtest - level 3 active link - greentest - level 3 active link - redtest - level 3 list none active (and fixed level 2 test data) - greentest - level 3 list none active - redcleanup - privatize fnstest - level 2 list none active - greentest - level 2 list none active - redtest - header only multiple level 1 channel - greentest - header only multiple level 1 channel - redtest - header only - greentest - header only - redtest - no channels data - greentest - no channels data - redtest - no channels - greenadded sidebar renderer - no channels test - redrenamed build->render fnadded build fntest - build max child levels of set to more - greentest - no match 3 levels, 2 exist, 2 shown (fixed bug) - greentest - no match 3 levels, 2 exist, 2 shown - redtest - no match level 1, 2 exist - fix - greentest - no match level 1, 2 exist - greentest - no match levels more than exists - greentest - no match level 1 - greentest - no match level 1 - redcleanup long fn signaturestest - show only active subchannels - greentest - show only active subchannels - redtest - 1 child level all children - greentest - 1 child level, 2 exist - greentest - 1 child level, 2 exist - redtest - 1 child level - greentest - child level - redtest - get closest url - greentest - get closest url - redtest - match level 2 - greentest - match level 2 - redtest - active chg - match whole tree & match lvl1 partial tree - greentest - active chg - match whole tree & match lvl1 partial tree - redtest - match lvl1 partial tree - redtest - match whole tree - greentest - match whole tree - redRename isolateChannels -> isolateRename nav -> channelRename nav2 -> navAdded gitignoreInitial import - base test cases (no input)
  • 23. Demoable parametersstyle adjustmentadded stylesheet for sidebaradded demo page, adjusted reverse tree builder to handle queried docstestng runner addedxqtest ver .5 - update to work with new apitest - limit shallow non-hierarchical url - greentest - shallow limit 1 level, 2 exist - greentest - adjust test (only work with hierarchical urls) - shallow …test - shallow limit 1 level, 2 exist - redtest - deep limit 1 level, 2 exist - greentest - adjust test - deep limit 1 level, 2 exist - redtest - limit 1 level, 2 exist - redtest - level 3 active link - greentest - level 3 active link - redtest - level 3 list none active (and fixed level 2 test data) - greentest - level 3 list none active - redcleanup - privatize fnstest - level 2 list none active - greentest - level 2 list none active - redtest - header only multiple level 1 channel - greentest - header only multiple level 1 channel - redtest - header only - greentest - header only - redtest - no channels data - greentest - no channels data - redtest - no channels - greenadded sidebar renderer - no channels test - redrenamed build->render fnadded build fntest - build max child levels of set to more - greentest - no match 3 levels, 2 exist, 2 shown (fixed bug) - greentest - no match 3 levels, 2 exist, 2 shown - redtest - no match level 1, 2 exist - fix - greentest - no match level 1, 2 exist - greentest - no match levels more than exists - greentest - no match level 1 - greentest - no match level 1 - redcleanup long fn signaturestest - show only active subchannels - greentest - show only active subchannels - redtest - 1 child level all children - greentest - 1 child level, 2 exist - greentest - 1 child level, 2 exist - redtest - 1 child level - greentest - child level - redtest - get closest url - greentest - get closest url - redtest - match level 2 - greentest - match level 2 - redtest - active chg - match whole tree & match lvl1 partial tree - greentest - active chg - match whole tree & match lvl1 partial tree - redtest - match lvl1 partial tree - redtest - match whole tree - greentest - match whole tree - redRename isolateChannels -> isolateRename nav -> channelRename nav2 -> navAdded gitignoreInitial import - base test cases (no input)
  • 24. Al l Cre a t ive Co m mons 2.0 At t ribution Li ce n se un l e ss o t herwise n o tedGreen Apple (modified) Hugging Fingers Decoupledhttp://www.flickr.com/photos/_fxr/2335186114/ http://www.flickr.com/photos/ganesha_isis/4439563089/ http://www.flickr.com/photos/daquellamanera/2213902727/_FXRs photostream ganesha.isis photostream Daquella maneras photostreamSalt Lake Temple Fail Whale Water Droplethttp://www.flickr.com/photos/midiman/2503776540/ http://www.flickr.com/photos/coletivomambembe/3882172324/ http://www.flickr.com/photos/shannonkringen/4680455907/midimans photostream Coletivo Mambembes photostream shannonkringens photostreamBelay Lego Death Starhttp://www.flickr.com/photos/picsbycam/5173647461/ http://www.flickr.com/photos/dudeoflego/5104751355/Cameron Cassans photostream Dude of Legos photostreamThinker Road Runnerhttp://www.flickr.com/photos/garysoup/2705102139/ http://www.flickr.com/photos/snowpeak/4815685925/Gary Soups photostream snowpeaks photostreamMultipurpose Tool Keyboard Sleephttp://www.flickr.com/photos/paalb/11772559/ http://www.flickr.com/photos/ytruly/1718468702/Pål Berges photostream YTrulys photostreamClean Up Lotushttp://www.flickr.com/photos/emilyrides/5188289325/lightbox/ http://www.flickr.com/photos/thecarspy/2571799871/in/photostream/emilydickinsonridesabmxs photostream The Car Spys photostreamCliff Neon Pipeshttp://www.flickr.com/photos/elsie/144702426/ http://www.flickr.com/photos/krossbow/3619931451/Elsie esq.s photostream krossbows photostream - CC Attribution licensePaper Pile Calculatorhttp://www.flickr.com/photos/loty/326761635/ http://www.flickr.com/photos/scoobay/2463236523/lotylotys photostream Scoobays photostreamCandles Dragon Boathttp://www.flickr.com/photos/picsbycam/4023646774/in/photostream/ http://www.flickr.com/photos/wiifm69/3410782558/Cameron Cassans photostream wiifms photostreamElephant Ping Ponghttp://www.flickr.com/photos/greenboots76/2924276160/ http://www.flickr.com/photos/ncc_badiey/4932760753/Henry Bretts photostream Nima Badieys photostreamAssembly Line Solitary Confinementhttp://www.flickr.com/photos/jurvetson/5201796697/ http://www.flickr.com/photos/59195512@N00/143521274/jurvetsons photostream dospazs photostreamChihuahua Flagshttp://www.flickr.com/photos/62337512@N00/3727104807/ http://www.flickr.com/photos/markchapmanphoto/5138832677/apdks photostream ::: Radar Communication :::s photostreamAngry Dog Pointing Caterpillarhttp://www.flickr.com/photos/javiercito/2083111412/lightbox/ http://www.flickr.com/photos/pinkstockphotos/5266977224/Javiercit0s photostream PinkStock Photos!s photostreamClean up Pet Multiplicationhttp://www.flickr.com/photos/pointshoot/1144799868/ http://www.flickr.com/photos/somegeekintn/4048155967/Eddie~S photostream somegeekintns photostream
  • 25. Pig Egg Safety Net Mexican Hat Rock Formationhttp://www.flickr.com/photos/katerha/5423107783/ http://www.flickr.com/photos/iamchadhearmesnore/3629410556/ http://www.flickr.com/photos/wolfgangstaudt/4879456199/in/set-katerhas photostream iamchads photostream 72157624891093384 Wolfgang Staudts photostreamSpaghetti Noodle Black Boxhttp://www.flickr.com/photos/pinksherbet/2201247567/ http://www.flickr.com/photos/amagill/34762677/ Red Apple (modified)Pink Sherbet Photographys photostream AMagills photostream http://www.flickr.com/photos/emzee/273289101/ *Mickys photostreamGirl Eating Spaghetti Tool Belthttp://www.flickr.com/photos/pinksherbet/5008273559/ http://www.flickr.com/photos/blue_diamond/3995257652/ Kodak RollPink Sherbet Photographys photostream Blue Diamond Photographys photostream http://www.flickr.com/photos/picsbycam/4550970565/in/photostream/ Cameron Cassans photostreamSalute Flag The Swedehttp://www.flickr.com/photos/soldiersmediacenter/3920702814/ By David Quinn, used with permission Gnuolane Free fontThe U.S. Armys photostream http://typodermicfonts.com Garden Gnome Free for personal and commercial useCamoflauge Lizard http://www.flickr.com/photos/mukluk/196177233/http://www.flickr.com/photos/lofink/4453281926/ Danos photostreamdlofinks photostream BlueprintCall on Beach http://www.flickr.com/photos/wscullin/3770016707/http://www.flickr.com/photos/zitona/2268296925/ Will Scullins photostream» Zitona «s photostream Flux CapacitorIndian Chief http://www.flickr.com/photos/popculturegeek/4533455371/http://www.flickr.com/photos/boston_public_library/4100786887/in/photo popculturegeek.coms photostreamstream/Boston Public Librarys photostream Power Plug http://www.flickr.com/photos/randomurl/438112948/Ninja Turtle Zevotrons photostreamhttp://www.flickr.com/photos/86805724@N00/1581833575/ianmalcms photostream Runner http://www.flickr.com/photos/lululemonathletica/5197327623/lightbox/Phone Booth lululemon athleticas photostreamhttp://www.flickr.com/photos/bexwalton/4204409348/Bex.Waltons photostream Yoga Man http://www.flickr.com/photos/lululemonathletica/4774517755/Suit lululemon athleticas photostreamhttp://www.flickr.com/photos/pss/4830803189/Paul Stevensons photostream Lego City Men http://www.flickr.com/photos/mac_filko/5491561002/Underwater mac_filkos photostreamhttp://www.flickr.com/photos/andreweick/4000567498/in/photostream/AndrewEicks photostream Port Said http://www.flickr.com/photos/48722974@N07/5036484008/Picasso eutrophication&hypoxias photostreamhttp://www.flickr.com/photos/oddsock/101164507/oddsocks photostream Vote for Pedro http://www.flickr.com/photos/emdot/79863648/Spider Tunnel emdots photostreamhttp://www.flickr.com/photos/b1gw1ght/900924265/b1gw1ghts photostream Dusty Road http://www.flickr.com/photos/generated/5554654197/ jareds photostream