-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomated_test.rb
164 lines (96 loc) · 3.36 KB
/
automated_test.rb
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
152
153
154
155
156
157
158
159
160
161
162
163
164
require 'byebug'
require 'tmsu_file_db'
require './automated_test_helper.rb'
`rm -rf db/*`
TmsuFileDb.start
# Model declaration
class User < TmsuModel
configure root_path: "./db/users"
validate do |record|
record[:name].nil? ? ["name can't be blank"] : []
end
validate(:email) do |attr, record|
attr&.include?("@") ? [] : ["email #{attr} isn't valid"]
end
end
# Case 1 - validation errors on save + update
u = User.new name: "max"
fail_if { u.valid? }
fail_unless { u.errors == ["email isn't valid"] }
fail_if { u.save }
fail_if { u.update(name: "potato") }
fail_unless { u.name == "max" }
# Case 2 - succesful save + update
u[:email] = "[email protected]"
fail_if { u.persisted? }
fail_unless { u.valid? }
fail_unless { u.save }
fail_unless { u.persisted? }
fail_unless { u.update(email: "[email protected]") }
fail_if { u.update(email: "") }
# Case 3 - getters + setters
fail_unless { u.name == "max" }
fail_unless { u["name"] == "max" }
fail_unless { u[:name] == "max" }
u[:name] = "max p."
fail_unless { u[:name] == "max p." }
fail_unless { u.attributes[:name] == "max p." }
fail_unless { File.exists? u.path }
fail_unless { File.read(u.path).empty? }
u.save
u.write "hello"
fail_unless { File.read(u.path) == "hello" }
fail_unless { u.tags == { 'email' => "[email protected]", 'name' => "max\\ p." } }
fail_unless { u.tags == u.attributes }
# Case 4 - destroy attribute, destroy_record
u.delete :name
# tag has been deleted
fail_unless { u.tags == { 'email' => "[email protected]" } }
# destroy deletes the file from the filesystem
u.destroy
# destroy makes it no longer persisted
fail_if { u.persisted? }
# initialize a record from another's attributes
u = User.create(u.attributes.merge(name: "max p."))
# clone saved succesfully
fail_unless { u.persisted? }
fail_unless { User.where(name: "max\\ p.")[0]&.name == "max\\ p." }
fail_unless { User.find_by(name: "max\\ p.")&.name == "max\\ p." }
fail_unless { User.update_all(name: "max") }
fail_unless { User.all[0].name == "max" }
# Case4 - TmsuRuby.file
file_path = "db/#{SecureRandom.hex}"
`touch #{file_path}`
tmsu_file = TmsuRuby.file file_path
fail_unless { tmsu_file.tags == {} }
tmsu_file.tag "foo"
fail_unless { tmsu_file.tags == { 'foo' => nil } }
tmsu_file.untag "foo"
fail_unless { tmsu_file.tags == { } }
tmsu_file.tag ["foo", "bar"]
fail_unless { tmsu_file.tags == { 'foo' => nil, 'bar' => nil } }
tmsu_file.tag(a: 1, b: 2)
fail_unless { tmsu_file.tags == { 'foo' => nil, 'bar' => nil, 'a' => "1", 'b' => "2" } }
`mkdir db/sample`
filepath = "db/sample/#{SecureRandom.hex}.txt"
`touch #{filepath}`
glob_selector = "./db/sample/*.txt"
tmsu_file = TmsuRuby.file glob_selector
tmsu_file.tag_selector "foo"
tmsu_file.tag_selector ["a", "b"]
tmsu_file.tag_selector c: 1, d: 2
# When working with TmsuRuby.file, the untag operation requires the value to
# be specified as well
# This is because TMSU can have duplicate keys
tmsu_file.untag_selector "c=1"
# check results of glob actions
fail_unless { TmsuRuby.file(filepath).tags == { 'foo' => nil, 'a' => nil, 'b' => nil, 'd' => "2" } }
# scoped search
fail_unless { TmsuRuby.file("./db/sample").files("foo").include? "./#{filepath}" }
# scope defaults to .
fail_unless { TmsuRuby.file.files("foo").include? "./#{filepath}" }
puts "\n\nTEST PASSED\n\n"
at_exit do
`rm -rf db/sample`
`rm -rf db/*`
end