def test_unload_list(self, mock_popen, mock_pgver, mock_isext): pop = mock_popen.return_value pop.returncode = 0 mock_pgver.return_value = (9,1,0) mock_isext.return_value = True tdir = tempfile.mkdtemp() try: from pgxnclient.utils.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) shutil.copyfile( get_test_filename('META-manyext.json'), os.path.join(dir, 'META.json')) from pgxnclient.cli import main main(['unload', '--yes', dir, 'baz', 'foo']) finally: shutil.rmtree(tdir) self.assertEquals(mock_popen.call_count, 2) self.assert_('psql' in mock_popen.call_args[0][0][0]) self.assertEquals(pop.communicate.call_args_list[0][0][0], 'DROP EXTENSION baz;') self.assertEquals(pop.communicate.call_args_list[1][0][0], 'DROP EXTENSION foo;')
def test_unload_extensions_order(self): tdir = tempfile.mkdtemp() try: from pgxnclient.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) shutil.copyfile( get_test_filename('META-manyext.json'), os.path.join(dir, 'META.json')) from pgxnclient.cli import main main(['unload', '--yes', dir]) finally: shutil.rmtree(tdir) self.assertEquals(self.mock_popen.call_count, 4) self.assert_('psql' in self.mock_popen.call_args[0][0][0]) communicate = self.mock_popen.return_value.communicate self.assertEquals(communicate.call_args_list[0][0][0], b('DROP EXTENSION qux;')) self.assertEquals(communicate.call_args_list[1][0][0], b('DROP EXTENSION baz;')) self.assertEquals(communicate.call_args_list[2][0][0], b('DROP EXTENSION bar;')) self.assertEquals(communicate.call_args_list[3][0][0], b('DROP EXTENSION foo;'))
def test_unload_extensions_order(self): tdir = tempfile.mkdtemp() try: from pgxnclient.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) shutil.copyfile(get_test_filename('META-manyext.json'), os.path.join(dir, 'META.json')) from pgxnclient.cli import main main(['unload', '--yes', dir]) finally: shutil.rmtree(tdir) self.assertEquals(self.mock_popen.call_count, 4) self.assert_('psql' in self.mock_popen.call_args[0][0][0]) communicate = self.mock_popen.return_value.communicate self.assertEquals(communicate.call_args_list[0][0][0], b('DROP EXTENSION qux;')) self.assertEquals(communicate.call_args_list[1][0][0], b('DROP EXTENSION baz;')) self.assertEquals(communicate.call_args_list[2][0][0], b('DROP EXTENSION bar;')) self.assertEquals(communicate.call_args_list[3][0][0], b('DROP EXTENSION foo;'))
def test_unload_missing(self): tdir = tempfile.mkdtemp() try: from pgxnclient.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) shutil.copyfile(get_test_filename('META-manyext.json'), os.path.join(dir, 'META.json')) from pgxnclient.cli import main self.assertRaises(PgxnClientException, main, ['unload', '--yes', dir, 'foo', 'ach']) finally: shutil.rmtree(tdir) self.assertEquals(self.mock_popen.call_count, 0)
def test_unload_missing(self): tdir = tempfile.mkdtemp() try: from pgxnclient.utils.zip import unpack dir = unpack(get_test_filename("foobar-0.42.1.zip"), tdir) shutil.copyfile(get_test_filename("META-manyext.json"), os.path.join(dir, "META.json")) from pgxnclient.cli import main self.assertRaises(PgxnClientException, main, ["unload", "--yes", dir, "foo", "ach"]) finally: shutil.rmtree(tdir) self.assertEquals(self.mock_popen.call_count, 0)
def fake_get_file(url, urlmap=None): if urlmap: url = urlmap.get(url, url) fn = get_test_filename(quote(url, safe="")) if not os.path.exists(fn): raise ResourceNotFound(fn) f = FakeFile(fn, 'rb') f.url = url return f
def test_unload_missing(self): tdir = tempfile.mkdtemp() try: from pgxnclient.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) shutil.copyfile( get_test_filename('META-manyext.json'), os.path.join(dir, 'META.json')) from pgxnclient.cli import main self.assertRaises(PgxnClientException, main, ['unload', '--yes', dir, 'foo', 'ach']) finally: shutil.rmtree(tdir) self.assertEquals(self.mock_popen.call_count, 0)
def test_install_local_zip(self, mock_unpack): from pgxnclient.utils.zip import unpack mock_unpack.side_effect = unpack from pgxnclient.cli import main main(["install", "--sudo", "--", get_test_filename("foobar-0.42.1.zip")]) self.assertEquals(self.mock_popen.call_count, 2) self.assertEquals(["make"], self.mock_popen.call_args_list[0][0][0][:1]) self.assertEquals(["sudo", "make"], self.mock_popen.call_args_list[1][0][0][:2]) make_cwd = self.mock_popen.call_args_list[1][1]["cwd"] self.assertEquals(mock_unpack.call_count, 1) zipname, tmpdir = mock_unpack.call_args[0] self.assertEqual(zipname, get_test_filename("foobar-0.42.1.zip")) self.assertEqual(make_cwd, os.path.join(tmpdir, "foobar-0.42.1"))
def test_unload_list(self): tdir = tempfile.mkdtemp() try: from pgxnclient.utils.zip import unpack dir = unpack(get_test_filename("foobar-0.42.1.zip"), tdir) shutil.copyfile(get_test_filename("META-manyext.json"), os.path.join(dir, "META.json")) from pgxnclient.cli import main main(["unload", "--yes", dir, "baz", "foo"]) finally: shutil.rmtree(tdir) self.assertEquals(self.mock_popen.call_count, 2) self.assert_("psql" in self.mock_popen.call_args[0][0][0]) communicate = self.mock_popen.return_value.communicate self.assertEquals(communicate.call_args_list[0][0][0], "DROP EXTENSION baz;") self.assertEquals(communicate.call_args_list[1][0][0], "DROP EXTENSION foo;")
def test_install_local_zip(self, mock_get, mock_popen, mock_unpack): mock_get.side_effect = lambda *args: self.fail('network invoked') pop = mock_popen.return_value pop.returncode = 0 from pgxnclient.utils.zip import unpack mock_unpack.side_effect = unpack from pgxnclient.cli import main main(['install', get_test_filename('foobar-0.42.1.zip')]) self.assertEquals(mock_popen.call_count, 2) self.assertEquals(['make'], mock_popen.call_args_list[0][0][0][:1]) self.assertEquals(['sudo', 'make'], mock_popen.call_args_list[1][0][0][:2]) make_cwd = mock_popen.call_args_list[1][1]['cwd'] self.assertEquals(mock_unpack.call_count, 1) zipname, tmpdir = mock_unpack.call_args[0] self.assertEqual(zipname, get_test_filename('foobar-0.42.1.zip')) self.assertEqual(make_cwd, os.path.join(tmpdir, 'foobar-0.42.1'))
def test_install_url_file(self): fn = get_test_filename('foobar-0.42.1.zip') url = 'file://' + os.path.abspath(fn).replace("f", '%%%2x' % ord('f')) from pgxnclient.cli import main main(['install', '--sudo', '--', url]) self.assertEquals(self.mock_popen.call_count, 2) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1]) self.assertCallArgs(['sudo', self.make], self.mock_popen.call_args_list[1][0][0][:2])
def test_missing_meta_dir(self): # issue #19 tdir = tempfile.mkdtemp() try: from pgxnclient.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) os.unlink(os.path.join(dir, 'META.json')) from pgxnclient.cli import main self.assertRaises(PgxnClientException, main, ['load', dir]) finally: shutil.rmtree(tdir)
def test_load_local_tar(self, mock_get, mock_unpack): mock_get.side_effect = lambda *args: self.fail('network invoked') mock_unpack.side_effect = TarArchive.unpack_orig from pgxnclient.cli import main main(['load', '--yes', get_test_filename('foobar-0.42.1.tar.gz')]) self.assertEquals(mock_unpack.call_count, 0) self.assertEquals(self.mock_popen.call_count, 1) self.assert_('psql' in self.mock_popen.call_args[0][0][0]) communicate = self.mock_popen.return_value.communicate self.assertEquals(communicate.call_args[0][0], b('CREATE EXTENSION foobar;'))
def test_unload_missing(self, mock_popen, mock_pgver, mock_isext): pop = mock_popen.return_value pop.returncode = 0 mock_pgver.return_value = (9,1,0) mock_isext.return_value = True tdir = tempfile.mkdtemp() try: from pgxnclient.utils.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) shutil.copyfile( get_test_filename('META-manyext.json'), os.path.join(dir, 'META.json')) from pgxnclient.cli import main self.assertRaises(PgxnClientException, main, ['unload', '--yes', dir, 'foo', 'ach']) finally: shutil.rmtree(tdir) self.assertEquals(mock_popen.call_count, 0)
def test_load_local_zip(self, mock_get, mock_unpack): mock_get.side_effect = lambda *args: self.fail("network invoked") from pgxnclient.utils.zip import unpack mock_unpack.side_effect = unpack from pgxnclient.cli import main main(["load", "--yes", get_test_filename("foobar-0.42.1.zip")]) self.assertEquals(mock_unpack.call_count, 0) self.assertEquals(self.mock_popen.call_count, 1) self.assert_("psql" in self.mock_popen.call_args[0][0][0]) communicate = self.mock_popen.return_value.communicate self.assertEquals(communicate.call_args[0][0], "CREATE EXTENSION foobar;")
def test_install_local_zip(self, mock_unpack): fn = get_test_filename('foobar-0.42.1.zip') mock_unpack.side_effect = ZipArchive(fn).unpack_orig from pgxnclient.cli import main main(['install', '--sudo', '--', fn]) self.assertEquals(self.mock_popen.call_count, 2) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1]) self.assertCallArgs(['sudo', self.make], self.mock_popen.call_args_list[1][0][0][:2]) make_cwd = self.mock_popen.call_args_list[1][1]['cwd'] self.assertEquals(mock_unpack.call_count, 1) tmpdir, = mock_unpack.call_args[0] self.assertEqual(make_cwd, os.path.join(tmpdir, 'foobar-0.42.1'))
def test_load_local_zip(self, mock_get, mock_popen, mock_unpack, mock_pgver, mock_isext): mock_get.side_effect = lambda *args: self.fail('network invoked') pop = mock_popen.return_value pop.returncode = 0 from pgxnclient.utils.zip import unpack mock_unpack.side_effect = unpack mock_pgver.return_value = (9,1,0) mock_isext.return_value = True from pgxnclient.cli import main main(['load', '--yes', get_test_filename('foobar-0.42.1.zip')]) self.assertEquals(mock_unpack.call_count, 0) self.assertEquals(mock_popen.call_count, 1) self.assert_('psql' in mock_popen.call_args[0][0][0]) self.assertEquals(pop.communicate.call_args[0][0], 'CREATE EXTENSION foobar;')
def test_load_local_dir(self, mock_get): mock_get.side_effect = lambda *args: self.fail('network invoked') tdir = tempfile.mkdtemp() try: from pgxnclient.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) from pgxnclient.cli import main main(['load', '--yes', dir]) finally: shutil.rmtree(tdir) self.assertEquals(self.mock_popen.call_count, 1) self.assert_('psql' in self.mock_popen.call_args[0][0][0]) communicate = self.mock_popen.return_value.communicate self.assertEquals(communicate.call_args[0][0], b('CREATE EXTENSION foobar;'))
def test_install_local_dir(self): self.mock_get.side_effect = lambda *args: self.fail('network invoked') tdir = tempfile.mkdtemp() try: from pgxnclient.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) from pgxnclient.cli import main main(['install', '--sudo', '--', dir]) finally: shutil.rmtree(tdir) self.assertEquals(self.mock_popen.call_count, 2) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1]) self.assertCallArgs(dir, self.mock_popen.call_args_list[0][1]['cwd']) self.assertCallArgs(['sudo', self.make], self.mock_popen.call_args_list[1][0][0][:2]) self.assertEquals(dir, self.mock_popen.call_args_list[1][1]['cwd'])
def test_install_local_dir(self): self.mock_get.side_effect = lambda *args: self.fail("network invoked") tdir = tempfile.mkdtemp() try: from pgxnclient.utils.zip import unpack dir = unpack(get_test_filename("foobar-0.42.1.zip"), tdir) from pgxnclient.cli import main main(["install", "--sudo", "--", dir]) finally: shutil.rmtree(tdir) self.assertEquals(self.mock_popen.call_count, 2) self.assertEquals(["make"], self.mock_popen.call_args_list[0][0][0][:1]) self.assertEquals(dir, self.mock_popen.call_args_list[0][1]["cwd"]) self.assertEquals(["sudo", "make"], self.mock_popen.call_args_list[1][0][0][:2]) self.assertEquals(dir, self.mock_popen.call_args_list[1][1]["cwd"])
def test_install_local_dir(self, mock_get, mock_popen): mock_get.side_effect = lambda *args: self.fail('network invoked') pop = mock_popen.return_value pop.returncode = 0 tdir = tempfile.mkdtemp() try: from pgxnclient.utils.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) from pgxnclient.cli import main main(['install', dir]) finally: shutil.rmtree(tdir) self.assertEquals(mock_popen.call_count, 2) self.assertEquals(['make'], mock_popen.call_args_list[0][0][0][:1]) self.assertEquals(dir, mock_popen.call_args_list[0][1]['cwd']) self.assertEquals(['sudo', 'make'], mock_popen.call_args_list[1][0][0][:2]) self.assertEquals(dir, mock_popen.call_args_list[1][1]['cwd'])
def test_load_local_dir(self, mock_get, mock_popen, mock_pgver, mock_isext): mock_get.side_effect = lambda *args: self.fail('network invoked') pop = mock_popen.return_value pop.returncode = 0 mock_pgver.return_value = (9,1,0) mock_isext.return_value = True tdir = tempfile.mkdtemp() try: from pgxnclient.utils.zip import unpack dir = unpack(get_test_filename('foobar-0.42.1.zip'), tdir) from pgxnclient.cli import main main(['load', '--yes', dir]) finally: shutil.rmtree(tdir) self.assertEquals(mock_popen.call_count, 1) self.assert_('psql' in mock_popen.call_args[0][0][0]) self.assertEquals(pop.communicate.call_args[0][0], 'CREATE EXTENSION foobar;')
def test_can_open_noext(self): fn = get_test_filename('zip.ext') a = zip.ZipArchive(fn) self.assert_(a.can_open()) a.open() a.close()
def test_can_open(self): fn = get_test_filename('foobar-0.42.1.zip') a = zip.ZipArchive(fn) self.assert_(a.can_open()) a.open() a.close()
def test_cannot_open(self): fn = get_test_filename('foobar-0.42.1.tar.gz') a = zip.ZipArchive(fn) self.assert_(not a.can_open())
def test_can_open(self): fn = get_test_filename('foobar-0.42.1.tar.gz') a = tar.TarArchive(fn) self.assert_(a.can_open()) a.open() a.close()
def test_from_file_unknown(self): fn = get_test_filename('META-manyext.json') self.assertRaises(PgxnClientException(archive.from_file, fn))
def test_from_file_tar(self): fn = get_test_filename('foobar-0.42.1.tar.gz') a = archive.from_file(fn) self.assert_(isinstance(a, tar.TarArchive)) self.assertEqual(a.filename, fn)
def test_from_file_zip(self): fn = get_test_filename('foobar-0.42.1.zip') a = archive.from_file(fn) self.assert_(isinstance(a, zip.ZipArchive)) self.assertEqual(a.filename, fn)
def test_cannot_open(self): fn = get_test_filename('foobar-0.42.1.zip') a = tar.TarArchive(fn) self.assert_(not a.can_open())
def test_can_open_noext(self): fn = get_test_filename('tar.ext') a = tar.TarArchive(fn) self.assert_(a.can_open()) a.open() a.close()