File: test_faker_zip_code.rb

package info (click to toggle)
ruby-faker 2.21.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,076 kB
  • sloc: ruby: 19,088; makefile: 6
file content (66 lines) | stat: -rw-r--r-- 1,713 bytes parent folder | download | duplicates (2)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

require_relative '../../test_helper'

class TestFakerZipCode < Test::Unit::TestCase
  def setup
    @zip_codes_without_state = %w[50817 48666 55551 14242 99852]
    @zip_codes_with_state = %w[55555 44444 33333 22222 11111]

    @old_locales = I18n.config.available_locales

    locale_without_state = {
      faker: {
        address: {
          state_abbreviation: [''],
          postcode: @zip_codes_without_state
        }
      }
    }

    locale_with_state = {
      faker: {
        address: {
          postcode_by_state: {
            NY: @zip_codes_with_state
          }
        }
      }
    }

    I18n.config.available_locales += %i[xy xz]
    I18n.backend.store_translations(:xy, locale_without_state)
    I18n.backend.store_translations(:xz, locale_with_state)
    @tester = Faker::Address
  end

  def teardown
    I18n.config.available_locales = @old_locales
  end

  def test_zip_code_can_have_leading_zero
    zip_codes = []
    1000.times { zip_codes << @tester.zip_code }
    assert zip_codes.any? { |zip_code| zip_code[0].to_i.zero? }
  end

  def test_default_zip_codes_without_states
    I18n.with_locale(:xy) do
      zip_codes = @zip_codes_without_state
      100.times do
        zip_code = @tester.zip_code
        assert zip_codes.include?(zip_code), "Expected <#{zip_codes.join(' / ')}>, but got #{zip_code}"
      end
    end
  end

  def test_zip_codes_with_states
    I18n.with_locale(:xz) do
      zip_codes = @zip_codes_with_state
      100.times do
        zip_code = @tester.zip_code(state_abbreviation: 'NY')
        assert zip_codes.include?(zip_code), "Expected <#{zip_codes.join(' / ')}>, but got #{zip_code}"
      end
    end
  end
end