cs 0.1.0beta3
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.
- data/.gitignore +21 -0
- data/Gemfile +10 -0
- data/LICENSE +22 -0
- data/README.md +132 -0
- data/Rakefile +7 -0
- data/cs.gemspec +22 -0
- data/lib/commonsense-ruby-lib.rb +127 -0
- data/lib/commonsense-ruby-lib/auth/http.rb +117 -0
- data/lib/commonsense-ruby-lib/auth/oauth.rb +101 -0
- data/lib/commonsense-ruby-lib/end_point.rb +276 -0
- data/lib/commonsense-ruby-lib/end_point/group.rb +28 -0
- data/lib/commonsense-ruby-lib/end_point/sensor.rb +36 -0
- data/lib/commonsense-ruby-lib/end_point/sensor_data.rb +70 -0
- data/lib/commonsense-ruby-lib/end_point/user.rb +50 -0
- data/lib/commonsense-ruby-lib/error.rb +51 -0
- data/lib/commonsense-ruby-lib/relation.rb +233 -0
- data/lib/commonsense-ruby-lib/relation/sensor_data_relation.rb +116 -0
- data/lib/commonsense-ruby-lib/relation/sensor_relation.rb +162 -0
- data/lib/commonsense-ruby-lib/serializer.rb +20 -0
- data/lib/commonsense-ruby-lib/session.rb +105 -0
- data/lib/commonsense-ruby-lib/version.rb +3 -0
- data/spec/features/sensor_data_management_spec.rb +4 -0
- data/spec/features/sensor_management_spec.rb +105 -0
- data/spec/features/user_management_spec.rb +70 -0
- data/spec/lib/commonsense-ruby-lib/end_point/sensor_data_spec.rb +68 -0
- data/spec/lib/commonsense-ruby-lib/end_point/sensor_spec.rb +98 -0
- data/spec/lib/commonsense-ruby-lib/end_point/user_spec.rb +36 -0
- data/spec/lib/commonsense-ruby-lib/end_point_spec.rb +190 -0
- data/spec/lib/commonsense-ruby-lib/relation/sensor_data_relation_spec.rb +444 -0
- data/spec/lib/commonsense-ruby-lib/relation/sensor_relation_spec.rb +165 -0
- data/spec/lib/commonsense-ruby-lib/session_spec.rb +43 -0
- data/spec/lib/commonsense-ruby-lib_spec.rb +51 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/support/spec_config.yml.sample +6 -0
- data/spec/support/vcr.rb +5 -0
- metadata +175 -0
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module CommonSense
|
4
|
+
module EndPoint
|
5
|
+
describe SensorData do
|
6
|
+
|
7
|
+
let!(:value) do
|
8
|
+
{"x-axis" => 1.0, "y-axis" => 2.0, "z-axis" => 3.0}
|
9
|
+
end
|
10
|
+
|
11
|
+
let!(:now) do
|
12
|
+
Time.now.to_f
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "Initiating new data point" do
|
16
|
+
it "should assign the data point property on initialize" do
|
17
|
+
data = SensorData.new(sensor_id: 1, date: now, value: value)
|
18
|
+
|
19
|
+
data.sensor_id.should eq(1)
|
20
|
+
data.date.should eq(now)
|
21
|
+
data.value.should eq(value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "Creating" do
|
26
|
+
it "should create a new data point" do
|
27
|
+
data = SensorData.new(sensor_id: 1, date: now, value: value)
|
28
|
+
|
29
|
+
session = double("CommonSense::Session")
|
30
|
+
session.should_receive(:post).with("/sensors/1/data.json", {data: [{date: now, value: value.to_json}]})
|
31
|
+
session.stub(:response_headers => {"location" => "http://foo.bar/sensors/1/data/1"})
|
32
|
+
session.stub(:response_code => 201)
|
33
|
+
data.session = session
|
34
|
+
|
35
|
+
data.create!.should be_true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "Get specific data point" do
|
40
|
+
it "should request data point from commonSense" do
|
41
|
+
data = SensorData.new
|
42
|
+
expect { data.retrieve!.should }.to raise_error(Error::NotImplementedError)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "Update specific data point" do
|
47
|
+
it "should request data point from commonSense" do
|
48
|
+
data = SensorData.new
|
49
|
+
expect { data.retrieve!.should }.to raise_error(Error::NotImplementedError)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "Delete specific data point" do
|
54
|
+
it "should perform DELETE request to commonSense" do
|
55
|
+
data = SensorData.new(sensor_id: 1, id: "abcdef")
|
56
|
+
|
57
|
+
session = double("CommonSense::Session")
|
58
|
+
session.should_receive(:delete).with("/sensors/1/data/abcdef.json")
|
59
|
+
session.stub(:response_code => 200)
|
60
|
+
data.session = session
|
61
|
+
|
62
|
+
data.delete!.should be_true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module CommonSense
|
4
|
+
module EndPoint
|
5
|
+
describe SensorData do
|
6
|
+
|
7
|
+
let(:sensor_info) do
|
8
|
+
{
|
9
|
+
name: "accelerometer",
|
10
|
+
display_name: "Accelerometer",
|
11
|
+
device_type: "BMA123",
|
12
|
+
pager_type: "email",
|
13
|
+
data_type: "json",
|
14
|
+
data_structure: {"x-axis" => "Float", "y-axis" => "Float", "z-axis" => "Float"}
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "Initiating new sensor" do
|
19
|
+
it "should assign the data point property on initialize" do
|
20
|
+
info = sensor_info
|
21
|
+
info[:id] = 1
|
22
|
+
sensor = Sensor.new(info)
|
23
|
+
sensor.id.should eq(1)
|
24
|
+
sensor.name.should eq(info[:name])
|
25
|
+
sensor.display_name.should eq(info[:display_name])
|
26
|
+
sensor.device_type.should eq(info[:device_type])
|
27
|
+
sensor.pager_type.should eq(info[:pager_type])
|
28
|
+
sensor.data_type.should eq(info[:data_type])
|
29
|
+
sensor.data_structure.should eq(info[:data_structure])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "Creating" do
|
34
|
+
it "should POST to /sensors.json" do
|
35
|
+
sensor = Sensor.new(sensor_info)
|
36
|
+
|
37
|
+
session = double("CommonSense::Session")
|
38
|
+
expected = {sensor: sensor_info }
|
39
|
+
expected[:sensor][:data_structure] = (sensor_info[:data_structure].to_json)
|
40
|
+
session.should_receive(:post).with("/sensors.json", expected)
|
41
|
+
session.stub(:response_headers => {"location" => "http://foo.bar/sensors/1"})
|
42
|
+
session.stub(:response_code => 201)
|
43
|
+
sensor.session = session
|
44
|
+
|
45
|
+
sensor.save!.should be_true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "Get specific data point" do
|
50
|
+
it "should request GET to /sensors/:id.json" do
|
51
|
+
sensor = Sensor.new(sensor_info)
|
52
|
+
sensor_id = 1
|
53
|
+
sensor.id = sensor_id
|
54
|
+
|
55
|
+
session = double("CommonSense::Session")
|
56
|
+
session.should_receive(:get).with("/sensors/#{sensor_id}.json")
|
57
|
+
session.stub(:response_code => 200)
|
58
|
+
sensor.session = session
|
59
|
+
|
60
|
+
sensor.retrieve!.should be_true
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "Update specific data point" do
|
65
|
+
it "should request data point from commonSense" do
|
66
|
+
sensor = Sensor.new(sensor_info)
|
67
|
+
sensor_id = 1
|
68
|
+
sensor.id = sensor_id
|
69
|
+
|
70
|
+
session = double("CommonSense::Session")
|
71
|
+
expected = {sensor: sensor_info }
|
72
|
+
expected[:sensor][:data_structure] = (sensor_info[:data_structure].to_json)
|
73
|
+
expected[:sensor][:id] = 1
|
74
|
+
session.should_receive(:put).with("/sensors/#{sensor_id}.json", expected)
|
75
|
+
session.stub(:response_code => 200)
|
76
|
+
sensor.session = session
|
77
|
+
|
78
|
+
sensor.save!.should be_true
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "Delete specific data point" do
|
83
|
+
it "should perform DELETE request to commonSense" do
|
84
|
+
sensor = Sensor.new(sensor_info)
|
85
|
+
sensor_id = 1
|
86
|
+
sensor.id = sensor_id
|
87
|
+
|
88
|
+
session = double("CommonSense::Session")
|
89
|
+
session.should_receive(:delete).with("/sensors/#{sensor_id}.json")
|
90
|
+
session.stub(:response_code => 200)
|
91
|
+
sensor.session = session
|
92
|
+
|
93
|
+
sensor.delete!.should be_true
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'commonsense-ruby-lib/error'
|
3
|
+
|
4
|
+
module CommonSense
|
5
|
+
module EndPoint
|
6
|
+
describe User do
|
7
|
+
def valid_user
|
8
|
+
{
|
9
|
+
id: 1, email: "foo@bar.com", username: "foo@bar.com", name: "foo",
|
10
|
+
surname: "bar", address: "foo", zipcode: "12345", country: "NL",
|
11
|
+
mobile: "12345", uuid: "12345", openid: "12345"
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "current_user" do
|
16
|
+
it "should return current logged in user" do
|
17
|
+
user = User.new
|
18
|
+
session = double("CommonSense::Session")
|
19
|
+
session.stub(:get).with("/users/current.json").and_return({"user" => valid_user})
|
20
|
+
|
21
|
+
user.stub(:session).and_return(session);
|
22
|
+
|
23
|
+
user.current_user.should_not be_nil
|
24
|
+
valid_user.each {|key, value| user.send(key).should eq(value) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
describe "save" do
|
30
|
+
|
31
|
+
describe "with id specified" do
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module CommonSense
|
4
|
+
describe EndPoint do
|
5
|
+
before(:each) do
|
6
|
+
class FooEndPoint
|
7
|
+
include EndPoint
|
8
|
+
attribute :attribute1, :attribute2
|
9
|
+
resources :foos
|
10
|
+
resource :foo
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:valid_foo) do
|
15
|
+
{
|
16
|
+
id: 1,
|
17
|
+
attribute1: "attribute1",
|
18
|
+
attribute2: "attribute2"
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:valid_foo_response) do
|
23
|
+
{
|
24
|
+
"id" => 1,
|
25
|
+
"attribute1" => "attribute1",
|
26
|
+
"attribute2" => "attribute2"
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "save!" do
|
31
|
+
describe "without a session" do
|
32
|
+
it "should raise Error::SessionException" do
|
33
|
+
expect {
|
34
|
+
foo = FooEndPoint.new
|
35
|
+
foo.save!
|
36
|
+
}.to raise_error(Error::SessionEmptyError)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "without id" do
|
41
|
+
it "should call create" do
|
42
|
+
foo = FooEndPoint.new
|
43
|
+
foo.session = double("CommonSense::Session")
|
44
|
+
foo.should_receive(:create!)
|
45
|
+
foo.save!
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "with id" do
|
50
|
+
it "should call update" do
|
51
|
+
foo = FooEndPoint.new
|
52
|
+
foo.session = double("CommonSense::Session")
|
53
|
+
foo.id = 1
|
54
|
+
foo.should_receive(:update!)
|
55
|
+
foo.save!
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "save" do
|
61
|
+
it "should not raise error" do
|
62
|
+
foo = FooEndPoint.new
|
63
|
+
foo.session = double("CommonSense::Session")
|
64
|
+
foo.should_receive(:create!).and_return { raise Error }
|
65
|
+
foo.save.should be_false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "create!" do
|
70
|
+
it "should POST resource to CommonSense" do
|
71
|
+
foo_data = valid_foo
|
72
|
+
foo_data.delete(:id)
|
73
|
+
foo = FooEndPoint.new(foo_data)
|
74
|
+
session = double("CommonSense::Session")
|
75
|
+
session.should_receive(:post).with("/foos.json", {foo: foo_data}).and_return({"foo" => valid_foo})
|
76
|
+
session.stub(:response_headers => {"location" => "http://foo.bar/foos/1"})
|
77
|
+
session.stub(:response_code => 201)
|
78
|
+
foo.stub(:session).and_return(session);
|
79
|
+
|
80
|
+
foo.create!.should be_true
|
81
|
+
foo.id.should eq("1")
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should raise exception where recive error (not 201) from commonSense" do
|
85
|
+
expect {
|
86
|
+
foo = FooEndPoint.new
|
87
|
+
session = double("CommonSense::Session")
|
88
|
+
session.stub(:post)
|
89
|
+
session.stub(response_code: 409)
|
90
|
+
session.stub(:errors)
|
91
|
+
foo.session = session
|
92
|
+
foo.create!
|
93
|
+
}.to raise_error(Error::ResponseError)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "create" do
|
98
|
+
it "should not raise exception" do
|
99
|
+
foo = FooEndPoint.new
|
100
|
+
foo.stub(:create!).and_return { raise Error }
|
101
|
+
foo.create.should be_false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "retrive!" do
|
106
|
+
it "should raise exception with no id" do
|
107
|
+
expect {
|
108
|
+
foo = FooEndPoint.new
|
109
|
+
foo.session = double("CommonSense::Session")
|
110
|
+
foo.retrieve!
|
111
|
+
}.to raise_error(Error::ResourceIdError)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should GET Resource from CommonSense" do
|
115
|
+
foo = FooEndPoint.new(id: 1)
|
116
|
+
session = double("CommonSense::Session")
|
117
|
+
session.should_receive(:get).with("/foos/1.json").and_return({"foo" => valid_foo})
|
118
|
+
session.stub(:response_code => 200)
|
119
|
+
foo.session = session
|
120
|
+
|
121
|
+
result = foo.retrieve!
|
122
|
+
result.should be_true
|
123
|
+
foo.id.should eq(1)
|
124
|
+
foo.attribute1.should eq("attribute1")
|
125
|
+
foo.attribute2.should eq("attribute2")
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "retrieve" do
|
130
|
+
it "should not raise error" do
|
131
|
+
foo = FooEndPoint.new
|
132
|
+
foo.stub(:retrieve!).and_return { raise Error }
|
133
|
+
foo.retrieve.should be_false
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "update!" do
|
138
|
+
it "should PUT Resource to CommonSense" do
|
139
|
+
foo_data = valid_foo
|
140
|
+
foo = FooEndPoint.new(foo_data)
|
141
|
+
session = double("CommonSense::Session")
|
142
|
+
session.should_receive(:put).with("/foos/1.json", {foo: foo_data}).and_return({"foo" => valid_foo})
|
143
|
+
session.stub(:response_code => 200)
|
144
|
+
foo.session = session
|
145
|
+
|
146
|
+
foo.update!.should be_true
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "update" do
|
151
|
+
it "should not raise error" do
|
152
|
+
foo = FooEndPoint.new
|
153
|
+
foo.stub(:update!).and_return { raise Error }
|
154
|
+
foo.update.should be_false
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe "delete!" do
|
159
|
+
it "should raise exception with no id" do
|
160
|
+
expect {
|
161
|
+
foo = FooEndPoint.new
|
162
|
+
foo.session = double("CommonSense::Session")
|
163
|
+
foo.delete!
|
164
|
+
}.to raise_error(Error::ResourceIdError)
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should DELETE Resource from CommonSense" do
|
169
|
+
foo = FooEndPoint.new(id: 1)
|
170
|
+
session = double("CommonSense::Session")
|
171
|
+
session.should_receive(:delete).with("/foos/1.json")
|
172
|
+
session.stub(:response_code => 200)
|
173
|
+
foo.session = session
|
174
|
+
|
175
|
+
result = foo.delete!
|
176
|
+
result.should be_true
|
177
|
+
foo.id.should be_nil
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe "delete" do
|
182
|
+
it "should not raise error" do
|
183
|
+
foo = FooEndPoint.new
|
184
|
+
foo.session = double("CommonSense::Session")
|
185
|
+
foo.stub(:delete!).and_return { raise Error }
|
186
|
+
foo.delete.should be_false
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
@@ -0,0 +1,444 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module CommonSense
|
4
|
+
module Relation
|
5
|
+
describe SensorDataRelation do
|
6
|
+
|
7
|
+
let(:relation) do
|
8
|
+
relation = SensorDataRelation.new
|
9
|
+
relation.stub("check_session!").and_return(true)
|
10
|
+
relation.stub("get_data").and_return(sensors)
|
11
|
+
relation
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:data) do
|
15
|
+
{
|
16
|
+
"data" => [{
|
17
|
+
"id" => "5150e509b4b735f6290238d3",
|
18
|
+
"sensor_id" => "1",
|
19
|
+
"value" => "{\"x-axis\":0.259,\"y-axis\":-0.15,\"z-axis\":-9.807}",
|
20
|
+
"date" => 1364256004.651,
|
21
|
+
"month" => 3,
|
22
|
+
"week" => 13,
|
23
|
+
"year" => 2013
|
24
|
+
},
|
25
|
+
{
|
26
|
+
"id" => "5150e760b4b735fa29027b27",
|
27
|
+
"sensor_id" => "1",
|
28
|
+
"value" => "{\"x-axis\":0.191,\"y-axis\":0.069,\"z-axis\":-9.875}",
|
29
|
+
"date" => 1364256603.675,
|
30
|
+
"month" => 3,
|
31
|
+
"week" => 13,
|
32
|
+
"year" => 2013
|
33
|
+
},
|
34
|
+
{
|
35
|
+
"id" => "5150e9b7b4b735d04e010ed9",
|
36
|
+
"sensor_id" => "1",
|
37
|
+
"value" => "{\"x-axis\":0.191,\"y-axis\":-0.028,\"z-axis\":-9.766}",
|
38
|
+
"date" => 1364257203.315,
|
39
|
+
"month" => 3,
|
40
|
+
"week" => 13,
|
41
|
+
"year" => 2013
|
42
|
+
}]
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
let(:relation) do
|
47
|
+
sensor_id = "1"
|
48
|
+
relation = SensorDataRelation.new(sensor_id)
|
49
|
+
relation.stub("check_session!").and_return(true)
|
50
|
+
relation.stub("get_data!").and_return(data)
|
51
|
+
relation
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "build" do
|
55
|
+
it "should return a sensorData object" do
|
56
|
+
sensor_id = 1
|
57
|
+
SensorDataRelation.new(sensor_id).build.should be_a_kind_of(EndPoint::SensorData)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "get_data!" do
|
62
|
+
it "should fetch the data point from commonSense" do
|
63
|
+
sensor_id = 1
|
64
|
+
session = double("Session")
|
65
|
+
option = { page: 100, per_page: 99, start_date: 1365278885,
|
66
|
+
end_date: 1365278886, last: 1, sort: 'ASC', interval: 300 }
|
67
|
+
session.should_receive(:get).with("/sensors/#{sensor_id}/data.json", option)
|
68
|
+
|
69
|
+
relation = SensorDataRelation.new(sensor_id, session)
|
70
|
+
relation.get_data!(page:100, per_page: 99, start_date: 1365278885,
|
71
|
+
end_date: 1365278886, last: true, sort: 'ASC', interval: 300)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "get_data" do
|
76
|
+
it "call get_data! and not throw exception" do
|
77
|
+
sensor_id = 1
|
78
|
+
relation = SensorDataRelation.new(sensor_id)
|
79
|
+
relation.stub(:get_data!).and_return { raise Error }
|
80
|
+
|
81
|
+
expect { relation.get_data }.to_not raise_error
|
82
|
+
relation.get_data.should be_nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "each" do
|
87
|
+
it "should get all sensor data based on the criteria and yield" do
|
88
|
+
expect { |b| relation.each(&b) }.to yield_successive_args(EndPoint::SensorData, EndPoint::SensorData, EndPoint::SensorData)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "count" do
|
93
|
+
it "should return the total number of sensor data match with criteria" do
|
94
|
+
relation.count.should eq(3)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "first" do
|
99
|
+
it "should return the first record" do
|
100
|
+
session = double("Session")
|
101
|
+
option = { page: 0, per_page: 1, sort: 'ASC'}
|
102
|
+
sensor_id = "1"
|
103
|
+
response = {
|
104
|
+
"data" => [{
|
105
|
+
"id" => "5150e509b4b735f6290238d3",
|
106
|
+
"sensor_id" => sensor_id,
|
107
|
+
"value" => "{\"x-axis\":0.259,\"y-axis\":-0.15,\"z-axis\":-9.807}",
|
108
|
+
"date" => 1364256004.651,
|
109
|
+
"month" => 3,
|
110
|
+
"week" => 13,
|
111
|
+
"year" => 2013
|
112
|
+
}]
|
113
|
+
}
|
114
|
+
|
115
|
+
session.should_receive(:get).with("/sensors/#{sensor_id}/data.json", option).and_return(response)
|
116
|
+
relation = SensorDataRelation.new(sensor_id, session)
|
117
|
+
|
118
|
+
first = relation.first
|
119
|
+
first.id.should eq("5150e509b4b735f6290238d3")
|
120
|
+
first.sensor_id.should eq("1")
|
121
|
+
first.value.should eq("{\"x-axis\":0.259,\"y-axis\":-0.15,\"z-axis\":-9.807}")
|
122
|
+
first.date.should eq(1364256004.651)
|
123
|
+
first.month.should eq(3)
|
124
|
+
first.week.should eq(13)
|
125
|
+
first.year.should eq(2013)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "last" do
|
130
|
+
it "should return the last record" do
|
131
|
+
session = double("Session")
|
132
|
+
option = { page: 0, per_page: 1, sort: 'DESC'}
|
133
|
+
sensor_id = "1"
|
134
|
+
response = {
|
135
|
+
"data" => [{
|
136
|
+
"id" => "5150e9b7b4b735d04e010ed9",
|
137
|
+
"sensor_id" => sensor_id,
|
138
|
+
"value" => "{\"x-axis\":0.191,\"y-axis\":-0.028,\"z-axis\":-9.766}",
|
139
|
+
"date" => 1364257203.315,
|
140
|
+
"month" => 3,
|
141
|
+
"week" => 13,
|
142
|
+
"year" => 2013
|
143
|
+
}]
|
144
|
+
}
|
145
|
+
session.should_receive(:get).with("/sensors/#{sensor_id}/data.json", option).and_return(response)
|
146
|
+
relation = SensorDataRelation.new(sensor_id, session)
|
147
|
+
|
148
|
+
last = relation.last
|
149
|
+
last.id.should eq("5150e9b7b4b735d04e010ed9")
|
150
|
+
last.sensor_id.should eq("1")
|
151
|
+
last.value.should eq("{\"x-axis\":0.191,\"y-axis\":-0.028,\"z-axis\":-9.766}")
|
152
|
+
last.date.should eq(1364257203.315)
|
153
|
+
last.month.should eq(3)
|
154
|
+
last.week.should eq(13)
|
155
|
+
last.year.should eq(2013)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
describe "where" do
|
160
|
+
before(:each) do
|
161
|
+
sensor_id = 1
|
162
|
+
@relation = SensorDataRelation.new(sensor_id)
|
163
|
+
@relation.stub("check_session!").and_return(true)
|
164
|
+
end
|
165
|
+
|
166
|
+
describe "page" do
|
167
|
+
it "should update page" do
|
168
|
+
@relation.where(page: 2)
|
169
|
+
@relation.page.should eq(2)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe "per_page" do
|
174
|
+
it "should update per_page" do
|
175
|
+
@relation.where(per_page: 99)
|
176
|
+
@relation.per_page.should eq(99)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe "start_date" do
|
181
|
+
describe "number given" do
|
182
|
+
it "should update the start_date" do
|
183
|
+
@relation.where(start_date: 2)
|
184
|
+
@relation.start_date.to_f.should eq(2.0)
|
185
|
+
@relation.start_date.should be_kind_of(Time)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
describe "Time given" do
|
190
|
+
it "should update the start_date" do
|
191
|
+
@relation.where(start_date: Time.at(19))
|
192
|
+
@relation.start_date.to_f.should eq(19)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
describe "Object that respond to 'to_time` given" do
|
197
|
+
it "should update the start_date" do
|
198
|
+
double = double()
|
199
|
+
double.should_receive(:to_time).and_return (Time.at(2))
|
200
|
+
@relation.where(start_date: double)
|
201
|
+
@relation.start_date.to_f.should eq(2.0)
|
202
|
+
@relation.start_date.should be_kind_of(Time)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe "Object that not respond to 'to_time' given" do
|
207
|
+
it "should raise error" do
|
208
|
+
expect { @relation.where(end_date: 'foo') }.to raise_error(NoMethodError)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
describe "end_date" do
|
215
|
+
describe "number given" do
|
216
|
+
it "should update the end_date" do
|
217
|
+
@relation.where(end_date: 2)
|
218
|
+
@relation.end_date.to_f.should eq(2.0)
|
219
|
+
@relation.end_date.should be_kind_of(Time)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
describe "Time given" do
|
224
|
+
it "should update the end_date" do
|
225
|
+
@relation.where(end_date: Time.at(19))
|
226
|
+
@relation.end_date.to_f.should eq(19)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "Object that respond to 'to_time` given" do
|
231
|
+
it "should update the end_date" do
|
232
|
+
double = double()
|
233
|
+
double.should_receive(:to_time).and_return (Time.at(2))
|
234
|
+
@relation.where(end_date: double)
|
235
|
+
@relation.end_date.to_f.should eq(2.0)
|
236
|
+
@relation.end_date.should be_kind_of(Time)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
describe "Object that not respond to 'to_time' given" do
|
241
|
+
it "should raise error" do
|
242
|
+
expect { @relation.where(end_date: 'foo') }.to raise_error(NoMethodError)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
describe "last" do
|
248
|
+
describe "with boolean value" do
|
249
|
+
it "should update the last parameter" do
|
250
|
+
[true, false].each do |value|
|
251
|
+
@relation.where(last: value)
|
252
|
+
@relation.parameter(:last).should be_true
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
describe "with invalid parameter" do
|
258
|
+
it "should assign true" do
|
259
|
+
@relation.where(last: 'a')
|
260
|
+
@relation.parameter(:last).should be_true
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
describe "sort" do
|
266
|
+
describe "valid parameter given" do
|
267
|
+
it "should update the sort parameter" do
|
268
|
+
@relation.where(sort: 'ASC')
|
269
|
+
@relation.parameter(:sort).should eq('ASC')
|
270
|
+
|
271
|
+
@relation.where(sort: 'DESC')
|
272
|
+
@relation.parameter(:sort).should eq('DESC')
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
describe "invalid parameter given" do
|
277
|
+
it "should set parameter to nil" do
|
278
|
+
expect { @relation.where(sort: 'foo') }.to raise_error(ArgumentError)
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
describe "interval" do
|
284
|
+
describe "valid parameter given" do
|
285
|
+
it "should update the sort parameter" do
|
286
|
+
[604800, 86400, 3600, 1800, 600, 300, 60].each do |interval|
|
287
|
+
@relation.where(interval: interval)
|
288
|
+
@relation.interval.should eq(interval)
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
describe "invalid parameter given" do
|
294
|
+
it "should set the parameter to nil" do
|
295
|
+
expect { @relation.where(interval: 10) }.to raise_error(ArgumentError)
|
296
|
+
expect { @relation.where(interval: 'a') }.to raise_error(ArgumentError)
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
describe "from" do
|
302
|
+
describe "number given" do
|
303
|
+
it "should update the end_date" do
|
304
|
+
@relation.where(from: 2)
|
305
|
+
@relation.start_date.to_f.should eq(2.0)
|
306
|
+
@relation.start_date.should be_kind_of(Time)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
describe "Time given" do
|
311
|
+
it "should update the end_date" do
|
312
|
+
@relation.where(from: Time.at(19))
|
313
|
+
@relation.start_date.to_f.should eq(19)
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
describe "Object that respond to 'to_time` given" do
|
318
|
+
it "should update the end_date" do
|
319
|
+
double = double()
|
320
|
+
double.should_receive(:to_time).and_return (Time.at(2))
|
321
|
+
@relation.where(from: double)
|
322
|
+
@relation.start_date.to_f.should eq(2.0)
|
323
|
+
@relation.start_date.should be_kind_of(Time)
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
describe "Object that not respond to 'to_time' given" do
|
328
|
+
it "should raise error" do
|
329
|
+
expect { @relation.where(from: 'foo') }.to raise_error(NoMethodError)
|
330
|
+
end
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
describe "to" do
|
335
|
+
describe "number given" do
|
336
|
+
it "should update the end_date" do
|
337
|
+
@relation.where(to: 2)
|
338
|
+
@relation.end_date.to_f.should eq(2.0)
|
339
|
+
@relation.end_date.should be_kind_of(Time)
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
describe "Time given" do
|
344
|
+
it "should update the end_date" do
|
345
|
+
@relation.where(to: Time.at(19))
|
346
|
+
@relation.end_date.to_f.should eq(19)
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
describe "Object that respond to 'to_time` given" do
|
351
|
+
it "should update the end_date" do
|
352
|
+
double = double()
|
353
|
+
double.should_receive(:to_time).and_return (Time.at(2))
|
354
|
+
@relation.where(to: double)
|
355
|
+
@relation.end_date.to_f.should eq(2.0)
|
356
|
+
@relation.end_date.should be_kind_of(Time)
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
describe "Object that not respond to 'to_time' given" do
|
361
|
+
it "should raise error" do
|
362
|
+
expect { @relation.where(to: 'foo') }.to raise_error(NoMethodError)
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
describe "from" do
|
369
|
+
describe "number given" do
|
370
|
+
it "should update the end_date" do
|
371
|
+
relation.from(2)
|
372
|
+
relation.start_date.to_f.should eq(2.0)
|
373
|
+
relation.start_date.should be_kind_of(Time)
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
describe "Time given" do
|
378
|
+
it "should update the end_date" do
|
379
|
+
relation.from(Time.at(19))
|
380
|
+
relation.start_date.to_f.should eq(19)
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
describe "Object that respond to 'to_time` given" do
|
385
|
+
it "should update the end_date" do
|
386
|
+
double = double()
|
387
|
+
double.should_receive(:to_time).and_return (Time.at(2))
|
388
|
+
relation.from(double)
|
389
|
+
relation.start_date.to_f.should eq(2.0)
|
390
|
+
relation.start_date.should be_kind_of(Time)
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
describe "Object that not respond to 'to_time' given" do
|
395
|
+
it "should raise error" do
|
396
|
+
expect { relation.from('foo') }.to raise_error(NoMethodError)
|
397
|
+
end
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
describe "to" do
|
402
|
+
describe "number given" do
|
403
|
+
it "should update the end_date" do
|
404
|
+
relation.to(2)
|
405
|
+
relation.end_date.to_f.should eq(2.0)
|
406
|
+
relation.end_date.should be_kind_of(Time)
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
describe "Time given" do
|
411
|
+
it "should update the end_date" do
|
412
|
+
relation.to(Time.at(19))
|
413
|
+
relation.end_date.to_f.should eq(19)
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
describe "Object that respond to 'to_time` given" do
|
418
|
+
it "should update the end_date" do
|
419
|
+
double = double()
|
420
|
+
double.should_receive(:to_time).and_return (Time.at(2))
|
421
|
+
relation.to(double)
|
422
|
+
relation.end_date.to_f.should eq(2.0)
|
423
|
+
relation.end_date.should be_kind_of(Time)
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
describe "Object that not respond to 'to_time' given" do
|
428
|
+
it "should raise error" do
|
429
|
+
expect { relation.to('foo') }.to raise_error(NoMethodError)
|
430
|
+
end
|
431
|
+
end
|
432
|
+
end
|
433
|
+
|
434
|
+
describe "all" do
|
435
|
+
it "return array of all matching sensor data" do
|
436
|
+
data = relation.to_a
|
437
|
+
data.should be_a_kind_of(Array)
|
438
|
+
data.size.should eq(3)
|
439
|
+
data[0].should be_a_kind_of(EndPoint::SensorData)
|
440
|
+
end
|
441
|
+
end
|
442
|
+
end
|
443
|
+
end
|
444
|
+
end
|