Changeset - e6224a7c3d4e
[Not reviewed]
default
0 2 1
FUJIWARA Katsunori - 9 years ago 2017-01-22 18:17:38
foozy@lares.dti.ne.jp
tests: introduce more test coverage of whoosh filtering

This has been extracted from other changesets by Mads Kiilerich to establish a
test baseline so we clearly can see what the following fixes are fixing.

Some of these tests will thus demonstrate bad behaviour - that will be fixed
later.
3 files changed with 262 insertions and 16 deletions:
0 comments (0 inline, 0 general)
kallithea/tests/fixtures/journal_dump.csv
Show inline comments
 
@@ -1940,96 +1940,98 @@ user_log_id,user_id,username,repository_
 
2003,3,demo,385,aaa-test-repo2-fork,"",user_commented_revision:96abb74e8e96a81a7dc5d472c70158da5318a623,2012-11-28 12:24:53.581825
 
2004,3,demo,385,aaa-test-repo2-fork,"",user_commented_revision:872eeeb3a8a2783129bfd7d988323f5addac7ab1,2012-11-28 12:25:16.961119
 
2005,3,demo,398,aaa-project-fork-fork,"",started_following_repo,2012-11-29 14:08:09.275983
 
2006,3,demo,68,aaa-project,"",user_forked_repo:aaa-project-fork-fork,2012-11-29 14:08:09.413733
 
2007,3,demo,398,aaa-project-fork-fork,"",user_created_fork:aaa-project-fork-fork,2012-11-29 14:08:09.427854
 
2008,3,demo,68,aaa-project,"",user_updated_repo,2012-11-29 14:09:10.934065
 
2009,3,demo,68,aaa-project,"",user_updated_repo,2012-11-29 14:09:24.706033
 
2010,3,demo,399,tra,"",started_following_repo,2012-11-29 14:11:48.839858
 
2011,3,demo,399,tra,"",user_created_repo,2012-11-29 14:11:48.85278
 
2012,3,demo,42,foo,"",user_commented_revision:1b9a14ca778d48ebef5643f7aa2ce142f3a27617,2012-11-29 20:05:08.767523
 
2013,3,demo,42,foo,"",push_local:bb4dab01c0bd4241b903a4dc84a79c70a01b0f6b,2012-11-29 20:06:18.477822
 
2014,1,default,159,group/Test-for-code-review,"",push_local:eaca071ebe45eaf5fc6631b15d7cf079a0d5aaf1,2012-11-29 23:52:50.718898
 
2015,375,paul,400,hophop,"",started_following_repo,2012-11-30 08:50:31.403305
 
2016,375,paul,400,hophop,"",user_created_repo,2012-11-30 08:50:31.416463
 
2017,375,paul,400,hophop,"",push_local:558613aaa69def5e130b8f9583de37f911a80dd8,2012-11-30 08:51:37.956737
 
2018,375,paul,401,flepflep,"",started_following_repo,2012-11-30 08:52:56.146141
 
2019,375,paul,400,hophop,"",user_forked_repo:flepflep,2012-11-30 08:52:56.162784
 
2020,375,paul,401,flepflep,"",user_created_fork:flepflep,2012-11-30 08:52:56.171068
 
2021,375,paul,400,hophop,213.154.235.68,push:0c64f45ec8f43a5267d593a2af65c3b9965c312c,2012-11-30 08:54:23.238053
 
2022,376,mrntest,402,mrntest,"",started_following_repo,2012-11-30 10:10:14.463671
 
2023,376,mrntest,402,mrntest,"",user_created_repo,2012-11-30 10:10:14.475507
 
2024,1,default,159,group/Test-for-code-review,"",push_local:da1270d35961130d6fe12ba4777f412dabb533f7,2012-11-30 18:39:23.68212
 
2025,3,demo,177,blah,"",user_updated_repo,2012-11-30 21:43:04.925873
 
2026,1,default,159,group/Test-for-code-review,"",push_local:3bfcf2015dcc85f3334a4439099b1f4fe2ab87cc,2012-12-01 04:40:25.326381
 
