Skip to content

Commit 9f468f9

Browse files
committed
improve tests and ext with integer type
1 parent 902571f commit 9f468f9

8 files changed

+28
-11
lines changed

ext/WasabiPostgreSQLExt.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using LibPQ
66
using Mocking
77
using Dates
88

9-
Wasabi.mapping(db::Type{LibPQ.Connection}, t::Type{Int64}) = "INTEGER"
9+
Wasabi.mapping(db::Type{LibPQ.Connection}, t::Type{T}) where{T<:Integer} = "INTEGER"
1010
Wasabi.mapping(db::Type{LibPQ.Connection}, t::Type{String}) = "TEXT"
1111
Wasabi.mapping(db::Type{LibPQ.Connection}, t::Type{Bool}) = "BOOLEAN"
1212
Wasabi.mapping(db::Type{LibPQ.Connection}, t::Type{Float64}) = "REAL"

ext/WasabiSQLiteExt.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using DataFrames
66
using Mocking
77
using Dates
88

9-
Wasabi.mapping(db::Type{SQLite.DB}, t::Type{Int64}) = "INTEGER"
9+
Wasabi.mapping(db::Type{SQLite.DB}, t::Type{T}) where{T<:Integer} = "INTEGER"
1010
Wasabi.mapping(db::Type{SQLite.DB}, t::Type{String}) = "TEXT"
1111
Wasabi.mapping(db::Type{SQLite.DB}, t::Type{Bool}) = "INTEGER"
1212
Wasabi.mapping(db::Type{SQLite.DB}, t::Type{Float64}) = "REAL"

test/colnames.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@testset "colnames" begin
2-
@test Wasabi.colnames(User) == Symbol[:id, :name, :created_at, :roles]
2+
@test Wasabi.colnames(User) == Symbol[:id, :name, :created_at, :roles, :profile]
33
@test Wasabi.colnames(Role) == Symbol[:id, :name, :user_id]
44
end

test/coltype.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@testset "coltype" begin
2-
@test Wasabi.coltype(User, :id) == Int
2+
@test Wasabi.coltype(User, :id) == Integer
33
@test Wasabi.coltype(User, :name) == String
44
@test Wasabi.coltype(User, :created_at) == DateTime
55
end

test/model.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
using DataFrames
33

44
u = User("John", now())
5+
@test Wasabi.model2tuple(u) == ((:id, nothing), (:name, "John"), (:created_at, u.created_at), (:roles, Role[]), (:profile, nothing))
56

6-
@test Wasabi.model2tuple(u) == ((:id, nothing), (:name, "John"), (:created_at, u.created_at), (:roles, Role[]))
7+
Base.:(==)(a::Role, b::Role) = a.name == b.name && a.user_id == b.user_id && a.id == b.id
8+
Base.:(==)(a::UserProfile, b::UserProfile) = a.bio == b.bio && a.user_id == b.user_id && a.id == b.id
9+
10+
u = User("John", now(), [Role("admin", 1), Role("user", 1)], UserProfile(1, "bio"))
11+
@test Wasabi.model2tuple(u) == ((:id, nothing), (:name, "John"), (:created_at, u.created_at), (:roles, Role[Role("admin", 1), Role("user", 1)]), (:profile, UserProfile(1, "bio")))
712

813
df = DataFrame(id = [1, 2, 3], name = ["John", "Jane", "Joe"], created_at = [now(), now(), now()])
914
models = Wasabi.df2model(User, df)

test/postgresql.jl

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
conn = Wasabi.connect(configuration)
1212

1313
@test Wasabi.mapping(LibPQ.Connection, Int64) == "INTEGER"
14+
@test Wasabi.mapping(LibPQ.Connection, Int32) == "INTEGER"
1415
@test Wasabi.mapping(LibPQ.Connection, String) == "TEXT"
1516
@test Wasabi.mapping(LibPQ.Connection, Bool) == "BOOLEAN"
1617
@test Wasabi.mapping(LibPQ.Connection, Float64) == "REAL"

test/runtests.jl

+16-6
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,31 @@ using Mocking
99
Random.seed!(42)
1010

1111
mutable struct Role <: Wasabi.Model
12-
id::Union{Nothing, Int}
12+
id::Union{Nothing, Integer}
1313
name::String
14+
user_id::Integer
15+
16+
Role(name::String, user_id::Integer) = new(nothing, name, user_id)
17+
end
18+
19+
mutable struct UserProfile <: Wasabi.Model
20+
id::Union{Nothing, Int}
1421
user_id::Int
22+
bio::Union{String,Nothing}
1523

16-
Role(name::String, user_id::Int) = new(nothing, name, user_id)
24+
UserProfile(user_id::Integer, bio::Union{String,Nothing}) = new(nothing, user_id, bio)
25+
UserProfile(id::Integer, user_id::Integer, bio::Union{String,Nothing}) = new(id, user_id, bio)
1726
end
1827

1928
mutable struct User <: Wasabi.Model
20-
id::Union{Nothing, Int}
29+
id::Union{Nothing, Integer}
2130
name::String
2231
created_at::DateTime
2332
roles::Vector{Role}
33+
profile::Union{Nothing, UserProfile}
2434

25-
User(name::String, created_at::DateTime, roles::Vector{Role} = Role[]) = new(nothing, name, created_at, roles)
26-
User(id::Number, name::String, created_at::DateTime, roles::Vector{Role} = Role[]) = new(id, name, created_at, roles)
35+
User(name::String, created_at::DateTime, roles::Vector{Role} = Role[], profile::Union{Nothing, UserProfile} = nothing) = new(nothing, name, created_at, roles, profile)
36+
User(id::Integer, name::String, created_at::DateTime, roles::Vector{Role} = Role[]) = new(id, name, created_at, roles, nothing)
2737
end
2838

2939
# struct UserProfile <: Wasabi.Model
@@ -41,7 +51,7 @@ end
4151
Wasabi.primary_key(m::Type{User}) = Wasabi.PrimaryKeyConstraint(Symbol[:id])
4252
Wasabi.primary_key(m::Type{Role}) = Wasabi.PrimaryKeyConstraint(Symbol[:id])
4353
Wasabi.foreign_keys(m::Type{Role}) = [Wasabi.ForeignKeyConstraint(Symbol[:user_id], :user, Symbol[:id])]
44-
Wasabi.exclude_fields(m::Type{User}) = [:roles]
54+
Wasabi.exclude_fields(m::Type{User}) = [:roles, :profile]
4555

4656
# Wasabi.primary_key(m::Type{UserProfile}) = Wasabi.PrimaryKeyConstraint(Symbol[:id])
4757

test/sqlite.jl

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
conn = Wasabi.connect(configuration)
66

77
@test Wasabi.mapping(SQLite.DB, Int64) == "INTEGER"
8+
@test Wasabi.mapping(SQLite.DB, Int32) == "INTEGER"
89
@test Wasabi.mapping(SQLite.DB, String) == "TEXT"
910
@test Wasabi.mapping(SQLite.DB, Bool) == "INTEGER"
1011
@test Wasabi.mapping(SQLite.DB, Float64) == "REAL"

0 commit comments

Comments
 (0)