Skip to content

Commit b9f546b

Browse files
committed
some tests
1 parent ce0c835 commit b9f546b

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ SQLiteGraph.findedges(db, b=2)
6363

6464
SQLiteGraph is **STRONGLY** influenced (much has been copied verbatim) from [https://github.com/dpapathanasiou/simple-graph](https://github.com/dpapathanasiou/simple-graph).
6565

66-
The differences here are minor, opinionated changes made by `@joshday`:
67-
68-
- Node IDs are `Int`: 1, 2, 3...
69-
- Both `nodes` and `edges` tables have field `props`
70-
7166
## TODOs
7267

7368
- Prepare SQL into compiled `SQLite.Stmt`s.

src/SQLiteGraph.jl

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using SQLite: SQLite
44
import DBInterface: execute
55
using JSON3: JSON3
66

7-
export DB, ReadAs
7+
export DB, Node, Edge, find_nodes, find_edges
88

99
#-----------------------------------------------------------------------------# utils
1010
read_sql(fn::String) = read(joinpath(@__DIR__, "sql", fn), String)
@@ -19,15 +19,24 @@ struct Node{T}
1919
props::T
2020
end
2121
Node(id::Integer, props=nothing) = Node(id, props)
22-
Base.show(io::IO, o::Node) = (print(io, "Node($(o.id)) with props: "); show(io, o.props))
22+
function Base.show(io::IO, o::Node)
23+
print(io, "Node($(o.id)) with props: ")
24+
print(io, o.props)
25+
end
2326

2427
struct Edge{T}
2528
source::Int
2629
target::Int
2730
props::T
2831
end
29-
Base.show(io::IO, o::Edge) = (print(io, "Edge($(o.source)$(o.target)) with props: "); show(io, o.props))
3032
Edge(source::Integer, target::Integer, props=nothing) = Edge(source, target, props)
33+
Edge(T::DataType, e::Edge{<:AbstractString}) = Edge(e.source, e.target, JSON3.read(e.props, T))
34+
Edge(e::Edge{<:AbstractString}, T::DataType) = Edge(e.source, e.target, JSON3.read(e.props, T))
35+
function Base.show(io::IO, o::Edge)
36+
print(io, "Edge($(o.source)$(o.target)) with props: ")
37+
print(io, o.props)
38+
end
39+
3140

3241
#-----------------------------------------------------------------------------# DB
3342
"""
@@ -61,6 +70,7 @@ struct DB
6170

6271
function DB(file = ":memory:")
6372
db = SQLite.DB(file)
73+
SQLite.@register db SQLite.regexp
6474
statements = [
6575
"CREATE TABLE IF NOT EXISTS nodes (
6676
id INTEGER NOT NULL UNIQUE,
@@ -146,8 +156,8 @@ function Base.deleteat!(db::DB, id::Integer)
146156
execute(db, "DELETE FROM edges WHERE source = ? OR target = ?", (id, id))
147157
db
148158
end
149-
150-
function findnodes(db::DB; kw...)
159+
#-----------------------------------------------------------------------------# find_nodes
160+
function find_nodes(db::DB; kw...)
151161
param = join(map(collect(kw)) do kw
152162
k, v = kw
153163
"json_extract(props, '\$.$k') = $v"
@@ -156,6 +166,11 @@ function findnodes(db::DB; kw...)
156166
res = execute(db, "SELECT * FROM nodes WHERE $param")
157167
isempty(res) ? nothing : [Node(row...) for row in res]
158168
end
169+
function find_nodes(db::DB, r::Regex)
170+
res = execute(db, "SELECT * FROM nodes WHERE props REGEXP ?", (r.pattern,))
171+
isempty(res) ? nothing : [Node(row...) for row in res]
172+
end
173+
159174

160175
#-----------------------------------------------------------------------------# edges
161176
function Base.setindex!(db::DB, props, i::Integer, j::Integer)
@@ -192,7 +207,8 @@ function Base.deleteat!(db::DB, i::Integer, j::Integer)
192207
db
193208
end
194209

195-
function findedges(db::DB; kw...)
210+
#-----------------------------------------------------------------------------# find_edges
211+
function find_edges(db::DB; kw...)
196212
param = join(map(collect(kw)) do kw
197213
k, v = kw
198214
"json_extract(props, '\$.$k') = $v"
@@ -201,5 +217,9 @@ function findedges(db::DB; kw...)
201217
res = execute(db, "SELECT * FROM edges WHERE $param")
202218
isempty(res) ? nothing : [Edge(row...) for row in res]
203219
end
220+
function find_edges(db::DB, r::Regex)
221+
res = execute(db, "SELECT * FROM edges WHERE props REGEXP ?", (r.pattern,))
222+
isempty(res) ? nothing : [Edge(row...) for row in res]
223+
end
204224

205225
end

test/runtests.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
using SQLiteGraph
22
using Test
3+
using JSON3
34

45
@testset "SQLiteGraph.jl" begin
6+
db = DB()
57
@testset "empty DB" begin
6-
db = DB()
78
@test length(db) == 0
89
@test size(db) == (nodes=0, edges=0)
910
@test_throws BoundsError db[5]
1011
@test isnothing(db[1,2])
1112
end
13+
@testset "setindex! & getindex" begin
14+
db[1] = (x=1, y=2)
15+
db[2] = (x=1, y=3)
16+
db[1,2] = (a=1, b=2)
17+
@test db[1] isa Node{String}
18+
@test db[2] isa Node{String}
19+
@test db[1,2] isa Edge{String}
20+
@testset "getindex Range" begin
21+
@test db[1:2] isa Vector{Node{String}}
22+
@test db[1:2][1] == Node(1, JSON3.write((x=1,y=2)))
23+
@test db[1:2][2] == Node(2, JSON3.write((x=1,y=3)))
24+
@test db[1:2,2] == [Edge(1,2,JSON3.write((a=1,b=2)))]
25+
end
26+
end
1227
end

0 commit comments

Comments
 (0)