dbAccessor 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a41358d7b31fa18301d319fb3c36551ca9aff86
4
- data.tar.gz: f71da5be6755d7d1b56a34713530c958a3a501ac
3
+ metadata.gz: b880eb3b9ef4c01a57315c4b04b50bf3ca90dd7d
4
+ data.tar.gz: 1fc7cca67db5273802dea1010b932e7582c0281c
5
5
  SHA512:
6
- metadata.gz: 2a681cbec809a0b117631dad40ca02201f6d3c6244b2fcc5e84436f6670a0b59262581bf3ecdde9cf644735f294a3a1b5678b36c1bc672b7b4e587b5621c7e05
7
- data.tar.gz: 1ba240a0cc60456647e82bba6da60c4b33cde3c7f55759748a7eaf467dd7af6b36f9904f80ea15bb3e654078180c4a0f0c623c91f6a170c94d809d46fb4c6276
6
+ metadata.gz: 6603078e3d289a6a5a02d1a08e879ffbd5a290766b8e90599531df582b3c075f8fa4d399fa8b472af84d5d2554d7aedc960feb241ad018228f12192a7bcfdc94
7
+ data.tar.gz: 32a808da4e3bf5fc7dcb4b1aff528dd592c9f45e53b464d1084fdde2619aa546e146dc5d3c024446b0d3537df89824eb0cdbb77e958e211f92e40f3d8f84ec9a
@@ -1,18 +1,14 @@
1
1
  module Db_Accessor
2
-
3
2
  class Base
4
- USER_MODEL = 'User'
5
- APP_MODEL = 'Application'
6
- ENGINE_MODEL = 'Engine'
3
+ USER_MODEL = 'User'
4
+ APP_MODEL = 'Application'
5
+ ENGINE_MODEL = 'Engine'
7
6
  ENGINE_DATA_MODEL = 'Engine_data'
8
- ACTION_MODEL = 'Action'
9
- ASSIGNMENT_MODEL = 'Assignment'
10
-
11
-
7
+
12
8
  def self.validate_argument(given_param)
13
9
  resp = nil
14
10
  given_param.values.each do |v|
15
- if v.nil?
11
+ if v.nil?
16
12
  resp = true
17
13
  break
18
14
  elsif v.is_a?(String) && v.empty?
@@ -34,7 +30,7 @@ module Db_Accessor
34
30
  end
35
31
 
36
32
  def self.is_model?(param)
37
- model = [ USER_MODEL, APP_MODEL, ENGINE_MODEL, ENGINE_DATA_MODEL,ACTION_MODEL,ASSIGNMENT_MODEL]
33
+ model = [ USER_MODEL, APP_MODEL, ENGINE_MODEL, ENGINE_DATA_MODEL ]
38
34
  model.include?(param)
39
35
  end
40
36
 
@@ -42,8 +38,7 @@ module Db_Accessor
42
38
  param.is_a?(Hash)
43
39
  end
44
40
 
45
-
46
- def self.valid_keys?(param,waited_keys)
41
+ def self.valid_keys?(param,waited_keys)
47
42
  waited_keys.each do |k|
48
43
  if !(param.has_key?(k))
49
44
  return false
@@ -51,9 +46,9 @@ module Db_Accessor
51
46
  end
52
47
  end
53
48
  return true
54
- end
49
+ end
55
50
 
56
- def self.verify_keys(keys,attributes)
51
+ def self.verify_keys(keys,attributes)
57
52
  keys.each do |k|
58
53
  if !(attributes.include?(k))
59
54
  return false
@@ -61,31 +56,27 @@ module Db_Accessor
61
56
  end
62
57
  end
63
58
  return true
64
- end
59
+ end
65
60
 
