File: test_faker_json.rb

package info (click to toggle)
ruby-faker 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,596 kB
  • sloc: ruby: 20,656; sh: 6; makefile: 6
file content (43 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

require_relative '../../test_helper'

class TestFakerJson < Test::Unit::TestCase
  require 'json'

  def test_simple_json_lenght
    expected_result_length = JSON.parse(simple_json).flatten.length

    assert_same(expected_result_length, 6)
  end

  def test_nested_json_length
    result = Faker::Json.add_depth_to_json(json: simple_json, options: { key: 'Name.first_name', value: 'Name.first_name' })

    first_json_length = JSON.parse(result).flatten[1].flatten.length
    second_json_length = JSON.parse(result).flatten[3].flatten.length
    third_json_length = JSON.parse(result).flatten[5].flatten.length

    assert_same(first_json_length, 6)
    assert_same(second_json_length, 6)
    assert_same(third_json_length, 6)
  end

  def test_nested_json_width_length
    json = Faker::Json.add_depth_to_json(json: nested_json, width: 3, options: { key: 'Name.first_name', value: 'Name.first_name' })

    first_json_length = JSON.parse(json).flatten[1].flatten[1].flatten.length
    second_json_length = JSON.parse(json).flatten[3].flatten[3].flatten.length

    assert_same(first_json_length, 6)
    assert_same(second_json_length, 6)
  end

  def simple_json
    Faker::Json.shallow_json(options: { key: 'Name.first_name', value: 'Name.first_name' })
  end

  def nested_json
    Faker::Json.add_depth_to_json(json: simple_json, width: 3, options: { key: 'Name.first_name', value: 'Name.first_name' })
  end
end