stats_package_syntax_file_generator 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,224 @@
1
+ # This file is part of the Minnesota Population Center's stats_package_syntax_file_generator project.
2
+ # For copyright and licensing information, see the NOTICE and LICENSE files
3
+ # in this project's top-level directory, and also on-line at:
4
+ # https://github.com/mnpopcenter/stats_package_syntax_file_generator
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'setup.rb'))
7
+
8
+ module StatsPackageSyntaxFileGeneratorTest
9
+ class MakerSTATA < Test::Unit::TestCase
10
+
11
+ include StatsPackageSyntaxFileGeneratorTestSetup
12
+
13
+ def test_create_maker_stata
14
+ msg = 'Try to create an object.'
15
+ mk = new_maker('stata')
16
+ assert_instance_of StatsPackageSyntaxFileGenerator::MakerSTATA, mk, msg
17
+ end
18
+
19
+ def test_syn_infix_start
20
+ msg = 'Compare against hardcoded result.'
21
+ mk = new_maker('stata')
22
+ expected = [
23
+ 'clear',
24
+ 'quietly infix ///',
25
+ ]
26
+ actual = mk.syn_infix_start
27
+ assert_equal expected, actual, msg
28
+ end
29
+
30
+ def test_syn_infix_var_locs
31
+ msg = 'Compare against hardcoded result.'
32
+ mk = new_maker('stata')
33
+ expected = [
34
+ ' str rectype 1-1 ///',
35
+ ' long dwnum 2-7 ///',
36
+ ' byte hhnum 8-8 ///',
37
+ ' int fbig_nd 45-48 ///',
38
+ ' float canton 67-69 ///',
39
+ ' byte lit 101-101 ///',
40
+ ]
41
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE DWNUM HHNUM FBIG_ND CANTON LIT) )
42
+ actual = mk.syn_infix_var_locs(var_list)
43
+ assert_equal expected, actual, msg
44
+ end
45
+
46
+ def test_syn_dfh_infix_gen
47
+ mk = new_maker('stata')
48
+
49
+ msg = 'Compare against hardcoded result: rectangularize=false.'
50
+ expected = [
51
+ 'gen _line_num = _n',
52
+ ]
53
+ mk.sfc.rectangularize = false
54
+ actual = mk.syn_dfh_infix_gen
55
+ assert_equal expected, actual, msg
56
+
57
+ msg = 'Compare against hardcoded result: rectangularize=true.'
58
+ expected = [
59
+ 'gen _line_numH = _n',
60
+ 'gen _line_numP = _n',
61
+ 'replace _line_numH = _line_numH[_n - 1] if _n > 1 & rectype != `"H"\'',
62
+ 'replace _line_numP = _line_numP[_n - 1] if _n > 1 & rectype != `"P"\'',
63
+ ]
64
+ mk.sfc.rectangularize = true
65
+ actual = mk.syn_dfh_infix_gen
66
+ assert_equal expected, actual, msg
67
+ end
68
+
69
+ def test_syn_dfh_combine_append
70
+ mk = new_maker('stata')
71
+
72
+ msg = 'Compare against hardcoded result: rectangularize=false.'
73
+ expected = [
74
+ 'use __temp_ipums_hier_H.dta',
75
+ 'append using __temp_ipums_hier_P.dta',
76
+ ]
77
+ mk.sfc.rectangularize = false
78
+ actual = mk.syn_dfh_combine_append
79
+ assert_equal expected, actual, msg
80
+
81
+ msg = 'Compare against hardcoded result: rectangularize=true.'
82
+ expected = [
83
+ 'use __temp_ipums_hier_P.dta',
84
+ 'merge m:1 _line_numH using __temp_ipums_hier_H.dta, keep(master match)',
85
+ 'drop _merge',
86
+ ]
87
+ mk.sfc.rectangularize = true
88
+ actual = mk.syn_dfh_combine_append
89
+ assert_equal expected, actual, msg
90
+ end
91
+
92
+ def test_syn_dfh_combine_save
93
+ mk = new_maker('stata')
94
+
95
+ msg = 'Compare against hardcoded result: rectangularize=false.'
96
+ expected = [
97
+ 'sort _line_num',
98
+ 'drop _line_num',
99
+ # 'save xx9999a.dta',
100
+ ]
101
+ mk.sfc.rectangularize = false
102
+ actual = mk.syn_dfh_combine_save
103
+ assert_equal expected, actual, msg
104
+
105
+ msg = 'Compare against hardcoded result: rectangularize=true.'
106
+ expected = [
107
+ 'sort _line_numH _line_numP',
108
+ 'drop _line_numH _line_numP',
109
+ # 'save xx9999a.dta',
110
+ ]
111
+ mk.sfc.rectangularize = true
112
+ actual = mk.syn_dfh_combine_save
113
+ assert_equal expected, actual, msg
114
+ end
115
+
116
+ def test_syn_dfh_combine_erase
117
+ mk = new_maker('stata')
118
+
119
+ msg = 'Compare against hardcoded result.'
120
+ expected = [
121
+ 'erase __temp_ipums_hier_H.dta',
122
+ 'erase __temp_ipums_hier_P.dta',
123
+ ]
124
+ mk.sfc.rectangularize = false
125
+ actual = mk.syn_dfh_combine_erase
126
+ assert_equal expected, actual, msg
127
+ end
128
+
129
+ def test_syn_convert_implied_decim
130
+ msg = 'Compare against hardcoded result.'
131
+ mk = new_maker('stata')
132
+ expected = [
133
+ 'replace canton = canton / 10',
134
+ 'replace resprev2 = resprev2 / 1000',
135
+ "replace bigdec = bigdec / 100000"
136
+ ]
137
+ actual = mk.syn_convert_implied_decim
138
+ assert_equal expected, actual, msg
139
+ end
140
+
141
+ def test_syn_display_format
142
+ msg = 'Compare against hardcoded result.'
143
+ mk = new_maker('stata')
144
+ expected = [
145
+ "format canton %3.1f",
146
+ "format resprev2 %4.3f",
147
+ "format bigdec %10.5f",
148
+ "format bigint %19.0f"
149
+ ]
150
+ actual = mk.syn_display_format
151
+ assert_equal expected, actual, msg
152
+ end
153
+
154
+ def test_syn_var_labs
155
+ msg = 'Compare against hardcoded result.'
156
+ mk = new_maker('stata')
157
+ expected = [
158
+ 'label var rectype `"Record type"\'',
159
+ 'label var hdfirstd `"Head not first ["dwelling-wide"] {note}"\'',
160
+ 'label var canton `"Canton "geo area""\'',
161
+ 'label var age `"Age"\'',
162
+ ]
163
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE HDFIRSTD CANTON AGE) )
164
+ actual = mk.syn_var_labs(var_list)
165
+ assert_equal expected, actual, msg
166
+ end
167
+
168
+ def test_syn_val_labs_for_var
169
+ msg = 'Compare against hardcoded result.'
170
+ mk = new_maker('stata')
171
+ expected = [
172
+ [
173
+ 'label define urban_lbl 1 `"Urban"\'',
174
+ 'label define urban_lbl 2 `"Rural"\', add',
175
+ ],
176
+ [
177
+ 'label define sex_lbl 1 `"Male"\'',
178
+ 'label define sex_lbl 2 `"Female"\', add',
179
+ ],
180
+ ]
181
+ var_list = names_to_vars( mk.sfc, %w(URBAN SEX) )
182
+ var_list.each_index { |i|
183
+ actual = mk.syn_val_labs_for_var(var_list[i])
184
+ assert_equal expected[i], actual, msg
185
+ }
186
+ end
187
+
188
+ def test_temp_file_names
189
+ msg = 'Compare against hardcoded result.'
190
+ mk = new_maker('stata')
191
+ expected = [
192
+ '__temp_ipums_hier_H.dta',
193
+ '__temp_ipums_hier_P.dta',
194
+ ]
195
+ actual = mk.temp_file_names
196
+ assert_equal expected, actual, msg
197
+
198
+ msg = 'Should return [] if there are no record types.'
199
+ mk.sfc.record_types = []
200
+ actual = mk.temp_file_names
201
+ assert_equal [], actual, msg
202
+ end
203
+
204
+ def test_sort_vars
205
+ msg = 'Compare against hardcoded result.'
206
+ mk = new_maker('stata')
207
+ mk.sfc.rectangularize = true
208
+ expected = ['_line_numH', '_line_numP']
209
+ assert_equal expected, mk.sort_vars, msg
210
+
211
+ mk.sfc.rectangularize = false
212
+ expected = ['_line_num']
213
+ assert_equal expected, mk.sort_vars, msg
214
+ end
215
+
216
+ def test_rt_ne_statement
217
+ msg = 'Compare against hardcoded result.'
218
+ mk = new_maker('stata')
219
+ expected = 'rectype != `"H"\''
220
+ assert_equal expected, mk.rt_ne_statement('H'), msg
221
+ end
222
+
223
+ end
224
+ end
@@ -0,0 +1,190 @@
1
+ # This file is part of the Minnesota Population Center's stats_package_syntax_file_generator project.
2
+ # For copyright and licensing information, see the NOTICE and LICENSE files
3
+ # in this project's top-level directory, and also on-line at:
4
+ # https://github.com/mnpopcenter/stats_package_syntax_file_generator
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'setup.rb'))
7
+
8
+ module StatsPackageSyntaxFileGeneratorTest
9
+ class MakerSTS < Test::Unit::TestCase
10
+
11
+ include StatsPackageSyntaxFileGeneratorTestSetup
12
+
13
+ def test_create_maker_sts
14
+ msg = 'Try to create an object.'
15
+ mk = new_maker('sts')
16
+ assert_instance_of StatsPackageSyntaxFileGenerator::MakerSTS, mk, msg
17
+ end
18
+
19
+ def test_syn_quoting
20
+ msg = 'Quoting empty string.'
21
+ mk = new_maker('sts')
22
+ expected = "\"\""
23
+ actual = mk.q('')
24
+ assert_equal expected, actual, msg
25
+
26
+ msg = 'Quoting string with quotes.'
27
+ expected = "\"before''this is a ''test''after\""
28
+ actual = mk.q('before"this is a "test"after')
29
+ assert_equal expected, actual, msg
30
+ end
31
+
32
+ def test_syn_var_fmt
33
+ msg = 'Compare against hardcoded result.'
34
+ mk = new_maker('sts')
35
+ expected = [
36
+ ' (A)',
37
+ ' (F3.1)',
38
+ '',
39
+ ' (F4.3)',
40
+ ]
41
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE CANTON URBAN RESPREV2) )
42
+ actual = var_list.map { |v| mk.var_fmt(v) }
43
+ assert_equal expected, actual, msg
44
+ end
45
+
46
+ def test_syn_var_loc_with_fmt
47
+ msg = 'Compare against hardcoded result.'
48
+ mk = new_maker('sts')
49
+ expected = [
50
+ '1-1 (A)',
51
+ '67 (F3.1)',
52
+ '80-80',
53
+ '80 (F4.3)',
54
+ ]
55
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE CANTON URBAN RESPREV2) )
56
+ actual = var_list.map { |v| mk.var_loc_with_fmt(v) }
57
+ assert_equal expected, actual, msg
58
+ end
59
+
60
+ def test_supported_val
61
+ msg = 'Incompat value label -- empty label'
62
+ mk = new_maker('sts')
63
+ expected = false
64
+ actual = mk.supported_val?(StatsPackageSyntaxFileGenerator::Value.new(:value => 'FOO', :label => ''))
65
+ assert_equal expected, actual, msg
66
+
67
+ msg = 'Incompat value label -- pure whitespace label'
68
+ mk = new_maker('sts')
69
+ expected = false
70
+ actual = mk.supported_val?(StatsPackageSyntaxFileGenerator::Value.new(:value => 'FOO', :label => ' '))
71
+ assert_equal expected, actual, msg
72
+
73
+ msg = 'Incompat value value -- pure whitespace value'
74
+ mk = new_maker('sts')
75
+ expected = false
76
+ actual = mk.supported_val?(StatsPackageSyntaxFileGenerator::Value.new(:value => ' ', :label => 'FOO'))
77
+ assert_equal expected, actual, msg
78
+
79
+ msg = 'Incompat value value -- blank value'
80
+ mk = new_maker('sts')
81
+ expected = false
82
+ actual = mk.supported_val?(StatsPackageSyntaxFileGenerator::Value.new(:value => '', :label => 'FOO'))
83
+ assert_equal expected, actual, msg
84
+
85
+ msg = 'Incompat value value -- nonalphanumeric value'
86
+ mk = new_maker('sts')
87
+ expected = false
88
+ actual = mk.supported_val?(StatsPackageSyntaxFileGenerator::Value.new(:value => '@#(BAR)', :label => 'FOO'))
89
+ assert_equal expected, actual, msg
90
+
91
+ msg = 'Incompat value value -- value with space'
92
+ mk = new_maker('sts')
93
+ expected = false
94
+ actual = mk.supported_val?(StatsPackageSyntaxFileGenerator::Value.new(:value => 'a thing', :label => 'FOO'))
95
+ assert_equal expected, actual, msg
96
+
97
+ msg = 'Compat value'
98
+ mk = new_maker('sts')
99
+ expected = true
100
+ actual = mk.supported_val?(StatsPackageSyntaxFileGenerator::Value.new(:value => 'BAR', :label => 'FOO'))
101
+ assert_equal expected, actual, msg
102
+ end
103
+
104
+ def test_syn_val_labs_for_var
105
+ msg = 'Compare against hardcoded result.'
106
+ mk = new_maker('sts')
107
+ expected = [
108
+ [
109
+ " \\URBAN",
110
+ " 1 \"Urban\"",
111
+ " 2 \"Rural\""
112
+ ],
113
+ [
114
+ " \\RECTYPE",
115
+ " 'H' \"Household\"",
116
+ " 'P' \"Person\""
117
+ ],
118
+ [
119
+ ],
120
+ [" \\RENT",
121
+ " '......................................40........50........60........70........80........90.......100.......110.......120.......130.......140.......150.......160.......170.......180.......190.......200.......210.......220.......230.......240.......250.......260.......270.......280.......290.......300' \"[no label]............................40........50........60........70........80........90.......100.......110.......120.......130.......140.......150.......160.......170.......180.......190.......200.......210.......220.......230.......240.......250.......260.......270.......280.......290.......300\"",
122
+ " '0050' \"[no label]\"",
123
+ " '0070' \"[no label]\"",
124
+ " '0075' \"[no label]\"",
125
+ " '0080' \"[no label]\"",
126
+ " '0085' \"[no label]\"",
127
+ " '0100' \"[no label]\"",
128
+ " '0120' \"[no label]\"",
129
+ " '0125' \"[no label]\"",
130
+ " '0130' \"[no label]\"",
131
+ " '0140' \"[no label]\"",
132
+ " '0150' \"[no label]\"",
133
+ " '0160' \"[no label]\"",
134
+ " '0175' \"[no label]\"",
135
+ " '0200' \"[no label]\"",
136
+ " '0210' \"[no label]\"",
137
+ " '0225' \"[no label]\"",
138
+ " '0240' \"[no label]\"",
139
+ " '0250' \"[no label]\"",
140
+ " '0300' \"[no label]\"",
141
+ " '0400' \"[no label]\"",
142
+ " '0425' \"[no label]\"",
143
+ " '0454' \"[no label]\"",
144
+ " '0500' \"[no label]\"",
145
+ " '0550' \"[no label]\"",
146
+ " '0700' \"[no label]\"",
147
+ " '1800' \"[no label]\"",
148
+ " '2000' \"[no label]\"",
149
+ " '8501' \"[no label]\""]
150
+ ]
151
+ var_list = names_to_vars( mk.sfc, %w(URBAN RECTYPE DWNUM RENT) )
152
+ var_list.each_index { |i|
153
+ actual = mk.syn_val_labs_for_var(var_list[i])
154
+ assert_equal expected[i], actual, msg
155
+ }
156
+ end
157
+
158
+ def test_syn_var_labs
159
+ msg = 'Compare against hardcoded result.'
160
+ mk = new_maker('sts')
161
+ expected = ["VARIABLE LABELS",
162
+ " HHNUM \"''Household number''\"",
163
+ " HDFIRSTD \"Head not first [''dwelling-wide''] {note}\"",
164
+ " CANTON \"Canton ''geo area''\"",
165
+ " SEX \".........x.........x.........x.........x.........x.........x.........x.........x.........x.........x.........x.........x.........x.........x.........x\"",
166
+ "",
167
+ ""]
168
+ var_list = names_to_vars( mk.sfc, %w(HHNUM HDFIRSTD CANTON SEX) )
169
+ var_list[-1].label = '.........x' * 15
170
+ actual = mk.syn_var_labs(var_list)
171
+ assert_equal expected, actual, msg
172
+ end
173
+
174
+ def test_syn_var_locations
175
+ msg = 'Compare against hardcoded result.'
176
+ mk = new_maker('sts')
177
+ expected = [
178
+ "VARIABLES",
179
+ " RECTYPE 1-1 (A) \\RECTYPE",
180
+ " CANTON 67 (F3.1) ",
181
+ " AGE 69-71 ",
182
+ " LIT 101-101 \\LIT"
183
+ ]
184
+ var_list = names_to_vars( mk.sfc, %w(RECTYPE CANTON AGE LIT) )
185
+ actual = mk.syn_var_locations(var_list)
186
+ assert_equal expected, actual, msg
187
+ end
188
+
189
+ end
190
+ end
data/tests/tc_value.rb ADDED
@@ -0,0 +1,23 @@
1
+ # This file is part of the Minnesota Population Center's stats_package_syntax_file_generator project.
2
+ # For copyright and licensing information, see the NOTICE and LICENSE files
3
+ # in this project's top-level directory, and also on-line at:
4
+ # https://github.com/mnpopcenter/stats_package_syntax_file_generator
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'setup.rb'))
7
+
8
+ module StatsPackageSyntaxFileGeneratorTest
9
+ class Value < Test::Unit::TestCase
10
+
11
+ include StatsPackageSyntaxFileGeneratorTestSetup
12
+
13
+ def test_create_value
14
+ msg = 'Try to create an object.'
15
+ v = StatsPackageSyntaxFileGenerator::Value.new(:value => 1234)
16
+ assert_instance_of StatsPackageSyntaxFileGenerator::Value, v, msg
17
+
18
+ msg = 'Try to create an object with required parameters missing.'
19
+ assert_raise(ArgumentError, msg) { StatsPackageSyntaxFileGenerator::Value.new }
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ # This file is part of the Minnesota Population Center's stats_package_syntax_file_generator project.
2
+ # For copyright and licensing information, see the NOTICE and LICENSE files
3
+ # in this project's top-level directory, and also on-line at:
4
+ # https://github.com/mnpopcenter/stats_package_syntax_file_generator
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'setup.rb'))
7
+
8
+ module StatsPackageSyntaxFileGeneratorTest
9
+ class Variable < Test::Unit::TestCase
10
+
11
+ include StatsPackageSyntaxFileGeneratorTestSetup
12
+
13
+ def test_create_variable
14
+ msg = 'Try to create an object.'
15
+ v = new_variable()
16
+ assert_instance_of StatsPackageSyntaxFileGenerator::Variable, v, msg
17
+
18
+ msg = 'Try to create an object with required parameters missing.'
19
+ assert_raise(ArgumentError, msg) { StatsPackageSyntaxFileGenerator::Variable.new }
20
+ end
21
+
22
+ def test_column_locations_as_s
23
+ msg = 'Compare against hardcoded result.'
24
+ v = new_variable()
25
+ e = params_variable_lookup(:column_locations_as_s)
26
+ assert_equal e, v.column_locations_as_s, msg
27
+ end
28
+
29
+ def test_end_column
30
+ msg = 'Compare against hardcoded result.'
31
+ v = new_variable()
32
+ e = params_variable_lookup(:end_column)
33
+ assert_equal e, v.end_column, msg
34
+ end
35
+
36
+ def test_add_and_clear_values
37
+ v = new_variable()
38
+
39
+ msg = 'New variable should have no values.'
40
+ assert_equal 0, v.values.size, msg
41
+
42
+ msg = 'Adding a value N times should yield a variable with N values.'
43
+ n = params_values().size
44
+ add_new_values_to_var(v)
45
+ assert_equal n, v.values.size, msg
46
+
47
+ msg = 'After clearing its values, a variable should have no values.'
48
+ v.clear_values
49
+ assert_equal 0, v.values.size, msg
50
+ end
51
+
52
+ end
53
+ end