scb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,148 @@
1
+ {
2
+ "title": "Ombyggda lägenheter i flerbostadshus med statligt stöd efter åtgärd, byggnadsperiod, ägarkategori, tabellinnehåll och tid",
3
+ "variables": [
4
+ {
5
+ "code": "Atgard",
6
+ "text": "åtgärd",
7
+ "values": [
8
+ "10",
9
+ "11",
10
+ "12",
11
+ "13",
12
+ "14",
13
+ "15",
14
+ "16",
15
+ "17",
16
+ "18",
17
+ "19",
18
+ "9"
19
+ ],
20
+ "valueTexts": [
21
+ "utbyte av elledningar",
22
+ "grundförstärkning",
23
+ "hissinstallation",
24
+ "utbyte av hygienutrustning",
25
+ "utbyte av köksutrustning",
26
+ "stomförstärkning",
27
+ "utbyte av vattenledningar",
28
+ "utbyte av ventilationssystem",
29
+ "utbyte av värmerör",
30
+ "utbyte av värmesystem",
31
+ "utbyte av avloppsledningar"
32
+ ]
33
+ },
34
+ {
35
+ "code": "Byggnadsperiod",
36
+ "text": "byggnadsperiod",
37
+ "values": [
38
+ "-1901",
39
+ "1901-1920",
40
+ "1921-1930",
41
+ "1931-1940",
42
+ "1941-1950",
43
+ "1951-1960",
44
+ "1961-1970",
45
+ "1971-1980",
46
+ "1981-1990",
47
+ "1991-2000",
48
+ "2001-2010",
49
+ "2011-"
50
+ ],
51
+ "valueTexts": [
52
+ "före 1901",
53
+ "1901-1920",
54
+ "1921-1930",
55
+ "1931-1940",
56
+ "1941-1950",
57
+ "1951-1960",
58
+ "1961-1970",
59
+ "1971-1980",
60
+ "1981-1990",
61
+ "1991-2000",
62
+ "2001-2010",
63
+ "2011-"
64
+ ],
65
+ "elimination": true
66
+ },
67
+ {
68
+ "code": "Agarkategori",
69
+ "text": "ägarkategori",
70
+ "values": [
71
+ "1",
72
+ "2",
73
+ "3",
74
+ "4",
75
+ "ENSK PERS",
76
+ "ANNAN ÄGARE"
77
+ ],
78
+ "valueTexts": [
79
+ "stat, landsting, kommun",
80
+ "kommunägda företag",
81
+ "HSB, Riksbyggen",
82
+ "annan bostadsrättsförening",
83
+ "enskild person",
84
+ "annan ägare"
85
+ ],
86
+ "elimination": true
87
+ },
88
+ {
89
+ "code": "ContentsCode",
90
+ "text": "tabellinnehåll",
91
+ "values": [
92
+ "BO0102G1"
93
+ ],
94
+ "valueTexts": [
95
+ "Ombyggda lägenheter i flerbostadshus med statligt stöd"
96
+ ]
97
+ },
98
+ {
99
+ "code": "Tid",
100
+ "text": "tid",
101
+ "values": [
102
+ "1989",
103
+ "1990",
104
+ "1991",
105
+ "1992",
106
+ "1993",
107
+ "1994",
108
+ "1995",
109
+ "1996",
110
+ "1997",
111
+ "1998",
112
+ "1999",
113
+ "2000",
114
+ "2001",
115
+ "2002",
116
+ "2003",
117
+ "2004",
118
+ "2005",
119
+ "2006",
120
+ "2007",
121
+ "2008"
122
+ ],
123
+ "valueTexts": [
124
+ "1989",
125
+ "1990",
126
+ "1991",
127
+ "1992",
128
+ "1993",
129
+ "1994",
130
+ "1995",
131
+ "1996",
132
+ "1997",
133
+ "1998",
134
+ "1999",
135
+ "2000",
136
+ "2001",
137
+ "2002",
138
+ "2003",
139
+ "2004",
140
+ "2005",
141
+ "2006",
142
+ "2007",
143
+ "2008"
144
+ ],
145
+ "time": true
146
+ }
147
+ ]
148
+ }
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe "Config" do
6
+ subject {
7
+ SCB::API::Config.new do |c|
8
+ c.api_host = api_host
9
+ c.api_version = api_version
10
+ end
11
+ }
12
+
13
+ let(:api_host) { 'config.test' }
14
+ let(:api_version) { 'v2' }
15
+
16
+ it "returns the api_host" do
17
+ subject.api_host.must_equal api_host
18
+ end
19
+
20
+ it "returns the api_version" do
21
+ subject.api_version.must_equal api_version
22
+ end
23
+
24
+ it "returns the base_url" do
25
+ subject.base_url.must_equal 'http://config.test/OV0104/v2/sv/ssd'
26
+ end
27
+
28
+ describe "json_parser" do
29
+ it "is a lambda by default" do
30
+ subject.json_parser.
31
+ parse('{"foo":123}')['foo'].must_equal 123
32
+ end
33
+
34
+ it "can be changed" do
35
+ subject.json_parser = "foo"
36
+ subject.json_parser.must_equal "foo"
37
+ end
38
+ end
39
+
40
+ describe "http_client" do
41
+ it "is Net::HTTP by default" do
42
+ subject.http_client.must_equal SCB::HTTP
43
+ end
44
+
45
+ it "can be changed" do
46
+ subject.http_client = "bar"
47
+ subject.http_client.must_equal "bar"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,101 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe "API" do
6
+ subject { SCB::API }
7
+
8
+ let(:api) { api_with_test_config(SCB::API.new) }
9
+ let(:base_url) { "http://api.test/name/v0/lang/db" }
10
+ let(:expected_uri) { URI.parse "#{base_url}/endpoint" }
11
+
12
+ let(:fake_http_post) {
13
+ # With UTF8 BOM, since the real API has this bug.
14
+ ->(path, json){ fake_response("\xEF\xBB\xBF#{json}") }
15
+ }
16
+
17
+ describe "initialize" do
18
+ it "takes a config" do
19
+ subject.new("foo").config.must_equal "foo"
20
+ end
21
+
22
+ it "instantiates a default config" do
23
+ subject.new.config.api_host.must_equal "api.scb.se"
24
+ end
25
+ end
26
+
27
+ describe "get" do
28
+ it "checks for nil responses" do
29
+ api.stub(:http_get, nil) do
30
+ api.get('nil').must_be_nil
31
+ end
32
+ end
33
+
34
+ it "retrieves the data using a HTTP GET" do
35
+ api.stub(:http_get, fake_response('foo')) do
36
+ api.get.must_equal "foo"
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "get_and_parse" do
42
+ it "parses the returned body" do
43
+ api.stub(:http_get, fixture_response('sv/ssd/BO')) do
44
+ api.get_and_parse.last["id"].must_equal "BO0701"
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "post" do
50
+ it "unfortunately receives the body with an UTF8 BOM" do
51
+ api.stub(:http_post, fake_http_post) do
52
+ api.post(nil, { foo: "bar"}).
53
+ must_equal "\xEF\xBB\xBF{\"foo\":\"bar\"}"
54
+ end
55
+ end
56
+
57
+ it "strips the UTF8 BOM if the format is json" do
58
+ api.stub(:http_post, fake_http_post) do
59
+ query = { foo: "bar", response: { format: "json" } }
60
+
61
+ api.post(nil, query).
62
+ must_equal '{"foo":"bar","response":{"format":"json"}}'
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "post_and_parse" do
68
+ it "forces the format to be JSON" do
69
+ api.stub(:http_post, fake_http_post) do
70
+ query = { response: { format: "png" } }
71
+ api.post_and_parse(nil, query).must_equal({
72
+ "response" => { "format" => "json" }
73
+ })
74
+ end
75
+ end
76
+ end
77
+
78
+ describe "base_url" do
79
+ it "returns the base URL" do
80
+ api.base_url.must_equal base_url
81
+ end
82
+ end
83
+
84
+ describe "uri" do
85
+ it "it returns a URI pointing to the provided endpoint" do
86
+ api.uri("endpoint").must_equal expected_uri
87
+ end
88
+ end
89
+
90
+ describe "load_json" do
91
+ it "can load JSON" do
92
+ api.load_json('{"baz":[1,2,3]}')['baz'].first.must_equal 1
93
+ end
94
+ end
95
+
96
+ describe "dump_json" do
97
+ it "can dump JSON" do
98
+ api.dump_json({ foo: 123}).must_equal '{"foo":123}'
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe SCB::DB::Level do
6
+ subject { SCB::DB::Level }
7
+
8
+ let(:api) { SCB::API.new }
9
+
10
+ let(:level) {
11
+ s('BO', 'Boende, byggande och bebyggelse')
12
+ }
13
+
14
+ describe "initialize" do
15
+ it "takes a name" do
16
+ level.name.must_equal 'BO'
17
+ end
18
+
19
+ it "also takes an optional text" do
20
+ level.text.must_equal 'Boende, byggande och bebyggelse'
21
+ end
22
+ end
23
+
24
+ describe "levels" do
25
+ it "has a list of levels" do
26
+ level.stub(:data, parsed_fixture('sv/ssd/BO')) do
27
+ level.levels.last.name.must_equal 'BO0701'
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "tables" do
33
+ it "has a list of tables" do
34
+ level.stub(:data, parsed_fixture('sv/ssd/BO/BO0102')) do
35
+ level.tables[3].name.must_equal 'LagenhetRivAkBpLtRAr'
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,102 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe SCB::DB::Table do
6
+ subject { SCB::DB::Table }
7
+
8
+ let(:name) { 'LagenhetOmbAkBpAtAr' }
9
+ let(:table) { s(name) }
10
+
11
+ describe "initialize" do
12
+ it "takes a name" do
13
+ table.name.must_equal name
14
+ end
15
+ end
16
+
17
+ describe "query" do
18
+ it "parses the simple query and loads the JSON" do
19
+ table.stub(:json_query, ->(sq) { sq }) do
20
+ table.query({ Foo: [1,2] }).
21
+ first[:selection][:values].must_equal ["1","2"]
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "json_query" do
27
+ it "loads JSON" do
28
+ table.stub(:post_query, '{"foo":123}') do
29
+ table.json_query['foo'].must_equal 123
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "png_query" do
35
+ it "parses the simple_query and sets the format to png" do
36
+ table.stub(:post_query, ->(q, f){ [q,f] }) do
37
+ response = table.png_query({ Foo: 1 }, "bar")
38
+
39
+ response[0].must_equal [{
40
+ code: "Foo",
41
+ selection: {
42
+ filter: "bar",
43
+ values: ["1"]
44
+ }
45
+ }]
46
+
47
+ response[1].must_equal "png"
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "post_query" do
53
+ it "rescues any http exception and raises its own instead" do
54
+ table.stub(:api_post, ->(n, sq) { raise SCB::HTTP::Exception }) do
55
+ ->{ table.post_query({}) }.must_raise SCB::DB::InvalidQuery
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "title" do
61
+ it "extracts the title from the data" do
62
+ table.stub(:data, { 'title' => 'bar' }) do
63
+ table.title.must_equal 'bar'
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "uri" do
69
+ it "returns the URI of the table" do
70
+ table.stub(:api_uri, URI) do
71
+ table.uri.must_equal URI
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "write_png_query" do
77
+ it "writes the image data to a file" do
78
+ expected = [[{
79
+ code: "Bar",
80
+ selection: { filter: "item", values: ["1"] }
81
+ }], "png" ]
82
+
83
+ table.stub(:post_query, ->(*args){ args }) do
84
+ table.stub(:write_file, ->(*args){ args }) do
85
+ table.write_png_query(nil, { Bar: 1 })[1].
86
+ must_equal expected
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ describe "write_png_query_raw" do
93
+ it "writes the image data to a file" do
94
+ table.stub(:post_query, ->(*args){ args }) do
95
+ table.stub(:write_file, ->(*args){ args }) do
96
+ table.write_png_query_raw('foo', { Bar: 123 }).
97
+ must_equal ["foo", [{:Bar=>123}, "png"]]
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe SCB::DB::Variable do
6
+ subject { SCB::DB::Variable }
7
+
8
+ let(:fixture_file) { 'sv/ssd/BO/BO0102/LagenhetOmbAkBpAtAr' }
9
+ let(:variables_data) { parsed_fixture(fixture_file)['variables'] }
10
+ let(:data) { variables_data.sample }
11
+ let(:variable) { subject.new(data) }
12
+
13
+ describe "initialize" do
14
+ it "takes data as input" do
15
+ subject.new("foo").data.must_equal "foo"
16
+ end
17
+ end
18
+
19
+ describe "code" do
20
+ it "returns the code of the variable" do
21
+ variable.code.must_equal data["code"]
22
+ end
23
+ end
24
+
25
+ describe "text" do
26
+ it "returns the text of the variable" do
27
+ variable.text.must_equal data["text"]
28
+ end
29
+ end
30
+
31
+ describe "values" do
32
+ it "returns the values of the variable" do
33
+ variable.values.must_equal data["values"]
34
+ end
35
+ end
36
+
37
+ describe "value_texts" do
38
+ it "returns the value texts of the variable" do
39
+ variable.value_texts.must_equal data["valueTexts"]
40
+ end
41
+ end
42
+
43
+ describe "elimination?" do
44
+ it "returns true/false if the variable is an elimination" do
45
+ variable.elimination?.must_equal !!data["elimination"]
46
+ end
47
+ end
48
+
49
+ describe "time?" do
50
+ it "returns true/false if the variable is a time" do
51
+ variable.time?.must_equal !!data["time"]
52
+ end
53
+ end
54
+ end