Created
November 27, 2011 05:15
-
-
Save fmpwizard/1397007 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fmpwizard.weyesowl.code | |
package snippet | |
/** | |
* Created by IntelliJ IDEA. | |
* User: Diego Medina | |
* Date: 11/20/11 | |
* Time: 4:15 PM | |
*/ | |
import org.specs2._ | |
import org.specs2.mock._ | |
import lib.search.DoSearch | |
import net.liftweb.json._ | |
import net.liftweb.common.{Full, Box} | |
import net.liftweb.http.S | |
import net.liftweb.mockweb.MockWeb._ | |
import specification.Step | |
import net.liftweb.mapper.{Schemifier, Like} | |
import model._ | |
class InventorySearchTestSpec extends Specification with Mockito { def is = | |
"Unit test for InventorySearch" ^ | |
//It runs the boot method, like jetty does on a real running application | |
Step(initBoot) ^ | |
// deletes and recreates my tables | |
Step(cleanDb) ^ | |
// fills in some dummy data | |
Step(addProduct) ^ | |
p^ | |
"InventorySearch should" ^ | |
"Start with a blank result page" ! blankResult^ | |
"Show 'No Results found.' message" ! noResultSpec^ | |
"Show a list of items on the result page" ! listResultSpec^ | |
end | |
//tests the default render method | |
def blankResult = { | |
val snippet= new InventorySearch | |
snippet.results(html) must beEqualToIgnoringSpace(blankHtml) | |
} | |
def noResultSpec = { | |
// see http://www.assembla.com/spaces/liftweb/wiki/Mocking_HTTP_Requests for how to simulate a session | |
// you basically wrap your test code in a testS ... call and the snippet behaves as if you are hitting that url | |
testS("http://foo.com/inventorysearch?p=no+valid") { | |
//use mockito to mock what my elasticsearch instance would return | |
val mockedSearch: DoSearch = mock[DoSearch] | |
mockedSearch.queryElasticSearch(any[Box[String]]) returns jsonEmptyResult | |
val inventorySearch = new InventorySearch { | |
// here we are passing our mocked search instance | |
override def newDoSearch = mockedSearch | |
} | |
// using specs2's xml path matcher | |
inventorySearch.results(html) must \\(<h5 id="description">No Results Found.</h5>) | |
} | |
} | |
def listResultSpec = { | |
testS("http://foo.com/inventorysearch?p=ibm") { | |
val mockedSearch: DoSearch = mock[DoSearch] | |
mockedSearch.queryElasticSearch(any[Box[String]]) returns jsonListResult | |
val inventorySearch = new InventorySearch { | |
override def newDoSearch = mockedSearch | |
} | |
inventorySearch.results(html) must \\(<h5 id="description">No Results Found.</h5>) | |
} | |
} | |
def initBoot = { | |
import bootstrap.liftweb._ | |
val boot= new Boot | |
boot.boot | |
} | |
def cleanDb = { | |
//delete everything | |
Schemifier.destroyTables_!!(Schemifier.infoF _, Inventory, InventoryDescription, | |
Categories, SubCategories, InventoryImages, InventoryOptionFields, InventoryPrice, InventoryStock, | |
InventoryXInventoryRelated, OptionFields/*, OptionFieldNames*/) | |
// create everything | |
Schemifier.schemify(true, Schemifier.infoF _, Inventory, InventoryDescription, | |
Categories, SubCategories, InventoryImages, InventoryOptionFields, InventoryPrice, InventoryStock, | |
InventoryXInventoryRelated, OptionFields/*, OptionFieldNames*/) | |
} | |
def addCategories = { | |
val cat= Categories.create | |
cat.category_name("Power") | |
cat.language("EN") | |
cat.sort_order(1) | |
cat.save | |
cat | |
} | |
//add two products | |
def addProduct = { | |
val product1= Inventory.create | |
product1.part_number("02k7018") | |
val desc= InventoryDescription.create | |
desc.text("Ac Adapter for IBM laptops") | |
desc.language("EN") | |
product1.description += desc | |
val cat= addCategories | |
product1.category(cat) | |
product1.save | |
val product2= Inventory.create | |
product2.part_number("02k7019") | |
product2.description += desc | |
product2.category(cat) | |
product2.save | |
} | |
val html= | |
<div class="row"> | |
<div class="span19"> | |
<div class="span2"> | |
<a href="#" id="item_link"> | |
<img class="thumbnail" src="images/90x90.gif" alt="" /> | |
</a> | |
</div> | |
<div class="span16"> | |
<div class="span2"> | |
<h4 id="part_number">02K7018</h4> | |
</div> | |
<div class="span7"> | |
<h5 id="description">Ac Adapter for IBM Laptops</h5> | |
</div> | |
<div class="span3"> | |
<h5 id="category">Power</h5> | |
</div> | |
<div class="span3"> | |
<h5 id="subcategory">Ac Adapter</h5> | |
</div> | |
</div> | |
</div> | |
</div> | |
val blankHtml= <div class="row"> | |
<div class="span19"> | |
<div class="span2"> | |
<a href="#" id="item_link"> | |
</a> | |
</div> | |
<div class="span16"> | |
<div class="span2"> | |
<h4 id="part_number"></h4> | |
</div> | |
<div class="span7"> | |
<h5 id="description"></h5> | |
</div> | |
<div class="span3"> | |
<h5 id="category"></h5> | |
</div> | |
<div class="span3"> | |
<h5 id="subcategory"></h5> | |
</div> | |
</div> | |
</div> | |
</div> | |
val jsonListResult= Full(parse( | |
"""{ | |
"took" : 282, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 0.7887809, | |
"hits" : [ { | |
"_index" : "inventory", | |
"_type" : "description", | |
"_id" : "1", | |
"_score" : 0.7887809, | |
"fields" : { | |
"description" : "Ac Adapter for IBM" | |
} | |
}, { | |
"_index" : "inventory", | |
"_type" : "description", | |
"_id" : "2", | |
"_score" : 0.7036324, | |
"fields" : { | |
"description" : "Battery for IBM laprop" | |
} | |
} ] | |
} | |
}""" | |
)) | |
val jsonEmptyResult= Full(parse( | |
"""{ | |
"took" : 3, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 0, | |
"max_score" : null, | |
"hits" : [ ] | |
} | |
}""" | |
)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment