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