Skip to content

Commit

Permalink
added tests, modified update methods
Browse files Browse the repository at this point in the history
  • Loading branch information
milktrader committed Sep 14, 2016
1 parent c52e85a commit b4bea66
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
18 changes: 10 additions & 8 deletions src/modify.jl
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
###### update ####################

function update{T,N,D}(ta::TimeArray{T,N,D}, tstamp::D, val::Vector{T})
function update{T,N,D}(ta::TimeArray{T,N,D}, tstamp::D, val::Array{T,N})

if length(ta) == 0
t = vcat(tstamp)
vals = vcat(val')
uta = TimeArray(tstamp, val, ta.colnames, ta.meta)
elseif tstamp < maximum(ta.timestamp)
error("only appending operations supported")
else
t = vcat(ta.timestamp, tstamp)
vals = vcat(ta.values, val')
vals = vcat(ta.values, val)
uta = TimeArray(t, vals, ta.colnames, ta.meta)
end
TimeArray(t, vals, ta.colnames, ta.meta)
uta
end

function update{T,N,D}(ta::TimeArray{T,N,D}, tstamp::D, val::T)

if length(ta) == 0
t = vcat(tstamp)
vals = vcat(val)
uta = TimeArray(tstamp, [val], ta.colnames, ta.meta)
elseif tstamp < maximum(ta.timestamp)
error("only appending operations supported")
else
t = vcat(ta.timestamp, tstamp)
vals = vcat(ta.values, val)
uta = TimeArray(t, vals, ta.colnames, ta.meta)
end
TimeArray(t, vals, ta.colnames, ta.meta)
uta
end

###### rename ####################
Expand Down
22 changes: 19 additions & 3 deletions test/modify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@ facts("update method works") do

new_cls = update(cl, today(), 111.11)
new_clv = update(cl, today(), [111.11])
new_ohlc = update(ohlc, today(), [111.11, 222.22, 333.33, 444.44])
new_ohlc = update(ohlc, today(), [111.11 222.22 333.33 444.44])
empty1 = TimeArray(Vector{Date}(), Array{Int}(0,1), ["foo"], "bar")
empty2 = TimeArray(Vector{Date}(), Array{Int}(0,2), ["foo", "bar"], "baz")
update1 = update(empty1, Date(2000,1,1), 12)
update2 = update(empty2, Date(2000,1,1), [10 11])

context("update a single column time array with single value") do
@fact last(new_cls.values) --> 111.11

context("update an empty time array") do
@fact length(empty1) --> 0
@fact length(empty2) --> 0
@fact length(update1) --> 1
@fact length(update2) --> 1
@fact update1.timestamp[1] --> Date(2000,1,1)
@fact update2.timestamp[1] --> Date(2000,1,1)
@fact update1.values --> [12]
@fact update2.values --> [10 11]
@fact update1.colnames --> ["foo"]
@fact update2.colnames --> ["foo", "bar"]
@fact update1.meta --> "bar"
@fact update2.meta --> "baz"
end

context("update a single column time array with single value vector") do
Expand Down

0 comments on commit b4bea66

Please sign in to comment.