unittests/tests/serializers/JSONSerializer.js
branchpopcorn-port
changeset 234 43b198dc932d
child 317 a3492448fa9a
equal deleted inserted replaced
233:126de77ee73e 234:43b198dc932d
       
     1 function test_JSONSerializer() {
       
     2   module("JSON Serializer tests", 
       
     3     { setup: function() {      
       
     4       this.dt = new IriSP.DataLoader();
       
     5       }
       
     6     }
       
     7     );    
       
     8     
       
     9     test("should return the correct JSON", function() {
       
    10       var arr = ["ab", {"de" : "fg"}, "lp"];
       
    11       var serializer = new IriSP.JSONSerializer(this.dt);
       
    12 
       
    13       equal(serializer.serialize(arr), JSON.stringify(arr), "assert that the outputted json is correct");
       
    14     });
       
    15     
       
    16     test("sync()", function() {
       
    17       this.xhr = this.sandbox.useFakeXMLHttpRequest();
       
    18       this.requests = [];
       
    19       this.xhr.onCreate = function (request) {
       
    20         this.requests.push(request);
       
    21       };
       
    22       
       
    23       var response_array = { media: 12, content: "Hey there", 
       
    24                              annotations: [{"begin": "32", "end" : 64}, {"begin": "08", "end" : 27},{"begin": "02", "end" : 61}]  };
       
    25 
       
    26       /* sorted array is our comparision array */
       
    27       var sorted_array = IriSP.jQuery.extend({}, response_array);
       
    28       sorted_array.annotations.sort(function(a, b) 
       
    29           { var a_begin = +a.begin;
       
    30             var b_begin = +b.begin;
       
    31             return a_begin - b_begin;
       
    32           });
       
    33           
       
    34       var response_string = JSON.stringify(response_array);
       
    35   
       
    36       var spy_callback = this.spy();
       
    37       var ser = new IriSP.JSONSerializer(this.dt, "/url");
       
    38       
       
    39       ser.sync(spy_callback);
       
    40       
       
    41       equals(this.xhr.requests.length, 1, "the mock ajax object should have received the request");
       
    42       equals(this.xhr.requests[0].url, "/url", "the requested url is correct");
       
    43 
       
    44       this.xhr.requests[0].respond(200, { "Content-Type": "application/json" },
       
    45                              response_string);
       
    46         
       
    47       ok(spy_callback.calledOnce, "callback called");
       
    48       ok(spy_callback.calledWith(response_array), "callback called with correct value");
       
    49       deepEqual(ser._data, response_array, "the internal variable is initialized to the correct value");
       
    50       
       
    51       var order_preserved = true;
       
    52       
       
    53       var i = 0;
       
    54       for(i = 0; i < ser._data.length - 1; i++) {
       
    55         if (ser._data.annotations[i].begin > ser._data.annotations[i+1].begin) {
       
    56             order_preserved = false;
       
    57             break;
       
    58         }
       
    59       }
       
    60       
       
    61       ok(order_preserved, "the annotation sub-array is sorted by begin time");
       
    62     });
       
    63 
       
    64     test("currentMedia should return the current media", function() {
       
    65       var ser = new IriSP.JSONSerializer(this.dt, "/url");
       
    66 
       
    67       ser._data = {}
       
    68       ser._data.medias = [0];
       
    69       equal(ser.currentMedia(), 0, "currentMedia() returns the correct value");
       
    70     });
       
    71 
       
    72     test("test annotation search", function() {
       
    73       var ser = new IriSP.JSONSerializer(this.dt, "../test/test.json");
       
    74             
       
    75       ser._data = { annotations : [
       
    76       {"content": {        
       
    77         "description": "professeur", 
       
    78         "title": "garrigou"
       
    79         }},
       
    80       { "content": {        
       
    81         "description": "interview", 
       
    82         "title": "Revue de presse - Hervé Gardette"
       
    83       }},
       
    84       {"content": {        
       
    85         "description": "concept", 
       
    86         "title": "idée"
       
    87       }},
       
    88       { "content": {        
       
    89         "description": "", 
       
    90         "title": "sans titre"
       
    91       }}
       
    92       ]};
       
    93       
       
    94       equal(ser.searchAnnotations("GarriGOU", "", "").length, 1, "requesting on title works");
       
    95       equal(ser.searchAnnotations("", "IntErView", "").length, 1, "requesting on description works");      
       
    96       equal(ser.searchAnnotations("", "", "").length, 4, "empty request works");
       
    97       equal(ser.searchAnnotations("idée", "concept", "").length, 1, "specific request works");
       
    98       
       
    99       
       
   100     });
       
   101     
       
   102     test("test occurence count", function() {
       
   103     var ser = new IriSP.JSONSerializer(this.dt, "../test/test.json");
       
   104             
       
   105       ser._data = { annotations : [
       
   106       {"content": {        
       
   107         "description": "professeur", 
       
   108         "title": "garrigou"
       
   109         }, "id" : 1 },
       
   110       { "content": {        
       
   111         "description": "interview", 
       
   112         "title": "Revue de presse - Hervé Gardette"
       
   113       }, "id" : 2},
       
   114       {"content": {        
       
   115         "description": "concept", 
       
   116         "title": "idée"
       
   117       }, "id" : 3},
       
   118       { "content": {        
       
   119         "description": "", 
       
   120         "title": "sans titre"
       
   121       }, "id" : 4}
       
   122       ]};
       
   123       
       
   124       // a function to get the number of fields in a dict.
       
   125       function countOccurences(queryString) {
       
   126         var count = 0;
       
   127         for (var i in ser.searchOccurences(queryString)) {
       
   128           count++;
       
   129         };
       
   130         
       
   131         return count;
       
   132       };
       
   133       
       
   134       equal(countOccurences("garrigou"), 1, "first request works");
       
   135       deepEqual(ser.searchOccurences("garrigou"), {1 : 1}, "returned object is correctly defined");
       
   136           
       
   137       equal(countOccurences("garrigou interview"), 2, "second request works");
       
   138       equal(countOccurences("garrigou idée interview"), 3, "third request works");
       
   139     });
       
   140 };