cirandas.net

ref: master

test/unit/set_profile_region_from_city_state_test.rb


 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
require_relative "../test_helper"

class SetProfileRegionFromCityStateTest < ActiveSupport::TestCase

  should 'set city and state from names' do
    c, _ = create_city_in_state('Pindamonhangaba', 'Sao Paulo', 'SP')

    p = fast_create(Person, :user_id => fast_create(User).id)
    p.state_with_region = 'SP'
    p.city_with_region = 'Pindamonhangaba'
    p.save!
    assert p.region == c
  end

  should 'set region to null if city not found' do
    create_city_in_state(nil, 'Sao Paulo', 'SP')

    p = fast_create(Person, :user_id => fast_create(User).id)
    p.state_with_region = 'SP'
    p.city_with_region = 'Pindamonhangaba'
    p.save!
    assert p.region.nil?
  end

  should 'set region to null if state not found' do
    create_city_in_state('Pindamonhangaba', 'Sao Paulo', 'SP')

    p = fast_create(Person, :user_id => fast_create(User).id)
    p.state_with_region = 'RJ'
    p.city_with_region = 'Pindamonhangaba'
    p.save!
    assert p.region.nil?
  end

  def create_city_in_state(city_name, state_name, state_acronym)
    environment_id = Environment.default.id

    state = State.new(:name => state_name, :acronym => state_acronym)
    state.environment_id = environment_id
    state.save!

    city = nil
    if city_name
      city = City.new(:name => city_name, :parent_id => state.id)
      city.environment_id = environment_id
      city.save!
    end

    return [city, state]
  end

end