66
- def self.valid_object_columns?(model_name,content)
67
- engine_columns = [:id,:name,:application_id]
68
- engine_data_columns = [:id,:customer_id,:item_id,:preference,:engine_id]
69
- action_columns = [:id,:name,:score]
70
- assignment_columns = [:id,:engine_id,:action_id]
71
- app_columns = [:id,:name,:api_key,:url,:user_id]
72
- user_columns = [:id,:login,:password,:firstname,:lastname,:gender,:email]
73
-
74
- keys = content.keys
75
- if model_name == "User"
76
- attributes = user_columns
77
- elsif model_name == "Application"
78
- attributes = app_columns
79
- elsif model_name == "Engine"
61
+ def self.valid_object_columns?(model_name,content)
62
+ engine_columns = [:id,:engine_conf,:application_id]
63
+ engine_data_columns = [:id,:customer_id,:item_id,:preference,:engine_id]
64
+ app_columns = [:id,:name,:api_key,:url,:user_id]
65
+ user_columns = [:id,:login,:password,:firstname,:lastname,:gender,:email]
66
+
67
+ keys = content.keys
68
+ if model_name == "User"
69
+ attributes = user_columns
70
+ elsif model_name == "Application"
71
+ attributes = app_columns
72
+ elsif model_name == "Engine"
80
73
  attributes = engine_columns
81
- elsif model_name == "Engine_data"
74
+ else
82
75
  attributes = engine_data_columns
83
- else
84
- attributes = action_columns
76
+ end
77
+
78
+ verify_keys(keys,attributes)
85
79
  end
86
-
87
- verify_keys(keys,attributes)
88
- end
89
80
 
90
81
  def self.valid_class?(keys,columns,attributes)
91
82
  keys.each do |k|
@@ -111,10 +102,5 @@ module Db_Accessor
111
102
  valid_class?(keys,string_columns,attributes)
112
103
  end
113
104
 
114
-
115
-
116
-
117
-
118
- end
119
-
105
+ end
120
106
  end
@@ -3,48 +3,44 @@ require_relative '../base'
3
3
 
4
4
  class DbAccessor::Reader < Db_Accessor::Base
5
5
 
6
-
6
+
7
7
  def self.simple_select(given_param)
8
8
  waited_keys = [:model_name,:action_on]
9
9
  if validate(given_param,waited_keys)
10
- result = get_model_name(given_param).where(deleted: false).send(given_param[:action_on])
11
- result.nil? ? return_response(404,"Not found") : return_response(200,result)
10
+ result = get_model_name(given_param).where(deleted: false).send(given_param[:action_on])
11
+ result.nil? ? return_response(404,"Not found") : return_response(200,result)
12
12
  else
13
- return_response(404,"Not found")
13
+ return_response(404,"Not found")
14
14
  end
15
- rescue ActiveRecord::RecordNotFound => e
16
- return_response(404,"Not found")
15
+ rescue ActiveRecord::RecordNotFound => e
16
+ return_response(404,"Not found")
17
17
  end
18
-
19
-
18
+
20
19
  def self.condition_select(given_param)
21
20
  waited_keys = [:model_name,:condition]
22
- if validate(given_param,waited_keys)
23
- result = get_model_name(given_param).where(given_param[:condition],deleted: false)
24
- ( result.nil? || result == []) ? return_response(404,"Not found") : return_response(200,result)
21
+ if validate(given_param,waited_keys)
22
+ result = get_model_name(given_param).where(given_param[:condition],deleted: false)
23
+ ( result.nil? || result == []) ? return_response(404,"Not found") : return_response(200,result)
25
24
  else
26
25
  return_response(404,"Not found")
27
26
  end
28
- rescue ActiveRecord::RecordNotFound => e
29
- return_response(400,"Not found")
27
+ rescue ActiveRecord::RecordNotFound => e
28
+ return_response(400,"Not found")
30
29
  end
31
30
 
32
-
33
31
  def self.select_by_id(given_param)
34
32
  waited_keys = [:model_name,:identifiers]
35
33
  if validate(given_param,waited_keys) && validate_id(given_param[:identifiers])
36
34
  model_name = get_model_name(given_param)
37
- result = model_name.where(deleted: false).where(id: given_param[:identifiers])
35
+ result = model_name.where(deleted: false).where(id: given_param[:identifiers])
38
36
  result.nil? ? return_response(404,"Not found") : return_response(200,result)
39
37
  else
40
38
  return_response(404,"Not found")
41
39
  end
