def test_freeze(self): from clld.scripts.freeze import freeze_func, unfreeze_func tmp = Path(mkdtemp()) tmp.joinpath('data').mkdir() tmp.joinpath('appname').mkdir() class Args(object): env = self.env module_dir = tmp.joinpath('appname').resolve() module = Mock(__name__='appname') def data_file(self, *comps): return tmp.resolve().joinpath('data', *comps) DBSession.flush() args = Args() freeze_func(args, dataset=Dataset.first(), with_history=False) self.assert_(tmp.joinpath('data.zip').exists()) engine = create_engine('sqlite://') Base.metadata.create_all(engine) self.assertEqual( engine.execute('select count(*) from language').fetchone()[0], 0) unfreeze_func(args, engine=engine) s1 = DBSession s2 = sessionmaker(bind=engine)() self.assertEqual( s1.query(Language).count(), s2.query(Language).count()) l1 = s1.query(Language).filter(Language.latitude != null()).first() l2 = s2.query(Language).filter(Language.pk == l1.pk).first() self.assertEqual(l1.created, l2.created) self.assertEqual(l1.latitude, l2.latitude) self.assertEqual(l1.description, l2.description) contrib = s2.query(Contribution).filter( Contribution.id == 'contribution').one() self.assert_(contrib.primary_contributors) self.assert_(contrib.secondary_contributors) rmtree(tmp, ignore_errors=True)
def test_freeze(self): from clld.scripts.freeze import freeze_func, unfreeze_func tmp = Path(mkdtemp()) tmp.joinpath('data').mkdir() tmp.joinpath('appname').mkdir() class Args(object): env = self.env module_dir = tmp.joinpath('appname').resolve() module = Mock(__name__='appname') def data_file(self, *comps): return tmp.resolve().joinpath('data', *comps) DBSession.flush() args = Args() freeze_func(args, dataset=Dataset.first(), with_history=False) self.assert_(tmp.joinpath('data.zip').exists()) engine = create_engine('sqlite://') Base.metadata.create_all(engine) self.assertEqual( engine.execute('select count(*) from language').fetchone()[0], 0) unfreeze_func(args, engine=engine) s1 = DBSession s2 = sessionmaker(bind=engine)() self.assertEqual(s1.query(Language).count(), s2.query(Language).count()) l1 = s1.query(Language).filter(Language.latitude != null()).first() l2 = s2.query(Language).filter(Language.pk == l1.pk).first() self.assertEqual(l1.created, l2.created) self.assertEqual(l1.latitude, l2.latitude) self.assertEqual(l1.description, l2.description) contrib = s2.query(Contribution).filter(Contribution.id == 'contribution').one() self.assert_(contrib.primary_contributors) self.assert_(contrib.secondary_contributors) rmtree(tmp, ignore_errors=True)
def freeze(): # pragma: no cover """ Create a dump a an app's database as set of csv files in an archive data.zip """ freeze_func(parsed_args(bootstrap=True, description=freeze.__doc__))