Added API tests
This commit is contained in:
parent
1b6f7d1717
commit
cdd8dc4615
4 changed files with 45 additions and 10 deletions
19
api/api.py
19
api/api.py
|
|
@ -6,16 +6,17 @@ import falcon
|
|||
ELIXIR_DIR = os.path.dirname(__file__) + '/..'
|
||||
|
||||
def build_query(env, project):
|
||||
basedir = env['LXR_PROJ_DIR']
|
||||
os.environ['LXR_DATA_DIR'] = basedir + '/' + project + '/data'
|
||||
os.environ['LXR_REPO_DIR'] = basedir + '/' + project + '/repo'
|
||||
if 'LXR_DATA_DIR' not in os.environ or 'LXR_REPO_DIR' not in os.environ:
|
||||
basedir = env['LXR_PROJ_DIR']
|
||||
os.environ['LXR_DATA_DIR']= basedir + '/' + project + '/data'
|
||||
os.environ['LXR_REPO_DIR'] = basedir + '/' + project + '/repo'
|
||||
|
||||
import sys
|
||||
sys.path = [ ELIXIR_DIR ] + sys.path
|
||||
import query
|
||||
return query.query
|
||||
|
||||
class IdentResource:
|
||||
class IdentGetter:
|
||||
def on_get(self, req, resp, project, ident):
|
||||
query = build_query(req.env, project)
|
||||
if 'version' in req.params:
|
||||
|
|
@ -34,8 +35,10 @@ class IdentResource:
|
|||
})
|
||||
resp.status = falcon.HTTP_200
|
||||
|
||||
application = falcon.API()
|
||||
def create_ident_getter():
|
||||
application = falcon.API()
|
||||
idents = IdentGetter()
|
||||
application.add_route('/ident/{project}/{ident}', idents)
|
||||
return application
|
||||
|
||||
idents = IdentResource()
|
||||
|
||||
application.add_route('/ident/{project}/{ident}', idents)
|
||||
application = create_ident_getter()
|
||||
|
|
|
|||
|
|
@ -113,6 +113,10 @@ run_produces_ok('file query (existent), .h',
|
|||
[qr{i2c-core\.h}, qr{\bWe\b}],
|
||||
MUST_SUCCEED);
|
||||
|
||||
# Test the api using pytest. Prints the test results.
|
||||
|
||||
run_produces_ok('api pytest suite', ["pytest", "-v"], [], MUST_SUCCEED, 1);
|
||||
|
||||
#system('bash'); # Uncomment this if you want to interact with the test repo
|
||||
|
||||
done_testing;
|
||||
|
|
|
|||
|
|
@ -145,17 +145,18 @@ Run a program and check whether it produces expected output.
|
|||
Usage:
|
||||
|
||||
run_produces_ok($desc, \@program_and_args, \@expected_regexes,
|
||||
<optional> $mustSucceed)
|
||||
<optional> $mustSucceed, <optional> $printOutput)
|
||||
|
||||
The test passes if each regex in C<@expected_regexes> matches at least one
|
||||
line in the output of C<@program_and_args>, and if each C<< { not => regex } >>
|
||||
in C<@expected_regexes> is NOT found in that output.
|
||||
If C<$mustSucceed> is true, also tests for exit status 0 and empty stderr.
|
||||
If C<$printOutput> is true, prints the output of C<@program_and_args>.
|
||||
|
||||
=cut
|
||||
|
||||
sub run_produces_ok {
|
||||
my ($desc, $lrProgram, $lrRegexes, $mustSucceed) = @_;
|
||||
my ($desc, $lrProgram, $lrRegexes, $mustSucceed, $printOutput) = @_;
|
||||
|
||||
# Run program and capture stdout and stderr
|
||||
my ($in , $out, $err); # Filehandles
|
||||
|
|
@ -187,6 +188,10 @@ sub run_produces_ok {
|
|||
waitpid $pid, 0;
|
||||
my $exit_status = $? >> 8;
|
||||
|
||||
if ($printOutput) {
|
||||
diag "@outlines";
|
||||
}
|
||||
|
||||
# Basic checks
|
||||
if($mustSucceed) {
|
||||
cmp_ok($exit_status, '==', 0, "$desc: exit status 0");
|
||||
|
|
|
|||
23
t/api_test.py
Normal file
23
t/api_test.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
|
||||
from falcon import testing
|
||||
|
||||
api_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..','api'))
|
||||
sys.path.insert(0, api_dir)
|
||||
|
||||
from api import create_ident_getter
|
||||
|
||||
class APITest(testing.TestCase):
|
||||
def setUp(self):
|
||||
super(APITest, self).setUp()
|
||||
|
||||
self.app = create_ident_getter()
|
||||
|
||||
class TestMyApp(APITest):
|
||||
def test_identifier_not_found(self):
|
||||
result = self.simulate_get('/ident/tree/SOME_NONEXISTENT_IDENTIFIER', query_string="version=latest")
|
||||
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertEqual(result.json, {'definitions': [], 'references':[]})
|
||||
Loading…
Reference in a new issue