def test_import(self): # TODO: create a test to check that we aren't using an insert # statement for import that assumes a column value from the previous # insert values, could probably create an insert statement from a # row in the test data and then create an insert statement from some # other dummy data that has different columns from the test data and # see if any of the columns from the second insert statement has values # from the first statement # TODO: this test doesn't really test yet that any of the data was # correctly imported or exported, only that export and importing # run successfuly # 1. write the test data to a temporary file or files # 2. import the data and make sure the objects match field for field # the exporters and importers show logging information, turn it off logging.getLogger('bauble.info').setLevel(logging.ERROR) import tempfile tempdir = tempfile.mkdtemp() # export all the testdata exporter = CSVExporter() exporter.start(tempdir) # import all the files in the temp directory filenames = os.listdir(tempdir) importer = CSVImporter() # import twice to check for regression Launchpad #??? importer.start([os.path.join(tempdir, name) for name in filenames], force=True) importer.start([os.path.join(tempdir, name) for name in filenames], force=True)
def install(cls, import_defaults=True): import bauble.paths as paths if not import_defaults: return path = os.path.join(paths.lib_dir(), "plugins", "plants", "default") filenames = os.path.join(path, 'family.txt') from bauble.plugins.imex.csv_ import CSVImporter csv = CSVImporter() try: csv.start([filenames], metadata=db.metadata, force=True) except Exception, e: logger.error(e) raise
def install(cls, import_defaults=True): """ Do any setup and configuration required by this plugin like creating tables, etc... """ if not import_defaults: return path = os.path.join(paths.lib_dir(), "plugins", "plants", "default") filenames = [ os.path.join(path, f) for f in 'family.txt', 'family_synonym.txt', 'genus.txt', 'genus_synonym.txt', 'geography.txt', 'habit.txt' ] from bauble.plugins.imex.csv_ import CSVImporter csv = CSVImporter() #import_error = False #import_exc = None csv.start(filenames, metadata=db.metadata, force=True)
def test_sequences(self): """ Test that the sequences are set correctly after an import, bauble.util.test already has a method to test utils.reset_sequence but this test makes sure that it works correctly after an import This test requires the PlantPlugin """ # turn off logger logging.getLogger('bauble.info').setLevel(logging.ERROR) # import the family data filename = os.path.join('bauble', 'plugins', 'plants', 'default', 'family.txt') importer = CSVImporter() importer.start([filename], force=True) # the highest id number in the family file is assumed to be # num(lines)-1 since the id numbers are sequential and # subtract for the file header highest_id = len(open(filename).readlines())-1 currval = None conn = db.engine.contextual_connect() if db.engine.name == 'postgres': stmt = "SELECT currval('family_id_seq');" currval = conn.execute(stmt).fetchone()[0] self.assertEquals(currval, 0) elif db.engine.name == 'sqlite': # max(id) isn't really safe in production use but is ok for a test stmt = "SELECT max(id) from family;" nextval = conn.execute(stmt).fetchone()[0] + 1 else: raise "no test for engine type: %s" % db.engine.name #debug(list(conn.execute("SELECT * FROM family").fetchall())) maxid = conn.execute("SELECT max(id) FROM family").fetchone()[0] assert nextval > highest_id, \ "bad sequence: highest_id(%s) > nexval(%s) -- %s" % \ (highest_id, nextval, maxid)