2027,3,demo,385,aaa-test-repo2-fork,"",push_local:8caf16b59f0ac54d002daeaf54208319a055829a,2012-12-01 12:40:27.987379
 
2028,3,demo,385,aaa-test-repo2-fork,"",user_commented_revision:8caf16b59f0ac54d002daeaf54208319a055829a,2012-12-01 12:40:35.315142
 
2029,3,demo,403,aaa-test-repo2-fork1,"",started_following_repo,2012-12-01 12:43:30.698526
 
2030,3,demo,384,aaa-test-repo2,"",user_forked_repo:aaa-test-repo2-fork1,2012-12-01 12:43:30.7817
 
2031,3,demo,403,aaa-test-repo2-fork1,"",user_created_fork:aaa-test-repo2-fork1,2012-12-01 12:43:30.789849
 
2032,3,demo,403,aaa-test-repo2-fork1,"",push_local:7d46cab71f090b82e40f82aa164a237bffae8ca8,2012-12-01 12:43:54.847211
 
2033,3,demo,384,aaa-test-repo2,"",user_commented_pull_request:81,2012-12-01 12:45:15.004235
 
2034,3,demo,384,aaa-test-repo2,"",user_closed_pull_request:81,2012-12-01 12:45:15.01175
 
2035,1,default,159,group/Test-for-code-review,"",push_local:88f2b300127f7249d6b2ac03d4e368cd7a043073,2012-12-02 06:49:39.201427
 
2036,380,han,42,foo,"",user_commented_revision:ca953e8c5c1ada44e9a8f945a0a6999d2f44310d,2012-12-03 05:54:40.845102
 
2037,380,han,42,foo,"",user_commented_revision:ca953e8c5c1ada44e9a8f945a0a6999d2f44310d,2012-12-03 05:54:52.733614
 
2038,380,han,42,foo,"",user_commented_revision:ca953e8c5c1ada44e9a8f945a0a6999d2f44310d,2012-12-03 05:55:06.032348
 
2039,380,han,184,fork-aaa-project,"",user_commented_revision:e3fef0be8dc20eb1fa68fc45292d9d66de1cc67f,2012-12-03 05:55:57.293424
 
2040,380,han,184,fork-aaa-project,"",user_commented_revision:e3fef0be8dc20eb1fa68fc45292d9d66de1cc67f,2012-12-03 05:56:05.503132
 
2041,380,han,184,fork-aaa-project,"",user_commented_revision:e3fef0be8dc20eb1fa68fc45292d9d66de1cc67f,2012-12-03 05:56:11.78452
 
2042,381,tmilos,265,mercurial-trunk,"",user_commented_revision:b3f0f9a39c4e1d0250048cd803ab03542d6f140a,2012-12-03 08:18:24.126434
 
2043,381,tmilos,265,mercurial-trunk,"",user_commented_revision:b3f0f9a39c4e1d0250048cd803ab03542d6f140a,2012-12-03 08:18:53.783535
 
2044,3,demo,68,aaa-project,"",user_commented_revision:0c33fa58efc5a5541d8d3f1a3d3b77367e3d94f5,2012-12-03 13:51:26.584571
 
2045,3,demo,384,aaa-test-repo2,"",user_updated_repo,2012-12-03 13:59:51.134475
 
2046,3,demo,68,aaa-project,"",user_updated_repo,2012-12-03 14:12:42.587904
 
2047,3,demo,404,Test-Repo-5,"",started_following_repo,2012-12-03 19:58:28.17924
 
2048,3,demo,404,Test-Repo-5,"",admin_created_repo,2012-12-03 19:58:28.215537
 
2049,3,demo,382,aaa-repo-fork,"",push_local:d49133c59d2b74bf5754fab639520fe0f15f660b,2012-12-03 23:45:38.954154
 
2050,5,natosha.bard,405,test-largefiles-push,"",started_following_repo,2012-12-04 14:15:02.062567
 
2051,5,natosha.bard,405,test-largefiles-push,"",admin_created_repo,2012-12-04 14:15:02.07345
 
