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