facemock 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.coveralls.yml +1 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +154 -0
  8. data/Rakefile +6 -0
  9. data/db/.gitkeep +0 -0
  10. data/facemock.gemspec +31 -0
  11. data/lib/facemock/config.rb +88 -0
  12. data/lib/facemock/database/application.rb +20 -0
  13. data/lib/facemock/database/permission.rb +21 -0
  14. data/lib/facemock/database/table.rb +340 -0
  15. data/lib/facemock/database/user.rb +26 -0
  16. data/lib/facemock/database.rb +121 -0
  17. data/lib/facemock/errors.rb +7 -0
  18. data/lib/facemock/fb_graph/application/test_users.rb +36 -0
  19. data/lib/facemock/fb_graph/application/user/permission.rb +10 -0
  20. data/lib/facemock/fb_graph/application/user.rb +69 -0
  21. data/lib/facemock/fb_graph/application.rb +48 -0
  22. data/lib/facemock/fb_graph/user.rb +13 -0
  23. data/lib/facemock/fb_graph.rb +30 -0
  24. data/lib/facemock/version.rb +3 -0
  25. data/lib/facemock.rb +19 -0
  26. data/spec/facemock/config_spec.rb +185 -0
  27. data/spec/facemock/database/application_spec.rb +73 -0
  28. data/spec/facemock/database/permission_spec.rb +52 -0
  29. data/spec/facemock/database/tables_spec.rb +728 -0
  30. data/spec/facemock/database/user_spec.rb +169 -0
  31. data/spec/facemock/database_spec.rb +270 -0
  32. data/spec/facemock/errors_spec.rb +9 -0
  33. data/spec/facemock/fb_graph/application/test_users_spec.rb +155 -0
  34. data/spec/facemock/fb_graph/application/user_spec.rb +208 -0
  35. data/spec/facemock/fb_graph/application_spec.rb +132 -0
  36. data/spec/facemock/fb_graph/user_spec.rb +36 -0
  37. data/spec/facemock/fb_graph_spec.rb +47 -0
  38. data/spec/facemock_spec.rb +74 -0
  39. data/spec/spec_helper.rb +18 -0
  40. data/spec/support/tables_helper.rb +46 -0
  41. metadata +64 -3
