I'm new to automated testing, actually it's the first time I'm writing any kind of automated test. I have a function which creates deltas for/from two JSON Objects. I know the function works the way I want it to because I tested it on my own. If I do a test prior to calling the comparison function the deltas should be empty, once the comparison function is called they should not be empty. Fine, that's one test that does nothing for me. How do I write a test to validate the data inside the deltas? I guess in the end my question is the variables which I need for my test are located inside a function within my object, is there anyway to access those variables for testing, when debugging we're able to see inside those variables, does testing work the same way?
class JsonCompare(jsonBefore: JsValue, jsonAfter: JsValue) {
var deltaBefore = scala.collection.mutable.Map[String, Any]()
var deltaAfter = scala.collection.mutable.Map[String, Any]()
//Original json object
var jsonAMap:Map[String, Any] = Map()
val jsonAString: String = Json.stringify(jsonBefore)
jsonAMap = jsonStrToMap(jsonAString)
//Changed json object
var jsonBMap:Map[String, Any] = Map()
val jsonBString: String = Json.stringify(jsonAfter)
jsonBMap = jsonStrToMap(jsonBString)
implicit val formats = org.json4s.DefaultFormats
compareJson(jsonAMap, jsonBMap, deltaBefore, deltaAfter)
//convert json to Map using json4s library
def jsonStrToMap(jsonStr: String): Map[String, Any] = {
implicit val formats = org.json4s.DefaultFormats
parse(jsonStr).extract[Map[String, Any]]
}
def compareJson(before: Map[String,Any], after: Map[String,Any], deltaBefore: scala.collection.mutable.Map[String, Any], deltaAfter: scala.collection.mutable.Map[String, Any], rKey: String="") {
var rdeltaAfter = scala.collection.mutable.Map[String, Any]()
var rdeltaBefore = scala.collection.mutable.Map[String, Any]()
//Maps which hold kvs with potentially different values
var beforeComparisonMap:Map[String, Any] = Map()
var afterComparisonMap:Map[String, Any] = Map()
//sets to hold the keys from the json maps (before & after)
var beforeKeys:Set[String] = Set()
var afterKeys:Set[String] = Set()
// json DiffKeys is the set of keys containing the added/deleted keys
var jsonDiffKeys:Set[String] = Set()
//extract the keys from both json maps (A & B)
beforeKeys = before.keySet
afterKeys = after.keySet
//Symmetric difference of before & after, getting both elements that are in before not in after and vice versa.
jsonDiffKeys = beforeKeys diff afterKeys
jsonDiffKeys ++= afterKeys diff beforeKeys
//if there are differences, investigate further
if(!jsonDiffKeys.isEmpty){
var droppedKeys:Set[String] = Set()
var addedKeys:Set[String] = Set()
droppedKeys = beforeKeys -- afterKeys
addedKeys = afterKeys -- beforeKeys
//if there are keys in the droppedKeys set, keys that are in Before are not in After means some keys were dropped from Before
if(!droppedKeys.isEmpty){
//A new map of Before not containing the dropped keys (needed for comparison)
beforeComparisonMap = before -- droppedKeys
//add the deleted keys and values from Before to the beforeDelta
for(droppedKey <- droppedKeys){
if(rKey != ""){
rdeltaBefore += (droppedKey -> (before get droppedKey))
}else{
deltaBefore += (droppedKey -> (before get droppedKey))
}
}
}else{
beforeComparisonMap = before
}
//if there are keys in the addedKeys set, keys that are in B are not in A means some keys were added to B
if(!addedKeys.isEmpty){
//A new map of B not containing the added keys (needed for comparison)
afterComparisonMap = after -- addedKeys
//add the added keys and values from B to delta map, append "added" to value
for(addedKey <- addedKeys){
if(rKey != ""){
rdeltaAfter += (addedKey -> (after get addedKey))
}else{
deltaAfter += (addedKey -> (after get addedKey))
}
}
}else{
afterComparisonMap = after
}
}else{
//if there were no dropped or added keys keep the same map for comparison.
beforeComparisonMap = before
afterComparisonMap = after
}
//extract the values from A & B
var beforeVals = Iterator[Any]()
var afterVals = Iterator[Any]()
var bKeys = Iterator[Any]()
var sameKeyList = new scala.collection.mutable.ArrayBuffer[Any]()
var beforeValsList = new scala.collection.mutable.ArrayBuffer[Any]()
var afterValsList = new scala.collection.mutable.ArrayBuffer[Any]()
beforeVals = beforeComparisonMap.valuesIterator
afterVals = afterComparisonMap.valuesIterator
bKeys = afterComparisonMap.keysIterator
//need to convert to ArrayBuffer because an Iterator can only be iterated through once, calling the .size or .length method
//empties an iterator
while(beforeVals.hasNext){
beforeValsList.append(beforeVals.next())
}
while(afterVals.hasNext){
afterValsList.append(afterVals.next())
}
while(bKeys.hasNext){
sameKeyList.append(bKeys.next())
}
if(beforeValsList.length == afterValsList.length){
if(rKey != ""){
for(i <- 0 to beforeValsList.length - 1){
beforeValsList(i) match{
case _ : Map[_,_] =>{ compareJson(beforeValsList(i).asInstanceOf[Map[String,Any]], afterValsList(i).asInstanceOf[Map[String,Any]], rdeltaBefore, rdeltaAfter, sameKeyList(i).toString()) }
case _ if (beforeValsList(i) != afterValsList(i)) => {
//if i'm from a recursion, build a new map and add me to the deltas as a key->value pair
rdeltaBefore += (sameKeyList(i).toString -> beforeValsList(i))
rdeltaAfter += (sameKeyList(i).toString -> afterValsList(i))
}
case _ => println("catch all: " + beforeValsList(i).toString + " "+ afterValsList(i).toString)
}
}//end for
deltaBefore += (rKey -> rdeltaBefore)
deltaAfter += (rKey -> rdeltaAfter)
}else{
for(i <- 0 to beforeValsList.length - 1){
beforeValsList(i) match{
case _ : Map[_,_] =>{ compareJson(beforeValsList(i).asInstanceOf[Map[String,Any]], afterValsList(i).asInstanceOf[Map[String,Any]], deltaBefore, deltaAfter, sameKeyList(i).toString()) }
case _ if (beforeValsList(i) != afterValsList(i)) => {
//if i'm from a recursion, build a new map and add me to the deltas as a key->value pair
deltaBefore += (sameKeyList(i).toString -> beforeValsList(i))
deltaAfter += (sameKeyList(i).toString -> afterValsList(i))
}
case _ => println("catch all: " + beforeValsList(i).toString + " "+ afterValsList(i).toString)
}
}
}
}
}//end of compare json
//end of class
}
For example is there anyway for me to access dropped keys and added keys (in the code above) other than declaring those variables outside the function and passing them into the function, just so I can access them in my test?
test("New JsonCompariosn Object has empty deltas") {
var bSize = jsc.deltaBefore.size
assert(bSize == 0)
var aSize = jsc.deltaAfter.size
assert(aSize == 0)
}
Can someone could point me in the correct way of setting up my class so I can test properly.
Aucun commentaire:
Enregistrer un commentaire