2052,5,natosha.bard,405,test-largefiles-push,62.116.219.97,push:3055a4c8ccb9b695a7973aa25aa118b0dde122e4,2012-12-04 14:15:19.101956
 
2053,5,natosha.bard,405,test-largefiles-push,62.116.219.97,push:88adc5944c74488164ae575511429ed582a72e17,2012-12-04 14:40:10.373812
 
2054,384,test1234,406,test123456,"",started_following_repo,2012-12-04 16:07:07.967514
 
2055,384,test1234,406,test123456,"",admin_created_repo,2012-12-04 16:07:07.987393
 
2058,5,natosha.bard,408,test-repo-natosha,"",started_following_repo,2012-12-04 20:41:39.763055
 
2059,5,natosha.bard,408,test-repo-natosha,"",admin_created_repo,2012-12-04 20:41:39.775062
 
2056,5,natosha.bard,,hg,"",started_following_repo,2012-12-04 20:40:00.327829
 
2057,5,natosha.bard,,hg,"",admin_created_repo,2012-12-04 20:40:00.342796
 
2060,5,natosha.bard,,hg,"",user_deleted_repo,2012-12-04 20:42:03.236535
 
2062,5,natosha.bard,409,test-repo-natosha-fork,"",started_following_repo,2012-12-04 20:43:25.363199
 
2063,5,natosha.bard,408,test-repo-natosha,"",user_forked_repo:test-repo-natosha-fork,2012-12-04 20:43:25.413303
 
2064,5,natosha.bard,409,test-repo-natosha-fork,"",user_created_fork:test-repo-natosha-fork,2012-12-04 20:43:25.421503
 
2065,5,natosha.bard,409,test-repo-natosha-fork,62.116.219.97,"push:132a288f32d5b70a9170e504028c693cfffc5c39,fac6d36d04cb1838cd10bdcecd487567787fb853,e83c482c6c5fd60f14d1a5f5cf9b4f7b2a2eb3cb,ce2c709a8e908747376a3fda6059c1f81de321e7,45bd0cd7ca04f8cda0a61cddded8838dd312675a,35ba170c0f82dba18f0207ef4bd93216e6de8bbf,407209261f6373b4b3f35e343d1498b63263d38e,6f79c32c0bdfb304b984b5e6ba318cc63032cfa4,54cedee86e5126188b0dcfbd7015bcdca7f6c2e2,b0affcb67cba58db03ca3e443105fbaabd1bc88a,4c29668ca316422891e28c55bd794eee52823554,6180dcb29ec531c5d7457005ef142ea304ef49d8,2c63896783e31908f18088a7e9c583de6ec530bf,b3ec0b5fd7771b401392a83f47e0c2360e7f6d90,e689b0d9154605b8b04989c4fdd36e76d6cf26b8,b74361cf7c0ad6930a865e7eada13e2d2d6ed380,f94ead93406764ffd0c3febfc2ea70d36a2a28bb,848345a8d6adaa58704baecbb7567b275df3cc9d,0c10cf8191469e7c3c8844922e17e71a176cb7cb",2012-12-04 20:44:13.658236
 
2066,1,default,159,group/Test-for-code-review,"",push_local:b77f1d54f1d18c22e497f1d9d502e0d540506d21,2012-12-05 00:22:33.617864
 
2067,3,demo,410,aaa-project-fork-fork-fork-fork-fork-blah,"",started_following_repo,2012-12-05 10:07:05.333082
 
2068,3,demo,398,aaa-project-fork-fork,"",user_forked_repo:aaa-project-fork-fork-fork-fork-fork-blah,2012-12-05 10:07:05.47808
 
2069,3,demo,410,aaa-project-fork-fork-fork-fork-fork-blah,"",user_created_fork:aaa-project-fork-fork-fork-fork-fork-blah,2012-12-05 10:07:05.487482
 
2070,387,ssssss,411,sss-test,"",started_following_repo,2012-12-05 13:33:27.008097
 
