Skip to content

Commit

Permalink
[api] little more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
coolo committed Nov 16, 2013
1 parent a314de4 commit e687aa8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/api/app/helpers/status_helper.rb
Expand Up @@ -7,15 +7,18 @@ def self.resample(values, samples = 400)
return result if values.empty?

lastvalue = 0
now = values[0][0]
now = values[0][0].to_f
samplerate = (values[-1][0] - now) / samples
if samples < values.length
now -= samplerate / 2
end

index = 0

1.upto(samples) do |i|
value = 0.0
count = 0
while index < values.length && values[index][0] < now + samplerate
while index < values.length && values[index][0] <= now + samplerate
value += values[index][1]
index += 1
count += 1
Expand All @@ -25,7 +28,7 @@ def self.resample(values, samples = 400)
else
value = lastvalue
end
result << [now + samplerate / 2, value]
result << [now, value]
now += samplerate
lastvalue = value
end
Expand Down
4 changes: 4 additions & 0 deletions src/api/test/functional/message_controller_test.rb
Expand Up @@ -47,6 +47,10 @@ def test_index
assert_response :success
ret = ActiveXML::Node.new @response.body
ret.each_message do |m|
# test show too
get "/message/#{m.msg_id}"
assert_response :success

delete "/message/#{m.msg_id}"
assert_response :success

Expand Down
35 changes: 35 additions & 0 deletions src/api/test/unit/status_helper_test.rb
@@ -0,0 +1,35 @@
require File.expand_path(File.dirname(__FILE__) + "/..") + "/test_helper"

class StatusHelperTest < ActiveSupport::TestCase

test 'resample' do
# it can't do magic
assert_equal([], StatusHelper.resample([], 1000))

now = 10000
testarray = []
10.times do |i|
testarray << [now - i * 10, i]
end
# [[10000, 0], [9990, 1], [9980, 2], [9970, 3], [9960, 4], [9950, 5], [9940, 6], [9930, 7], [9920, 8], [9910, 9]]
# while the testarray increases, the timestamps go down, so the result needs to decrease
assert_equal([[9910.0, 9.0], [9919.0, 8.0], [9928.0, 7.0], [9937.0, 6.0], [9946.0, 5.0], [9955.0, 4.0], [9964.0, 3.0], [9973.0, 2.0], [9982.0, 1.0], [9991.0, 0.0]],
StatusHelper.resample(testarray, 10))
assert_equal([[9901.0, 9.0], [9919.0, 7.5], [9937.0, 5.5], [9955.0, 3.5], [9973.0, 1.5]],
StatusHelper.resample(testarray, 5))

# now increase the last sequence
testarray << [now+1, 1000]
assert_equal([[9900.9, 9.0], [9919.1, 7.5], [9937.300000000001, 5.5], [9955.500000000002, 3.5], [9973.700000000003, 1.5]],
StatusHelper.resample(testarray, 5))

# now add a gap
testarray.delete [9980, 2]
testarray.delete [9970, 3]
testarray.delete [9960, 4]
# the value stays the same in 2 intervals
assert_equal([[9900.9, 9.0], [9919.1, 7.5], [9937.300000000001, 5.5], [9955.500000000002, 5.5], [9973.700000000003, 1.0]],
StatusHelper.resample(testarray, 5))
end

end

0 comments on commit e687aa8

Please sign in to comment.