def test_download_rename(self, mock): mock.side_effect = fake_get_file fn = 'foobar-0.42.1.zip' fn1= 'foobar-0.42.1-1.zip' fn2= 'foobar-0.42.1-2.zip' for tmp in (fn, fn1, fn2): self.assert_(not os.path.exists(tmp)) try: f = open(fn, "w") f.write('test') f.close() from pgxnclient.cli import main main(['download', 'foobar']) self.assert_(os.path.exists(fn1)) self.assert_(not os.path.exists(fn2)) main(['download', 'foobar']) self.assert_(os.path.exists(fn2)) f = open(fn) self.assertEquals(f.read(), 'test') f.close() finally: ifunlink(fn) ifunlink(fn1) ifunlink(fn2)
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_search_quoting(self, mock_get, stdout): stdout.encoding = 'UTF-8' mock_get.side_effect = fake_get_file from pgxnclient.cli import main main(['search', '--docs', 'foo bar', 'baz'])
def test_load_list(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(['load', '--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], b'CREATE EXTENSION baz;') self.assertEquals(communicate.call_args_list[1][0][0], b'CREATE EXTENSION foo;')
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_load_list(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(['load', '--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], b'CREATE EXTENSION baz;' ) self.assertEquals( communicate.call_args_list[1][0][0], b'CREATE EXTENSION foo;' )
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_download_rename(self, mock): mock.side_effect = fake_get_file fn = 'foobar-0.42.1.zip' fn1 = 'foobar-0.42.1-1.zip' fn2 = 'foobar-0.42.1-2.zip' for tmp in (fn, fn1, fn2): self.assert_(not os.path.exists(tmp)) try: f = open(fn, "w") f.write('test') f.close() from pgxnclient.cli import main main(['download', 'foobar']) self.assert_(os.path.exists(fn1)) self.assert_(not os.path.exists(fn2)) main(['download', 'foobar']) self.assert_(os.path.exists(fn2)) f = open(fn) self.assertEquals(f.read(), 'test') f.close() finally: ifunlink(fn) ifunlink(fn1) ifunlink(fn2)
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_check_latest(self): from pgxnclient.cli import main main(['check', 'foobar']) self.assertEquals(self.mock_popen.call_count, 1) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1])
def test_check_latest(self): from pgxnclient.cli import main main(["check", "foobar"]) self.assertEquals(self.mock_popen.call_count, 1) self.assertEquals(["make"], self.mock_popen.call_args_list[0][0][0][:1])
def test_install_latest(self): from pgxnclient.cli import main main(['install', '--sudo', '--', 'foobar']) 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_install_sudo(self): from pgxnclient.cli import main main(["install", "--sudo", 'gksudo -d "hello world"', "foobar"]) self.assertEquals(self.mock_popen.call_count, 2) self.assertEquals(["make"], self.mock_popen.call_args_list[0][0][0][:1]) self.assertEquals(["gksudo", "-d", "hello world", "make"], self.mock_popen.call_args_list[1][0][0][:4])
def do(mock, stdout): stdout.encoding = 'UTF-8' mock.side_effect = fake_get_file from pgxnclient.cli import main main(cmdline) return get_stdout_data(stdout)
def test_install_latest(self): from pgxnclient.cli import main main(["install", "--sudo", "--", "foobar"]) 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])
def test_install_sudo(self): from pgxnclient.cli import main main(['install', '--sudo', 'gksudo -d "hello world"', 'foobar']) self.assertEquals(self.mock_popen.call_count, 2) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1]) self.assertCallArgs(['gksudo', '-d', 'hello world', self.make], self.mock_popen.call_args_list[1][0][0][:4])
def test_check_latest(self): from pgxnclient.cli import main main(['check', 'foobar']) self.assertEquals(self.mock_popen.call_count, 1) self.assertCallArgs( [self.make], self.mock_popen.call_args_list[0][0][0][:1] )
def test_libexec(self, stdout): stdout.encoding = 'UTF-8' from pgxnclient.cli import main main(['help', '--libexec']) out = get_stdout_data(stdout) assert out.strip() assert out.count(b'\n') == 1
def test_check_url(self): from pgxnclient.cli import main main([ 'check', 'http://api.pgxn.org/dist/foobar/0.42.1/foobar-0.42.1.zip' ]) self.assertEquals(self.mock_popen.call_count, 1) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1])
def test_install_nosudo(self): self.mock_pgconfig.side_effect = fake_pg_config(libdir=os.environ['HOME']) from pgxnclient.cli import main main(['install', '--nosudo', 'foobar']) self.assertEquals(self.mock_popen.call_count, 2) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1]) self.assertCallArgs([self.make], self.mock_popen.call_args_list[1][0][0][:1])
def run(self): if self.opts.command: from pgxnclient.cli import main main([self.opts.command, '--help']) elif self.opts.all: self.print_all_commands() elif self.opts.libexec: self.print_libexec() else: self._parser.print_help()
def test_check_latest(self, mock_get, mock_popen): mock_get.side_effect = fake_get_file pop = mock_popen.return_value pop.returncode = 0 from pgxnclient.cli import main main(['check', 'foobar']) self.assertEquals(mock_popen.call_count, 1) self.assertEquals(['make'], mock_popen.call_args_list[0][0][0][:1])
def test_install_url(self): self.mock_pgconfig.side_effect = fake_pg_config( libdir=os.environ['HOME'], bindir='/') from pgxnclient.cli import main main(['install', 'http://api.pgxn.org/dist/foobar/0.42.1/foobar-0.42.1.zip']) self.assertEquals(self.mock_popen.call_count, 2) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1]) self.assertCallArgs([self.make], self.mock_popen.call_args_list[1][0][0][:1])
def test_install_nosudo(self): self.mock_pgconfig.side_effect = fake_pg_config(libdir=os.environ["HOME"]) from pgxnclient.cli import main main(["install", "--nosudo", "foobar"]) self.assertEquals(self.mock_popen.call_count, 2) self.assertEquals(["make"], self.mock_popen.call_args_list[0][0][0][:1]) self.assertEquals(["make"], self.mock_popen.call_args_list[1][0][0][: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_install_nosudo(self, mock_get, mock_popen): mock_get.side_effect = fake_get_file pop = mock_popen.return_value pop.returncode = 0 from pgxnclient.cli import main main(['install', '--nosudo', 'foobar']) self.assertEquals(mock_popen.call_count, 2) self.assertEquals(['make'], mock_popen.call_args_list[0][0][0][:1]) self.assertEquals(['make'], mock_popen.call_args_list[1][0][0][:1])
def test_download_testing(self, mock): mock.side_effect = fake_get_file fn = 'foobar-0.43.2b1.zip' self.assert_(not os.path.exists(fn)) from pgxnclient.cli import main try: main(['download', '--testing', 'foobar']) self.assert_(os.path.exists(fn)) finally: ifunlink(fn)
def test_download_url(self, mock): mock.side_effect = fake_get_file fn = 'foobar-0.43.2b1.zip' self.assert_(not os.path.exists(fn)) from pgxnclient.cli import main try: main(['download', 'http://api.pgxn.org/dist/foobar/0.43.2b1/foobar-0.43.2b1.zip']) self.assert_(os.path.exists(fn)) finally: ifunlink(fn)
def test_empty_command(self, stdout): from pgxnclient.cli import main stdout.encoding = None main([]) out = get_stdout_data(stdout) # The output is the same of 'help' stdout.reset_mock() main(["help"]) out_help = get_stdout_data(stdout) assert out == out_help
def test_install_nosudo(self): self.mock_pgconfig.side_effect = fake_pg_config( libdir=os.environ['HOME']) from pgxnclient.cli import main main(['install', '--nosudo', 'foobar']) self.assertEquals(self.mock_popen.call_count, 2) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1]) self.assertCallArgs([self.make], self.mock_popen.call_args_list[1][0][0][:1])
def test_download_ext(self, mock): mock.side_effect = fake_get_file fn = 'pg_amqp-0.3.0.zip' self.assert_(not os.path.exists(fn)) from pgxnclient.cli import main try: main(['download', 'amqp']) self.assert_(os.path.exists(fn)) finally: ifunlink(fn)
def test_download_latest(self, mock): mock.side_effect = fake_get_file fn = "foobar-0.42.1.zip" self.assert_(not os.path.exists(fn)) from pgxnclient.cli import main try: main(["download", "foobar"]) self.assert_(os.path.exists(fn)) finally: ifunlink(fn)
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_load_tar_url(self, mock_get, mock_unpack): mock_get.side_effect = fake_get_file mock_unpack.side_effect = TarArchive.unpack_orig from pgxnclient.cli import main main(['load', '--yes', 'http://example.org/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_check_url(self): from pgxnclient.cli import main main( [ 'check', 'https://api.pgxn.org/dist/foobar/0.42.1/foobar-0.42.1.zip', ] ) self.assertEquals(self.mock_popen.call_count, 1) self.assertCallArgs( [self.make], self.mock_popen.call_args_list[0][0][0][:1] )
def test_check_psql_options(self, mock_get, mock_popen, mock_pgver, mock_isext): mock_get.side_effect = fake_get_file pop = mock_popen.return_value pop.returncode = 0 pop.communicate.return_value = (b(''), b('')) mock_pgver.return_value = (9,1,0) mock_isext.return_value = True from pgxnclient.cli import main main(['load', '--yes', '--dbname', 'dbdb', 'foobar']) args = mock_popen.call_args[0][0] self.assertEqual('dbdb', args[args.index('--dbname') + 1]) main(['load', '--yes', '-U', 'meme', 'foobar']) args = mock_popen.call_args[0][0] self.assertEqual('meme', args[args.index('--username') + 1]) main(['load', '--yes', '--port', '666', 'foobar']) args = mock_popen.call_args[0][0] self.assertEqual('666', args[args.index('--port') + 1]) main(['load', '--yes', '-h', 'somewhere', 'foobar']) args = mock_popen.call_args[0][0] self.assertEqual('somewhere', args[args.index('--host') + 1])
def test_download_url(self, mock): mock.side_effect = fake_get_file fn = 'foobar-0.43.2b1.zip' self.assert_(not os.path.exists(fn)) from pgxnclient.cli import main try: main([ 'download', 'http://api.pgxn.org/dist/foobar/0.43.2b1/foobar-0.43.2b1.zip' ]) self.assert_(os.path.exists(fn)) finally: ifunlink(fn)
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_url(self): self.mock_pgconfig.side_effect = fake_pg_config( libdir=os.environ['HOME'], bindir='/') from pgxnclient.cli import main main([ 'install', 'http://api.pgxn.org/dist/foobar/0.42.1/foobar-0.42.1.zip' ]) self.assertEquals(self.mock_popen.call_count, 2) self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1]) self.assertCallArgs([self.make], self.mock_popen.call_args_list[1][0][0][:1])
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_zip_url(self, mock_get, mock_unpack): mock_get.side_effect = fake_get_file mock_unpack.side_effect = ZipArchive.unpack_orig from pgxnclient.cli import main main([ 'load', '--yes', 'https://api.pgxn.org/dist/foobar/0.42.1/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], b'CREATE EXTENSION foobar;')
def test_download_case_insensitive(self, mock): mock.side_effect = fake_get_file fn = 'pyrseas-0.4.1.zip' self.assert_(not os.path.exists(fn)) from pgxnclient.cli import main try: main(['download', 'pyrseas']) self.assert_(os.path.exists(fn)) finally: ifunlink(fn) try: main(['download', 'Pyrseas']) self.assert_(os.path.exists(fn)) finally: ifunlink(fn)
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_nonascii(self, mock_get, stdout): mock_get.side_effect = fake_get_file from pgxnclient.cli import main stdout.encoding = 'UTF-8' main(['info', 'first_last_agg']) out = max(get_stdout_data(stdout)) if not isinstance(out, int): out = ord(out) assert out > 127 stdout.reset_mock() stdout.encoding = None main(['info', 'first_last_agg']) out = max(get_stdout_data(stdout)) if not isinstance(out, int): out = ord(out) assert out < 127
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_check_psql_options(self, mock_get): mock_get.side_effect = fake_get_file from pgxnclient.cli import main main(['load', '--yes', '--dbname', 'dbdb', 'foobar']) args = self.mock_popen.call_args[0][0] self.assertEqual('dbdb', args[args.index('--dbname') + 1]) main(['load', '--yes', '-U', 'meme', 'foobar']) args = self.mock_popen.call_args[0][0] self.assertEqual('meme', args[args.index('--username') + 1]) main(['load', '--yes', '--port', '666', 'foobar']) args = self.mock_popen.call_args[0][0] self.assertEqual('666', args[args.index('--port') + 1]) main(['load', '--yes', '-h', 'somewhere', 'foobar']) args = self.mock_popen.call_args[0][0] self.assertEqual('somewhere', args[args.index('--host') + 1])
def do(mock, stdout): mock.side_effect = fake_get_file from pgxnclient.cli import main main(cmdline) return u''.join([a[0] for a, k in stdout.write.call_args_list]) \ .encode('ascii')