-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtest_ar_search.rb
309 lines (261 loc) · 9.52 KB
/
test_ar_search.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
require "minitest/autorun"
# require_relative "../lib/litestack/litedb"
require "active_record"
require "active_record/base"
require_relative "patch_ar_adapter_path"
require_relative "../lib/active_record/connection_adapters/litedb_adapter"
ActiveRecord::Base.establish_connection(
adapter: "litedb",
database: ":memory:"
)
# ActiveRecord::Base.logger = Logger.new(STDOUT)
db = ActiveRecord::Base.connection.raw_connection
db.execute("CREATE TABLE authors(id INTEGER PRIMARY KEY, name TEXT, created_at TEXT, updated_at TEXT)")
db.execute("CREATE TABLE publishers(id INTEGER PRIMARY KEY, name TEXT, created_at TEXT, updated_at TEXT)")
db.execute("CREATE TABLE books(id INTEGER PRIMARY KEY, title TEXT, description TEXT, published_on TEXT, author_id INTEGER, publisher_id INTEGER, state TEXT, created_at TEXT, updated_at TEXT, active INTEGER)")
db.execute("CREATE TABLE reviews(id INTEGER PRIMARY KEY, book_id INTEGER)")
db.execute("CREATE TABLE comments(id INTEGER PRIMARY KEY, review_id INTEGER)")
# simulate action text
db.execute("CREATE TABLE rich_texts(id INTEGER PRIMARY KEY, body TEXT, record_id INTEGER, record_type TEXT, created_at TEXT, updated_at TEXT) ")
# custom primary and foreing key columns
db.execute("CREATE TABLE users(user_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))), name TEXT, created_at TEXT, updated_at TEXT)")
db.execute("CREATE TABLE posts(post_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))), author_id TEXT, title TEXT, content TEXT, created_at TEXT, updated_at TEXT)")
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end
class Author < ApplicationRecord
has_many :books
has_many :publishers, through: :books
include Litesearch::Model
litesearch do |schema|
schema.field :name
end
end
class Publisher < ApplicationRecord
has_many :books
has_many :authors, through: :books
include Litesearch::Model
end
class Book < ApplicationRecord
belongs_to :author
belongs_to :publisher
has_many :reviews
include Litesearch::Model
litesearch do |schema|
schema.fields [:description, :state]
schema.field :publishing_year, col: :published_on
schema.field :title, weight: 10
schema.field :ignored, weight: 0
schema.field :author, target: "authors.name"
schema.field :publisher, target: "publishers.name", col: :publisher_id
schema.filter_column :active
schema.tokenizer :porter
end
end
class RichText < ApplicationRecord
belongs_to :record, polymorphic: true
def self.table_name
"rich_texts"
end
end
module ActionText
class RichText < ::RichText
end
end
class Review < ApplicationRecord
belongs_to :book
has_one :rich_text, as: :record
include Litesearch::Model
litesearch do |schema|
schema.field :body, target: "rich_texts.body", as: :record
end
end
class Comment < ApplicationRecord
belongs_to :review
has_one :rich_text, as: :record
include Litesearch::Model
litesearch do |schema|
schema.field :body, rich_text: true
end
end
# no table items was created
class Item < ApplicationRecord
include Litesearch::Model
litesearch do |schema|
schema.field :name
end
end
class User < ApplicationRecord
self.primary_key = "user_id"
has_many :posts, foreign_key: :author_id
include Litesearch::Model
litesearch do |schema|
schema.fields %w[ name ]
schema.primary_key :user_id
end
end
class Post < ApplicationRecord
self.primary_key = "post_id"
belongs_to :author, class_name: "User", primary_key: :user_id
include Litesearch::Model
litesearch do |schema|
schema.fields %w[ title content ]
schema.field :author, target: "users.name", primary_key: :user_id
schema.primary_key :post_id
end
end
class TestActiveRecordLitesearch < Minitest::Test
def setup
Book.drop_index!
Book.delete_all
Author.delete_all
Publisher.delete_all
User.delete_all
Post.delete_all
Book.litesearch do |schema|
schema.fields [:description, :state]
schema.field :publishing_year, col: :published_on
schema.field :title, weight: 10
schema.field :ignored, weight: 0
schema.field :author, target: "authors.name"
schema.field :publisher, target: "publishers.name", col: :publisher_id
schema.filter_column :active
schema.tokenizer :porter
end
Publisher.create(name: "Penguin")
Publisher.create(name: "Adams")
Publisher.create(name: "Flashy")
Author.create(name: "Hanna Spiegel")
Author.create(name: "David Antrop")
Author.create(name: "Aly Lotfy")
Author.create(name: "Osama Penguin")
Book.create(title: "In a middle of a night", description: "A tale of sleep", published_on: "2008-10-01", state: "available", active: true, publisher_id: 1, author_id: 1)
Book.create(title: "In a start of a night", description: "A tale of watching TV", published_on: "2006-08-08", state: "available", active: false, publisher_id: 2, author_id: 1)
u = User.create(name: "Gabriel")
Post.create(author: u, title: "Post #1", content: "Whenever you create a table without specifying the WITHOUT ROWID option, you get an implicit auto-increment column called rowid. The rowid column store 64-bit signed integer that uniquely identifies a row in the table.")
Post.create(author: u, title: "Post #2", content: "If a table has the primary key that consists of one column, and that column is defined as INTEGER then this primary key column becomes an alias for the rowid column.")
end
def test_polymorphic
review = Review.create(book_id: 1)
rt = RichText.create(record: review, body: "a new review")
rs = Review.search("review")
assert_equal 1, rs.length
rt.destroy
review.destroy
end
def test_rich_text
review = Review.create(book_id: 1)
rt = RichText.create(record: review, body: "a new review")
comment = Comment.create(review: review)
ct = RichText.create(record: comment, body: "a new comment on the review")
rs = Comment.search("comment")
assert_equal 1, rs.length
ct.destroy
comment.destroy
rt.destroy
review.destroy
end
def test_similar
newbook = Book.create(title: "A night", description: "A tale of watching TV", published_on: "2006-08-08", state: "available", active: true, publisher_id: 2, author_id: 2)
book = Book.find 1
books = book.similar
assert_equal 1, books.length
assert_equal "A night", books.first.title
newbook.destroy
end
def test_search
rs = Author.search("Hanna")
assert_equal 1, rs.length
assert_equal Author, rs[0].class
end
def test_search_custom_primary_key
rs = User.search("gabriel")
assert_equal 1, rs.length
assert_equal User, rs[0].class
end
def test_search_field
rs = Book.search("author: Hanna")
assert_equal 1, rs.length
assert_equal Book, rs[0].class
end
def test_search_field_custom_primary_key
rs = Post.search("author: gabriel")
assert_equal 2, rs.length
assert_equal true, [Post, Post] - [rs[0].class, rs[1].class] == []
end
def test_search_all
rs = Book.search_all("Hanna", {models: [Author, Book]})
assert_equal 2, rs.length
assert_equal true, [Author, Book] - [rs[0].class, rs[1].class] == []
end
def test_search_all_custom_primary_key
rs = Post.search_all("gabriel", {models: [Post, User]})
assert_equal 3, rs.length
assert_equal true, [Post, User, Post] - [rs[0].class, rs[1].class, rs[2].class] == []
end
def test_modify_schema
Book.litesearch do |schema|
schema.fields [:description, :state]
schema.field :publishing_year, col: :published_on
schema.field :title, weight: 10
schema.field :ignored, weight: 0
schema.field :author, target: "authors.name"
schema.field :publisher, target: "publishers.name", col: :publisher_id
schema.rebuild_on_modify true
end
rs = Book.search("night tale")
assert_equal 2, rs.length
Book.rebuild_index!
rs = Book.search("night tale")
assert_equal 2, rs.length
end
def test_modify_schema_rebuild_later
Book.litesearch do |schema|
schema.fields [:description, :state]
schema.field :publishing_year, col: :published_on
schema.field :title, weight: 10
schema.field :ignored, weight: 0
schema.field :author, target: "authors.name"
schema.field :publisher, target: "publishers.name", col: :publisher_id
end
rs = Book.search("night tale")
assert_equal 1, rs.length
Book.rebuild_index!
rs = Book.search("night tale")
assert_equal 2, rs.length
end
def test_update_referenced_column
rs = Book.search("Hanna")
assert_equal 1, rs.length
Author.find(1).update(name: "Hayat")
rs = Book.search("Hanna")
assert_equal 0, rs.length
rs = Book.search("Hayat")
assert_equal 1, rs.length
end
def test_rebuild_on_create
Publisher.litesearch do |schema|
schema.field :name
schema.rebuild_on_create true
end
rs = Publisher.search("Penguin")
assert_equal 1, rs.length
end
def test_uncreated_table
db = ActiveRecord::Base.connection.raw_connection
db.execute("CREATE TABLE items(id INTEGER PRIMARY KEY, name TEXT, created_at TEXT, updated_at TEXT)")
rs = Item.search("some")
assert_equal 0, rs.length
Item.create(name: "some item")
rs = Item.search("some")
assert_equal 1, rs.length
Item.create(name: "another item")
rs = Item.search("item")
assert_equal 2, rs.length
end
def test_ignore_tables
assert_equal false, ActiveRecord::SchemaDumper.ignore_tables.empty?
# we have created 8 models, one ignore regex for each
assert_equal 8, ActiveRecord::SchemaDumper.ignore_tables.count
end
end