2071,387,ssssss,411,sss-test,"",admin_created_repo,2012-12-05 13:33:27.018183
 
2072,387,ssssss,411,sss-test,"",user_updated_repo,2012-12-05 13:37:35.771478
 
2073,387,ssssss,411,sss-test,"",user_updated_repo,2012-12-05 13:38:00.979078
 
2074,387,ssssss,411,sss-test,"",user_updated_repo,2012-12-05 13:38:05.497667
 
2075,387,ssssss,411,sss-test,"",user_updated_repo,2012-12-05 13:38:12.513828
 
2076,387,ssssss,173,code-review-dev,"",started_following_repo,2012-12-05 13:46:51.940453
 
2077,387,ssssss,173,code-review-dev,"",stopped_following_repo,2012-12-05 13:46:54.738489
 
2078,387,ssssss,411,sss-test,109.195.177.171,push:7d42587da267871709bc82e462018b8b7d66a4a7,2012-12-05 13:59:09.546916
 
2079,387,ssssss,411,sss-test,109.195.177.171,push:bb869421c7c8f88590536d559cebd3ee5e9c3aa7,2012-12-05 14:00:27.04582
 
2080,387,ssssss,411,sss-test,"",user_updated_repo,2012-12-05 14:03:48.47955
 
2081,389,wwwwww,412,sss-test-fork,"",started_following_repo,2012-12-05 14:05:13.355226
 
2082,389,wwwwww,411,sss-test,"",user_forked_repo:sss-test-fork,2012-12-05 14:05:13.663527
 
2083,389,wwwwww,412,sss-test-fork,"",user_created_fork:sss-test-fork,2012-12-05 14:05:13.678367
 
2084,389,wwwwww,412,sss-test-fork,"",user_updated_repo,2012-12-05 14:05:56.566769
 
2085,389,wwwwww,412,sss-test-fork,109.195.177.171,push:20bd46e65a056aef107cc060d16c97b1ef2cc211,2012-12-05 14:07:21.399133
 
2086,389,wwwwww,412,sss-test-fork,"",user_commented_revision:20bd46e65a056aef107cc060d16c97b1ef2cc211,2012-12-05 14:16:56.716099
 
2087,387,ssssss,411,sss-test,"",user_updated_repo,2012-12-05 14:20:41.385001
 
2088,389,wwwwww,411,sss-test,109.195.177.171,push:20bd46e65a056aef107cc060d16c97b1ef2cc211,2012-12-05 14:20:54.878385
 
2089,387,ssssss,411,sss-test,"",user_updated_repo,2012-12-05 14:21:15.330812
 
2090,389,wwwwww,412,sss-test-fork,109.195.177.171,push:1203172be2e12aa31ec40ba4d07b75a3e17bd63f,2012-12-05 14:23:18.297453
 
2091,389,wwwwww,412,sss-test-fork,"",user_commented_revision:1203172be2e12aa31ec40ba4d07b75a3e17bd63f,2012-12-05 14:23:34.233486
 
2092,5,natosha.bard,413,natosha-repo1,"",started_following_repo,2012-12-05 16:46:55.328513
 
2093,5,natosha.bard,413,natosha-repo1,"",admin_created_repo,2012-12-05 16:46:55.342146
 
2094,5,natosha.bard,413,natosha-repo1,62.116.219.97,"push:6fbe2a2357168359aacbc524f4acd805dff72327,5787c391ba8f9d67b4e96f47d71adab79b693a30",2012-12-05 16:47:09.181828
 
2095,5,natosha.bard,414,natosha-repo1-fork,"",started_following_repo,2012-12-05 16:47:24.080447
 
2096,5,natosha.bard,413,natosha-repo1,"",user_forked_repo:natosha-repo1-fork,2012-12-05 16:47:24.101525
 
2097,5,natosha.bard,414,natosha-repo1-fork,"",user_created_fork:natosha-repo1-fork,2012-12-05 16:47:24.113866
 