42
- rescue ActiveRecord::RecordNotFound => e
43
- return_response(404,"Not found")
40
+ rescue ActiveRecord::RecordNotFound => e
41
+ return_response(404,"Not found")
44
42
  end
45
43
 
46
-
47
-
48
44
  def self.linked_models_select(given_param)
49
45
  waited_keys = [:model_name,:condition,:models]
50
46
  if validate(given_param,waited_keys)
@@ -52,30 +48,30 @@ class DbAccessor::Reader < Db_Accessor::Base
52
48
  condition = given_param[:condition]
53
49
  models = given_param[:models]
54
50
  result = model_name.where(condition,deleted: false).first
55
- result.nil? ? return_response(404,"Not found") : return_response(200,result.send(models))
51
+ result.nil? ? return_response(404,"Not found") : return_response(200,result.send(models))
56
52
  else
57
53
  return_response(404,"Not found")
58
54
  end
59
- rescue ActiveRecord::RecordNotFound => e
60
- return_response(400,"Not found")
61
- end
62
- private ##################################################################
55
+ rescue ActiveRecord::RecordNotFound => e
56
+ return_response(400,"Not found")
57
+ end
58
+ private ##################################################################
63
59
 
64
60
  def self.validate(param,waited_keys)
65
- valid_hash?(param) && valid_keys?(param,waited_keys) && !validate_argument(param) && is_model?(param[:model_name])
61
+ valid_hash?(param) && valid_keys?(param,waited_keys) && !validate_argument(param) && is_model?(param[:model_name])
66
62
  end
67
-
63
+
68
64
  def self.validate_id(param)
69
- test_id = lambda do |param|
70
- param.each do |i|
71
- if !(i.is_a?(Fixnum))
72
- return false
73
- break
74
- end
75
- end
76
- return true
77
- end
78
- param.is_a?(Array) && test_id.call(param)
65
+ test_id = lambda do |param|
66
+ param.each do |i|
67
+ if !(i.is_a?(Fixnum))
68
+ return false
69
+ break
70
+ end
71
+ end
72
+ return true
73
+ end
74
+ param.is_a?(Array) && test_id.call(param)
79
75
  end
80
76
 
81
- end
77
+ end
@@ -1,9 +1,9 @@
1
1
  require_relative '../../../spec/feed/resources/environment'
2
2
  require_relative '../base'
3
3
 
4
- class DbAccessor::Updater < Db_Accessor::Base
4
+ class DbAccessor::Updater < Db_Accessor::Base
5
+
5
6
 
6
- #updates given object(s) with given attributes using object's identifier named 'id'
7
7
  def self.update_by_id(given_object)
8
8
  waited_keys = [:model_name,:attributes]
9
9
  if validate(given_object,waited_keys) && valid_attributes_pattern?(given_object[:model_name],given_object[:attributes])
@@ -16,7 +16,7 @@ class DbAccessor::Updater < Db_Accessor::Base
16
16
  return_response(400,"Not updated")
17
17
  end
18
18
 
19
- #updates all model's objects or model's objects that satisfy the given condition with given attributes
19
+
20
20
  def self.update_all_objects(given_param,condition="")
21
21
  waited_keys = [:model_name,:attributes]
22
22
  if validate(given_param,waited_keys) && valid_object_columns?(given_param[:model_name],given_param[:attributes]) && valid_attributes_values?(given_param[:attributes])
@@ -30,8 +30,8 @@ class DbAccessor::Updater < Db_Accessor::Base
30
30
  result = update_by_id(hash)
31
31
  break if !result
32
32
  end
33
- result ? return_response(200,"Successfully updated") : return_response(400,"Not updated")
34
- else
33
+ result ? return_response(200,"Successfully updated") : return_response(400,"Not updated")
34
+ else
35
35
  return_response(400,"Not updated")
36
36
  end
37
37
  end
@@ -45,8 +45,8 @@ class DbAccessor::Updater < Db_Accessor::Base
45
45
  def self.validate_attributes(given_param)
46
46
  given_param.is_a?(Hash) && !given_param.empty?
47
47
  end
