server_data_mapper_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var mapper;
  20. require('mappers/server_data_mapper');
  21. describe('App.QuickDataMapper', function () {
  22. var testJson = {
  23. a1: {
  24. b1: {
  25. c1: 'val1'
  26. },
  27. b2: 'val2',
  28. b3: [
  29. {
  30. c2: 'val4'
  31. },
  32. {
  33. c2: 'val5'
  34. },
  35. {
  36. c2: 'val1'
  37. }
  38. ]
  39. },
  40. a2: 'val3',
  41. item: {
  42. 'key.dotted': 'val6'
  43. }
  44. };
  45. beforeEach(function () {
  46. mapper = App.QuickDataMapper.create();
  47. });
  48. describe('#getJsonProperty', function() {
  49. var tests = [
  50. {i:'a1.b1.c1',e:'val1'},
  51. {i:'a1.b2',e:'val2'},
  52. {i:'a2',e:'val3'},
  53. {i:'a1.b3[0].c2',e:'val4'},
  54. {i:'a1.b3[1].c2',e:'val5'}
  55. ];
  56. tests.forEach(function(test) {
  57. it(test.i, function() {
  58. expect(mapper.getJsonProperty(testJson, test.i)).to.equal(test.e);
  59. });
  60. });
  61. });
  62. describe('#parseIt', function() {
  63. var config = {
  64. $a2: 'a2',
  65. f1: 'a1.b1.c1',
  66. f2: 'a1.b3[0].c2',
  67. f3: 'a1.b3',
  68. f4_key: 'a1.b3',
  69. f4_type: 'array',
  70. f4: {
  71. item: 'c2'
  72. }
  73. };
  74. var result;
  75. beforeEach(function () {
  76. result = mapper.parseIt(testJson, config);
  77. });
  78. it('Property starts with $', function() {
  79. expect(result.a2).to.equal('a2');
  80. });
  81. it('Multi-components path', function() {
  82. expect(result.f1).to.equal('val1');
  83. });
  84. it('Path with array index', function() {
  85. expect(result.f2).to.equal('val4');
  86. });
  87. it('Path returns array', function() {
  88. expect(result.f3.length).to.equal(3);
  89. });
  90. it('Generate array of json fields', function() {
  91. expect(result.f4).to.eql(['val1','val4','val5']);
  92. });
  93. });
  94. describe('#binaryIndexOf', function () {
  95. var array1 = [1,2,3,4,5,6,7,8,9];
  96. var array2 = ['b','c','d','e','f','g'];
  97. array1.forEach(function(item, index) {
  98. it('numeric array. test ' + (index + 1), function () {
  99. expect(mapper.binaryIndexOf(array1, item)).to.equal(index);
  100. });
  101. });
  102. it('numeric array. element doesn\'t exists', function () {
  103. expect(mapper.binaryIndexOf(array1, 0)).to.be.below(0);
  104. });
  105. it('numeric array. element doesn\'t exists 2', function () {
  106. expect(mapper.binaryIndexOf(array1, 10)).to.be.below(0);
  107. });
  108. array2.forEach(function(item, index) {
  109. it('string array. test ' + (index + 1), function () {
  110. expect(mapper.binaryIndexOf(array2, item)).to.equal(index);
  111. });
  112. });
  113. it('string array. element doesn\'t exists', function () {
  114. expect(mapper.binaryIndexOf(array2, 'a')).to.be.below(0);
  115. });
  116. it('string array. element doesn\'t exists 2', function () {
  117. expect(mapper.binaryIndexOf(array2, 'q')).to.be.below(0);
  118. });
  119. });
  120. });