def test_move_file(self): lfs.copy('tests/hello.txt', 'tests/hello.txt.bak') lfs.move('tests/hello.txt.bak', 'tests/hello.txt.old') file = lfs.open('tests/hello.txt.old') self.assertEqual(file.read(), 'hello world\n') self.assertEqual(lfs.exists('tests/hello.txt.bak'), False) lfs.remove('tests/hello.txt.old')
def tearDown(self): folder = lfs.open('tests') for name in 'toto.txt', 'fofo.txt', 'fofo2.txt', 'empty': if folder.exists(name): folder.remove(name) if lfs.exists("test_dir"): lfs.remove("test_dir")
def test_atime(self): lfs.make_file('tests/file') atime = lfs.get_atime('tests/file') self.assertEqual(atime.year, datetime.now().year) self.assertEqual(atime.month, datetime.now().month) self.assertEqual(atime.day, datetime.now().day) lfs.remove('tests/file')
def test_stop_server(self): server = SERVER server.stop() # Remove database path = DATABASE_TEST_PATH if lfs.exists(path): lfs.remove(path)
def test_remove_folder(self): # Create hierarchy lfs.make_folder('tests/folder') lfs.make_folder('tests/folder/a') lfs.make_file('tests/folder/a/hello.txt') # Remove and test lfs.remove('tests/folder') self.assertEqual(lfs.exists('tests/folder'), False)
def tearDown(self): # Restore logging register_logger(None, 'itools.database') # Clean file-system paths = [ 'fables/catalog', 'fables/database/.git', 'fables/database/31.txt', 'fables/database/agenda', 'fables/database/broken.txt' ] for path in paths: if lfs.exists(path): lfs.remove(path)
def test_simple_value(self): # Init data value = "HELLO, WORLD!" self._init_test(value) # Read data config2 = ConfigFile(self.config_path) config2_value = config2.get_value("test") lfs.remove(self.config_path) # Test data self.assertEqual(config2_value, value)
def test_simple_value(self): # Init data value = "HELLO, WORLD!" self._init_test(value) # Read data config2 = rw_database.get_handler(self.config_path, ConfigFile) config2_value = config2.get_value("test") lfs.remove(self.config_path) # Test data self.assertEqual(config2_value, value)
def tearDown(self): # Restore logging register_logger(None, "itools.database") # Clean file-system paths = [ "fables/catalog", "fables/database/.git", "fables/database/31.txt", "fables/database/agenda", "fables/database/broken.txt", ] for path in paths: if lfs.exists(path): lfs.remove(path)
def test_append(self): # Initialize file = lfs.make_file('tests/toto.txt') try: file.write('hello\n') finally: file.close() # Test file = lfs.open('tests/toto.txt', APPEND) try: file.write('bye\n') finally: file.close() self.assertEqual(open('tests/toto.txt').read(), 'hello\nbye\n') # Remove temporary file lfs.remove('tests/toto.txt')
def test_write_and_truncate(self): # Initialize file = lfs.make_file('tests/toto.txt') try: file.write('hello\n') finally: file.close() # Test file = lfs.open('tests/toto.txt', WRITE) try: file.write('bye\n') finally: file.close() self.assertEqual(open('tests/toto.txt').read(), 'bye\n') # Remove temporary file lfs.remove('tests/toto.txt')
def test_last_line_empty(self): # Init data value = "HELLO, WORLD!\n\n" self._init_test(value) # Write data config = rw_database.get_handler(self.config_path, ConfigFile) config.set_value("test", value) config.save_state() # Read data config2 = rw_database.get_handler(self.config_path, ConfigFile) config2_value = config2.get_value("test") lfs.remove(self.config_path) # Test data self.assertEqual(config2_value, value)
def test_last_line_empty(self): # Init data value = "HELLO, WORLD!\n\n" self._init_test(value) # Write data config = ConfigFile(self.config_path) config.set_value("test", value) config.save_state() # Read data config2 = ConfigFile(self.config_path) config2_value = config2.get_value("test") lfs.remove(self.config_path) # Test data self.assertEqual(config2_value, value)
def test_save(self): agenda = Agenda(string=agenda_file) agenda.save_state_to('tests/agenda') # Change agenda = Agenda('tests/agenda') fake = agenda.add_record({'firstname': u'Toto', 'lastname': u'Fofo'}) agenda.add_record({'firstname': u'Albert', 'lastname': u'Einstein'}) agenda.del_record(fake.id) agenda.save_state() # Test agenda = Agenda('tests/agenda') ids = [x.id for x in agenda.search('firstname', u'Toto')] self.assertEqual(len(ids), 0) ids = [x.id for x in agenda.search('firstname', u'Albert')] self.assertEqual(len(ids), 1) # Clean lfs.remove('tests/agenda')
def test_save(self): agenda = Agenda(string=agenda_file) agenda.save_state_to('tests/agenda') # Change agenda = ro_database.get_handler('tests/agenda', Agenda) fake = agenda.add_record({'firstname': u'Toto', 'lastname': u'Fofo'}) agenda.add_record({'firstname': u'Albert', 'lastname': u'Einstein'}) agenda.del_record(fake.id) agenda.save_state() # Test agenda = ro_database.get_handler('tests/agenda', Agenda) ids = [ x.id for x in agenda.search('firstname', u'Toto') ] self.assertEqual(len(ids), 0) ids = [ x.id for x in agenda.search('firstname', u'Albert') ] self.assertEqual(len(ids), 1) # Clean lfs.remove('tests/agenda')
def tearDown(self): for name in ['fables/31.txt', 'fables/agenda', 'fables/.git']: if lfs.exists(name): lfs.remove(name)
def tearDown(self): paths = ["fables/catalog", "fables/database/.git"] for path in paths: if lfs.exists(path): lfs.remove(path)
def reindex_catalog(self, quiet=False, quick=False, as_test=False): # FIXME: should be moved into backend from itools.database.backends.catalog import make_catalog msg = 'reindex catalog %s %s %s' % (quiet, quick, as_test) log_info(msg) if self.is_running_in_rw_mode(): print 'Cannot proceed, the server is running in read-write mode.' return # Create a temporary new catalog catalog_path = '%s/catalog.new' % self.target if lfs.exists(catalog_path): lfs.remove(catalog_path) catalog = make_catalog(catalog_path, get_register_fields()) # Get the root root = self.root # Update t0, v0 = time(), vmsize() doc_n = 0 error_detected = False if as_test: log = open('%s/log/update-catalog' % self.target, 'w').write with self.database.init_context() as context: for obj in root.traverse_resources(): if not quiet or doc_n % 10000==0: print('{0} {1}'.format(doc_n, obj.abspath)) doc_n += 1 context.resource = obj values = obj.get_catalog_values() # Index the document try: catalog.index_document(values) except Exception: if as_test: error_detected = True log('*** Error detected ***\n') log('Abspath of the resource: %r\n\n' % str(obj.abspath)) log(format_exc()) log('\n') else: raise # Free Memory del obj self.database.make_room() if not error_detected: if as_test: # Delete the empty log file remove('%s/log/update-catalog' % self.target) # Update / Report t1, v1 = time(), vmsize() v = (v1 - v0)/1024 print '[Update] Time: %.02f seconds. Memory: %s Kb' % (t1 - t0, v) # Commit print '[Commit]', sys.stdout.flush() catalog.save_changes() catalog.close() # Commit / Replace old_catalog_path = '%s/catalog' % self.target if lfs.exists(old_catalog_path): lfs.remove(old_catalog_path) lfs.move(catalog_path, old_catalog_path) # Commit / Report t2, v2 = time(), vmsize() v = (v2 - v1)/1024 print 'Time: %.02f seconds. Memory: %s Kb' % (t2 - t1, v) return True else: print '[Update] Error(s) detected, the new catalog was NOT saved' print ('[Update] You can find more infos in %r' % join(self.target, 'log/update-catalog')) return False
def setUp(self): self.config_path = "tests/setup.conf.test" if lfs.exists(self.config_path): lfs.remove(self.config_path)
def get_test_filenames(test_path, force_download): """Return the test file names If the test files does'nt exists, we download it """ uris = {'http://download.wikimedia.org/qualitywiki/latest': [('qualitywiki-latest-stub-articles.xml', '.gz'), #~ 3.1 KB ('qualitywiki-latest-stub-meta-current.xml', '.gz'), #~ 11.0 KB ('qualitywiki-latest-stub-meta-history.xml', '.gz')], #~ 28.9 KB 'http://download.wikimedia.org/tawiki/latest': [('tawiki-latest-stub-articles.xml', '.gz'), #~ 1.2 MB ('tawiki-latest-stub-meta-history.xml', '.gz')], #~ 7.3 MB 'http://www.w3.org/XML/Test/': [('xmlts20080205', '.tar.gz')] } compressed_dir_path = join(test_path, 'compressed_files') if force_download is True: if lfs.exists(compressed_dir_path): print 'Remove compressed directory ', compressed_dir_path lfs.remove(compressed_dir_path) for names in uris.itervalues(): for (name, ext) in names: path = join(test_path, name) if lfs.exists(path): print 'Remove %s file' % path lfs.remove(path) # test directory if lfs.exists(test_path) is False: lfs.make_folder(test_path) # compressed directory if lfs.exists(compressed_dir_path) is False: lfs.make_folder(compressed_dir_path) else: lfs.open(compressed_dir_path) test_dir_filenames = lfs.get_names(test_path) for base_uri, names in uris.iteritems(): for (name, ext) in names: if test_dir_filenames.count(name): continue compressed_dest = join(compressed_dir_path, '%s%s' % (name, ext)) # check if tarball already exists if lfs.exists(compressed_dest) is False: src = join(base_uri, '%s%s' % (name, ext)) print 'GET %s file' % src dest = join(test_path, name) if vfs.exists(src) is False: print "%s uri does not exists" % src continue src_file = vfs.open(src) # save Gzip file compressed_dest_file = lfs.make_file(compressed_dest) compressed_dest_file.write(src_file.read()) compressed_dest_file.close() src_file.close() print 'Extract file %s' % compressed_dest # Uncompressed File Path if name == 'xmlts20080205': # uncompress only xmlconf.xml file tar = open_tar(compressed_dest) xmlconf_file = tar.extractfile('xmlconf/xmlconf.xml') ucf_path = join(test_path, name) ucf_file = lfs.make_file(ucf_path) ucf_file.write(xmlconf_file.read()) ucf_file.close() else: # untar Gzip file compressed_dest_file = lfs.open(compressed_dest) gzip_file = GzipFile(compressed_dest) ucf_path = join(test_path, name) ucf_file = lfs.make_file(ucf_path) ucf_file.write(gzip_file.read()) compressed_dest_file.close() gzip_file.close() ucf_file.close() tests = [] # update test dir name test_dir_filenames = lfs.get_names(test_path) for filename in test_dir_filenames: real_path = join(test_path, filename) if lfs.is_file(real_path): bytes = lfs.get_size(real_path) tests.append((real_path, filename, bytes, get_string_size(bytes))) tests.sort(key=lambda x: x[2]) return tests
def tearDown(self): paths = ['fables/catalog', 'fables/database/.git'] for path in paths: if lfs.exists(path): lfs.remove(path)
def test_remove_empty_folder(self): lfs.make_folder('tests/folder') lfs.remove('tests/folder') self.assertEqual(lfs.exists('tests/folder'), False)
def tearDown(self): if lfs.exists(self.config_path): lfs.remove(self.config_path)
self.assertEqual(config2_value, value) def test_long_value(self): # Init data value = "HELLO, WORLD!\n\nHELLO WORLD2222" self._init_test(value) # Read data config2 = rw_database.get_handler(self.config_path, ConfigFile) try: config2_value = config2.get_value("test") except SyntaxError, e: self.fail(e) finally: lfs.remove(self.config_path) # Test data self.assertEqual(config2_value, value) def test_last_line_empty(self): # Init data value = "HELLO, WORLD!\n\n" self._init_test(value) # Write data config = rw_database.get_handler(self.config_path, ConfigFile) config.set_value("test", value) config.save_state()
def tearDown(self): lfs.remove('tests/catalog')
def tearDown(self): if lfs.exists('sandbox'): lfs.remove('sandbox')
def tearDown(self): if lfs.exists('tmp'): lfs.remove('tmp')
def test_make_file(self): lfs.make_file('tests/file') self.assertEqual(lfs.is_file('tests/file'), True) lfs.remove('tests/file')
def tearDown(self): for name in ['agenda', 'books']: name = 'tests/%s' % name if lfs.exists(name): lfs.remove(name)
def test_make_folder(self): lfs.make_folder('tests/folder') self.assertEqual(lfs.is_folder('tests/folder'), True) lfs.remove('tests/folder')
# Test data self.assertEqual(config2_value, value) def test_long_value(self): # Init data value = "HELLO, WORLD!\n\nHELLO WORLD2222" self._init_test(value) # Read data config2 = ConfigFile(self.config_path) try: config2_value = config2.get_value("test") except SyntaxError, e: self.fail(e) finally: lfs.remove(self.config_path) # Test data self.assertEqual(config2_value, value) def test_last_line_empty(self): # Init data value = "HELLO, WORLD!\n\n" self._init_test(value) # Write data config = ConfigFile(self.config_path) config.set_value("test", value) config.save_state() # Read data
def test_remove_file(self): lfs.make_file('tests/file') lfs.remove('tests/file') self.assertEqual(lfs.exists('tests/file'), False)
def tearDown(self): lfs.remove("tests/catalog")
def reindex_catalog(self, quiet=False, quick=False, as_test=False): if self.is_running_in_rw_mode(): print 'Cannot proceed, the server is running in read-write mode.' return # Check for database consistency if quick is False and check_database(self.target) is False: return False # Create a temporary new catalog catalog_path = '%s/catalog.new' % self.target if lfs.exists(catalog_path): lfs.remove(catalog_path) catalog = make_catalog(catalog_path, get_register_fields()) # Get the root root = self.root # Build a fake context context = self.get_fake_context() # Update t0, v0 = time(), vmsize() doc_n = 0 error_detected = False if as_test: log = open('%s/log/update-catalog' % self.target, 'w').write for obj in root.traverse_resources(): if not isinstance(obj, Resource): continue if not quiet: print doc_n, obj.abspath doc_n += 1 context.resource = obj # Index the document try: catalog.index_document(obj) except Exception: if as_test: error_detected = True log('*** Error detected ***\n') log('Abspath of the resource: %r\n\n' % str(obj.abspath)) log(format_exc()) log('\n') else: raise # Free Memory del obj self.database.make_room() if not error_detected: if as_test: # Delete the empty log file remove('%s/log/update-catalog' % self.target) # Update / Report t1, v1 = time(), vmsize() v = (v1 - v0)/1024 print '[Update] Time: %.02f seconds. Memory: %s Kb' % (t1 - t0, v) # Commit print '[Commit]', sys.stdout.flush() catalog.save_changes() # Commit / Replace old_catalog_path = '%s/catalog' % self.target if lfs.exists(old_catalog_path): lfs.remove(old_catalog_path) lfs.move(catalog_path, old_catalog_path) # Commit / Report t2, v2 = time(), vmsize() v = (v2 - v1)/1024 print 'Time: %.02f seconds. Memory: %s Kb' % (t2 - t1, v) return True else: print '[Update] Error(s) detected, the new catalog was NOT saved' print ('[Update] You can find more infos in %r' % join(self.target, 'log/update-catalog')) return False
test_modules = [test_metadata, test_server, test_database] loader = TestLoader() if __name__ == '__main__': usage = '%prog [OPTIONS]' description = 'Run ikaaro tests' parser = OptionParser(usage, description=description) parser.add_option('-m', '--mode', default='standard', help='tests mode') options, args = parser.parse_args() # Remove old test database if exists path = 'demo.hforge.org' if lfs.exists(path): lfs.remove(path) # Create test server create_server(path, '*****@*****.**', 'password', 'ikaaro', website_languages=['en', 'fr']) # Launch test suite = TestSuite() for module in test_modules: suite.addTest(loader.loadTestsFromModule(module)) if options.mode == 'standard': ret = TextTestRunner(verbosity=1).run(suite) elif options.mode == 'junitxml': path = get_abspath('./junit.xml') print('Result is here: %s' % path) f = file(path, 'wb') result = JUnitXmlResult(f) result.startTestRun()