48
-
49
-
48
+
49
+
50
50
  def self.valid_fixnum_keys?(keys)
51
51
  keys.each do |k|
52
52
  if !(k.is_a?(Fixnum))
@@ -59,7 +59,7 @@ class DbAccessor::Updater < Db_Accessor::Base
59
59
 
60
60
  def self.valid_values_pattern?(values,model_name)
61
61
  values.each do |v|
62
- if !(v.nil?)
62
+ if !(v.nil?)
63
63
  if v.empty? || !(v.is_a?(Hash)) || !valid_object_columns?(model_name,v) || !valid_attributes_values?(v)
64
64
  return false
65
65
  break
@@ -81,7 +81,7 @@ class DbAccessor::Updater < Db_Accessor::Base
81
81
  end
82
82
 
83
83
  def self.validate(param,waited_keys)
84
- param.is_a?(Hash) && valid_keys?(param,waited_keys) && is_model?(param[:model_name]) && validate_attributes(param[:attributes]) && !validate_argument(param)
84
+ param.is_a?(Hash) && valid_keys?(param,waited_keys) && is_model?(param[:model_name]) && validate_attributes(param[:attributes]) && !validate_argument(param)
85
85
  end
86
86
 
87
87
  end
@@ -1,3 +1,3 @@
1
1
  module DbAccessor
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -3,8 +3,10 @@ require_relative '../base'
3
3
 
4
4
  class DbAccessor::Writer < Db_Accessor::Base
5
5
 
6
- USER_MODEL = 'User'
7
- APP_MODEL = 'Application'
6
+ USER_MODEL = 'User'
7
+ ENGINE_MODEL = 'Engine'
8
+ ENGINE_DATA_MODEL = 'Engine_data'
9
+ APP_MODEL = 'Application'
8
10
 
9
11
  def self.write_object(object_param)
10
12
  waited_keys =[:model_name,:content]
@@ -12,14 +14,13 @@ class DbAccessor::Writer < Db_Accessor::Base
12
14
  @given_object = get_object(object_param)
13
15
  @given_object.save
14
16
  !@given_object.new_record? ? code = 201 : code = 400
15
- return_response(code,"Success")
17
+ return_response(code,"Success")
16
18
  else
17
19
  return_response(400,"Failed")
18
20
  end
19
21
  end
20
22
 
21
23
  private ######################################################################
22
-
23
24
  def self.get_content(object_param)
24
25
  object_param[:content]
25
26
  end
@@ -27,47 +28,30 @@ private ######################################################################
27
28
  def self.get_object(object_param)
28
29
  get_model_name(object_param).new(get_content(object_param))
29
30
  end
30
-
31
+
31
32
  def self.validate_required_params(param)
32
33
  content = param[:content]
33
34
  model_name = param[:model_name]
34
- if model_name == 'User'
35
+ if model_name == USER_MODEL
35
36
  email_pattern = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
36
- content[:login].is_a?(String) && content[:password].is_a?(String) && content[:email].is_a?(String) && !(email_pattern.match(content[:email])).nil?
37
- elsif model_name == 'Application'
37
+ content[:login].is_a?(String) && content[:password].is_a?(String) && content[:email].is_a?(String) && !(email_pattern.match(content[:email])).nil?
38
+
39
+ elsif model_name == APP_MODEL
38
40
  content[:name].is_a?(String) && content[:api_key].is_a?(String) && content[:url].is_a?(String)
39
- elsif model_name == 'Engine'
40
- content[:name].is_a?(String) && content[:application_id].is_a?(Fixnum)
41
+
42
+ elsif model_name == ENGINE_MODEL
43
+ content[:engine_conf].is_a?(String) && content[:application_id].is_a?(Fixnum)
44
+
45
+ elsif model_name == ENGINE_DATA_MODEL
46
+ content[:customer_id].is_a?(Fixnum) && content[:item_id].is_a?(Fixnum) && content[:preference].is_a?(Fixnum) && content[:engine_id].is_a?(Fixnum)
41
47
  else
42
48
  false
43
49
  end
44
50
  end
45
51
 