2098,5,natosha.bard,414,natosha-repo1-fork,62.116.219.97,"push:a25c825fd81d069596d614efcf92505aed46227a,ac513595518923aca8b39f0a1c33c4c6e0f9d83a,d395e22e8e16373a1fffbe66b322581d69a7db17",2012-12-05 16:47:44.701535
 
2099,2,admin,38,code-review-test,"",user_commented_pull_request:73,2012-12-05 17:23:18.059481
 
2100,390,this-is-it,415,this,"",user_created_repo,2017-01-22 00:00:00.000000
 
2101,390,this-is-it,416,this/is-it,"",user_created_repo,2017-01-22 00:00:01.000000
kallithea/tests/functional/test_admin.py
Show inline comments
 
import os
 
import csv
 
import datetime
 
from kallithea.tests.base import *
 
from kallithea.model.db import UserLog
 
from kallithea.model.meta import Session
 
from kallithea.lib.utils2 import safe_unicode
 
from os.path import dirname
 

	
 
FIXTURES = os.path.join(dirname(dirname(os.path.abspath(__file__))), 'fixtures')
 

	
 

	
 
class TestAdminController(TestController):
 

	
 
    @classmethod
 
    def setup_class(cls):
 
        UserLog.query().delete()
 
        Session().commit()
 

	
 
        def strptime(val):
 
            fmt = '%Y-%m-%d %H:%M:%S'
 
            if '.' not in val:
 
                return datetime.datetime.strptime(val, fmt)
 

	
 
            nofrag, frag = val.split(".")
 
            date = datetime.datetime.strptime(nofrag, fmt)
 

	
 
            frag = frag[:6]  # truncate to microseconds
 
            frag += (6 - len(frag)) * '0'  # add 0s
 
            return date.replace(microsecond=int(frag))
 

	
 
        with open(os.path.join(FIXTURES, 'journal_dump.csv')) as f:
 
            for row in csv.DictReader(f):
 
                ul = UserLog()
 
                for k, v in row.iteritems():
 
                    v = safe_unicode(v)
 
                    if k == 'action_date':
 
                        v = strptime(v)
 
                    if k in ['user_id', 'repository_id']:
 
                        # nullable due to FK problems
 
                        v = None
 
                    setattr(ul, k, v)
 
                Session().add(ul)
 
            Session().commit()
 

	
 
    @classmethod
 
    def teardown_class(cls):
 
        UserLog.query().delete()
 
        Session().commit()
 

	
 
    def test_index(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index'))
 
        response.mustcontain('Admin Journal')
 

	
 
    def test_filter_all_entries(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',))
 
        response.mustcontain('2034 Entries')
 
        response.mustcontain(' 2036 Entries')
 

	
 
    def test_filter_journal_filter_exact_match_on_repository(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='repository:xxx'))
 
        response.mustcontain('3 Entries')
 
        response.mustcontain(' 3 Entries')
 

	
 
    def test_filter_journal_filter_exact_match_on_repository_CamelCase(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='repository:XxX'))
 
        response.mustcontain('3 Entries')
 
        response.mustcontain(' 3 Entries')
 

	
 
    def test_filter_journal_filter_wildcard_on_repository(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='repository:*test*'))
 
        response.mustcontain('862 Entries')
 
        response.mustcontain(' 862 Entries')
 

	
 
    def test_filter_journal_filter_prefix_on_repository(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='repository:test*'))
 
        response.mustcontain('257 Entries')
 
        response.mustcontain(' 257 Entries')
 

	
 
    def test_filter_journal_filter_prefix_on_repository_CamelCase(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='repository:Test*'))
 
        response.mustcontain('257 Entries')
 
        response.mustcontain(' 257 Entries')
 

	
 
    def test_filter_journal_filter_prefix_on_repository_and_user(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='repository:test* AND username:demo'))
 
        response.mustcontain('130 Entries')
 
        response.mustcontain(' 130 Entries')
 

	
 
    def test_filter_journal_filter_prefix_on_repository_or_other_repo(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='repository:test* OR repository:xxx'))
 
        response.mustcontain('260 Entries')  # 257 + 3
 
        response.mustcontain(' 260 Entries')  # 257 + 3
 

	
 
    def test_filter_journal_filter_exact_match_on_username(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='username:demo'))
 
        response.mustcontain('1087 Entries')
 
        response.mustcontain(' 1087 Entries')
 

	
 
    def test_filter_journal_filter_exact_match_on_username_camelCase(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='username:DemO'))
 
        response.mustcontain('1087 Entries')
 
        response.mustcontain(' 1087 Entries')
 

	
 
    def test_filter_journal_filter_wildcard_on_username(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='username:*test*'))
 
        response.mustcontain('100 Entries')
 
        response.mustcontain(' 100 Entries')
 

	
 
    def test_filter_journal_filter_prefix_on_username(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='username:demo*'))
 
        response.mustcontain('1101 Entries')
 
        response.mustcontain(' 1101 Entries')
 

	
 
    def test_filter_journal_filter_prefix_on_user_or_other_user(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='username:demo OR username:volcan'))
 
        response.mustcontain('1095 Entries')  # 1087 + 8
 
        response.mustcontain(' 1095 Entries')  # 1087 + 8
 

	
 
    def test_filter_journal_filter_wildcard_on_action(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='action:*pull_request*'))
 
        response.mustcontain('187 Entries')
 
        response.mustcontain(' 187 Entries')
 

	
 
    def test_filter_journal_filter_on_date(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='date:20121010'))
 
        response.mustcontain('47 Entries')
 
        response.mustcontain(' 47 Entries')
 

	
 
    def test_filter_journal_filter_on_date_2(self):
 
        self.log_user()
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter='date:20121020'))
 
        response.mustcontain('17 Entries')
 
        response.mustcontain(' 17 Entries')
 

	
 
    @parametrize('filter,hit', [
 
        #### "repository:" filtering
 
        # "/" is used for grouping
 
        ('repository:group/test', 0),
 
        # "-" is often used for "-fork"
 
        ('repository:fork-test1', 0),
 
        # using "stop words"
 
        ('repository:this', 2036),
 
        ('repository:this/is-it', 2036),
 

	
 
        ## additional tests to quickly find out regression in the future
 
        ## (and check case-insensitive search, too)
 
        # non-ascii character "." and "-"
 
        ('repository:TESTIES1.2.3', 4),
 
        ('repository:test_git_repo', 2),
 
        # combination with wildcard "*"
 
        ('repository:GROUP/*', 182),
 
        ('repository:*/test', 7),
 
        ('repository:fork-*', 273),
 
        ('repository:*-Test1', 5),
 

	
 
        #### "username:" filtering
 
        # "-" is valid character
 
        ('username:peso-xxx', 0),
 
        # using "stop words"
 
        ('username:this-is-it', 2036),
 

	
 
        ## additional tests to quickly find out regression in the future
 
        ## (and check case-insensitive search, too)
 
        # non-ascii character "." and "-"
 
        ('username:ADMIN_xanroot', 6),
 
        ('username:robert.Zaremba', 3),
 
        # combination with wildcard "*"
 
        ('username:THIS-*', 2),
 
        ('username:*-IT', 2),
 
    ])
 
    def test_filter_journal_filter_tokenization(self, filter, hit):
 
        self.log_user()
 

	
 
        response = self.app.get(url(controller='admin/admin', action='index',
 
                                    filter=filter))
 
        if hit != 1:
 
            response.mustcontain(' %s Entries' % hit)
 
        else:
 
            response.mustcontain(' 1 Entry')
