def test_date_ranges_are_searchable(self): # Given fname = join(self.root, 'root.txt') dt = datetime.datetime(2015, 1, 1) ts = time.mktime(dt.timetuple()) os.utime(fname, (ts, ts)) p = Project(name='test', path=self.root) p.scan() # When result = list(p.search("mtime:2015")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'root.txt') # When fname = join(self.root, 'hello.py') dt = datetime.datetime(2015, 2, 1) ts = time.mktime(dt.timetuple()) os.utime(fname, (ts, ts)) p.refresh() result = list(p.search("mtime:2015")) # Then self.assertEqual(len(result), 2) names = sorted(x[0] for x in result) self.assertEqual(names, ['hello.py', 'root.txt']) # When result = list(p.search("mtime:201501")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'root.txt') # When result = list(p.search("mtime:[jan 2015 TO feb 2015]")) # Then self.assertEqual(len(result), 2) names = sorted(x[0] for x in result) self.assertEqual(names, ['hello.py', 'root.txt']) # When result = list(p.search("mtime:>20150202")) # Then self.assertEqual(len(result), 3) names = sorted(x[0] for x in result) self.assertNotIn('hello.py', names) self.assertNotIn('root.txt', names)
def test_simple_search_works(self): # Given p = Project(name='test', path=self.root) p.scan() # When result = list(p.search("hello")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], "hello.py") self.assertEqual(result[0][1], "hello.py")
def test_logical_operations_in_search(self): # Given p = Project(name='test', path=self.root) p.scan() # When result = list(p.search("hello subsub")) # Then self.assertEqual(len(result), 0) # When result = list(p.search("hello AND subsub")) # Then self.assertEqual(len(result), 0) # When result = list(p.search("hello OR subsub")) # Then self.assertEqual(len(result), 2) names = sorted(x[0] for x in result) self.assertEqual(names, ["hello.py", "subsub.txt"]) # When result = list(p.search("hello AND NOT .py")) # Then self.assertEqual(len(result), 0) # When result = list(p.search("NOT .txt")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], "hello.py")
def test_phrases_are_searchable(self): # Given tags = [ TagInfo(name='comment', type='string'), ] p = Project(name='test', path=self.root, tags=tags) p.scan() p.get('root.txt').tags['comment'] = 'Hola how are you?' # When result = list(p.search('comment:"hola how"')) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'root.txt')
def test_tags_in_search_work_correctly(self): # Given p = Project(name='test', path=self.root) p.scan() # When result = list(p.search("path:hello.py")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], "hello.py") # When result = list(p.search("file_name:test")) # Then self.assertEqual(len(result), 0) # When result = list(p.search("path:test")) # Then # Should get everything since everything is inside the # test directory! self.assertEqual(len(result), 5) # When p.get('root.txt').tags['completed'] = True result = list(p.search("completed:1")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], "root.txt") # When result = list(p.search("completed:yes")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], "root.txt") # When result = list(p.search("completed:0")) # Then self.assertEqual(len(result), 4) self.assertNotIn('root.txt', [x[0] for x in result])
def test_numeric_tags_and_ranges_are_searchable(self): # Given tags = [ TagInfo(name='fox', type='int'), TagInfo(name='age', type='float') ] p = Project(name='test', path=self.root, tags=tags) p.scan() p.get(join('sub2', 'sub2.txt')).tags['fox'] = 1 # When result = list(p.search("fox:1")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'sub2.txt') # When # This is an exclusive range, i.e. only the value 1 is searched. result = list(p.search("fox:{0 TO 2}")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'sub2.txt') # When # Here we have 1 and 2. result = list(p.search("fox:{0 TO 2]")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'sub2.txt') # When result = list(p.search("fox:>=1")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'sub2.txt') # When result = list(p.search("fox:<1")) # Then self.assertEqual(len(result), 4) self.assertNotIn('sub2.txt', [x[0] for x in result]) # When p.get('root.txt').tags['age'] = 50.5 result = list(p.search("age:50.5")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'root.txt') # When result = list(p.search("age:>50")) # Then self.assertEqual(len(result), 1) self.assertEqual(result[0][0], 'root.txt')