46
-
47
52
  def self.validate(param,waited_keys)
48
53
  valid_hash?(param) && valid_keys?(param,waited_keys) && is_model?(param[:model_name]) && valid_object_columns?(param[:model_name],param[:content]) && !validate_argument(param) && !validate_argument(param[:content]) && validate_required_params(param)
49
54
 
50
55
  end
51
-
52
-
53
-
54
- end
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
56
+ end
73
57
 
data/spec/deleter_spec.rb CHANGED
@@ -1,90 +1,90 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
- describe DbAccessor::Deleter do
3
+ describe DbAccessor::Deleter do
4
4
 
5
5
  describe ".delete_all" do
6
- given_param = { model_name: "User",condition: {id: 3}}
6
+ given_param = { model_name: "User",condition: {id: 3}}
7
7
  context "when the object is deleted" do
8
- it {expect((DbAccessor::Deleter.delete_all(given_param))[:code]).to eq 200 }
8
+ it {expect((DbAccessor::Deleter.delete_all(given_param))[:code]).to eq 200 }
9
9
 
10
- it {expect((DbAccessor::Deleter.delete_all({model_name: "Application",condition: {name: 'Wikia'}}))[:code]).to eq 200 }
11
-
12
- it {expect((DbAccessor::Deleter.delete_all({model_name: "User",condition: {login: 'fabira',password: 'passera'}}))[:code]).to eq 200 }
13
- end
10
+ it {expect((DbAccessor::Deleter.delete_all({model_name: "Application",condition: {name: 'Wikia'}}))[:code]).to eq 200 }
11
+
12
+ it {expect((DbAccessor::Deleter.delete_all({model_name: "User",condition: {login: 'fabira',password: 'passera'}}))[:code]).to eq 200 }
13
+ end
14
14
 
15
15
  context "when the conditon format is not valid" do
16
- param = {model_name: "User",condition: {ta: 1}}
16
+ param = {model_name: "User",condition: {ta: 1}}
17
17
 
18
- it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
18
+ it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
19
19
 
20
- param[:condition] = "coucou"
21
- it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
20
+ param[:condition] = "coucou"
21
+ it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
22
22
 
23
- param[:condition] = nil
24
- it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
23
+ param[:condition] = nil
24
+ it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
25
25
 
26
- param[:condition] = {login: 1}
27
- it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
26
+ param[:condition] = {login: 1}
27
+ it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
28
28
 
29
- param[:model_name] = "Application"
30
- param[:condition] = {login: 'fabi'}
31
- it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
32
- end
29
+ param[:model_name] = "Application"
30
+ param[:condition] = {login: 'fabi'}
31
+ it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
32
+ end
33
33
 
34
34
  context "when model name is invalid" do
35
- it {expect((DbAccessor::Deleter.delete_all({model_name: "A",condition: {}}))[:code]).to eq 400 }
36
- end
35
+ it {expect((DbAccessor::Deleter.delete_all({model_name: "A",condition: {}}))[:code]).to eq 400 }
36
+ end
37
37
 
38
38
  context "when one of the parameter's keys is invalid" do
39
- it {expect((DbAccessor::Deleter.delete_all({model: "User",condition: {}}))[:code]).to eq 400 }
40
- end
39
+ it {expect((DbAccessor::Deleter.delete_all({model: "User",condition: {}}))[:code]).to eq 400 }
40
+ end
41
41
 
42
- context "when the object which satisfy the given condition is deleted" do
43
- it {expect((DbAccessor::Deleter.delete_all({model_name: "User",condition: {id: 4}}))[:code]).to eq 200 }
42
+ context "when the object which satisfy the given condition is deleted" do
43
+ it {expect((DbAccessor::Deleter.delete_all({model_name: "User",condition: {id: 4}}))[:code]).to eq 200 }
44
+
45
+ it {expect((DbAccessor::Deleter.delete_all({model_name: "Application",condition: {id: 4}}))[:code]).to eq 200 }
46
+ end
44
47
 
45
- it {expect((DbAccessor::Deleter.delete_all({model_name: "Application",condition: {id: 4}}))[:code]).to eq 200 }
46
- end
47
-
48
48
  context "when one or many parameters are empty or nil" do