kallithea/tests/functional/test_search_indexing.py
Show inline comments
 
new file 100644
 
import mock
 

	
 
from kallithea import CONFIG
 
from kallithea.config.conf import INDEX_FILENAMES
 
from kallithea.model.meta import Session
 
from kallithea.model.repo import RepoModel
 
from kallithea.model.repo_group import RepoGroupModel
 
from kallithea.tests.base import *
 
from kallithea.tests.fixture import create_test_index, Fixture
 

	
 
fixture = Fixture()
 

	
 
def init_indexing_test(repo):
 
    prev = fixture.commit_change(repo.repo_name,
 
                                 filename='this_should_be_unique_filename.txt',
 
                                 content='this_should_be_unique_content\n',
 
                                 message='this_should_be_unique_commit_log',
 
                                 vcs_type='hg',
 
                                 newfile=True)
 

	
 
def init_stopword_test(repo):
 
    prev = fixture.commit_change(repo.repo_name,
 
                                 filename='this/is/it',
 
                                 content='def test\n',
 
                                 message='bother to ask where - in folder',
 
                                 vcs_type='hg',
 
                                 newfile=True)
 
    prev = fixture.commit_change(repo.repo_name,
 
                                 filename='join.us',
 
                                 content='def test\n',
 
                                 message='bother to ask where - top level',
 
                                 author='this is it <this-is-it@foo.bar.com>',
 
                                 vcs_type='hg',
 
                                 parent=prev,
 
                                 newfile=True)
 

	
 
