unittests/tests/serializers/JSONSerializer.js
branchnew-model
changeset 924 64c2eaafe5e2
parent 923 b3ee7d1b472a
child 925 28efc97b5d78
equal deleted inserted replaced
923:b3ee7d1b472a 924:64c2eaafe5e2
     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     
       
   141     test("test current annotation search", function() {
       
   142       var ser = new IriSP.JSONSerializer(this.dt, "../test/test.json");      
       
   143 
       
   144       ser._data = { 
       
   145       "views": [
       
   146           {
       
   147             "id": "0", 
       
   148             "contents": [
       
   149               "franceculture_retourdudimanche20100620"
       
   150             ], 
       
   151             "annotation_types": [
       
   152               "c_1F07824B-F512-78A9-49DB-6FB51DAB9560"
       
   153             ]
       
   154           }
       
   155         ], 
       
   156           annotations : [
       
   157       {"begin": 1234, "end" : 578900,
       
   158        "content": {        
       
   159         "description": "professeur", 
       
   160         "title": "garrigou"
       
   161         }, 
       
   162       "id" : 1,
       
   163       "meta": {
       
   164         "dc:contributor": "perso", 
       
   165         "id-ref": "c_1F07824B-F512-78A9-49DB-6FB51DAB9560", 
       
   166         "dc:created": "2011-10-20T13:36:18.286693", 
       
   167         "dc:modified": "2011-10-20T13:36:18.286693", 
       
   168         "dc:creator": "perso"
       
   169         } 
       
   170       }, 
       
   171       {"begin": 1234, "end" : 578900,
       
   172        "content": {        
       
   173         "description": "interview", 
       
   174         "title": "Revue de presse - Hervé Gardette"
       
   175         }, 
       
   176        "id" : 2, 
       
   177        "meta": {
       
   178         "dc:contributor": "perso", 
       
   179         "id-ref": "c_1F07824B-F512-78A9-49DB-6FB51DAB9560", 
       
   180         "dc:created": "2011-10-20T13:36:18.286693", 
       
   181         "dc:modified": "2011-10-20T13:36:18.286693", 
       
   182         "dc:creator": "perso"
       
   183         } 
       
   184       },
       
   185       {"begin": 1234, "end" : 578900,
       
   186        "content": {        
       
   187         "description": "interview", 
       
   188         "title": "lolol"
       
   189         }, 
       
   190        "id" : 2, 
       
   191        "meta": {
       
   192         "dc:contributor": "perso", 
       
   193         "id-ref": "c_dfdfdfdf", 
       
   194         "dc:created": "2011-10-20T13:36:18.286693", 
       
   195         "dc:modified": "2011-10-20T13:36:18.286693", 
       
   196         "dc:creator": "perso"
       
   197         } 
       
   198       }
       
   199  
       
   200       ]};
       
   201 
       
   202       var ret = ser.currentAnnotations(234);  
       
   203       equal(ret.length, 2, "the correct number of elements is returned");
       
   204       ok(ret[0].begin < 234 * 1000 && ret[0].end > 234 * 1000 && 
       
   205          ret[0].meta["id-ref"] == "c_1F07824B-F512-78A9-49DB-6FB51DAB9560",
       
   206          "the first element is correctly configured");
       
   207 
       
   208     });
       
   209  
       
   210 };