49
- it {expect((DbAccessor::Deleter.delete_all({model_name: nil,condition: {id: 3}}))[:code]).to eq 400 }
49
+ it {expect((DbAccessor::Deleter.delete_all({model_name: nil,condition: {id: 3}}))[:code]).to eq 400 }
50
50
  end
51
51
 
52
52
  end
53
53
 
54
54
  describe ".delete_by_id" do
55
- context "when the object find by id is deleted" do
55
+ context "when the object find by id is deleted" do
56
56
  given_param={model_name: "Application", id: [4]}
57
- it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 200 }
57
+ it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 200 }
58
58
 
59
59
  given_param={model_name: "User", id: [4]}
60
- it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 200 }
61
- end
60
+ it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 200 }
61
+ end
62
62
 
63
63
  context "when the id's format is invalid" do
64
64
  given_param={model_name: "User", id: "coucou"}
65
- it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
66
- end
65
+ it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
66
+ end
67
67
 
68
68
 
69
- context "when one or many parameters are empty or nil" do
70
- given_param={model_name: "User", id: nil}
71
- it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
69
+ context "when one or many parameters are empty or nil" do
70
+ given_param={model_name: "User", id: nil}
71
+ it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
72
72
  end
73
73
 
74
- context "when the model name is invalid" do
75
- given_param={model_name: "U3466", id: [1,2,3]}
76
- it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
74
+ context "when the model name is invalid" do
75
+ given_param={model_name: "U3466", id: [1,2,3]}
76
+ it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
77
77
  end
78
78
 
79
- context "when parameter's keys are invalid" do
80
- given_param={model_name: "User", i: [1,2] }
81
- it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
82
- given_param = {model: "Application",id: [3]}
83
- it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
79
+ context "when parameter's keys are invalid" do
80
+ given_param={model_name: "User", i: [1,2] }
81
+ it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
82
+ given_param = {model: "Application",id: [3]}
83
+ it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
84
84
  end
85
85
 
86
- context "when the parameter is nil" do
87
- it {expect((DbAccessor::Deleter.delete_by_id(nil))[:code]).to eq 400 }
86
+ context "when the parameter is nil" do
87
+ it {expect((DbAccessor::Deleter.delete_by_id(nil))[:code]).to eq 400 }
88
88
  end
89
89
  end
90
90
  end
@@ -2,18 +2,18 @@
2
2
  class CreateActions < ActiveRecord::Migration
3
3
  def self.up
4
4
  create_table :actions do |a|
5
- a.string :name
5
+ a.string :name
6
6
  a.integer :score
7
7
  a.timestamps
8
- end
9
- end
10
-
8
+ end
9
+ end
10
+
11
11
  def self.down
12
12
  drop_table :actions
13
13
  end
14
14
  end
15
15
 
16
16
  CreateActions.down
17
- CreateActions.up
17
+
18
18
 
19
19
 
@@ -2,18 +2,18 @@
2
2
  class CreateAssignments < ActiveRecord::Migration
3
3
  def self.up
4
4
  create_table :assignments do |a|
5
- a.integer :engine_id
5
+ a.integer :engine_id
6
6
  a.integer :action_id
7
7
  a.timestamps
8
- end
8
+ end
9
9
 
10
10
  add_index :assignments, :engine_id
11
11
  add_index :assignments, :action_id
12
- end
13
-
12
+ end
13
+
14
14
  def self.down
15
15
  drop_table :assignments
16
16
  end
17
17
  end
18
18
 
19
- CreateAssignments.up
19
+ CreateAssignments.down
@@ -15,5 +15,5 @@ class CreateEngineData < ActiveRecord::Migration
15
15
  end
16
16
 
17
17
  end
18
-
18
+ CreateEngineData.down
19
19
  CreateEngineData.up
@@ -1,7 +1,7 @@
1
1
  class CreateEngines < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :engines do |e|
4
- e.string :name
4
+ e.column :engine_conf, :json
5
5
  e.integer :application_id
6
6
  e.timestamps
