def test_command_factory_commands(self): # Given. cf = CommandFactory(dest=self.root1, input_extension='.py', output_extension='.rst', command="echo $input $output") p = Project(name='test', path=self.root) p.scan() # When jobs = cf.make_jobs(p.keys(), p) # Then. self.assertEqual(len(jobs), 1) job = jobs[0] m = p.get('hello.py') dest = os.path.join(self.root1, 'hello.rst') expect = ('echo %s %s' % (m.path, dest)).replace('\\', '\\\\') self.assertEqual(job.args, [expect.split(), m.path, dest])
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_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_command_factory_jobs(self): # Given. import sys command = """\ %r -c 'import shutil;shutil.copy("$input", "$output")'\ """ % sys.executable cf = CommandFactory(dest=self.root1, input_extension='.py', output_extension='.rst', command=command, copy_timestamps=True) p = Project(name='test', path=self.root) p.scan() # When jobs = cf.make_jobs(p.keys(), p) job = jobs[0] job.run() job.thread.join() # Then. self.assertEqual(len(jobs), 1) self.assertEqual(job.status, 'success') m = p.get('hello.py') dest = os.path.join(self.root1, 'hello.rst') self.assertTrue(os.path.exists(dest)) self.assertEqual(cf._done[dest], True) s_stat = os.stat(m.path) d_stat = os.stat(dest) self.assertTrue(abs(s_stat.st_mtime - d_stat.st_mtime) < 2) self.assertTrue(abs(s_stat.st_ctime - d_stat.st_ctime) < 2) jobs = cf.make_jobs(p.keys(), p) self.assertEqual(len(jobs), 0) # When. data = dump(cf) cf1 = load(data) # Then. for attr in cf.__dict__.keys(): self.assertEqual(getattr(cf1, attr), getattr(cf, attr))
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() # 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)) self.assertEqual(p1.processors[0].trait_get(), p.processors[0].trait_get()) # 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_tagger_factory_skips_unknown_tags(self): # Given. code = 'import sys; print("\nlength:10\nxxx:yes\n")' command = "python -c %r" % code factory = TaggerFactory(command=command) p = Project(name='test', path=self.root) 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.assertTrue('length' not in media.tags) self.assertTrue('xxx' not in media.tags) self.assertEqual(media.tags['completed'], False)
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_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_refresh_updates_new_media(self): # Given p = Project(name='test', path=self.root) p.scan() self.assertEqual(p.number_of_files, 5) m = p.get('root.txt') orig_size = m.size # Change this. m.tags['completed'] = True create_dummy_file(join(self.root, 'sub', 'sub1.txt')) with open(m.path, 'w') as fp: fp.write('hello world\n') # When p.refresh() # Then m = p.get('root.txt') self.assertEqual(m.tags['completed'], True) self.assertEqual(p.number_of_files, 6) self.assertTrue(m.size > orig_size) m = p.get(join('sub', 'sub1.txt')) self.assertEqual(m.tags['completed'], False)
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_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')