@@ -0,0 +1,169 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::Database::User do
4
+ include TableHelper
5
+
6
+ let(:db_name) { ".test" }
7
+ let(:table_name) { :users }
8
+ let(:column_names) { [ :id,
9
+ :name,
10
+ :email,
11
+ :password,
12
+ :installed,
13
+ :access_token,
14
+ :application_id,
15
+ :created_at] }
16
+
17
+ let(:id) { 1 }
18
+ let(:name) { "test user" }
19
+ let(:email) { "hoge@fugapiyo.com" }
20
+ let(:password) { "testpass" }
21
+ let(:installed) { true }
22
+ let(:access_token) { "test_token" }
23
+ let(:application_id) { 1 }
24
+ let(:created_at) { Time.now }
25
+ let(:options) { { id: id,
26
+ name: name,
27
+ email: email,
28
+ password: password,
29
+ installed: installed,
30
+ access_token: access_token,
31
+ application_id: application_id,
32
+ created_at: created_at } }
33
+
34
+ after do
35
+ remove_dynamically_defined_class_method(Facemock::Database::User)
36
+ remove_dynamically_defined_instance_method(Facemock::Database::User)
37
+ end
38
+
39
+ describe '::TABLE_NAME' do
40
+ subject { Facemock::Database::User::TABLE_NAME }
41
+ it { is_expected.to eq table_name }
42
+ end
43
+
44
+ describe '::COLUMN_NAMES' do
45
+ subject { Facemock::Database::User::COLUMN_NAMES }
46
+ it { is_expected.to eq column_names }
47
+ end
48
+
49
+ describe '#initialize' do
50
+ context 'without option' do
51
+ subject { Facemock::Database::User.new }
52
+ it { is_expected.to be_kind_of Facemock::Database::User }
53
+
54
+ describe '.id' do
55
+ subject { Facemock::Database::User.new.id }
56
+ it { is_expected.to be > 0 }
57
+ it { is_expected.to be < 100010000000000 }
58
+ end
59
+
60
+ describe '.name' do
61
+ subject { Facemock::Database::User.new.name }
62
+ it { is_expected.to be_kind_of String }
63
+
64
+ describe '.size' do
65
+ subject { Facemock::Database::User.new.name.size }
66
+ it { is_expected.to eq 10 }
67
+ end
68
+ end
69
+
70
+ describe '.email' do
71
+ before { @user = Facemock::Database::User.new }
72
+ subject { @user.email }
73
+ it { is_expected.to be_kind_of String }
74
+ it { is_expected.to eq "#{@user.name}@example.com" }
75
+ end
76
+
77
+ describe '.password' do
78
+ subject { Facemock::Database::User.new.password }
79
+ it { is_expected.to be_kind_of String }
80
+
81
+ describe '.size' do
82
+ subject { Facemock::Database::User.new.password.size }
83
+ it { is_expected.to eq 10 }
84
+ end
85
+ end
86
+
87
+ describe '.installed' do
88
+ subject { Facemock::Database::User.new.installed }
89
+ it { is_expected.to eq false }
90
+ end
91
+
92
+ describe '.access_token' do
93
+ subject { Facemock::Database::User.new.access_token }
94
+ it { is_expected.to be_kind_of String }
95
+
96
+ describe '.size' do
97
+ subject { Facemock::Database::User.new.access_token.size }
98
+ it { is_expected.to eq 128 }
99
+ end
100
+ end
101
+
102
+ describe '.application_id' do
103
+ subject { Facemock::Database::User.new.application_id }
104
+ it { is_expected.to be_nil }
105
+ end
106
+
107
+ describe '.created_at' do
108
+ subject { Facemock::Database::User.new.created_at }
109
+ it { is_expected.to be_nil }
110
+ end
111
+ end
112
+
113
+ context 'with id option but it is not integer' do
114
+ before { @opts = { id: "test_id" } }
115
+ subject { Facemock::Database::User.new(@opts) }
116
+ it { is_expected.to be_kind_of Facemock::Database::User }
117
+
118
+ describe '.id' do
119
+ subject { Facemock::Database::User.new(@opts).id }
120
+ it { is_expected.to be > 0 }
121
+ it { is_expected.to be < 100010000000000 }
122
+ end
123
+ end
124
+
125
+ context 'with application_id option but it is not integer' do
126
+ before { @opts = { application_id: "test_id" } }
127
+ subject { Facemock::Database::User.new(@opts) }
128
+ it { is_expected.to be_kind_of Facemock::Database::User }
129
+
130
+ describe '.application_id' do
131
+ subject { Facemock::Database::User.new(@opts).application_id }
132
+ it { is_expected.to be_nil }
133
+ end
134
+ end
135
+
136
+ context 'with name option' do
137
+ before do
138
+ @name = "test user"
139
+ @opts = { name: @name }
140
+ end
141
+ subject { Facemock::Database::User.new(@opts) }
142
+ it { is_expected.to be_kind_of Facemock::Database::User }
143
+
144
+ context '.name' do
145
+ subject { Facemock::Database::User.new(@opts).name }
146
+ it { is_expected.to eq @name }
147
+ end
148
+
149
+ context '.email' do
150
+ subject { Facemock::Database::User.new(@opts).email }
151
+ it { is_expected.to eq @name.gsub(" ", "_") + "@example.com" }
152
+ end
153
+ end
154
+
155
+ context 'with all options' do
156
+ subject { Facemock::Database::User.new(options) }
157
+ it { is_expected.to be_kind_of Facemock::Database::User }
158
+
159
+ context 'then attributes' do
160
+ it 'should set specified value by option' do
161
+ column_names.each do |column_name|
162
+ value = Facemock::Database::User.new(options).send(column_name)
163
+ expect(value).to eq options[column_name]
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,270 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::Database do
4
+ let(:db_name) { ".test" }
5
+ let(:default_db_name) { "facemock" }
6
+ let(:adapter) { "sqlite3" }
7
+ let(:table_names) { [:applications, :users, :permissions] }
8
+ let(:db_directory) { File.expand_path("../../../db", __FILE__) }
9
+ let(:db_filepath) { File.join(db_directory, "#{db_name}.#{adapter}") }
10
+
11
+ describe '::ADAPTER' do
12
+ subject { Facemock::Database::ADAPTER }
13
+ it { is_expected.to eq adapter }
14
+ end
15
+
16
+ describe '::DB_DIRECTORY' do
17
+ subject { Facemock::Database::DB_DIRECTORY }
18
+ it { is_expected.to eq db_directory }
19
+ end
20
+
21
+ describe '::TABLE_NAMES' do
22
+ subject { Facemock::Database::TABLE_NAMES }
23
+ it { is_expected.to eq table_names }
24
+ end
25
+
26
+ describe '::DEFAULT_DB_NAMES' do
27
+ subject { Facemock::Database::DEFAULT_DB_NAME }
28
+ it { is_expected.to eq default_db_name }
29
+ end
30
+
31
+ it 'should have a table class' do
32
+ expect(Facemock::Database::Table).to be_truthy
33
+ end
34
+
35
+ it 'should have a application class' do
36
+ expect(Facemock::Database::Application).to be_truthy
37
+ end
38
+
39
+ it 'should have a user class' do
40
+ expect(Facemock::Database::User).to be_truthy
41
+ end
42
+
43
+ it 'should have a permission class' do
44
+ expect(Facemock::Database::Permission).to be_truthy
45
+ end
46
+
47
+ describe '#initialize' do
48
+ before do
49
+ allow_any_instance_of(Facemock::Database).to receive(:connect) { true }
50
+ allow_any_instance_of(Facemock::Database).to receive(:create_tables) { true }
51
+ end
52
+
53
+ subject { lambda { Facemock::Database.new } }
54
+ it { is_expected.not_to raise_error }
55
+
56
+ describe '.name' do
57
+ subject { Facemock::Database.new.name }
58
+ it { is_expected.to eq default_db_name }
59
+ end
60
+ end
61
+
62
+ describe '#connect' do
63
+ before do
64
+ allow_any_instance_of(Facemock::Database).to receive(:create_tables) { true }
65
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
66
+ @database = Facemock::Database.new
67
+ end
68
+ after { @database.drop }
69
+
70
+ subject { lambda { @database.connect } }
71
+ it { is_expected.not_to raise_error }
72
+ it { expect(File.exist?(@database.filepath)).to eq true }
73
+ end
74
+
75
+ describe '#disconnect' do
76
+ before do
77
+ allow_any_instance_of(Facemock::Database).to receive(:create_tables) { true }
78
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
79
+ @database = Facemock::Database.new
80
+ end
81
+ after { @database.drop }
82
+
83
+ subject { lambda { @database.disconnect! } }
84
+ it { is_expected.not_to raise_error }
85
+
86
+ context 'when success' do
87
+ describe 'datbase file is not removed' do
88
+ before { @database.disconnect! }
89
+ it { expect(File.exist?(@database.filepath)).to eq true }
90
+ end
91
+ end
92
+ end
93
+
94
+ describe '#connected?' do
95
+ before do
96
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
97
+ @database = Facemock::Database.new
98
+ end
99
+ after { @database.drop }
100
+
101
+ context 'after new' do
102
+ subject { @database.connected? }
103
+ it { is_expected.to eq true }
104
+ end
105
+
106
+ context 'after disconnect!' do
107
+ before do
108
+ @database.disconnect!
109
+ end
110
+
111
+ subject { @database.connected? }
112
+ it { is_expected.to eq false }
113
+ end
114
+
115
+ context 'after connect' do
116
+ before do
117
+ @database.disconnect!
118
+ @database.connect
119
+ end
120
+
121
+ subject { @database.connected? }
122
+ it { is_expected.to eq true }
123
+ end
124
+ end
125
+
126
+ describe '#drop' do
127
+ before do
128
+ allow_any_instance_of(Facemock::Database).to receive(:create_tables) { true }
129
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
130
+ @database = Facemock::Database.new
131
+ end
132
+ after { @database.drop }
133
+
134
+ subject { lambda { @database.drop } }
135
+ it { is_expected.not_to raise_error }
136
+
137
+ context 'when success' do
138
+ describe 'database file does not exist' do
139
+ before { @database.drop }
140
+ it { expect(File.exist?(@database.filepath)).to eq false }
141
+ end
142
+
143
+ describe 're-drop is success' do
144
+ before { @database.drop }
145
+ subject { lambda { @database.drop } }
146
+ it { is_expected.not_to raise_error }
147
+ end
148
+ end
149
+ end
150
+
151
+ describe '#clear' do
152
+ before do
153
+ allow_any_instance_of(Facemock::Database).to receive(:create_tables) { true }
154
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
155
+ @database = Facemock::Database.new
156
+ expect(@database).to receive(:drop_tables)
157
+ expect(@database).to receive(:create_tables)
158
+ end
159
+ after { @database.drop }
160
+
161
+ subject { @database.clear }
162
+ it { is_expected.to be_truthy }
163
+ end
164
+
165
+ describe '#create_tables' do
166
+ before do
167
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
168
+ @database = Facemock::Database.new
169
+ @database.drop_tables
170
+ end
171
+ after { @database.drop }
172
+
173
+ subject { lambda { @database.create_tables } }
174
+ it { is_expected.not_to raise_error }
175
+ end
176
+
177
+ describe '#drop_table' do
178
+ before do
179
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
180
+ @database = Facemock::Database.new
181
+ end
182
+ after { @database.drop }
183
+
184
+ context 'when table exist' do
185
+ it 'should return true' do
186
+ table_names.each do |table_name|
187
+ expect(@database.drop_table(table_name)).to eq true
188
+ end
189
+ end
190
+ end
191
+
192
+ context 'when table does not exist' do
193
+ it 'should return true' do
194
+ @database.drop_tables
195
+ table_names.each do |table_name|
196
+ expect(@database.drop_table(table_name)).to eq false
197
+ end
198
+ end
199
+ end
200
+
201
+ context 'when database does not exist' do
202
+ it 'should return false' do
203
+ @database.drop
204
+ table_names.each do |table_name|
205
+ expect(@database.drop_table(table_name)).to eq false
206
+ end
207
+ end
208
+ end
209
+ end
210
+
211
+ describe '#drop_tables' do
212
+ before do
213
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
214
+ @database = Facemock::Database.new
215
+ end
216
+ after { @database.drop }
217
+
218
+ context 'when table exist' do
219
+ subject { @database.drop_tables }
220
+ it { is_expected.to eq true }
221
+ end
222
+
223
+ context 'when table does not exist' do
224
+ before { table_names.each{|table_name| @database.drop_table(table_name)} }
225
+ subject { @database.drop_tables }
226
+ it { is_expected.to eq true }
227
+ end
228
+
229
+ context 'when database does not exist' do
230
+ before { @database.drop }
231
+ subject { @database.drop_tables }
232
+ it { is_expected.to eq false }
233
+ end
234
+ end
235
+
236
+ describe '#filepath' do
237
+ before do
238
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
239
+ @database = Facemock::Database.new
240
+ end
241
+ after { @database.drop }
242
+
243
+ subject { @database.filepath }
244
+ it { is_expected.to eq db_filepath }
245
+
246
+ context 'then database file is exist' do
247
+ subject { File.exist? @database.filepath }
248
+ it { is_expected.to eq true }
249
+ end
250
+ end
251
+
252
+ describe '#table_exists?' do
253
+ before do
254
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
255
+ @database = Facemock::Database.new
256
+ end
257
+ after { @database.drop }
258
+
259
+ context 'when new' do
260
+ it 'should exist all tables' do
261
+ table_names.each do |table_name|
262
+ expect(@database.table_exists?(table_name)).to eq true
263
+ end
264
+ end
265
+ end
266
+
267
+ context 'when drop tables' do
268
+ end
269
+ end
270
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::Errors do
4
+ it 'should have a error module' do
5
+ expect(Facemock::Errors::Error.ancestors).to include StandardError
6
+ expect(Facemock::Errors::IncorrectDataFormat.ancestors).to include Facemock::Errors::Error
7
+ expect(Facemock::Errors::ColumnTypeNotNull.ancestors).to include Facemock::Errors::Error
8
+ end
9
+ end
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+
3
+ describe Facemock::FbGraph::Application::TestUsers do
4
+ let(:db_name) { ".test" }
5
+
6
+ let(:facebook_app_id) { "100000000000000" }
7
+
8
+ let(:default_limit) { 50 }
9
+ let(:default_after) { 0 }
10
+
11
+ before do
12
+ stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
13
+ @database = Facemock::Database.new
14
+ end
15
+ after { @database.drop }
16
+
17
+ describe '::DEFAULT_LIMIT' do
18
+ subject { Facemock::FbGraph::Application::TestUsers::DEFAULT_LIMIT }
19
+ it { is_expected.to eq default_limit }
20
+ end
21
+
22
+ describe '::DEFAULT_AFTER' do
23
+ subject { Facemock::FbGraph::Application::TestUsers::DEFAULT_AFTER }
24
+ it { is_expected.to eq default_after }
25
+ end
26
+
27
+ describe '#new' do
28
+ context 'without argument' do
29
+ subject { lambda { Facemock::FbGraph::Application::TestUsers.new } }
30
+ it { is_expected.to raise_error ArgumentError }
31
+ end
32
+
33
+ context 'with application id' do
34
+ subject { Facemock::FbGraph::Application::TestUsers.new(facebook_app_id) }
35
+ it { is_expected.to be_kind_of Array }
36
+
37
+ context 'when user does not exist' do
38
+ subject { Facemock::FbGraph::Application::TestUsers.new(facebook_app_id) }
39
+ it { is_expected.to be_empty }
40
+ end
41
+
42
+ context 'when user exist only one' do
43
+ before do
44
+ @user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
45
+ @user.save!
46
+ end
47
+
48
+ it 'should have user' do
49
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id)
50
+ expect(test_users).not_to be_empty
51
+ expect(test_users.count).to eq 1
52
+ test_user = test_users.first
53
+ Facemock::FbGraph::Application::User.column_names.each do |column_name|
54
+ expect(test_user.send(column_name)).to eq @user.send(column_name)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'with application id and options' do
61
+ context 'that limit is 1' do
62
+ context 'when user is exist only two' do
63
+ before do
64
+ 2.times do
65
+ Facemock::FbGraph::Application::User.new(application_id: facebook_app_id).save!
66
+ end
67
+ @limit = 1
68
+ end
69
+
70
+ it 'should have only one user' do
71
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id, limit: @limit)
72
+ expect(test_users).not_to be_empty
73
+ expect(test_users.count).to eq @limit
74
+ expect(test_users.first).to be_kind_of Facemock::FbGraph::Application::User
75
+ end
76
+ end
77
+ end
78
+
79
+ context 'that after is 1' do
80
+ context 'when user is exist only two' do
81
+ before do
82
+ 2.times do
83
+ Facemock::FbGraph::Application::User.new(application_id: facebook_app_id).save!
84
+ end
85
+ @after = 1
86
+ end
87
+
88
+ it 'should have only one user' do
89
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id, after: @after)
90
+ expect(test_users).not_to be_empty
91
+ expect(test_users.count).to eq 1
92
+ expect(test_users.first).to be_kind_of Facemock::FbGraph::Application::User
93
+ end
94
+ end
95
+ end
96
+
97
+ context 'that limit and after are both 1' do
98
+ context 'when user is exist only three' do
99
+ before do
100
+ 3.times do
101
+ Facemock::FbGraph::Application::User.new(application_id: facebook_app_id).save!
102
+ end
103
+ @options = { limit: 1, after: 1 }
104
+ end
105
+
106
+ it 'should have only one user' do
107
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id, @options)
108
+ expect(test_users).not_to be_empty
109
+ expect(test_users.count).to eq 1
110
+ expect(test_users.first).to be_kind_of Facemock::FbGraph::Application::User
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ describe '#collection' do
118
+ before do
119
+ user = Facemock::FbGraph::Application::User.new(application_id: facebook_app_id)
120
+ user.save!
121
+ @test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id)
122
+ end
123
+
124
+ it 'should equal self' do
125
+ collection = @test_users.collection
126
+ expect(collection.count).to eq @test_users.count
127
+ test_user = collection.first
128
+ Facemock::FbGraph::Application::User.column_names.each do |column_name|
129
+ expect(collection.first.send(column_name)).to eq @test_users.first.send(column_name)
130
+ end
131
+ end
132
+ end
133
+
134
+ describe '#next' do
135
+ before do
136
+ @app = Facemock::FbGraph::Application.new(facebook_app_id, secret: "test_secret")
137
+ 3.times { @app.test_user! }
138
+ end
139
+
140
+ it 'should get next users array' do
141
+ test_users = Facemock::FbGraph::Application::TestUsers.new(facebook_app_id, limit: 1)
142
+ expect(test_users.first).to be_kind_of Facemock::FbGraph::Application::User
143
+ expect(test_users.next.first).to be_kind_of Facemock::FbGraph::Application::User
144
+ expect(test_users.next.next.first).to be_kind_of Facemock::FbGraph::Application::User
145
+ expect(test_users.next.next.next.first).to be_kind_of NilClass
146
+ end
147
+ end
148
+
149
+ describe '#select' do
150
+ before { @expected = { limit: default_limit, after: default_after } }
151
+
152
+ subject { Facemock::FbGraph::Application::TestUsers.new(facebook_app_id).select }
153
+ it { is_expected.to eq @expected }
154
+ end
155
+ end