server_data_mapper_test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 Ember = require('ember');
  19. var App = require('app');
  20. require('mappers/server_data_mapper');
  21. describe('App.QuickDataMapper', function () {
  22. var test_json = {
  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. describe('#getJsonProperty', function() {
  46. var tests = [
  47. {i:'a1.b1.c1',e:'val1'},
  48. {i:'a1.b2',e:'val2'},
  49. {i:'a2',e:'val3'},
  50. {i:'a1.b3[0].c2',e:'val4'},
  51. {i:'a1.b3[1].c2',e:'val5'}
  52. ];
  53. tests.forEach(function(test) {
  54. it(test.i, function() {
  55. var mapper = App.QuickDataMapper.create();
  56. expect(mapper.getJsonProperty(test_json, test.i)).to.equal(test.e);
  57. });
  58. });
  59. });
  60. describe('#parseIt', function() {
  61. var config = {
  62. $a2: 'a2',
  63. f1: 'a1.b1.c1',
  64. f2: 'a1.b3[0].c2',
  65. f3: 'a1.b3',
  66. f4_key: 'a1.b3',
  67. f4_type: 'array',
  68. f4: {
  69. item: 'c2'
  70. },
  71. f5: 'item.["key.dotted"]'
  72. };
  73. var mapper = App.QuickDataMapper.create();
  74. var result = mapper.parseIt(test_json, config);
  75. it('Property starts with $', function() {
  76. expect(result.a2).to.equal('a2');
  77. });
  78. it('Multi-components path', function() {
  79. expect(result.f1).to.equal('val1');
  80. });
  81. it('Path with array index', function() {
  82. expect(result.f2).to.equal('val4');
  83. });
  84. it('Path returns array', function() {
  85. expect(result.f3.length).to.equal(3);
  86. });
  87. it('Generate array of json fields', function() {
  88. expect(result.f4).to.eql(['val1','val4','val5']);
  89. });
  90. it('Check value with dotted key', function() {
  91. expect(result.f5).to.eql('val6');
  92. });
  93. });
  94. });