dbAccessor 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +2 -0
  3. data/.gitignore +22 -0
  4. data/.rspec +2 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +29 -0
  8. data/Rakefile +2 -0
  9. data/dbAccessor.gemspec +34 -0
  10. data/lib/dbAccessor/base.rb +100 -0
  11. data/lib/dbAccessor/dbConnector.rb +14 -0
  12. data/lib/dbAccessor/deleter/deleter.rb +73 -0
  13. data/lib/dbAccessor/reader/reader.rb +81 -0
  14. data/lib/dbAccessor/updater/updater.rb +83 -0
  15. data/lib/dbAccessor/version.rb +3 -0
  16. data/lib/dbAccessor/writer/writer.rb +70 -0
  17. data/lib/dbAccessor.rb +20 -0
  18. data/spec/deleter_spec.rb +90 -0
  19. data/spec/feed/dbTest/migration/add_deleted_to_utilisateur.rb +1 -0
  20. data/spec/feed/dbTest/migration/create_table_actions.rb +6 -0
  21. data/spec/feed/dbTest/migration/create_table_applications.rb +21 -0
  22. data/spec/feed/dbTest/migration/create_table_items.rb +4 -0
  23. data/spec/feed/dbTest/migration/create_table_users.rb +22 -0
  24. data/spec/feed/dbTest/migration/create_table_utilisateurs.rb +4 -0
  25. data/spec/feed/dbTest/model/action.rb +8 -0
  26. data/spec/feed/dbTest/model/application.rb +10 -0
  27. data/spec/feed/dbTest/model/item.rb +8 -0
  28. data/spec/feed/dbTest/model/user.rb +9 -0
  29. data/spec/feed/dbTest/model/utilisateur.rb +8 -0
  30. data/spec/feed/resources/do_migration.rb +5 -0
  31. data/spec/feed/resources/environment.rb +3 -0
  32. data/spec/feed/resources/populate.rb +28 -0
  33. data/spec/fixtures/env.rb +1 -0
  34. data/spec/reader_spec.rb +85 -0
  35. data/spec/spec_helper.rb +2 -0
  36. data/spec/updater_spec.rb +96 -0
  37. data/spec/writer_spec.rb +72 -0
  38. 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,4 @@
1
+ ActiveRecord::Migration.create_table :items do |t|
2
+ t.string :name
3
+ t.timestamps
4
+ end
@@ -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,4 @@
1
+ ActiveRecord::Migration.create_table :utilisateurs do |t|
2
+ t.string :name
3
+ t.timestamps
4
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+ require_relative 'utilisateur'
3
+ require_relative 'item'
4
+
5
+ class Action < ActiveRecord::Base
6
+ belongs_to :utilisateur
7
+ belongs_to :item
8
+ end
@@ -0,0 +1,10 @@
1
+
2
+ require 'active_record'
3
+ require_relative 'user'
4
+
5
+ class Application < ActiveRecord::Base
6
+
7
+ validates_presence_of :name, :api_key, :url
8
+ validates_uniqueness_of :api_key, :url
9
+ belongs_to :user
10
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+ require_relative 'utilisateur'
3
+ require_relative 'action'
4
+
5
+ class Item < ActiveRecord::Base
6
+ has_many :actions
7
+ has_many :users, :through => :actions
8
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_record'
2
+ require_relative 'application'
3
+
4
+ class User < ActiveRecord::Base
5
+
6
+ validates_presence_of :login, :password, :email
7
+ validates_uniqueness_of :login, :email
8
+ has_many :applications
9
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+ require_relative 'item'
3
+ require_relative 'action'
4
+
5
+ class Utilisateur < ActiveRecord::Base
6
+ has_many :actions
7
+ has_many :items, :through => :actions
8
+ end
@@ -0,0 +1,5 @@
1
+ require_relative '../../../lib/dbAccessor/dbConnector.rb'
2
+ require_relative '../dbTest/migration/create_table_users'
3
+ require_relative '../dbTest/migration/create_table_applications'
4
+
5
+
@@ -0,0 +1,3 @@
1
+ require_relative '../../../lib/dbAccessor/dbConnector'
2
+ require_relative '../dbTest/model/application'
3
+ require_relative '../dbTest/model/user'
@@ -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'
@@ -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
@@ -0,0 +1,2 @@
1
+ require_relative 'fixtures/env'
2
+ require 'uuid'
@@ -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
@@ -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