Skip to content

Commit

Permalink
polydb: return either Polymake.BigObject or Mongoc.BSON, tests for bo…
Browse files Browse the repository at this point in the history
…th options
  • Loading branch information
apaffenholz committed Jul 15, 2024
1 parent 9062d1f commit e72ff4b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/polydb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ end
find_one(c::Collection{T}, d::Dict=Dict(); opts::Union{Nothing, Dict})
Return one document from a collection `c` matching the criteria given by `d`.
`T` can be chosen from `Polymake.BigObject` and `Mongoc.BSON`.
Apply search options `opts`.
# Examples
```julia-repl
Expand All @@ -186,17 +187,30 @@ julia> pm_object = Polymake.Polydb.find_one(collection, query, opts=opts);
julia> typeof(pm_object)
Polymake.LibPolymake.BigObjectAllocated
julia> collection_bson = Polymake.Polydb.Collection{Mongoc.BSON}(collection);
julia> pm_object = Polymake.Polydb.find_one(collection_bson, query, opts=opts);
julia> typeof(pm_object)
Mongoc.BSON
```
"""
function Mongoc.find_one(c::Collection{T}, d::Dict=Dict(); opts::Union{Nothing, Dict}=nothing) where T
function Mongoc.find_one(c::Collection{Polymake.BigObject}, d::Dict=Dict(); opts::Union{Nothing, Dict}=nothing)
p = Mongoc.find_one(c.mcol, Mongoc.BSON(d); options=(isnothing(opts) ? nothing : Mongoc.BSON(opts)))
return isnothing(p) ? nothing : parse_document(p)

Check warning on line 201 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L199-L201

Added lines #L199 - L201 were not covered by tests
end

function Mongoc.find_one(c::Collection{Mongoc.BSON}, d::Dict=Dict(); opts::Union{Nothing, Dict}=nothing)
p = Mongoc.find_one(c.mcol, Mongoc.BSON(d); options=(isnothing(opts) ? nothing : Mongoc.BSON(opts)))
return isnothing(p) ? nothing : p

Check warning on line 206 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L204-L206

Added lines #L204 - L206 were not covered by tests
end

"""
find_one(c::Collection{T}, d::Pair...)
Search a collection `c` for documents matching the criteria given by `d`.
Return one document from a collection `c` matching the criteria given by `d`.
`T` can be chosen from `Polymake.BigObject` and `Mongoc.BSON`.
# Examples
```julia-repl
julia> db = Polymake.Polydb.get_db();
Expand All @@ -207,13 +221,25 @@ julia> pm_object = Polymake.Polydb.find_one(collection, "DIM"=>3, "N_FACETS"=>5)
julia> typeof(pm_object)
Polymake.LibPolymake.BigObjectAllocated
julia> collection_bson = Polymake.Polydb.Collection{Mongoc.BSON}(collection);
julia> pm_object = Polymake.Polydb.find_one(collection_bson, "DIM"=>3, "N_FACETS"=>5);
julia> typeof(pm_object)
Mongoc.BSON
```
"""
function Mongoc.find_one(c::Collection{T}, d::Pair...) where T
function Mongoc.find_one(c::Collection{Polymake.BigObject}, d::Pair...)
p = Mongoc.find_one(c.mcol, Mongoc.BSON(d...))
return isnothing(p) ? nothing : parse_document(p)

Check warning on line 235 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L233-L235

Added lines #L233 - L235 were not covered by tests
end

function Mongoc.find_one(c::Collection{Mongoc.BSON}, d::Pair...)
p = Mongoc.find_one(c.mcol, Mongoc.BSON(d...))
return isnothing(p) ? nothing : p

Check warning on line 240 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L238-L240

Added lines #L238 - L240 were not covered by tests
end

"""
Collection{T}(c::Collection)
Expand Down
12 changes: 12 additions & 0 deletions test/polydb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ using Polymake.Polydb.Mongoc
@test Polymake.Polydb.Collection{Polymake.BigObject}(collection_bson) isa Polymake.Polydb.Collection{Polymake.BigObject}
constraints = _acp(["DIM" => 3, "N_VERTICES" => 8])
query = Dict(constraints...)
opts_success = Dict("skip"=>3)
opts_nothing = Dict("skip"=>13)
# Queries
@test Polymake.Polydb.find(collection_bo, query) isa Polymake.Polydb.Cursor
@test Polymake.Polydb.find(collection_bo, query) isa Polymake.Polydb.Cursor{Polymake.BigObject}
Expand All @@ -46,6 +48,16 @@ using Polymake.Polydb.Mongoc
@test Polymake.Polydb.Cursor{Polymake.BigObject}(results_bson) isa Polymake.Polydb.Cursor
@test Polymake.Polydb.Cursor{Polymake.BigObject}(results_bson) isa Polymake.Polydb.Cursor{Polymake.BigObject}

@test Polymake.Polydb.find_one(collection_bo, query) isa Polymake.BigObject
@test Polymake.Polydb.find_one(collection_bo, query, opts=opts_success) isa Polymake.BigObject
@test isnothing(Polymake.Polydb.find_one(collection_bo, query, opts=opts_nothing))
@test Polymake.Polydb.find_one(collection_bo, constraints...) isa Polymake.BigObject

@test Polymake.Polydb.find_one(collection_bson, query) isa Mongoc.BSON
@test Polymake.Polydb.find_one(collection_bson, query, opts=opts_success) isa Mongoc.BSON
@test isnothing(Polymake.Polydb.find_one(collection_bson, query, opts=opts_nothing))
@test Polymake.Polydb.find_one(collection_bson, constraints...) isa Mongoc.BSON

@test length(collection_bo, constraints...) == 7
@test length(collection_bo, query) == 7
@test length(collection_bson, constraints...) == 7
Expand Down

0 comments on commit e72ff4b

Please sign in to comment.