Merge pull request #95 from carmeli-tamir/api_test

Added API tests
This commit is contained in:
Michael Opdenacker 2020-03-31 16:52:16 +02:00 committed by GitHub
commit 588e5db181
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 10 deletions

View file

@ -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()

36
t/200-api.t Normal file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env perl
# 200-api.t: Test elixir's REST api against the files in tree/ .
#
# Copyright (c) 2020 Tamir Carmeli, <carmeli.tamir@gmail.com>.
#
# Elixir is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Elixir is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# # You should have received a copy of the GNU Affero General Public License
# along with Elixir. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# This file uses core Perl modules only.
use FindBin '$Bin';
use lib $Bin;
use Test::More;
use TestEnvironment;
use TestHelpers;
my $tenv = TestEnvironment->new;
$tenv->build_repo(sibling_abs_path('tree'))->build_db->update_env;
# Test the api using pytest. Prints the test results
run_produces_ok('api pytest suite', ["pytest", "-v"], [], MUST_SUCCEED, 1);
done_testing;

View file

@ -146,17 +146,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
@ -188,6 +189,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
View 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':[]})