repos = [
 
    # reponame,              init func or fork base, groupname
 
    (u'indexing_test',       init_indexing_test,     None),
 
    (u'indexing_test-fork',  u'indexing_test',       None),
 
    (u'group/indexing_test', u'indexing_test',       u'group'),
 
    (u'this-is-it',          u'indexing_test',       None),
 
    (u'indexing_test-foo',   u'indexing_test',       None),
 
    (u'indexing_test-FOO',   u'indexing_test',       None),
 
    (u'stopword_test',       init_stopword_test,     None),
 
]
 

	
 
# map: name => id
 
repoids = {}
 
groupids = {}
 

	
 
def rebuild_index(full_index):
 
    with mock.patch('kallithea.lib.indexers.daemon.log.debug',
 
                    lambda *args, **kwargs: None):
 
        # The more revisions managed repositories have, the more
 
        # memory capturing "log.debug()" output in "indexers.daemon"
 
        # requires. This may cause unintentional failure of subsequent
 
        # tests, if ENOMEM at forking "git" prevents from rebuilding
 
        # index for search.
 
        # Therefore, "log.debug()" is disabled regardless of logging
 
        # level while rebuilding index.
 
        # (FYI, ENOMEM occurs at forking "git" with python 2.7.3,
 
        # Linux 3.2.78-1 x86_64, 3GB memory, and no ulimit
 
        # configuration for memory)
 
        create_test_index(TESTS_TMP_PATH, CONFIG, full_index=full_index)
 

	
 

	
 
