-
Notifications
You must be signed in to change notification settings - Fork 33
/
test.py
799 lines (630 loc) · 32.3 KB
/
test.py
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# -*- coding: utf-8 -*-
import shutil
import tempfile
from unittest import TestCase
import string
from flexmock import flexmock
import whoosh
from whoosh.filedb.filestore import RamStorage
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
try:
from flask_sqlalchemy.query import Query
except ImportError:
from flask_sqlalchemy import BaseQuery as Query
from sqlalchemy.orm import Query as SQLAQuery
from sqlalchemy.sql import text
from flask_whooshee import AbstractWhoosheer, Whooshee, WhoosheeQuery
class BaseTestCases(object):
class BaseTest(TestCase):
def __init__(self, *args, **kwargs):
super(BaseTestCases.BaseTest, self).__init__(*args, **kwargs)
self.app = Flask(__name__)
self.app.config['WHOOSHEE_DIR'] = tempfile.mkdtemp()
self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
self.app.config['TESTING'] = True
self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.db = SQLAlchemy(self.app)
def setUp(self):
self.ctx = self.app.app_context()
self.ctx.push()
class User(self.db.Model):
id = self.db.Column(self.db.Integer, primary_key=True)
name = self.db.Column(self.db.String)
# separate index for just entry
@self.wh.register_model('title', 'content')
class Entry(self.db.Model):
id = self.db.Column(self.db.Integer, primary_key=True)
title = self.db.Column(self.db.String)
content = self.db.Column(self.db.Text)
user = self.db.relationship(User, backref = self.db.backref('entries'))
user_id = self.db.Column(self.db.Integer, self.db.ForeignKey('user.id'))
# index for both entry and user
@self.wh.register_whoosheer
class EntryUserWhoosheer(AbstractWhoosheer):
schema = whoosh.fields.Schema(
entry_id = whoosh.fields.NUMERIC(stored=True, unique=True),
user_id = whoosh.fields.NUMERIC(stored=True),
username = whoosh.fields.TEXT(),
title = whoosh.fields.TEXT(),
content = whoosh.fields.TEXT())
models = [Entry, User]
@classmethod
def update_user(cls, writer, user):
pass # TODO: update all users entries
@classmethod
def update_entry(cls, writer, entry):
writer.update_document(entry_id=entry.id,
user_id=entry.user.id,
username=entry.user.name,
title=entry.title,
content=entry.content)
@classmethod
def insert_user(cls, writer, user):
# nothing, user doesn't have entries yet
pass
@classmethod
def insert_entry(cls, writer, entry):
writer.add_document(entry_id=entry.id,
user_id=entry.user.id,
username=entry.user.name,
title=entry.title,
content=entry.content)
@classmethod
def delete_user(cls, writer, user):
# nothing, user doesn't have entries yet
pass
@classmethod
def delete_entry(cls, writer, entry):
writer.delete_by_term('entry_id', entry.id)
@self.wh.register_model('attribute')
class ModelWithNonIntID(self.db.Model):
id = self.db.Column(self.db.String, primary_key=True)
attribute = self.db.Column(self.db.String)
self.User = User
self.Entry = Entry
self.EntryUserWhoosheer = EntryUserWhoosheer
self.ModelWithNonIntID = ModelWithNonIntID
self.db.create_all()
self.u1 = User(name=u'chuck')
self.u2 = User(name=u'arnold')
self.u3 = User(name=u'silvester')
self.e1 = Entry(title=u'chuck nr. 1 article', content=u'blah blah blah', user=self.u1)
self.e2 = Entry(title=u'norris nr. 2 article', content=u'spam spam spam', user=self.u1)
self.e3 = Entry(title=u'arnold blah', content=u'spam is cool', user=self.u2)
self.e4 = Entry(title=u'the less dangerous', content=u'chuck is better', user=self.u3)
self.n1 = ModelWithNonIntID(id='guybrush', attribute='threepwood')
self.all_inst = [self.u1, self.u2, self.u3, self.e1, self.e2, self.e3, self.e4, self.n1]
def tearDown(self):
shutil.rmtree(self.app.config['WHOOSHEE_DIR'], ignore_errors=True)
Whooshee.whoosheers = []
self.db.drop_all()
self.ctx.pop()
# tests testing model whoosheers should have mw in their name, for custom whoosheers it's cw
# ideally, there should be a separate class for model whoosheer and custom whoosheer
# but we also want to test how they coexist
def test_nothing_found(self):
found = self.Entry.query.whooshee_search('not there!').all()
self.assertEqual(len(found), 0)
def test_no_autoupdate(self):
for whoosheer in self.wh.whoosheers:
whoosheer.auto_update = False
self.db.session.add(self.u1)
self.db.session.commit()
found = self.Entry.query.whooshee_search('chuck').all()
self.assertEqual(len(found), 0) # nothing is found
for whoosheer in self.wh.whoosheers:
whoosheer.auto_update = True
self.db.session.add(self.u2)
self.db.session.commit()
found = self.Entry.query.whooshee_search('arnold').all()
self.assertEqual(len(found), 1) # arnold is found
def test_mw_result_in_different_fields(self):
self.db.session.add_all(self.all_inst)
self.db.session.commit()
found = self.Entry.query.whooshee_search('chuck').all()
self.assertEqual(len(found), 2)
# there is no assertIn in Python 2.6
self.assertTrue(self.e1 in found)
self.assertTrue(self.e4 in found)
def test_cw_result_in_different_tables(self):
self.db.session.add_all(self.all_inst)
self.db.session.commit()
found = self.Entry.query.join(self.User).whooshee_search('chuck').all()
self.assertEqual(len(found), 3)
self.assertTrue(self.e1 in found)
self.assertTrue(self.e2 in found)
self.assertTrue(self.e4 in found)
def test_more_items(self):
expected_count = 0
# couldn't test for large set due to some bugs either in sqlite or whoosh or SA
# got: OperationalError: (OperationalError) too many SQL variables u'SELECT entry.id
# ... FROM entry \nWHERE entry.id IN (?, ?, .... when whooshee_search is invoked
#
# NOTE: This is caused by sqlite db paramater SQLITE_LIMIT_VARIABLE_NUMBER being set to 999 by default
for batch_size in [2, 5, 7, 20, 50, 300, 500]: # , 1000]:
expected_count += batch_size
self.entry_list = [
self.Entry(title=u'foobar_{0}_{1}'.format(expected_count, x),
content=u'xxxx', user=self.u1)
for x in range(batch_size)
]
self.db.session.add_all(self.entry_list)
self.db.session.commit()
found = self.Entry.query.whooshee_search('foobar', order_by_relevance=0).all()
assert len(found) == expected_count
def test_order_by_relevance(self):
entries_to_add = []
for x in range(1, len(string.ascii_lowercase)+1):
content = u' '.join([string.ascii_lowercase[i]*3 for i in range(x)])
entries_to_add.append(self.Entry(title=u'{0}'.format(x), content=content, user=self.u1))
self.db.session.add_all(entries_to_add)
self.db.session.commit()
search_string = u' '.join([string.ascii_lowercase[i]*3 for i in range(26)])
# no sorting (this assumes (hopes) rows won't be returned in the correct order by default)
found_entries = self.Entry.query.whooshee_search(search_string, order_by_relevance=0).all()
titles = [int(entry.title) for entry in found_entries]
self.assertNotEqual(titles, sorted(titles, reverse=True))
# sort all
found_entries = self.Entry.query.whooshee_search(search_string, order_by_relevance=-1).all()
titles = [int(entry.title) for entry in found_entries]
self.assertEqual(titles, sorted(titles, reverse=True))
# sort some (this assumes (hopes) the rest of the rows won't be returned in the correct order by default)
found_entries = self.Entry.query.whooshee_search(search_string, order_by_relevance=20).all()
titles = [int(entry.title) for entry in found_entries]
self.assertNotEqual(titles, sorted(titles, reverse=True))
# sort all (by setting order_by_relevance to the number of returned search results)
found_entries = self.Entry.query.whooshee_search(search_string, order_by_relevance=26).all()
titles = [int(entry.title) for entry in found_entries]
self.assertEqual(titles, sorted(titles, reverse=True))
# order_by after whooshee_search (note: order_by following whooshee_search has no impact for the first n results)
found_entries = self.Entry.query.whooshee_search(search_string, order_by_relevance=26).order_by(self.Entry.id).all()
titles = [int(entry.title) for entry in found_entries]
self.assertEqual(titles, sorted(titles, reverse=True))
# order_by before whooshee_search (note: order_by is a primary criterion here and search ordering is secondary)
found_entries = self.Entry.query.order_by(self.Entry.id).whooshee_search(search_string, order_by_relevance=26).all()
titles = [int(entry.title) for entry in found_entries]
self.assertEqual(titles, sorted(titles))
def test_whoosheer_search_option(self):
# alternative whoosheer
@self.wh.register_whoosheer
class EntryWhoosheer(AbstractWhoosheer):
schema = whoosh.fields.Schema(
entry_id = whoosh.fields.NUMERIC(stored=True, unique=True),
title = whoosh.fields.TEXT()
)
models = [self.Entry]
@classmethod
def update_entry(cls, writer, entry):
writer.update_document(entry_id=entry.id, title=entry.title+'cookie')
@classmethod
def insert_entry(cls, writer, entry):
writer.add_document(entry_id=entry.id, title=entry.title+'cookie')
entry = self.Entry(title=u'secret_', content=u'blah blah blah', user=self.u1)
self.db.session.add(entry)
self.db.session.commit()
found = self.Entry.query.join(self.User).whooshee_search('secret_cookie').all()
self.assertEqual(len(found), 0)
found = self.Entry.query.join(self.User).whooshee_search('secret_cookie', whoosheer=EntryWhoosheer).all()
self.assertEqual(len(found), 1)
def test_reindex(self):
self.db.session.add_all(self.all_inst)
self.db.session.commit()
# generall reindex
self.wh.reindex()
# put stallone directly in db and find him only after reindex
result = self.db.session.execute(text("INSERT INTO entry VALUES (100, 'rambo', 'pack of one two and three', {0})".format(self.u3.id)))
self.db.session.commit()
found = self.Entry.query.join(self.User).whooshee_search('rambo').all()
self.assertEqual(len(found), 0)
self.wh.reindex()
found = self.Entry.query.join(self.User).whooshee_search('rambo').all()
self.assertEqual(len(found), 1)
def test_add(self):
# test that the add operation works
found = self.Entry.query.whooshee_search('blah blah blah').all()
self.assertEqual(len(found), 0)
self.db.session.add(self.e1)
self.db.session.commit()
found = self.Entry.query.whooshee_search('blah blah blah').all()
self.assertEqual(len(found), 1)
def test_writer_releases_lock_on_exception(self):
found = self.Entry.query.whooshee_search('blah blah blah').all()
self.assertEqual(len(found), 0)
# simulate a onetime random exception raised when writing index
expectation = flexmock(self.Entry._whoosheer_).\
should_receive("insert_entry").\
and_raise(Exception).\
once()
with self.assertRaises(Exception):
self.db.session.add(self.e1)
self.db.session.commit()
self.db.session.rollback()
found = self.Entry.query.whooshee_search('blah blah blah').all()
self.assertEqual(len(found), 0)
# Compatibility with old flexmock for Python 2
if hasattr(expectation, "reset"):
expectation.reset()
else:
expectation._reset()
# now let's try writing without the exception to verify that lock was released
self.db.session.add(self.e1)
self.db.session.commit()
found = self.Entry.query.whooshee_search('blah blah blah').all()
self.assertEqual(len(found), 1)
# def test_update(self):
# # test that the update operation works
# self.db.session.add(self.e1)
# self.db.session.commit()
# self.db.session.remove()
#
# found = self.Entry.query.whooshee_search('blah blah blah').all()
# self.assertEqual(len(found), 1)
#
# # TODO there is an error here "InvalidRequestError: This session is in 'committed' state; no further SQL can be emitted within this transaction."
# self.e1.content = 'ramble ramble ramble'
# self.db.session.commit()
#
# found = self.Entry.query.whooshee_search('ramble ramble ramble').all()
# self.assertEqual(len(found), 1)
#
# found = self.Entry.query.whooshee_search('blah blah blah').all()
# self.assertEqual(len(found), 0)
def test_delete(self):
# test that the delete operation works
self.db.session.add(self.e1)
self.db.session.commit()
found = self.Entry.query.whooshee_search('blah blah blah').all()
self.assertEqual(len(found), 1)
self.db.session.delete(self.e1)
self.db.session.flush()
found = self.Entry.query.whooshee_search('blah blah blah').all()
self.assertEqual(len(found), 0)
# make sure that the entry has actually been deleted from the whoosh index
# https://github.com/fedora-copr/flask-whooshee/pull/26#issuecomment-257549715
whoosheer = next(w for w in self.wh.whoosheers if set(w.models) == set([self.Entry]))
self.assertEqual(len(whoosheer.search('blah blah blah')), 0)
def test_sqlalchemy_aliased(self):
# make sure that sqlalchemy aliased entities are recognized
self.db.session.add_all(self.all_inst)
self.db.session.commit()
alias = self.db.aliased(self.Entry)
self.assertEqual(len(self.User.query.join(alias).whooshee_search('chuck').all()), 3)
def test_unicode_search(self):
# we just need to make sure this doesn't fail (problem only on py-2)
self.Entry.query.whooshee_search('ěšč').all()
self.Entry.query.whooshee_search(u'ěšč').all()
def test_enable_indexing(self):
self.app.extensions['whooshee']['enable_indexing'] = False
self.db.session.add_all(self.all_inst)
self.db.session.commit()
# test joined search
found = self.Entry.query.join(self.User).whooshee_search('arnold').all()
self.assertEqual(found, [])
# test simple search
found = self.Entry.query.whooshee_search('arnold').all()
self.assertEqual(found, [])
# reenable and see if everything works now
self.app.extensions['whooshee']['enable_indexing'] = True
self.db.session.add(self.Entry(user=self.u1, title=u'newentry'))
self.db.session.commit()
found = self.Entry.query.whooshee_search('newentry').all()
self.assertEqual(len(found), 1)
def test_model_with_nonint_id(self):
self.db.session.add(self.n1)
self.db.session.commit()
found = self.ModelWithNonIntID.query.whooshee_search('threepwood').all()
self.assertEqual(len(found), 1)
found[0].attribute = 'LeChuck'
self.db.session.commit()
found = self.ModelWithNonIntID.query.whooshee_search('LeChuck').all()
self.assertEqual(len(found), 1)
self.n1.query.delete()
found = self.ModelWithNonIntID.query.whooshee_search('LeChuck').all()
self.assertEqual(len(found), 0)
class TestsWithApp(BaseTestCases.BaseTest):
def setUp(self):
self.wh = Whooshee(self.app)
super(TestsWithApp, self).setUp()
class TestsWithInitApp(BaseTestCases.BaseTest):
def setUp(self):
self.wh = Whooshee()
super(TestsWithInitApp, self).setUp()
# we intentionally call `init_app` after creating whoosheers, to test that lazy app
# intialization is possible
self.wh.init_app(self.app)
class TestsAppWithMemoryStorage(TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.config['WHOOSHEE_MEMORY_STORAGE'] = True
self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
self.app.config['TESTING'] = True
self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.db = SQLAlchemy(self.app)
self.wh = Whooshee(self.app)
self.ctx = self.app.app_context()
self.ctx.push()
class User(self.db.Model):
id = self.db.Column(self.db.Integer, primary_key=True)
name = self.db.Column(self.db.String)
# separate index for just entry
@self.wh.register_model('title', 'content')
class Entry(self.db.Model):
id = self.db.Column(self.db.Integer, primary_key=True)
title = self.db.Column(self.db.String)
content = self.db.Column(self.db.Text)
user = self.db.relationship(User, backref = self.db.backref('entries'))
user_id = self.db.Column(self.db.Integer, self.db.ForeignKey('user.id'))
# index for both entry and user
@self.wh.register_whoosheer
class EntryUserWhoosheer(AbstractWhoosheer):
schema = whoosh.fields.Schema(
entry_id = whoosh.fields.NUMERIC(stored=True, unique=True),
user_id = whoosh.fields.NUMERIC(stored=True),
username = whoosh.fields.TEXT(),
title = whoosh.fields.TEXT(),
content = whoosh.fields.TEXT())
models = [Entry, User]
@classmethod
def update_user(cls, writer, user):
pass # TODO: update all users entries
@classmethod
def update_entry(cls, writer, entry):
writer.update_document(entry_id=entry.id,
user_id=entry.user.id,
username=entry.user.name,
title=entry.title,
content=entry.content)
@classmethod
def insert_user(cls, writer, user):
# nothing, user doesn't have entries yet
pass
@classmethod
def insert_entry(cls, writer, entry):
writer.add_document(entry_id=entry.id,
user_id=entry.user.id,
username=entry.user.name,
title=entry.title,
content=entry.content)
@classmethod
def delete_user(cls, writer, user):
# nothing, user doesn't have entries yet
pass
@classmethod
def delete_entry(cls, writer, entry):
writer.delete_by_term('entry_id', entry.id)
self.User = User
self.Entry = Entry
self.EntryUserWhoosheer = EntryUserWhoosheer
self.db.create_all()
self.u1 = User(name=u'chuck')
self.u2 = User(name=u'arnold')
self.u3 = User(name=u'silvester')
self.e1 = Entry(title=u'chuck nr. 1 article', content=u'blah blah blah', user=self.u1)
self.e2 = Entry(title=u'norris nr. 2 article', content=u'spam spam spam', user=self.u1)
self.e3 = Entry(title=u'arnold blah', content=u'spam is cool', user=self.u2)
self.e4 = Entry(title=u'the less dangerous', content=u'chuck is better', user=self.u3)
self.all_inst = [self.u1, self.u2, self.u3, self.e1, self.e2, self.e3, self.e4]
self.db.session.add_all(self.all_inst)
self.db.session.commit()
def tearDown(self):
self.db.drop_all()
self.ctx.pop()
def test_memory_storage(self):
indexes = self.app.extensions['whooshee']['whoosheers_indexes']
self.assertTrue(isinstance(indexes[self.EntryUserWhoosheer].storage, RamStorage))
class TestBigInteger(TestCase):
# pylint: disable=too-many-instance-attributes
def setUp(self):
self.app = Flask(__name__)
self.app.config['WHOOSHEE_MEMORY_STORAGE'] = True
self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
self.app.config['TESTING'] = True
self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.db = SQLAlchemy(self.app)
self.wh = Whooshee(self.app)
self.ctx = self.app.app_context()
self.ctx.push()
class User(self.db.Model):
id = self.db.Column(self.db.Integer, primary_key=True)
name = self.db.Column(self.db.String)
# Need to make sure BigInteger PK's can be created
@self.wh.register_model('title', 'content')
class Entry(self.db.Model):
id = self.db.Column(self.db.BigInteger, primary_key=True)
title = self.db.Column(self.db.String)
content = self.db.Column(self.db.Text)
user = self.db.relationship(User, backref = self.db.backref('entries'))
user_id = self.db.Column(self.db.Integer, self.db.ForeignKey('user.id'))
self.User = User
self.Entry = Entry
self.db.create_all()
self.u1 = User(name=u'chuck')
self.e1 = Entry(id=1000000000000, title=u'chuck nr. 1 article', content=u'blah blah blah', user=self.u1)
self.db.session.commit()
def tearDown(self):
self.db.drop_all()
self.ctx.pop()
def test_add(self):
# test that the add operation works for Big Integer PK
found = self.Entry.query.whooshee_search('blah blah blah').all()
self.assertEqual(len(found), 0)
self.db.session.add(self.e1)
self.db.session.commit()
found = self.Entry.query.whooshee_search('blah blah blah').all()
self.assertEqual(len(found), 1)
class TestMultipleApps(TestCase):
def setUp(self):
self.db = SQLAlchemy()
self.wh = Whooshee()
for a in ['app1', 'app2']:
app = Flask(a)
app.config['WHOOSHEE_DIR'] = tempfile.mkdtemp()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
app.config['TESTING'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.db.init_app(app)
self.wh.init_app(app)
setattr(self, a, app)
class User(self.db.Model):
id = self.db.Column(self.db.Integer, primary_key=True)
name = self.db.Column(self.db.String)
# separate index for just entry
@self.wh.register_model('title', 'content')
class Entry(self.db.Model):
id = self.db.Column(self.db.Integer, primary_key=True)
title = self.db.Column(self.db.String)
content = self.db.Column(self.db.Text)
user = self.db.relationship(User, backref = self.db.backref('entries'))
user_id = self.db.Column(self.db.Integer, self.db.ForeignKey('user.id'))
# index for both entry and user
@self.wh.register_whoosheer
class EntryUserWhoosheer(AbstractWhoosheer):
schema = whoosh.fields.Schema(
entry_id = whoosh.fields.NUMERIC(stored=True, unique=True),
user_id = whoosh.fields.NUMERIC(stored=True),
username = whoosh.fields.TEXT(),
title = whoosh.fields.TEXT(),
content = whoosh.fields.TEXT())
models = [Entry, User]
@classmethod
def update_user(cls, writer, user):
pass # TODO: update all users entries
@classmethod
def update_entry(cls, writer, entry):
writer.update_document(entry_id=entry.id,
user_id=entry.user.id,
username=entry.user.name,
title=entry.title,
content=entry.content)
@classmethod
def insert_user(cls, writer, user):
# nothing, user doesn't have entries yet
pass
@classmethod
def insert_entry(cls, writer, entry):
writer.add_document(entry_id=entry.id,
user_id=entry.user.id,
username=entry.user.name,
title=entry.title,
content=entry.content)
@classmethod
def delete_user(cls, writer, user):
# nothing, user doesn't have entries yet
pass
@classmethod
def delete_entry(cls, writer, entry):
writer.delete_by_term('entry_id', entry.id)
self.User = User
self.Entry = Entry
self.EntryUserWhoosheer = EntryUserWhoosheer
with self.app1.app_context():
self.db.create_all()
with self.app2.app_context():
self.db.create_all()
self.u1 = User(name=u'chuck')
self.u2 = User(name=u'arnold')
self.u3 = User(name=u'silvester')
self.e1 = Entry(title=u'chuck nr. 1 article', content=u'blah blah blah', user=self.u1)
self.e2 = Entry(title=u'norris nr. 2 article', content=u'spam spam spam', user=self.u1)
self.e3 = Entry(title=u'arnold blah', content=u'spam is cool', user=self.u2)
self.e4 = Entry(title=u'the less dangerous', content=u'chuck is better', user=self.u3)
self.all_inst = [self.u1, self.u2, self.u3, self.e1, self.e2, self.e3, self.e4]
def tearDown(self):
shutil.rmtree(self.app1.config['WHOOSHEE_DIR'], ignore_errors=True)
shutil.rmtree(self.app2.config['WHOOSHEE_DIR'], ignore_errors=True)
with self.app1.app_context():
self.db.drop_all()
with self.app2.app_context():
self.db.drop_all()
def test_multiple_apps(self):
# IIUC, you can't add the same model instance under multiple apps with flask-sqlalchemy
# this pretty much reduces the testing to "make sure we used the right app to do
# the search"
with self.app1.test_request_context():
self.db.session.add_all([self.u2, self.u3, self.e3, self.e4])
self.db.session.commit()
with self.app2.test_request_context():
self.db.session.add_all([self.u1, self.e1, self.e2])
self.db.session.commit()
# make sure that entities stored only for app1 are only found for app1, same for app2
with self.app1.test_request_context():
q = self.Entry.query.whooshee_search('chuck')
self.assertEqual(len(q.all()), 1)
self.assertEqual(q[0].title, 'the less dangerous')
with self.app2.test_request_context():
q = self.Entry.query.whooshee_search('chuck')
self.assertEqual(len(q.all()), 1)
self.assertEqual(q[0].title, 'chuck nr. 1 article')
# try deleting everything from one app and then searching again
with self.app1.test_request_context():
self.Entry.query.delete()
q = self.Entry.query.whooshee_search('chuck')
self.assertEqual(len(q.all()), 0)
with self.app2.test_request_context():
q = self.Entry.query.whooshee_search('chuck')
self.assertEqual(len(q.all()), 1)
self.assertEqual(q[0].title, 'chuck nr. 1 article')
class TestDoesntMixesWithModelQueryClass(TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.config['WHOOSHEE_MEMORY_STORAGE'] = True
self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
self.app.config['TESTING'] = True
self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.db = SQLAlchemy(self.app)
self.wh = Whooshee(self.app)
def test_mixes_with_model_query(self):
class CustomQueryClass(Query):
pass
self._make_model_and_whoosheer(CustomQueryClass)
self.assertEqual('WhoosheeCustomQueryClass', self.user_model.query_class.__name__)
def test_doesnt_mix_with_default_query_class(self):
self._make_model_and_whoosheer(Query)
self.assertIs(self.wh.query, self.user_model.query_class)
def test_doesnt_mix_with_explicit_whooshee_query_class(self):
self._make_model_and_whoosheer(WhoosheeQuery)
self.assertIs(self.wh.query, self.user_model.query_class)
def test_doesnt_mix_with_whoosheequerywithapp(self):
self._make_model_and_whoosheer(self.wh.query)
self.assertIs(self.wh.query, self.user_model.query_class)
def test_doesnt_mix_with_SQLA_query_class(self):
self._make_model_and_whoosheer(SQLAQuery)
self.assertIs(self.wh.query, self.user_model.query_class)
def test_mixes_with_whooshee_query_subclass(self):
class CustomWhoosheeQuery(WhoosheeQuery):
pass
self._make_model_and_whoosheer(CustomWhoosheeQuery)
self.assertTrue(issubclass(self.user_model.query_class, CustomWhoosheeQuery))
self.assertTrue(issubclass(self.user_model.query_class, self.wh.query))
def test_overwrites_if_query_class_is_not_type(self):
self._make_model_and_whoosheer(5)
self.assertIs(self.wh.query, self.user_model.query_class)
def _make_model_and_whoosheer(self, query=None):
class User(self.db.Model):
query_class = query
id = self.db.Column(self.db.Integer, primary_key=True)
name = self.db.Column(self.db.String)
@self.wh.register_whoosheer
class UserWhoosheer(AbstractWhoosheer):
schema = whoosh.fields.Schema(
user_id = whoosh.fields.NUMERIC(stored=True),
username = whoosh.fields.TEXT())
models = [User]
@classmethod
def update_user(cls, writer, user):
pass # TODO: update all users entries
@classmethod
def update_entry(cls, writer, entry):
writer.update_document(user_id=entry.user.id,
username=entry.user.name)
@classmethod
def insert_user(cls, writer, user):
# nothing, user doesn't have entries yet
pass
@classmethod
def delete_user(cls, writer, user):
# nothing, user doesn't have entries yet
pass
self.user_model = User
self.user_whoosheer = UserWhoosheer