aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_postcodes.rb
blob: 3b38784710dc4a045bf458ca7d8cd3d244397e54 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
require 'minitest/autorun'
require_relative '../lib/norwegian-postcodes'

describe PostCodes::PostCode do
  before do
    @postcode = PostCodes::PostCode.new('1234', 'MYTOWN', '1378', 'MY MUNICIPALITY', 'G')
  end

  it "has a county" do
    @postcode.county.must_equal [13, '(BERGEN)']
  end

  it "can print it's original form" do
    @postcode.to_s.must_equal "1234\tMYTOWN\t1378\tMY MUNICIPALITY\tG"
  end
end

describe 'PostCodes::county' do
  it "must give the right county names" do
    PostCodes.county(1).must_equal('ØSTFOLD')
    PostCodes.county(2).must_equal('AKERSHUS')
    PostCodes.county(3).must_equal('OSLO')
    PostCodes.county(4).must_equal('HEDMARK')
    PostCodes.county(5).must_equal('OPPLAND')
    PostCodes.county(6).must_equal('BUSKERUD')
    PostCodes.county(7).must_equal('VESTFOLD')
    PostCodes.county(8).must_equal('TELEMARK')
    PostCodes.county(9).must_equal('AUST-AGDER')
    PostCodes.county(10).must_equal('VEST-AGDER')
    PostCodes.county(11).must_equal('ROGALAND')
    PostCodes.county(12).must_equal('HORDALAND')
    PostCodes.county(13).must_equal('(BERGEN)')
    PostCodes.county(14).must_equal('SOGN OG FJORDANE')
    PostCodes.county(15).must_equal('MØRE OG ROMSDAL')
    PostCodes.county(16).must_equal('SØR-TRØNDELAG')
    PostCodes.county(17).must_equal('NORD-TRØNDELAG')
    PostCodes.county(18).must_equal('NORDLAND')
    PostCodes.county(19).must_equal('TROMS')
    PostCodes.county(20).must_equal('FINNMARK')
    PostCodes.county(21).must_equal('SVALBARD')
    PostCodes.county(22).must_equal('JAN MAYEN')
    PostCodes.county(23).must_equal('KONTINENTALSOKKELEN')
  end

  it "must fail on invalid county codes" do
    PostCodes.county(0).must_equal nil
    PostCodes.county(24).must_equal nil
  end
end

describe PostCodes do
  before do
    codes = [
      "0100\tFIRST TOWN\t0101\tFIRST MUNICIPALITY\tG",
      "1234\tMYTONW\t1278\tMY MUNICIPALITY\tG",
      "6734\tOTHERTOWN\t2783\tOTHER MUNICIPALITY\tG"
    ]
    PostCodes.load(StringIO.new(codes.join("\n").encode(Encoding::ISO_8859_15)))
  end

  it "can find existing postcode" do
    c = PostCodes.search('0100')
    c.must_be_kind_of(PostCodes::PostCode)
    c.city.must_equal('FIRST TOWN')

    c1 = PostCodes.search('6734')
    c1.must_be_kind_of(PostCodes::PostCode)
    c1.city.must_equal('OTHERTOWN')
  end

  it "returns nil if postcode does not exist" do
    c = PostCodes.search('2222')
    c.must_equal(nil)
  end
end