def test_database_schema_basedata_mockup(self): self.db.drop_database() self.db.create_database() assert self.db.database_exists() with Given(app, ['db', 'schema']): assert stderr == '' assert status == 0 assert self.db.table_exists(FooModel.__tablename__) when(['db', 'basedata']) assert stderr == '' assert status == 0 with self.db.cursor( f'SELECT count(*) FROM foo_model WHERE title = %s', ('FooBase', )) as c: assert c.fetchone()[0] == 1 when(['db', 'mockup']) assert stderr == '' assert status == 0 with self.db.cursor( f'SELECT count(*) FROM foo_model WHERE title = %s', ('FooMock', )) as c: assert c.fetchone()[0] == 1
def test_subcommand(): app = Application('foo', 'tests.test_subcommand:Foo.quickstart') with Given(app): assert stdout == EXPECTED_HELP assert status == 0 when(['-h']) assert status == 0 assert stderr == '' assert stdout == EXPECTED_HELP when(['bar']) assert stderr == '' assert stdout == 'Bar done: False\n' assert status == 0 when(['ba']) assert stderr == '' assert stdout == 'Bar done: False\n' assert status == 0 when(['b']) assert stderr == '' assert stdout == 'Bar done: False\n' assert status == 0 when(['bar', '--baz']) assert stderr == '' assert stdout == 'Bar done: True\n' assert status == 1
def test_list_manipulators(): with Given(app, ['bar']): assert stderr == 'foo bar\n' with pytest.raises(ValueError): when(given + {'invalid': 'qux'}) with pytest.raises(ValueError): when(given | {'bar': 'qux'}) when(given - 'bar') assert stderr == 'foo\n' with pytest.raises(ValueError): when(given - 'missing') when(given + ['baz', 'qux', 'quux']) assert stderr == 'foo bar baz qux quux\n' when(given - ['bar']) assert stderr == 'foo\n' class InvalidType: pass with pytest.raises(TypeError): when(given - InvalidType()) with pytest.raises(TypeError): when(given | InvalidType())
def test_verb(): with Given(app, 'example.com'): assert status == 0 assert stdout == 'example.com' when(given + 'POST') assert status == 0 assert stdout == '-XPOST example.com'
def test_restfulpy_cli(db): with Given(app): assert stdout.startswith('usage') assert status == 0 when(given + '--version') assert stdout == f'{restfulpy.__version__}\n' assert status == 0
def test_servercli(freetcpport): cliapp = CLIApplication('foo', 'tests.test_builtincli:app.climain') with Given(cliapp, f'serve --bind {freetcpport}', nowait=True) as s: url = f'http://localhost:{freetcpport}' time.sleep(1) r = requests.get(url) assert r.text == 'foo' s.kill()
def test_working_directory(): with Given(app): assert f'{os.getcwd()}\n' == stdout if os.name == "nt": tmp_dir = os.getenv('TEMP') else: tmp_dir = '/tmp' when(working_directory=tmp_dir) assert stdout == tmp_dir + '\n'
def test_database_drop(self): self.db.create_database(exist_ok=True) assert self.db.database_exists() with Given(app, ['db', 'drop']): assert stderr == '' assert status == 0 assert not self.db.database_exists()
def test_appcli_root(): with Given(app): assert stderr == '' assert status == 0 assert stdout == EXPECTED_HELP when(given + '-V') assert stderr == '' assert status == 0 assert stdout == '0.1.0a0\n'
def test_jwtcli(): cliapp = CLIApplication('example', 'tests.test_cli:app.climain') with Given(cliapp, 'jwt --help'): assert status == 0 assert stderr == '' when('jwt create \'{"foo": "bar"}\'') print(stderr) assert status == 0 assert len(stdout.split('.')) == 3
def test_versioncli(): cliapp = CLIApplication('foo', 'tests.test_builtincli_version:app.climain') with Given(cliapp, '--version'): assert status == 0 assert stdout.strip() == __version__ assert stderr == '' when('') assert status == 0 assert stderr == ''
def test_configuration_encrypt(): with Given(app, 'configuration encrypt', stdin=b'abc'): assert stderr == b'' assert status == 0 assert stdout.startswith(b'#enc') binary = stdout.proxied_object when('configuration decrypt', stdin=binary) assert stderr == b'' assert status == 0 assert stdout == b'abc'
def test_json(): with Given(app, 'example.com'): assert status == 0 assert stdout == 'example.com' when('-c json example.com post {foo: "bar"}') assert status == 0 assert stdout == \ '-XPOST --data \'{"foo": "bar"}\' ' \ '-H"Content-Type: application/json" ' \ 'example.com'
def test_environ(): with Given(app, environ={'bar': 'baz'}): assert stdout == 'bar: baz\n' when(environ=given - 'bar') assert stdout == '\n' when(environ=given + {'qux': 'quux'}) assert stdout == 'bar: baz qux: quux\n' when(environ=given | {'bar': 'quux'}) assert stdout == 'bar: quux\n'
def test_signal(): with Given(app, nowait=True) as s: # Wait some moments time.sleep(1) s.wait('bar') assert stdout == 'bar\n' assert status == 0 when(stdin='baz') s.wait() assert stdout == 'baz\n' assert status == 0
def test_jwt(): with Given(app, 'jwt create'): assert stderr == '' assert status == 0 assert len(stdout) > 10 when(given + '\'{"foo": 1}\'') assert stderr == '' assert status == 0 header, payload, signature = stdout.encode().split(b'.') payload = base64.urlsafe_b64decode(payload) assert json.loads(payload) == {'foo': 1}
def test_configuration_dump(): with Given(app, 'configuration dump'): assert stderr == '' assert status == 0 assert len(stdout.proxied_object) > 100 filename = tempfile.mktemp() when(given + filename) assert stderr == '' assert status == 0 assert path.exists(filename)
def test_querystring(): with Given(app, 'example.com'): assert status == 0 assert stdout == 'example.com' when(given + 'POST ?foo=bar') assert status == 0 assert stdout == '-XPOST example.com?foo=bar' when(given + 'POST ?foo=bar ?baz=qux') assert status == 0 assert stdout == '-XPOST example.com?foo=bar&baz=qux'
def test_getdb(): with Given(apptextbot, ' getdb -language '): when(given + 'english') assert stdout.endswith('(\'en_core_web_sm\')\n') when(given + 'spanish') assert stdout.endswith('(\'es_core_news_sm\')\n') when(given + 'german') assert stdout.endswith('(\'de_core_news_sm\')\n') when(given + 'french') assert stdout.endswith('(\'fr_core_news_sm\')\n') when(given + 'multi-language') assert stdout.endswith('(\'xx_ent_wiki_sm\')\n') when(given + 'foo') assert stdout.endswith('We dont find your choice in our database\n') with Given(apptextbot, ' getdb -list'): assert stdout.endswith((', '.join(list(languages.keys()))) + '\n')
def test_applicationcli(): cliapp = CLIApplication('example', 'tests.test_cli:app.climain') with Given(cliapp, 'db --help'): assert status == 0 assert stderr == '' when('db drop') when('db create') assert status == 0 assert stderr == '' when('db drop') assert status == 0 assert stderr == ''
def test_basic_pipeline(): app = Application('foo', 'tests.test_pipeline:foo') with Given(app): assert status == 0 assert stdout == '' assert stderr == '' when(stdin='bar') assert status == 0 assert stdout == f'input: bar\n' when(stdin='bad') assert status == 1 assert stderr == f'error: bad\n'
def test_arguments(): with Given(app, ['bar']): assert stdout == 'foo bar\n' assert status == 0 when(given - 'bar') assert stdout == 'foo\n' when(given + 'baz') assert stdout == 'foo bar baz\n' with Given(app, 'bar'): assert stdout == 'foo bar\n' when('baz') assert stdout == 'foo baz\n' # Given without arguments with Given(app): # Then, when with the given when(given + 'qux') assert stdout == 'foo qux\n'
def test_applicationcli_default(): cliapp = CLIApplication('foo', 'tests.test_applicationcli:app.climain') with Given(cliapp, '--help'): assert status == 0 when('foo') assert status == 0 assert stdout == 'foo\n' with tempfile.NamedTemporaryFile() as f: f.write(b'title: bar') f.flush() when(f'--configuration-file {f.name} foo') assert status == 0 assert stdout == 'bar\n'
def test_signal(): with Given(app, nowait=True) as s: # Wait some moments time.sleep(1) s.kill() s.wait() assert stdout == 'Signal received: 15\n' assert status == -15 when() time.sleep(1) s.kill(signal.SIGINT) s.wait() assert stdout == 'Signal received: 2\n' assert status == -2
def test_simple(): app = Application('foo', 'tests.test_simple:Foo.quickstart') with Given(app): assert stderr == '' assert stdout == 'foo done\n' assert status == 0 when(['-h']) assert status == 0 assert stderr == '' assert stdout == EXPECTED_HELP when(['--invalid']) assert status == 2 assert stderr == EXPECTED_USAGE assert stdout == ''
def test_appcli_worker_start(db): session = db() task = WorkerTask() session.add(task) session.commit() with Given(app, 'worker start', nowait=True): time.sleep(2) story.kill() story.wait() assert status == -15 when(given + '--gap 1') time.sleep(2) story.kill() story.wait() assert status == -15
def test_multipart(): with Given(app, 'example.com'): assert status == 0 assert stdout == 'example.com' when('example.com post [email protected]') assert status == 0 assert stdout == \ '-XPOST [email protected] example.com' when(given + 'POST foo=bar') assert status == 0 assert stdout == '-XPOST -Ffoo=bar example.com' when(given + 'POST foo=bar baz=qux') assert status == 0 assert stdout == '-XPOST -Ffoo=bar -Fbaz=qux example.com'
def test_appcli_mule_start(db): session = db() task = WorkerTask() session.add(task) session.commit() with Given(app, 'mule start', nowait=True): time.sleep(2) story.kill() story.wait() assert status == -15 when(given + '--query-interval 1') time.sleep(2) story.kill() story.wait() assert status == -15
def test_bash_autocompletion_virtualenv(): app = Application('foo', 'tests.test_completion:Foo.quickstart') with tempfile.TemporaryDirectory() as venvdir: os.mkdir(path.join(venvdir, 'bin')) with Given(app, ['completion'], environ={'VIRTUAL_ENV': venvdir}): assert stdout == EXPECTED_HELP assert status == 0 when(given + ['install', '-s']) assert stderr == 'The -s/--system-wide flag can not be used ' \ 'within virtualenv\n' assert status == 1 when(['completion', 'install']) when(given + ['uninstall', '-s']) assert stderr == 'The -s/--system-wide flag can not be used ' \ 'within virtualenv\n' assert status == 1 when(['completion', 'uninstall'])
def test_contenttypes(): with Given(app, 'example.com'): assert status == 0 assert stdout == 'example.com' when(given + '-cplain') assert status == 0 assert stdout == '-H"Content-Type: text/plain" example.com' when(given + '-cjson') assert status == 0 assert stdout == '-H"Content-Type: application/json" example.com' when(given + '-cmultipart') assert status == 0 assert stdout == '-H"Content-Type: multipart/form-data" example.com' when(given + '-curlencoded') assert status == 0 assert stdout == \ '-H"Content-Type: application/x-www-form-urlencoded" example.com'