/javascripts/lib/test/unit/data/JsonReader.js

https://bitbucket.org/ksokmesa/sina-asian · JavaScript · 171 lines · 162 code · 3 blank · 6 comment · 0 complexity · 2f0e90ab15a540ddb906ad1248ded974 MD5 · raw file

  1. /*!
  2. * Ext JS Library 3.2.1
  3. * Copyright(c) 2006-2010 Ext JS, Inc.
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. var suite = Ext.test.session.getSuite('JsonReader');
  8. suite.add(new Y.Test.Case({
  9. name: 'buildExtractors',
  10. setUp: function() {
  11. this.reader = new Ext.data.JsonReader({
  12. root: 'data',
  13. idProperty: 'id',
  14. totalProperty: 'totalProp',
  15. messageProperty: 'messageProp',
  16. successProperty: 'successProp',
  17. fields: [
  18. {mapping: 'mappy', name: 'inter', type: 'integer'}
  19. ]
  20. });
  21. this.reader.buildExtractors();
  22. },
  23. tearDown: function() {
  24. delete this.reader;
  25. },
  26. test_getTotal: function() {
  27. Y.Assert.areSame(this.reader.getTotal({ totalProp: 500}), 500);
  28. },
  29. test_getSuccess: function() {
  30. Y.Assert.areSame(this.reader.getSuccess({ successProp: false }), false);
  31. },
  32. test_getMessage: function() {
  33. Y.Assert.areSame(this.reader.getMessage({ messageProp: 'Hello' }), 'Hello');
  34. },
  35. test_getRoot: function() {
  36. Y.Assert.areSame(this.reader.getRoot({ data: 'data' }), 'data');
  37. },
  38. test_getId: function() {
  39. Y.Assert.areSame(this.reader.getId({ id: 100 }), 100);
  40. },
  41. test_mapping: function() {
  42. Y.Assert.areSame(this.reader.ef[0]({ mappy: 200 }), 200);
  43. }
  44. }));
  45. suite.add(new Y.Test.Case({
  46. name: 'readRecords',
  47. setUp: function() {
  48. this.reader = new Ext.data.JsonReader({
  49. root: 'data',
  50. idProperty: 'id',
  51. totalProperty: 'totalProp',
  52. messageProperty: 'Hello World',
  53. successProperty: 'successProp',
  54. fields: [
  55. {name: 'id'},
  56. {name: 'floater', type: 'float'},
  57. {name: 'bool', type: 'boolean'},
  58. {name: 'inter', type: 'integer'}
  59. ]
  60. });
  61. this.data1 = {
  62. id: 1,
  63. floater: 1.23,
  64. bool: true,
  65. inter: 8675
  66. };
  67. this.rec1 = this.reader.readRecords({
  68. data: [this.data1],
  69. successProp: true,
  70. totalProp: 2
  71. });
  72. this.rec2 = this.reader.readRecords({
  73. data: [{
  74. id: 2,
  75. floater: 4.56,
  76. bool: false,
  77. inter: 309
  78. }],
  79. successProp: false,
  80. totalProp: 6
  81. });
  82. },
  83. test_tearDown: function() {
  84. delete this.reader;
  85. delete this.data1;
  86. delete this.rec1;
  87. delete this.rec2;
  88. },
  89. test_SuccessProperty: function() {
  90. Y.Assert.areSame(this.rec1.success, true);
  91. Y.Assert.areSame(this.rec2.success, false);
  92. },
  93. test_TotalRecords: function() {
  94. Y.Assert.areSame(this.rec1.totalRecords, 2);
  95. Y.Assert.areSame(this.rec2.totalRecords, 6);
  96. },
  97. test_Records: function() {
  98. Y.Assert.areSame(this.rec1.records[0].data.id, this.data1.id);
  99. Y.Assert.areSame(this.rec1.records[0].data.floater, this.data1.floater);
  100. Y.Assert.areSame(this.rec1.records[0].data.bool, this.data1.bool);
  101. Y.Assert.areSame(this.rec1.records[0].data.inter, this.data1.inter);
  102. }
  103. }));
  104. suite.add(new Y.Test.Case({
  105. name: 'readResponse',
  106. setUp: function() {
  107. this.reader = new Ext.data.JsonReader({
  108. root: 'data',
  109. idProperty: 'id',
  110. totalProperty: 'totalProp',
  111. messageProperty: 'messageProp',
  112. successProperty: 'successProp',
  113. fields: [
  114. {name: 'id'},
  115. {name: 'floater', type: 'float'},
  116. {name: 'bool', type: 'boolean'},
  117. {name: 'inter', type: 'integer'}
  118. ]
  119. });
  120. this.data1 = {
  121. id: 1,
  122. floater: 1.23,
  123. bool: true,
  124. inter: 8675
  125. };
  126. this.rec1 = this.reader.readResponse('read', {
  127. data: [this.data1],
  128. successProp: true,
  129. totalProp: 2,
  130. messageProp: 'Hello'
  131. });
  132. this.rec2 = this.reader.readResponse('read', {
  133. data: [{
  134. id: 2,
  135. floater: 4.56,
  136. bool: false,
  137. inter: 309
  138. }],
  139. successProp: false,
  140. totalProp: 6
  141. });
  142. },
  143. tearDown: function() {
  144. delete this.reader;
  145. delete this.data1;
  146. delete this.rec1;
  147. delete this.rec2;
  148. },
  149. test_SuccessProperty: function() {
  150. Y.Assert.areSame(this.rec1.success, true);
  151. Y.Assert.areSame(this.rec2.success, false);
  152. },
  153. test_Records: function() {
  154. Y.Assert.areSame(this.rec1.data[0].id, this.data1.id);
  155. Y.Assert.areSame(this.rec1.data[0].floater, this.data1.floater);
  156. Y.Assert.areSame(this.rec1.data[0].bool, this.data1.bool);
  157. Y.Assert.areSame(this.rec1.data[0].inter, this.data1.inter);
  158. },
  159. test_ActionProperty: function() {
  160. Y.Assert.areSame(this.rec1.action, 'read');
  161. },
  162. test_MessageProperty: function() {
  163. Y.Assert.areSame(this.rec1.message, 'Hello');
  164. },
  165. test_RawProperty: function() {
  166. Y.Assert.areSame(this.rec1.raw.data[0].id, this.data1.id);
  167. }
  168. }));