7
7
  end
@@ -1,14 +1,11 @@
1
1
  require 'active_record'
2
2
  require_relative 'engine_data'
3
3
  require_relative 'application'
4
- require_relative 'action'
5
- require_relative 'assignment'
6
4
 
7
5
  class Engine < ActiveRecord::Base
8
6
 
9
- validates_presence_of :name
7
+ validates_presence_of :engine_conf
10
8
  has_one :engine_data
11
9
  belongs_to :application
12
- has_many :actions, through: :assignments
13
10
 
14
11
  end
data/spec/reader_spec.rb CHANGED
@@ -2,11 +2,12 @@ require_relative 'spec_helper'
2
2
 
3
3
 
4
4
  describe DbAccessor::Reader do
5
-
5
+
6
6
  describe ".simple_select" do
7
7
  context "when all model's items are found" do
8
8
  it {expect((DbAccessor::Reader.simple_select({model_name: "User",action_on: "all"}))[:code]).to eq 200 }
9
- end
9
+ it {expect((DbAccessor::Reader.simple_select({model_name: "Engine",action_on: "all"}))[:code]).to eq 200 }
10
+ end
10
11
  context "when the first model's item is found" do
11
12
  it {expect((DbAccessor::Reader.simple_select({model_name: "User",action_on: "first"}))[:code]).to eq 200 }
12
13
  end
@@ -18,7 +19,7 @@ describe DbAccessor::Reader do
18
19
  context "when it reads many items according to the condition" do
19
20
  it {expect((DbAccessor::Reader.simple_select({model_name: "User",action_on: nil}))[:code]).to eq 404 }
20
21
  end
21
-
22
+
22
23
  context "when passed parameter's keys are not correct" do
23
24
  it {expect((DbAccessor::Reader.simple_select({model: "User",action: nil}))[:code]).to eq 404 }
24
25
  end
@@ -39,8 +40,8 @@ describe DbAccessor::Reader do
39
40
  end
40
41
 
41
42
  context "when condition is not correct" do
42
- it {expect((DbAccessor::Reader.condition_select({model_name: "", condition: 1 }))[:code]).to eq 404 }
43
-
43
+ it {expect((DbAccessor::Reader.condition_select({model_name: "", condition: 1 }))[:code]).to eq 404 }
44
+
44
45
  it {expect((DbAccessor::Reader.condition_select({model_name: "", condition: "pas une condition"}))[:code]).to eq 404 }
45
46
  end
46
47
 
@@ -52,13 +53,13 @@ describe DbAccessor::Reader do
52
53
  describe ".select_by_id" do
53
54
 
54
55
  context "when it reads on a model with an id" do
55
- it {expect((DbAccessor::Reader.select_by_id({model_name: "User",identifiers: [5]}))[:code]).to eq 200 }
56
+ it {expect((DbAccessor::Reader.select_by_id({model_name: "User",identifiers: [5]}))[:code]).to eq 200 }
56
57
 
57
- it {expect((DbAccessor::Reader.select_by_id({model_name: "User",identifiers: [5]}))[:message]).to_not eq nil }
58
+ it {expect((DbAccessor::Reader.select_by_id({model_name: "User",identifiers: [5]}))[:message]).to_not eq nil }
58
59
  end
59
-
60
+
60
61
  context "when one of the ids is incorrect" do
61
- it {expect((DbAccessor::Reader.select_by_id({model_name: "User",identifiers: [1,2,'c']}))[:code]).to eq 404 }
62
+ it {expect((DbAccessor::Reader.select_by_id({model_name: "User",identifiers: [1,2,'c']}))[:code]).to eq 404 }
62
63
  end
63
64
 
64
65
  context "when one of the parameter is nil" do
@@ -83,5 +84,5 @@ describe DbAccessor::Reader do
83
84
 
84
85
  end
85
86
 
86
-
87
+
87
88
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require_relative 'fixtures/env'
2
2
  require 'uuid'
3
+ require 'json'
data/spec/writer_spec.rb CHANGED
@@ -8,9 +8,9 @@ describe DbAccessor::Writer do
8
8
 
