appsent 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+
3
+ describe AppSent do
4
+
5
+ let(:fixtures_path) { File.expand_path(File.join(File.dirname(__FILE__),'fixtures')) }
6
+
7
+ before :each do
8
+ AppSent.class_variable_set(:@@config_path,nil)
9
+ AppSent.class_variable_set(:@@environment,nil)
10
+ AppSent.class_variable_set(:@@config_files,[])
11
+ end
12
+
13
+ context ".new" do
14
+
15
+ subject { AppSent.new }
16
+
17
+ %w(all_valid? load! full_error_message).each do |method|
18
+ it { should respond_to(method) }
19
+ end
20
+
21
+ end
22
+
23
+ context ".init" do
24
+
25
+ before :each do
26
+ @right_params = { :path => 'fixtures', :env => 'test' }
27
+ end
28
+
29
+ it "should require config path" do
30
+ @right_params.delete(:path)
31
+ expect { AppSent.init(@right_params) do; end }.to raise_exception(AppSent::ConfigPathNotSet)
32
+ end
33
+
34
+ it "should require environment variable" do
35
+ @right_params.delete(:env)
36
+ expect { AppSent.init(@right_params) do; end }.to raise_exception(AppSent::EnvironmentNotSet)
37
+ end
38
+
39
+ it "should require block" do
40
+ expect { AppSent.init(@right_params) }.to raise_exception(AppSent::BlockRequired)
41
+ end
42
+
43
+ it "should save config path to @@config_path" do
44
+ AppSent.init(@right_params) do; end
45
+ AppSent.config_path.should eq(fixtures_path)
46
+ end
47
+
48
+ it "should save environment to @@environment" do
49
+ AppSent.init(@right_params) do; end
50
+ AppSent.send(:class_variable_get,:@@environment).should eq('test')
51
+ end
52
+
53
+ it "should save array of configs to @@configs" do
54
+ AppSent.init(@right_params) do
55
+ simple_config
56
+ database do
57
+ username String
58
+ password String
59
+ port Fixnum
60
+ end
61
+
62
+ simple_config_with_just_type Array
63
+ end
64
+ AppSent.config_files.should eq(%w(simple_config database simple_config_with_just_type))
65
+ end
66
+
67
+ context "should send right variables to AppSent::ConfigFile" do
68
+
69
+ let(:mock_config_file) { mock(AppSent::ConfigFile, :valid? => true, :constantized => 'STR', :data => 'data') }
70
+
71
+ it "in a simple case" do
72
+ AppSent::ConfigFile.should_receive(:new).once.with(
73
+ File.join(File.dirname(__FILE__),'config'),
74
+ 'confname',
75
+ 'env'
76
+ ).and_return(mock_config_file)
77
+
78
+ AppSent.init(:path => 'config', :env => 'env') do
79
+ confname
80
+ end
81
+ end
82
+
83
+ it "with &block" do
84
+ block = lambda {}
85
+ AppSent::ConfigFile.should_receive(:new).once.with(
86
+ File.join(File.dirname(__FILE__),'config'),
87
+ 'confname',
88
+ 'env',
89
+ &block
90
+ ).and_return(mock_config_file)
91
+
92
+ AppSent.init(:path => 'config', :env => 'env') do
93
+ confname &block
94
+ end
95
+ end
96
+
97
+ it "with :skip_env" do
98
+ pending('unimplemented yet')
99
+ block = lambda {}
100
+ AppSent::ConfigFile.should_receive(:new).once.with(
101
+ File.join(File.dirname(__FILE__),'config'),
102
+ 'confname',
103
+ 'env',
104
+ :skip_env => true,
105
+ &block
106
+ ).and_return(mock_config_file)
107
+
108
+ AppSent.init(:path => 'config', :env => 'env') do
109
+ confname :skip_env => true, &block
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+ it "should create corresponding constants with values" do
116
+ AppSent.init(@right_params) do
117
+ simple_config
118
+ database do
119
+ username String
120
+ password String
121
+ port Fixnum
122
+ end
123
+
124
+ simple_config_with_just_type Array
125
+ end
126
+
127
+ AppSent::SIMPLE_CONFIG.should eq({:a=>1, :b=>'2'})
128
+ AppSent::DATABASE.should eq({:username => 'user', :password => 'pass', :port => 100500})
129
+ AppSent::SIMPLE_CONFIG_WITH_JUST_TYPE.should eq([1,2,3])
130
+ end
131
+
132
+ end
133
+
134
+ after :each do
135
+ %w( STR SIMPLE_CONFIG DATABASE SIMPLE_CONFIG_WITH_JUST_TYPE ).each do |const|
136
+ AppSent.send :remove_const, const if AppSent.const_defined?(const)
137
+ end
138
+ end
139
+
140
+ end
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+
3
+ describe AppSent do
4
+
5
+ let(:fixtures_path) { File.expand_path(File.join(File.dirname(__FILE__),'..','fixtures')) }
6
+
7
+ before :each do
8
+ AppSent.class_variable_set(:@@config_path,nil)
9
+ AppSent.class_variable_set(:@@environment,nil)
10
+ AppSent.class_variable_set(:@@config_files,[])
11
+ end
12
+
13
+ context ".new" do
14
+
15
+ subject { AppSent.new }
16
+
17
+ %w(all_valid? load! full_error_message).each do |method|
18
+ it { should respond_to(method) }
19
+ end
20
+
21
+ end
22
+
23
+ context ".init" do
24
+
25
+ before :each do
26
+ @right_params = { :path => '../fixtures', :env => 'test' }
27
+ end
28
+
29
+ it "should require config path" do
30
+ @right_params.delete(:path)
31
+ expect { AppSent.init(@right_params) do; end }.to raise_exception(AppSent::ConfigPathNotSet)
32
+ end
33
+
34
+ it "should require environment variable" do
35
+ @right_params.delete(:env)
36
+ expect { AppSent.init(@right_params) do; end }.to raise_exception(AppSent::EnvironmentNotSet)
37
+ end
38
+
39
+ it "should require block" do
40
+ expect { AppSent.init(@right_params) }.to raise_exception(AppSent::BlockRequired)
41
+ end
42
+
43
+ it "should save config path to @@config_path" do
44
+ AppSent.init(@right_params) do; end
45
+ AppSent.config_path.should eq(fixtures_path)
46
+ end
47
+
48
+ it "should save environment to @@environment" do
49
+ AppSent.init(@right_params) do; end
50
+ AppSent.send(:class_variable_get,:@@environment).should eq('test')
51
+ end
52
+
53
+ it "should save array of configs to @@configs" do
54
+ expect {
55
+ AppSent.init(@right_params) do
56
+ config1
57
+ config2
58
+ config3
59
+ end
60
+ }.to raise_exception(AppSent::Error)
61
+ AppSent.config_files.should eq(%w(config1 config2 config3))
62
+ end
63
+
64
+ context "should send right variables to AppSent::ConfigFile" do
65
+
66
+ let(:mock_config_file) { mock(AppSent::ConfigFile, :valid? => true, :constantized => 'STR', :data => 'data') }
67
+
68
+ it "in a simple case" do
69
+ AppSent::ConfigFile.should_receive(:new).once.with(
70
+ File.join(File.dirname(__FILE__),'config'),
71
+ 'confname',
72
+ 'env'
73
+ ).and_return(mock_config_file)
74
+
75
+ AppSent.init(:path => 'config', :env => 'env') do
76
+ confname
77
+ end
78
+ end
79
+
80
+ it "with &block" do
81
+ block = lambda {}
82
+ AppSent::ConfigFile.should_receive(:new).once.with(
83
+ File.join(File.dirname(__FILE__),'config'),
84
+ 'confname',
85
+ 'env',
86
+ &block
87
+ ).and_return(mock_config_file)
88
+
89
+ AppSent.init(:path => 'config', :env => 'env') do
90
+ confname &block
91
+ end
92
+ end
93
+
94
+ it "with :skip_env" do
95
+ pending('unimplemented yet')
96
+ block = lambda {}
97
+ AppSent::ConfigFile.should_receive(:new).once.with(
98
+ File.join(File.dirname(__FILE__),'config'),
99
+ 'confname',
100
+ 'env',
101
+ :skip_env => true,
102
+ &block
103
+ ).and_return(mock_config_file)
104
+
105
+ AppSent.init(:path => 'config', :env => 'env') do
106
+ confname :skip_env => true, &block
107
+ end
108
+ end
109
+
110
+ after :each do
111
+ AppSent.send :remove_const, 'STR' if AppSent.const_defined?("STR")
112
+ end
113
+
114
+ end
115
+
116
+ it "should create corresponding constants with values" do
117
+ AppSent.init(@right_params) do
118
+ simple_config
119
+ database do
120
+ username :type => String
121
+ password :type => String
122
+ port :type => Fixnum
123
+ end
124
+
125
+ simple_config_with_just_type :type => Array
126
+ end
127
+
128
+ AppSent::SIMPLE_CONFIG.should eq({:a=>1, :b=>'2'})
129
+ AppSent::DATABASE.should eq({:username => 'user', :password => 'pass', :port => 100500})
130
+ AppSent::SIMPLE_CONFIG_WITH_JUST_TYPE.should eq([1,2,3])
131
+ end
132
+
133
+ end
134
+
135
+ end
@@ -0,0 +1,222 @@
1
+ require 'spec_helper'
2
+
3
+ describe AppSent::ConfigFile do
4
+
5
+ subject { described_class }
6
+
7
+ before :each do
8
+ @params = {
9
+ 'config_path' => '/path/to/config',
10
+ 'config_name' => 'config_name',
11
+ 'env' => :environment,
12
+ 'type' => Hash
13
+ }
14
+ end
15
+
16
+ let(:fake_config_filename) { '/path/to/config/config_name.yml' }
17
+
18
+ let(:params) do
19
+ [
20
+ @params['config_path'],
21
+ @params['config_name'],
22
+ @params['env'],
23
+ { :type => @params['type'] }
24
+ ]
25
+ end
26
+
27
+ context ".new" do
28
+
29
+ %w(valid? options constantized error_message).each do |method|
30
+ it { subject.new(*params).should respond_to(method)}
31
+ end
32
+
33
+ context "should send right variables to AppSent::ConfigValue" do
34
+
35
+ let(:mock_config_value) { mock(AppSent::ConfigValue, :valid? => true) }
36
+
37
+ context "using old API(should be removed in future releases)" do
38
+
39
+ it "in a simple case" do
40
+ AppSent::ConfigValue.should_receive(:new).once.with(
41
+ 'username',
42
+ 'user', # data
43
+ :type => String
44
+ ).and_return(mock_config_value)
45
+
46
+ AppSent.init(:path => '../../fixtures', :env => 'test') do
47
+ database do
48
+ username :type => String
49
+ end
50
+ end
51
+ end
52
+
53
+ it "with a &block" do
54
+ block = lambda {}
55
+ AppSent::ConfigValue.should_receive(:new).once.with(
56
+ 'username',
57
+ 'user', # data
58
+ :type => String,
59
+ &block
60
+ ).and_return(mock_config_value)
61
+
62
+ AppSent.init(:path => '../../fixtures', :env => 'test') do
63
+ database do
64
+ username :type => String, &block
65
+ end
66
+ end
67
+ end
68
+
69
+ it "with description" do
70
+ AppSent::ConfigValue.should_receive(:new).once.with(
71
+ 'username',
72
+ 'user', # data
73
+ :type => String,
74
+ :desc => 'description'
75
+ ).and_return(mock_config_value)
76
+
77
+ AppSent.init(:path => '../../fixtures', :env => 'test') do
78
+ database do
79
+ username :type => String, :desc => 'description'
80
+ end
81
+ end
82
+ end
83
+
84
+ it "with description and example" do
85
+ AppSent::ConfigValue.should_receive(:new).once.with(
86
+ 'username',
87
+ 'user', # data
88
+ :type => String,
89
+ :desc => 'description',
90
+ :example => 'user'
91
+ ).and_return(mock_config_value)
92
+
93
+ AppSent.init(:path => '../../fixtures', :env => 'test') do
94
+ database do
95
+ username :type => String, :desc => 'description', :example => 'user'
96
+ end
97
+ end
98
+ end
99
+
100
+ end
101
+
102
+ context "using new API" do
103
+
104
+ it "in a simple case" do
105
+ AppSent::ConfigValue.should_receive(:new).once.with(
106
+ 'username',
107
+ 'user', # data
108
+ String
109
+ ).and_return(mock_config_value)
110
+
111
+ AppSent.init(:path => '../../fixtures', :env => 'test') do
112
+ database do
113
+ username String
114
+ end
115
+ end
116
+ end
117
+
118
+ it "with a &block" do
119
+ block = lambda {}
120
+ AppSent::ConfigValue.should_receive(:new).once.with(
121
+ 'username',
122
+ 'user', # data
123
+ String,
124
+ &block
125
+ ).and_return(mock_config_value)
126
+
127
+ AppSent.init(:path => '../../fixtures', :env => 'test') do
128
+ database do
129
+ username String, &block
130
+ end
131
+ end
132
+ end
133
+
134
+ it "with description" do
135
+ AppSent::ConfigValue.should_receive(:new).once.with(
136
+ 'username',
137
+ 'user', # data
138
+ String,
139
+ 'description'
140
+ ).and_return(mock_config_value)
141
+
142
+ AppSent.init(:path => '../../fixtures', :env => 'test') do
143
+ database do
144
+ username String, 'description'
145
+ end
146
+ end
147
+ end
148
+
149
+ it "with description and example" do
150
+ AppSent::ConfigValue.should_receive(:new).once.with(
151
+ 'username',
152
+ 'user', # data
153
+ String,
154
+ 'description' => 'user'
155
+ ).and_return(mock_config_value)
156
+
157
+ AppSent.init(:path => '../../fixtures', :env => 'test') do
158
+ database do
159
+ username String, 'description' => 'user'
160
+ end
161
+ end
162
+ end
163
+
164
+ end
165
+
166
+ end
167
+
168
+ after :each do
169
+ AppSent.send :remove_const, 'DATABASE' if AppSent.const_defined?("DATABASE")
170
+ end
171
+
172
+ end
173
+
174
+ context "#valid?" do
175
+
176
+ context "should be true" do
177
+
178
+ it "if config exists and environment presence(without type)(no values)" do
179
+ YAML.should_receive(:load_file).once.with(fake_config_filename).and_return('environment' => {:a=>100500})
180
+ subject.new(*params).should be_valid
181
+ end
182
+
183
+ it "if config exists and environment presence(with type specified)(no values)" do
184
+ @params['type']=Array
185
+ YAML.should_receive(:load_file).once.with(fake_config_filename).and_return(:environment => [1,2,3])
186
+ subject.new(*params).should be_valid
187
+ end
188
+
189
+ it "if config exists and environment presence(with values)" do
190
+ values_block = lambda {
191
+ value :type => String
192
+ }
193
+ YAML.should_receive(:load_file).once.with(fake_config_filename).and_return('environment' => {:value=>'100500'})
194
+ subject.new(*params,&values_block).should be_valid
195
+ end
196
+
197
+ end
198
+
199
+ context "should be false" do
200
+
201
+ it "if config does not exists" do
202
+ YAML.should_receive(:load_file).once.with(fake_config_filename).and_raise(Errno::ENOENT)
203
+ end
204
+
205
+ it "if environment does not presence in config" do
206
+ YAML.should_receive(:load_file).once.with(fake_config_filename).and_return('other_environment' => {:a=>100500})
207
+ end
208
+
209
+ it "if wrong type" do
210
+ @params['type'] = Array
211
+ YAML.should_receive(:load_file).once.with(fake_config_filename).and_return(:environment => 123)
212
+ end
213
+
214
+ after :each do
215
+ subject.new(*params).should_not be_valid
216
+ end
217
+
218
+ end
219
+
220
+ end
221
+
222
+ end