def test_update_tags_handles_type_changes_for_existing_tag(self): # Given tags = [ TagInfo(name='completed', type='bool'), TagInfo(name='foo', type='string') ] p = Project(name='test', path=self.root, tags=tags) p.scan() key = 'root.txt' m = p.get(key) m.tags['completed'] = True m.tags['foo'] = 'hello world' # When new_tags = [ TagInfo(name='foo', type='int'), TagInfo(name='completed', type='bool') ] p.update_tags(new_tags) # Then self.assertEqual(p.tags, new_tags) self.assertEqual(m.tags['completed'], True) self.assertEqual(m.tags['foo'], 0) for key in p.keys(): m = p.get(key) self.assertEqual(type(m.tags['completed']), bool) self.assertEqual(m.tags['foo'], 0)
def test_tagger_factory_tags_known_tags(self): # Given. code = 'import sys; print("args:"+sys.argv[1]'\ '+"\nlength:10\ncompleted:yes\n")' command = "python -c %r" % code factory = TaggerFactory(command=command) p = Project(name='test', path=self.root) p.add_tags([ TagInfo(name='args', type='string'), TagInfo(name='length', type='int') ]) p.scan() # When jobs = factory.make_jobs(p.keys(), p) for job in jobs: job.run() job.thread.join() # Then. self.assertEqual(len(jobs), 5) for job in jobs: self.assertEqual(job.status, 'success') for key in p.keys(): media = p.get(key) self.assertEqual(media.tags['completed'], True) expect = "%s" % (media.path) self.assertEqual(media.tags['args'], expect) self.assertEqual(media.tags['length'], 10) # When jobs = factory.make_jobs(p.keys(), p) # Then self.assertEqual(len(jobs), 0) # When. data = dump(factory) f1 = load(data) # Then. # The _tag_types should not be dumped. self.assertEqual(f1._tag_types, None) self.assertNotIn('_tag_types', data[1]) f = factory for attr in ['command', '_done']: self.assertEqual(getattr(f1, attr), getattr(f, attr))
def test_copy_project_works(self): # Setup # Create a new project, scan it, save it and re-load it for the test. ui = VixenUI() vixen = ui.vixen ui.add_project() p = vixen.projects[-1] p.add_tags([TagInfo(name='sometag', type='text')]) p.path = self.root p.scan() p.save() vixen.save() self.assertEqual(len(vixen.projects), 1) # Given ui = VixenUI() vixen = ui.vixen self.assertEqual(len(vixen.projects), 1) # When ui.copy_project(vixen.projects[0]) # Then self.assertEqual(len(vixen.projects), 2) p = vixen.projects[-1] self.assertEqual(p.name, 'Project1 copy') self.assertEqual(len(p.tags), 2) self.assertEqual(p.tags[0].name, 'completed') self.assertEqual(p.tags[0].type, 'bool') self.assertEqual(p.tags[1].name, 'sometag') self.assertEqual(p.tags[1].type, 'text')
def test_export_to_csv_with_unicode(self): # Given tags = [ TagInfo(name='completed', type='bool'), TagInfo(name='comment', type='string') ] p = Project(name='test', path=self.root, tags=tags) p.scan() m = p.get('root.txt') m.tags['completed'] = True m.tags['comment'] = u'hello, world; न Kévin' out_fname = tempfile.mktemp(dir=self.root, suffix='.csv') # When p.export_csv(out_fname) # Then with io.open(out_fname, newline='', encoding='utf-8') as fp: reader = csv.reader(fp) cols = next(reader) expected = [ 'comment', 'completed', 'ctime', 'file_name', 'mtime', 'path', 'relpath', 'size', 'type' ] self.assertEqual(cols, expected) expected = {'hello.py': 'False', 'root.txt': 'True'} data = [next(reader), next(reader), next(reader), next(reader)] data = sorted(data, key=lambda x: x[6]) row = data[0] self.assertEqual(basename(row[5]), u'hello.py') self.assertEqual(row[1], u'False') self.assertEqual(row[0], u'') row = data[1] self.assertEqual(basename(row[5]), u'root.txt') self.assertEqual(row[1], u'True') self.assertEqual(row[0], u'hello, world; न Kévin') row = data[2] self.assertTrue(basename(row[5]).startswith(u'sub')) self.assertEqual(row[1], u'False') self.assertEqual(row[0], u'') row = data[3] self.assertTrue(basename(row[5]).startswith(u'sub')) self.assertEqual(row[1], u'False') self.assertEqual(row[0], u'')
def test_project_copy_does_not_copy_data(self): # Given tags = [TagInfo(name='completed', type='bool'), TagInfo(name='comment', type='string')] p = Project(name='test', path=self.root, extensions=['.txt', '.py'], tags=tags) cf = CommandFactory(dest=self.root, input_extension='.py', output_extension='.rst', command='echo $input $output') p.processors = [cf] p.scan() # Update the _done trait the processors to check if it is copied. m = p.get('root.txt') cf._done[m.path] = True # When p1 = p.copy() # Then self.assertEqual(p.number_of_files, 5) self.assertEqual(p1.number_of_files, 0) self.assertEqual(p1.name, p.name + ' copy') self.assertEqual(p1.path, p.path) tag_info = [(x.name, x.type) for x in p.tags] tag_info1 = [(x.name, x.type) for x in p1.tags] self.assertEqual(tag_info, tag_info1) self.assertEqual(p1.extensions, p.extensions) self.assertEqual(len(p1._relpath2index), 0) self.assertEqual(len(p1.processors), len(p.processors)) p1_proc_traits = p1.processors[0].trait_get() p1_proc_traits.pop('_done') p_proc_traits = p.processors[0].trait_get() p_proc_traits.pop('_done') self.assertEqual(p1_proc_traits, p_proc_traits) self.assertEqual(len(p.processors[0]._done), 1) self.assertEqual(len(p1.processors[0]._done), 0) # When p.tags[0].type = 'int' # Then # This just checks that p1's tags are not a reference to p's. self.assertEqual(p1.tags[0].type, 'bool')
def test_query_schema_is_updated_when_tags_are_added(self): # Given p = Project(name='test', path=self.root) p.scan() # When p.add_tags([TagInfo(name='new_tag', type='int')]) # Then schema = p._query_parser.schema items = schema.items() self.assertIn(('new_tag', INT), items) # When p.add_tags([TagInfo(name='tag1', type='text')]) # Then schema = p._query_parser.schema items = schema.items() self.assertIn(('tag1', TEXT()), items)
def test_update_tags_works_without_scan(self): # Given p = Project(name='test', path=self.root) # When new_tags = [TagInfo(name='foo', type='string')] p.update_tags(new_tags) # Then self.assertEqual(p.tags, new_tags) self.assertEqual(sorted(x.name for x in new_tags), sorted(p._tag_data.keys()))
def test_tagger_factory_tags_known_tags(self): # Given. code = 'import sys; print("args:"+sys.argv[1]'\ '+"\nlength:10\ncompleted:yes\n")' command = "python -c %r" % code factory = TaggerFactory(command=command) p = Project(name='test', path=self.root) p.add_tags( [ TagInfo(name='args', type='string'), TagInfo(name='length', type='int') ] ) p.scan() # When jobs = factory.make_jobs(p.keys(), p) for job in jobs: job.run() job.thread.join() # Then. self.assertEqual(len(jobs), 5) for job in jobs: self.assertEqual(job.status, 'success') for key in p.keys(): media = p.get(key) self.assertEqual(media.tags['completed'], True) expect = "%s" % (media.path) self.assertEqual(media.tags['args'], expect) self.assertEqual(media.tags['length'], 10) # When jobs = factory.make_jobs(p.keys(), p) # Then self.assertEqual(len(jobs), 0)
def test_update_tags_updates_existing_media(self): # Given p = Project(name='test', path=self.root) p.scan() # When new_tags = [TagInfo(name='foo', type='string')] p.update_tags(new_tags) # Then self.assertEqual(p.tags, new_tags) for key in p.keys(): m = p.get(key) self.assertEqual(m.tags['foo'], '') self.assertTrue('completed' not in m.tags)
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_python_function_factory(self): # Given. from textwrap import dedent code = dedent(""" def process(relpath, media, dest): media.tags['completed'] = True media.tags['args'] = "%s %s"%(relpath, dest) """) factory = PythonFunctionFactory(code=code, dest=self.root1) p = Project(name='test', path=self.root) p.add_tags([TagInfo(name='args', type='string')]) p.scan() # When jobs = factory.make_jobs(p.keys(), p) for job in jobs: job.run() job.thread.join() # Then. self.assertEqual(len(jobs), 5) for key in p.keys(): media = p.get(key) self.assertEqual(media.tags['completed'], True) expect = "%s %s" % (key, self.root1) self.assertEqual(media.tags['args'], expect) # When jobs = factory.make_jobs(p.keys(), p) # Then self.assertEqual(len(jobs), 0) # When. data = dump(factory) f1 = load(data) # Then. # The func should not be dumped. self.assertEqual(f1._func, None) self.assertNotIn('_func', data[1]) f = factory for attr in ['code', '_done']: self.assertEqual(getattr(f1, attr), getattr(f, attr))
def test_import_csv_works(self): # Given p = Project(name='test', path=self.root) p.add_tags([TagInfo(name='fox', type='int')]) p.scan() data = dedent(u"""\ path,fox,junk %s,2,hello %s,1,bye """ % (join(self.root, 'root.txt'), join(self.root, 'hello.py'))) csv = self._write_csv(data) # Get one of the paths to see if cached media are handled correctly. self.assertEqual(p.get('root.txt').tags['fox'], 0) # When success, err = p.import_csv(csv) # Then self.assertTrue(success) self.assertEqual(p.get('root.txt').tags['fox'], 2) self.assertEqual(p.get('hello.py').tags['fox'], 1)
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')