9
9
  let(:user) { {model_name: 'User',content: {login: 'diakhoumpa'+nb.to_s,password: 'passeraa',firstname: 'Ndeye Fatou', lastname: 'Dieng',email: 'diakhouumpa'+nb.to_s+'@gmail.com',gender: 'M'}} }
10
10
 
11
- let(:app) { {model_name: 'Application',content: {name: 'desert'+nb.to_s,api_key: api_key,url: 'http://www.desert'+nb.to_s+'.com',user_id: 2}} }
12
-
13
- let(:engine) { {model_name: 'Engine',content: {name: 'engine'+nb.to_s,application_id: 2}} }
11
+ let(:app) { {model_name: 'Application',content: {name: 'desert'+nb.to_s,api_key: api_key,url: 'http://www.desert'+nb.to_s+'.com',user_id: 2}} }
12
+
13
+ let(:engine) { {model_name: 'Engine',content: {engine_conf: {name: 'engine'+nb.to_s,action: ['LIKE','SHARE'],score: [2,5],algorithm: 'Person',type: 'UserBased', neighborhood: 5 }.to_json ,application_id: 2}} }
14
14
 
15
15
  describe".write_object" do
16
16
 
@@ -31,35 +31,35 @@ describe DbAccessor::Writer do
31
31
  end
32
32
 
33
33
  context "when the model_name contains numbers" do
34
- it {expect((DbAccessor::Writer.write_object({model_name: 'DGH67889',content: user[:content]}))[:code]).to eq 400 }
34
+ it {expect((DbAccessor::Writer.write_object({model_name: 'DGH67889',content: user[:content]}))[:code]).to eq 400 }
35
35
  end
36
-
36
+
37
37
  context "when passed parameter's keys are incorrect" do
38
- it {expect((DbAccessor::Writer.write_object({model: 'DGH67889',con: user[:content]}))[:code]).to eq 400 }
38
+ it {expect((DbAccessor::Writer.write_object({model: 'DGH67889',con: user[:content]}))[:code]).to eq 400 }
39
39
  end
40
40
 
41
41
  context "when the parameter is not a hash" do
42
- it { expect((DbAccessor::Writer.write_object("coucou"))[:code]).to eq 400 }
42
+ it { expect((DbAccessor::Writer.write_object("coucou"))[:code]).to eq 400 }
43
43
  end
44
44
 
45
45
  context "when the parameter's keys are not correct" do
46
- it { expect((DbAccessor::Writer.write_object({t: "coucou"}))[:code]).to eq 400 }
46
+ it { expect((DbAccessor::Writer.write_object({t: "coucou"}))[:code]).to eq 400 }
47
47
  end
48
-
48
+
49
49
  context "when required parameters are not given" do
50
- user = {model_name: 'User',content: {firstname: 'ablaye', lastname: 'Dieng',gender: 'M'} }
50
+ user = {model_name: 'User',content: {firstname: 'ablaye', lastname: 'Dieng',gender: 'M'} }
51
51
  it { expect((DbAccessor::Writer.write_object(user))[:code]).to eq 400 }
52
52
  end
53
-
53
+
54
54
  context "when required parameters are not correct" do
55
- user = {model_name: 'User',content: {login: nb.to_s,password: 'passer',firstname: 'Ndeye Fatou', lastname: 'Dieng',email: 'fatou',gender: 'M'}}
55
+ user = {model_name: 'User',content: {login: nb.to_s,password: 'passer',firstname: 'Ndeye Fatou', lastname: 'Dieng',email: 'fatou',gender: 'M'}}
56
56
  it { expect((DbAccessor::Writer.write_object(user))[:code]).to eq 400 }
57
57
  end
58
58
 
59
59
  context "when the content doesn't correspond to the model_name" do
60
60
  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 }
61
61
  end
62
-
62
+
63
63
  context "when the content is incorrect" do
64
64
  it { expect((DbAccessor::Writer.write_object( {model_name: 'Utilisateur',content: "coucou"} ))[:code]).to eq 400 }
65
65
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbAccessor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - fabira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-23 00:00:00.000000000 Z
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler