Skip to content

Commit

Permalink
fixes from review comments
Browse files Browse the repository at this point in the history
thanks @jasonjensen!
  • Loading branch information
bbejanov committed Oct 30, 2023
1 parent a599838 commit 56d2a0c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
18 changes: 9 additions & 9 deletions src/dataecon/DataEcon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function __init__()
# make sure the loaded library is the same version as the one that generated our C.jl
version = VersionNumber(unsafe_string(C.de_version()))
if version != VersionNumber(C.DE_VERSION)
throw(ErrorException("Library version $(version) does not match expected version $(C.DE_VERSION)."))
throw(ErrorException("DataEcon library version $(version) does not match expected version $(C.DE_VERSION)."))
end
return
end
Expand Down Expand Up @@ -87,14 +87,14 @@ function _do_open(C_open, args...)
return handle
end

function opendaec(fname::AbstractString; readonly=false)
function opendaec(fname::AbstractString; readonly=true, write=!readonly)
fname = string(fname)
handle = _do_open(readonly ? C.de_open_readonly : C.de_open, fname)
return DEFile(handle, fname)
open_func = _do_open(write ? C.de_open : C.de_open_readonly, fname)
return DEFile(open_func, fname)
end

function opendaec(f::Function, fname::AbstractString; readonly=false)
de = opendaec(fname; readonly)
function opendaec(f::Function, fname::AbstractString; readonly=true, write=!readonly)
de = opendaec(fname; readonly, write)
try
f(de)
finally
Expand Down Expand Up @@ -214,7 +214,7 @@ end
# import ..LittleDict

"""
get_all_attributes(de, obejct_id)
get_all_attributes(de, object_id)
Retrieve the names and value of all attributes of the object with the given id.
They are returned in a dictionary.
Expand Down Expand Up @@ -509,7 +509,7 @@ end
writedb(de::DEFile, data::Workspace) = writedb(de, root_id, data)
writedb(de::DEFile, parent::AbstractString, data::Workspace) = writedb(de, find_fullpath(de, string(parent)), data)
function writedb(file::AbstractString, args...)
opendaec(file) do de
opendaec(file, write=true) do de
writedb(de, args...)
end
end
Expand Down Expand Up @@ -569,7 +569,7 @@ function readdb(de::DEFile, name::AbstractString)
read_data(de, oid)
end
function readdb(file::AbstractString, args...)
opendaec(file) do de
opendaec(file, readonly=true) do de
readdb(de, args...)
end
end
Expand Down
11 changes: 9 additions & 2 deletions src/dataecon/I.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ _check(rc::Cint) = rc == 0 || throw(DEError())

const StrOrSym = Union{Symbol,AbstractString}

_to_de_scalar_val(value) = throw(ArgumentError("Unable to write scalar value of type $(typeof(value))."))
_to_de_scalar_val(@nospecialize(value)) = throw(ArgumentError("DataEcon: unable to write scalar value of type $(typeof(value))."))
_to_de_scalar_val(value::Integer) = value
_to_de_scalar_val(value::Real) = float(value)
_to_de_scalar_val(value::Complex) = float(value)
Expand Down Expand Up @@ -245,8 +245,15 @@ end
#############################################################################
# write tseries and mvtseries

struct _ArrayData
eltype::C.type_t
elfreq::C.frequency_t
obj_type::C.type_t
nbytes::Int
val
end

_de_array_data(; eltype, elfreq=C.freq_none, obj_type, nbytes, val) = (; eltype, elfreq, obj_type, nbytes, val)
_de_array_data(; eltype, elfreq=C.freq_none, obj_type, nbytes, val) = _ArrayData(eltype, elfreq, obj_type, nbytes, val)

_to_de_array(value) = throw(ArgumentError("Unable to write value of type $(typeof(value))."))

Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Test
using TimeSeriesEcon
using Statistics
using Suppressor

include("test_mit.jl")
include("test_tseries.jl")
Expand Down
29 changes: 14 additions & 15 deletions test/test_dataecon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ rm(test_file * "-journal", force=true)
@testset "DE file" begin
global de
@test_throws DE.DEError DE.opendaec(test_file, readonly=true)
de = DE.opendaec(test_file)
de = DE.opendaec(test_file, write=true)
@test isopen(de)
@test (DE.closedaec!(de); true)
@test !isopen(de)
@test (de = DE.opendaec(test_file, readonly=true); isopen(de))
@test (DE.closedaec!(de); !isopen(de))
de = DE.opendaec(test_file)
de = DE.opendaec(test_file, write=true)

# test find_object throws exception or returns missing
@test_throws DE.DEError DE.find_object(de, DE.root_id, "nosuchobject")
Expand Down Expand Up @@ -322,7 +322,7 @@ end
DE.opendaec(test_file) do de
@test !isempty(DE.readdb(de))
end
DE.opendaec(test_file) do de
DE.opendaec(test_file, write=true) do de
@test (empty!(de); true)
@test isempty(DE.readdb(de))
end
Expand Down Expand Up @@ -372,19 +372,19 @@ end

# Frequency \ pack/unpack || year+period | year+month+day
# ===============================================================
# YP Date || works | not implemented
# YP Date || works | works as of DataEcon v0.3.1
# Cal Date || works | works

@testset "pack/unpack year_period" begin
# here we test the first column of the table - that is packing and unpacking
# MITs given year-period
Random.seed!(0x007)
fc = Dict{Type{<:Frequency},Base.RefValue{Int}}()
jfreqs = [Daily, BDaily, (Weekly{i} for i = 1:7)...,
all_freqs = [Daily, BDaily, (Weekly{i} for i = 1:7)...,
Monthly, (Quarterly{i} for i = 1:3)...,
(HalfYearly{i} for i = 1:6)..., (Yearly{i} for i = 1:12)...]
for i = 1:1000
fr = rand(jfreqs)
fr = rand(all_freqs)
d1 = convert(Int, rand(Int16))
if fr == BDaily && d1 < 1
# make sure it's non-negative to work around known bug
Expand All @@ -411,25 +411,24 @@ end
get!(fc, fr, Ref(0))[] += 1
end
# make sure we tested all frequencies
foreach(jfreqs) do fr
foreach(all_freqs) do fr
@test get!(fc, fr, Ref(0))[] > 10
end
end;


@testset "pack/unpack year_month_day" begin
# here we test the second column of the table - that is packing and unpacking
# MITs given year-month-day. This doesn't work for YP frequencies.
# MITs given year-month-day.
Random.seed!(0x007)
fc = Dict{Type{<:Frequency},Base.RefValue{Int}}()
calfreqs = [Daily, BDaily, (Weekly{i} for i = 1:7)...,]
# all_freqs = [Daily, BDaily, (Weekly{i} for i = 1:7)...,]
all_freqs = [Daily, BDaily, (Weekly{i} for i = 1:7)...,
Monthly, (Quarterly{i} for i = 1:3)...,
(HalfYearly{i} for i = 1:6)..., (Yearly{i} for i = 1:12)...]
for i = 1:1000
fr = rand(calfreqs)
fr = rand(all_freqs)
d1 = convert(Int, rand(Int16))
if fr == BDaily && d1 < 1
# make sure it's non-negative to work around known bug
d1 = 1 - d1
end
d = MIT{fr}(d1)
date = Date(d)
yr = Dates.year(date)
Expand Down Expand Up @@ -458,7 +457,7 @@ end;
get!(fc, fr, Ref(0))[] += 1
end
# make sure we tested all supported frequencies
foreach(calfreqs) do fr
foreach(all_freqs) do fr
@test get!(fc, fr, Ref(0))[] > 10
end
end;
Expand Down

0 comments on commit 56d2a0c

Please sign in to comment.