This repository has been archived by the owner on Oct 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_test.jl
151 lines (114 loc) · 4.67 KB
/
basic_test.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
mutable struct Author <: Model
id::DBId{Integer}
name::String
age::Integer
country::String
date::DateTime
end
function Author(;id::Union{Integer, Nothing} = nothing,
name::String="",
age::Integer=0,
country::String="",
date::DateTime=now())
return Author(id, name, age, country, date)
end
mutable struct Book <: Model
id::DBId{String}
title::String
author::ForeignKey{Author}
data::Dict{String, Integer}
end
function Book(;id::Union{String, Nothing}=nothing,
title::String="",
author::Foreign{Author}=Author(),
data::Dict{String, Integer}=Dict())
return Book(id, title, author, data)
end
function _test_on_delete()
mapper = DBMapper(creator)
register!(mapper, Author)
register!(mapper, Book)
configure_relation(mapper, Book, :author, on_delete=Cascade())
author = Author(name="Author 1", age=50)
insert!(mapper, Author(name="Author 1", age=50))
insert!(mapper, Author(name="Author 2", age=50))
insert!(mapper, Author(name="Author 3", age=50))
insert!(mapper, Author(name="Author 4", age=50))
author1 = select_one(mapper, Author, name="Author 1")
author3 = select_one(mapper, Author, name="Author 3")
insert!(mapper, Book(author=author1, title="Book 1", data=Dict("key"=>"Some 1")))
insert!(mapper, Book(author=author1, title="Book 2", data=Dict("key"=>"Some 2")))
insert!(mapper, Book(author=author1, title="Book 3", data=Dict("key"=>"Some 3")))
insert!(mapper, Book(author=author3, data=Dict("key"=>"Some 3")))
delete!(mapper, author3)
#select_all(mapper, Book, author=author1)
end
function _test_basic_functionalities(creator)
mapper = DBMapper(creator)
register!(mapper, Author)
register!(mapper, Book)
configure_relation(mapper, Book, :author, on_delete=Cascade())
@test haskey(mapper.tables, Author)
@test haskey(mapper.tables, Book)
create_table(mapper, Author)
create_table(mapper, Book)
author = Author(name="pirulo", age=50, country="Argentina")
insert!(mapper, author)
@test !isnothing(author.id.x)
id = author.id.x
author = Author(name="Author 1", age=3, country="Argentina")
insert!(mapper, author)
author = Author(name="Author 2", age=3, country="Brasil")
insert!(mapper, author)
author = Author(name="Author 5", age=25, country="Italia")
insert!(mapper, author)
author = Author(name="Author 3", age=3, country="Uruguay")
insert!(mapper, author)
@test StructDatabaseMapping.exists(mapper, Author, name="Enrique Banch") == false
@test StructDatabaseMapping.exists(mapper, Author, name="pirulo") == true
@test StructDatabaseMapping.exists(mapper, author) == true
@test StructDatabaseMapping.exists(mapper, Author, name="Author 3", age=3) == true
@test StructDatabaseMapping.exists(mapper, Author, name="Author 3", age=4) == false
@test StructDatabaseMapping.exists(mapper, Author, pk=author.id.x, age=3) == true
@test StructDatabaseMapping.exists(mapper, Author, pk=author.id.x, age=4) == false
authors = select_all(mapper, Author, age=3)
@test isa(authors[1], Author)
@test length(authors) == 3
authors = select_all(mapper, Author)
@test length(authors) == 5
authors = select_all(mapper, Author, age=3, fields=[:name, :country])
@test Set([author.country for author in authors]) == Set(["Brasil", "Argentina", "Uruguay"])
a = select_one(mapper, Author, id=999)
@test isnothing(a)
a = select_one(mapper, Author, id=id)
@test a.name == "pirulo"
a.name = "otro_pirulo"
a.age = 5
update!(mapper, a; fields=[:name])
author = select_one(mapper, Author, id=id)
@test author.name == "otro_pirulo"
@test author.age == 50
author.name = "some_other_name"
author.age = 5
update!(mapper, author)
author = select_one(mapper, Author, id=id)
@test author.name == "some_other_name"
@test author.age == 5
book = Book(id="super_string_id", author=author,
data=Dict{String, Integer}("some_data"=>5))
insert!(mapper, book)
a = select_one(mapper, Book, id="bbb")
@test isnothing(a)
a = select_one(mapper, Book, id="super_string_id")
@test a.id.x == "super_string_id"
@test get(a.author, mapper).name == "some_other_name"
@test a.data["some_data"] == 5
book = select_one(mapper, Book, id="super_string_id")
delete!(mapper, book)
author = select_one(mapper, Author, id=id)
@test author.name == "some_other_name"
@test author.age == 5
delete!(mapper, Author, name="some_other_name")
author = select_one(mapper, Author, id=id)
@test isnothing(author)
end