class TestSearchControllerIndexing(TestController):
 
    @classmethod
 
    def setup_class(cls):
 
        for reponame, init_or_fork, groupname in repos:
 
            if groupname and groupname not in groupids:
 
                group = fixture.create_repo_group(groupname)
 
                groupids[groupname] = group.group_id
 
            if callable(init_or_fork):
 
                repo = fixture.create_repo(reponame,
 
                                           repo_group=groupname)
 
                init_or_fork(repo)
 
            else:
 
                repo = fixture.create_fork(init_or_fork, reponame,
 
                                           repo_group=groupname)
 
            repoids[reponame] = repo.repo_id
 

	
 
        # treat "it" as indexable filename
 
        filenames_mock = list(INDEX_FILENAMES)
 
        filenames_mock.append('it')
 
        with mock.patch('kallithea.lib.indexers.daemon.INDEX_FILENAMES',
 
                        filenames_mock):
 
            rebuild_index(full_index=False) # only for newly added repos
 

	
 
    @classmethod
 
    def teardown_class(cls):
 
        # delete in reversed order, to delete fork destination at first
 
        for reponame, init_or_fork, groupname in reversed(repos):
 
            RepoModel().delete(repoids[reponame])
 

	
 
        for reponame, init_or_fork, groupname in reversed(repos):
 
            if groupname in groupids:
 
                RepoGroupModel().delete(groupids.pop(groupname),
 
                                        force_delete=True)
 

	
 
        Session().commit()
 
        Session.remove()
 

	
 
        rebuild_index(full_index=True) # rebuild fully for subsequent tests
 

	
 
    @parametrize('reponame', [
 
        (u'indexing_test'),
 
        (u'indexing_test-fork'),
 
        (u'group/indexing_test'),
 
        (u'this-is-it'),
 
        (u'*-fork'),
 
        (u'group/*'),
 
    ])
 
    @parametrize('searchtype,query,hit', [
 
        #('content', 'this_should_be_unique_content', 1),
 
        ('commit', 'this_should_be_unique_commit_log', 1),
 
        #('path', 'this_should_be_unique_filename.txt', 1),
 
    ])
 
    def test_repository_tokenization(self, reponame, searchtype, query, hit):
 
        self.log_user()
 

	
 
        q = 'repository:%s %s' % (reponame, query)
 
        response = self.app.get(url(controller='search', action='index'),
 
                                {'q': q, 'type': searchtype})
 
        response.mustcontain('>%d results' % hit)
 

	
 
    @parametrize('searchtype,query,hit', [
 
        ('content', 'this_should_be_unique_content', 2),
 
        ('commit', 'this_should_be_unique_commit_log', 1),
 
        ('path', 'this_should_be_unique_filename.txt', 2),
 
    ])
 
    def test_repository_case_sensitivity(self, searchtype, query, hit):
 
        self.log_user()
 

	
 
        lname = u'indexing_test-foo'
 
        uname = u'indexing_test-FOO'
 

	
 
        # (1) "repository:REPONAME" condition should match against
 
        # repositories case-insensitively
 
        q = 'repository:%s %s' % (lname, query)
 
        response = self.app.get(url(controller='search', action='index'),
 
                                {'q': q, 'type': searchtype})
 

	
 
        response.mustcontain('>%d results' % hit)
 

	
 
        # (2) on the other hand, searching under the specific
 
        # repository should return results only for that repository,
 
        # even if specified name matches against another repository
 
        # case-insensitively.
 
        response = self.app.get(url(controller='search', action='index',
 
                                    repo_name=uname),
 
                                {'q': query, 'type': searchtype})
 

	
 
        response.mustcontain('>%d results' % hit)
 

	
 
        # confirm that there is no matching against lower name repository
 
        assert uname in response
 
        #assert lname not in response
 

	
 
    @parametrize('searchtype,query,hit', [
 
        ('content', 'path:this/is/it def test', 37),
 
        ('commit', 'added:this/is/it bother to ask where', 4),
 
        # this condition matches against files below, because
 
        # "path:" condition is also applied on "repository path".
 
        # - "this/is/it" in "stopword_test" repo
 
        # - "this_should_be_unique_filename.txt" in "this-is-it" repo
 
        ('path', 'this/is/it', 0),
 

	
 
        ('content', 'extension:us', 0),
 
        ('path', 'extension:us', 0),
 
    ])
 
    def test_filename_stopword(self, searchtype, query, hit):
 
        response = self.app.get(url(controller='search', action='index'),
 
                                {'q': query, 'type': searchtype})
 

	
 
        response.mustcontain('>%d results' % hit)
 

	
 
    @parametrize('searchtype,query,hit', [
 
        # matching against both 2 files
 
        ('content', 'owner:"this is it"', 0),
 
        ('content', 'owner:this-is-it', 0),
 
        ('path', 'owner:"this is it"', 0),
 
        ('path', 'owner:this-is-it', 0),
 

	
 
        # matching against both 2 revisions
 
        ('commit', 'owner:"this is it"', 0),
 
        ('commit', 'owner:"this-is-it"', 0),
 

	
 
        # matching against only 1 revision
 
        ('commit', 'author:"this is it"', 0),
 
        ('commit', 'author:"this-is-it"', 0),
 
    ])
 
    def test_mailaddr_stopword(self, searchtype, query, hit):
 
        response = self.app.get(url(controller='search', action='index'),
 
                                {'q': query, 'type': searchtype})
 

	
 
        response.mustcontain('>%d results' % hit)
0 comments (0 inline, 0 general)