dbAccessor 0.0.7
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.
- checksums.yaml +7 -0
- data/.autotest +2 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/dbAccessor.gemspec +34 -0
- data/lib/dbAccessor/base.rb +100 -0
- data/lib/dbAccessor/dbConnector.rb +14 -0
- data/lib/dbAccessor/deleter/deleter.rb +73 -0
- data/lib/dbAccessor/reader/reader.rb +81 -0
- data/lib/dbAccessor/updater/updater.rb +83 -0
- data/lib/dbAccessor/version.rb +3 -0
- data/lib/dbAccessor/writer/writer.rb +70 -0
- data/lib/dbAccessor.rb +20 -0
- data/spec/deleter_spec.rb +90 -0
- data/spec/feed/dbTest/migration/add_deleted_to_utilisateur.rb +1 -0
- data/spec/feed/dbTest/migration/create_table_actions.rb +6 -0
- data/spec/feed/dbTest/migration/create_table_applications.rb +21 -0
- data/spec/feed/dbTest/migration/create_table_items.rb +4 -0
- data/spec/feed/dbTest/migration/create_table_users.rb +22 -0
- data/spec/feed/dbTest/migration/create_table_utilisateurs.rb +4 -0
- data/spec/feed/dbTest/model/action.rb +8 -0
- data/spec/feed/dbTest/model/application.rb +10 -0
- data/spec/feed/dbTest/model/item.rb +8 -0
- data/spec/feed/dbTest/model/user.rb +9 -0
- data/spec/feed/dbTest/model/utilisateur.rb +8 -0
- data/spec/feed/resources/do_migration.rb +5 -0
- data/spec/feed/resources/environment.rb +3 -0
- data/spec/feed/resources/populate.rb +28 -0
- data/spec/fixtures/env.rb +1 -0
- data/spec/reader_spec.rb +85 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/updater_spec.rb +96 -0
- data/spec/writer_spec.rb +72 -0
- metadata +288 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
class CreateApplications < ActiveRecord::Migration
|
3
|
+
def self.up
|
4
|
+
create_table :applications do |a|
|
5
|
+
a.string :name
|
6
|
+
a.uuid :api_key
|
7
|
+
a.string :url
|
8
|
+
a.boolean :deleted, :default => false
|
9
|
+
a.integer :user_id
|
10
|
+
a.timestamps
|
11
|
+
end
|
12
|
+
add_index :applications, :user_id
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
drop_table :applications
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
CreateApplications.up
|
21
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
class CreateUsers < ActiveRecord::Migration
|
4
|
+
def self.up
|
5
|
+
create_table :users do |u|
|
6
|
+
u.string :login
|
7
|
+
u.string :password
|
8
|
+
u.string :firstname
|
9
|
+
u.string :lastname
|
10
|
+
u.string :gender
|
11
|
+
u.string :email
|
12
|
+
u.boolean :deleted, :default => false
|
13
|
+
u.timestamps
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :users
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
CreateUsers.up
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'environment'
|
2
|
+
require 'uuid'
|
3
|
+
|
4
|
+
class Populate
|
5
|
+
#generating 10 items
|
6
|
+
# p "Creating 5 users:"
|
7
|
+
i = 1
|
8
|
+
5.times do
|
9
|
+
p User.create( login: 'fabira'+i.to_s,password: 'passer', firstname: 'binette'+i.to_s, lastname: 'dieng',gender: 'F',email: 'fabira'+i.to_s+'gmail.com',deleted: false )
|
10
|
+
i = i + 1
|
11
|
+
end
|
12
|
+
|
13
|
+
uuid = UUID.new
|
14
|
+
|
15
|
+
#linking users and items by actions
|
16
|
+
p "Creating actions on items by users"
|
17
|
+
p Application.create( name: 'Facebook',api_key: uuid.generate,url: 'http://www.facebook.com',deleted: false,user_id: User.first.id )
|
18
|
+
|
19
|
+
p Application.create( name: 'Twitter',api_key: uuid.generate,url: 'http://www.twitter.com',deleted: false,user_id: User.find(2).id )
|
20
|
+
|
21
|
+
p Application.create( name: 'Axolot',api_key: uuid.generate,url: 'http://www.axolot.com',deleted: false,user_id: User.last.id )
|
22
|
+
|
23
|
+
p Application.create( name: 'Gmail',api_key: uuid.generate,url: 'http://www.gmail.com',deleted: false,user_id: User.find(4).id )
|
24
|
+
|
25
|
+
|
26
|
+
p "Done! ;)"
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'dbAccessor'
|
data/spec/reader_spec.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe DbAccessor::Reader do
|
5
|
+
|
6
|
+
describe ".simple_select" do
|
7
|
+
context "when all model's items are found" do
|
8
|
+
it {expect((DbAccessor::Reader.simple_select({model_name: "User",action_on: "all"}))[:code]).to eq 200 }
|
9
|
+
end
|
10
|
+
context "when the first model's item is found" do
|
11
|
+
it {expect((DbAccessor::Reader.simple_select({model_name: "User",action_on: "first"}))[:code]).to eq 200 }
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when the last model's item is found" do
|
15
|
+
it {expect((DbAccessor::Reader.simple_select({model_name: "Application",action_on: "last"}))[:code]).to eq 200 }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "when it reads many items according to the condition" do
|
19
|
+
it {expect((DbAccessor::Reader.simple_select({model_name: "User",action_on: nil}))[:code]).to eq 404 }
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when passed parameter's keys are not correct" do
|
23
|
+
it {expect((DbAccessor::Reader.simple_select({model: "User",action: nil}))[:code]).to eq 404 }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe ".condition_select" do
|
28
|
+
|
29
|
+
context "when it reads one or many items on table with a condition" do
|
30
|
+
it {expect((DbAccessor::Reader.condition_select({model_name: "User" , condition: {login: "fabira3"}}))[:code]).to eq 200 }
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when the condition is not satisfied" do
|
34
|
+
it {expect((DbAccessor::Reader.condition_select({model_name: "Application" , condition: {name: "fab"}}))[:code]).to eq 404 }
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when one or many parameters are empty or nil" do
|
38
|
+
it {expect((DbAccessor::Reader.condition_select({model_name: "", condition: nil }))[:code]).to eq 404 }
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when condition is not correct" do
|
42
|
+
it {expect((DbAccessor::Reader.condition_select({model_name: "", condition: 1 }))[:code]).to eq 404 }
|
43
|
+
|
44
|
+
it {expect((DbAccessor::Reader.condition_select({model_name: "", condition: "pas une condition"}))[:code]).to eq 404 }
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when passed parameter's keys are not correct" do
|
48
|
+
it {expect((DbAccessor::Reader.simple_select({model: "User",cond: nil}))[:code]).to eq 404 }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe ".select_by_id" do
|
53
|
+
|
54
|
+
context "when it reads on a model with an id" do
|
55
|
+
it {expect((DbAccessor::Reader.select_by_id({model_name: "User",identifiers: [7,8,9]}))[:code]).to eq 200 }
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when one of the ids is incorrect" do
|
59
|
+
it {expect((DbAccessor::Reader.select_by_id({model_name: "User",identifiers: [1,2,'c']}))[:code]).to eq 404 }
|
60
|
+
end
|
61
|
+
|
62
|
+
context "when one of the parameter is nil" do
|
63
|
+
it {expect((DbAccessor::Reader.select_by_id({model_name: "",identifiers: [1,2,3]}))[:code]).to eq 404 }
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
describe ".linked_models_select" do
|
69
|
+
|
70
|
+
context "when it reads models on model with a condition" do
|
71
|
+
it {expect((DbAccessor::Reader.linked_models_select({model_name: "User", condition: { id: 2}, models: "applications"}))[:code]).to eq 200 }
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when a parameter is empty or empty" do
|
75
|
+
it {expect((DbAccessor::Reader.linked_models_select({model_name: "", condition: nil, models: nil }))[:code]).to eq 404 }
|
76
|
+
end
|
77
|
+
|
78
|
+
context "when models are incorrect" do
|
79
|
+
it {expect((DbAccessor::Reader.linked_models_select({model_name: "", condition: nil, models: "pas de model" }))[:code]).to eq 404 }
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe DbAccessor::Updater do
|
4
|
+
|
5
|
+
describe ".update_by_id" do
|
6
|
+
context "updates one or many users by id" do
|
7
|
+
param = {model_name: "User",attributes: { 3 => {login: "fabira"}} }
|
8
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 200 }
|
9
|
+
end
|
10
|
+
|
11
|
+
context "updates one or many applications by id" do
|
12
|
+
param = {model_name: "Application",attributes: { 3 => { name: "apache"},2 => {name: "site du zero"}} }
|
13
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 200 }
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when attributes format is not correct" do
|
17
|
+
param = {model_name: "Application",attributes: { 'c' => { name: "apache"},2 => {name: "site du zero"}} }
|
18
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 400 }
|
19
|
+
|
20
|
+
param[:attributes] = { 3 => { na: "apache"},2 => {name: "site du zero"}}
|
21
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 400 }
|
22
|
+
|
23
|
+
param[:attributes] = { 3 => { name: 1},2 => {name: "site du zero"}}
|
24
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 400 }
|
25
|
+
|
26
|
+
param[:attributes] = { 3 => nil,2 => {name: "site du zero"}}
|
27
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 400 }
|
28
|
+
|
29
|
+
param[:attributes] = { 3 => " ",2 => {name: "site du zero"}}
|
30
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 400 }
|
31
|
+
|
32
|
+
param[:attributes] = { 3 => {},2 => {name: "site du zero"}}
|
33
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 400 }
|
34
|
+
|
35
|
+
param[:attributes] = { 3 => {name: nil},2 => {name: "site du zero"}}
|
36
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 400 }
|
37
|
+
|
38
|
+
param[:attributes] = { 3 => {na: 'to'},2 => {name: "site du zero"}}
|
39
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 400 }
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when the object is not found" do
|
43
|
+
param = {model_name: "User",attributes: { 100 => { login: "fabi"}} }
|
44
|
+
it {expect((DbAccessor::Updater.update_by_id(param))[:code]).to eq 400 }
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when the passed parameter is nil" do
|
48
|
+
it {expect((DbAccessor::Updater.update_by_id(nil))[:code]).to eq 400 }
|
49
|
+
end
|
50
|
+
|
51
|
+
context "when attributes to update are empty" do
|
52
|
+
it {expect((DbAccessor::Updater.update_by_id(model_name: "User",attributes: {}))[:code]).to eq 400 }
|
53
|
+
end
|
54
|
+
|
55
|
+
context "when the model name is incorrect" do
|
56
|
+
it {expect((DbAccessor::Updater.update_by_id(model_name: "Us",attributes: {}))[:code]).to eq 400 }
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when the attributes are not correct" do
|
60
|
+
it {expect((DbAccessor::Updater.update_by_id(model_name: "Application",attributes: "coucou"))[:code]).to eq 400 }
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
describe ".update_all_objects" do
|
66
|
+
parameter={model_name: "User", attributes: {login: 'fabira3',password: 'passe'}}
|
67
|
+
context "when it updates all model objects" do
|
68
|
+
it {expect((DbAccessor::Updater.update_all_objects(parameter))[:code]).to eq 200 }
|
69
|
+
end
|
70
|
+
|
71
|
+
context "when all model objects that satisfy the condition are updated" do
|
72
|
+
condition="id == 3"
|
73
|
+
it {expect((DbAccessor::Updater.update_all_objects(parameter,condition))[:code]).to eq 200 }
|
74
|
+
end
|
75
|
+
|
76
|
+
context "when model's attributes are not correct" do
|
77
|
+
param={model_name: "User", attributes: {login: 'fabira',password: 3}}
|
78
|
+
it {expect((DbAccessor::Updater.update_all_objects(param))[:code]).to eq 400 }
|
79
|
+
end
|
80
|
+
|
81
|
+
context "when one of the model's attributes is nil" do
|
82
|
+
param={model_name: "User", attributes: {login: 'fabira',password: nil}}
|
83
|
+
it {expect((DbAccessor::Updater.update_all_objects(param))[:code]).to eq 400 }
|
84
|
+
end
|
85
|
+
|
86
|
+
context "when attributes'keys are not correct" do
|
87
|
+
param={model_name: "User", attributes: {lon: 'fabira',pasord: 'pass'}}
|
88
|
+
it {expect((DbAccessor::Updater.update_all_objects(param))[:code]).to eq 400 }
|
89
|
+
end
|
90
|
+
|
91
|
+
context "returns false when attributes keys don't correspond to the model" do
|
92
|
+
param={model_name: "Application", attributes: {login: 'fabira3',password: 'passera'}}
|
93
|
+
it {expect((DbAccessor::Updater.update_all_objects(param))[:code]).to eq 400 }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/spec/writer_spec.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe DbAccessor::Writer do
|
5
|
+
|
6
|
+
nb = rand(1000)
|
7
|
+
api_key = UUID.new.generate
|
8
|
+
|
9
|
+
let(:user) { {model_name: 'User',content: {login: 'ab'+nb.to_s,password: 'passer',firstname: 'Ndeye Fatou', lastname: 'Dieng',email: 'ab'+nb.to_s+'@gmail.com',gender: 'M'}} }
|
10
|
+
|
11
|
+
let(:app) { {model_name: 'Application',content: {name: 'Wiki'+nb.to_s,api_key: api_key,url: 'http://www.wiki'+nb.to_s+'.com',user_id: 2}} }
|
12
|
+
|
13
|
+
|
14
|
+
describe".write_object" do
|
15
|
+
|
16
|
+
context "when a user is saved in database" do
|
17
|
+
it {expect((DbAccessor::Writer.write_object(user))[:code]).to eq 201}
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when an application is saved in database" do
|
21
|
+
it {expect((DbAccessor::Writer.write_object(app))[:code]).to eq 201}
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when the object's parameters are empty" do
|
25
|
+
it {expect((DbAccessor::Writer.write_object({model_name: '',content: user[:content]}))[:code]).to eq 400 }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when the model_name contains numbers" do
|
29
|
+
it {expect((DbAccessor::Writer.write_object({model_name: 'DGH67889',content: user[:content]}))[:code]).to eq 400 }
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when passed parameter's keys are incorrect" do
|
33
|
+
it {expect((DbAccessor::Writer.write_object({model: 'DGH67889',con: user[:content]}))[:code]).to eq 400 }
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when the parameter is not a hash" do
|
37
|
+
it { expect((DbAccessor::Writer.write_object("coucou"))[:code]).to eq 400 }
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when the parameter's keys are not correct" do
|
41
|
+
it { expect((DbAccessor::Writer.write_object({t: "coucou"}))[:code]).to eq 400 }
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when required parameters are not given" do
|
45
|
+
user = {model_name: 'User',content: {firstname: 'ablaye', lastname: 'Dieng',gender: 'M'} }
|
46
|
+
it { expect((DbAccessor::Writer.write_object(user))[:code]).to eq 400 }
|
47
|
+
end
|
48
|
+
|
49
|
+
context "when required parameters are not correct" do
|
50
|
+
user = {model_name: 'User',content: {login: nb.to_s,password: 'passer',firstname: 'Ndeye Fatou', lastname: 'Dieng',email: 'fatou',gender: 'M'}}
|
51
|
+
it { expect((DbAccessor::Writer.write_object(user))[:code]).to eq 400 }
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when the content doesn't correspond to the model_name" do
|
55
|
+
it { expect((DbAccessor::Writer.write_object( {model_name: 'User',content: {name: 'Wiki',api_key: api_key,url: 'http://www.wiki.com',user_id: 2}} ))[:code]).to eq 400 }
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when the content is incorrect" do
|
59
|
+
it { expect((DbAccessor::Writer.write_object( {model_name: 'Utilisateur',content: "coucou"} ))[:code]).to eq 400 }
|
60
|
+
end
|
61
|
+
|
62
|
+
context "when the model_name is missing" do
|
63
|
+
it { expect((DbAccessor::Writer.write_object( {model_name: '',content: {name: 'Wiki',api_key: api_key,url: 'http://www.wiki.com',user_id: 2}} ))[:code]).to eq 400 }
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when the content is not correct" do
|
67
|
+
it { expect((DbAccessor::Writer.write_object( {model_name: 'Application',content: {login: nb.to_s,password: 'passer',firstname: 'Ndeye Fatou', lastname: 'Dieng',email: 'fatou',gender: 'M'}} ))[:code]).to eq 400 }
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|