Skip to content

Commit 9cfd484

Browse files
committed
fix: Do not send logout request if token is not set
1 parent 2eeaeb8 commit 9cfd484

18 files changed

Lines changed: 331 additions & 171 deletions

‎BUILD‎

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,97 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
load("@rules_python//python:defs.bzl", "py_library")
3+
load("@rules_python//python:defs.bzl", "py_test")
4+
5+
load("@knora_py_deps//:requirements.bzl", "requirement")
6+
17
py_library(
28
name = "knora",
39
srcs = glob(["knora/knora.py"]),
10+
deps = [
11+
requirement("rdflib"),
12+
requirement("lxml"),
13+
requirement("validators"),
14+
requirement("requests"),
15+
requirement("jsonschema"),
16+
requirement("click"),
17+
requirement("rfc3987"),
18+
requirement("pprint"),
19+
]
420
)
521

622
py_binary(
723
name = "knora_create_ontology",
824
srcs = ["knora/create_ontology.py"],
9-
deps = ["knora"]
25+
deps = [
26+
"knora",
27+
requirement("jsonschema"),
28+
requirement("pprint"),
29+
]
1030
)
1131

1232
py_binary(
13-
name = "knora-xml-import",
33+
name = "knora_xml_import",
1434
srcs = ["knora/xml2knora.py"],
15-
deps = ["knora"]
35+
deps = [
36+
"knora",
37+
requirement("lxml"),
38+
requirement("pprint"),
39+
],
1640
)
1741

1842
py_binary(
19-
name = "knora-reset-triplestore",
43+
name = "knora_reset_triplestore",
2044
srcs = ["knora/reset_triplestore.py"],
21-
deps = ["knora"]
45+
deps = [":knora"],
2246
)
2347

2448
py_binary(
2549
name = "knoractl",
2650
srcs = ["knora/knoractl.py"],
27-
deps = ["knora"]
51+
deps = [":knora"],
52+
)
53+
54+
py_test(
55+
name = "test_create_ontology",
56+
srcs = ["test/test_create_ontology.py"],
57+
deps = [":knora"],
58+
)
59+
60+
py_test(
61+
name = "test_create_resource",
62+
srcs = ["test/test_create_resource.py"],
63+
deps = [
64+
":knora",
65+
requirement("pprint"),
66+
],
2867
)
2968

3069
py_test(
3170
name = "test_knora",
32-
srcs = ["tests/test_knora.py"],
33-
deps = ["knora"],
71+
srcs = ["test/test_knora.py"],
72+
deps = [":knora"],
3473
)
3574

3675
test_suite(
3776
name = "all_tests",
3877
tests = [
78+
"test_create_ontology",
79+
"test_create_resource",
3980
"test_knora",
4081
],
4182
)
83+
84+
py_library(
85+
name = "test_lib",
86+
srcs = glob(["test/*.py"]),
87+
deps = [
88+
":knora",
89+
],
90+
)
91+
92+
py_binary(
93+
name = "run_tests",
94+
main = "test/run.py",
95+
srcs = ["test/run.py"],
96+
deps = ["test_lib"],
97+
)

‎Makefile‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ serve-docs: ## serve docs for local viewing
1616
publish-docs: ## build and publish docs to Github Pages
1717
mkdocs gh-deploy
1818

19+
.PHONY: install-requirements
20+
install-requirements: ## install requirements
21+
pip3 install -r requirements.txt
22+
23+
.PHONY: test
1924
test: ## runs all tests
20-
python3 -m pytest
25+
cd test && python3 -m unittest
2126

2227
clean: ## cleans the project directory
2328
rm -rf dist/ build/ knora.egg-info/ .pytest_cache/ site/

‎README.md‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ $ python3 setup.py install
3333
The project contains a Makefile defining management tasks. Please use
3434
`make help` to see what is available.
3535

36+
## Testing
37+
38+
```bash
39+
$ make install-requirements
40+
$ make test
41+
```
42+
3643
## Publishing to PyPi
3744

3845
Generate distribution package. Make sure you have the latest versions of `setuptools` and `wheel` installed:
@@ -70,15 +77,7 @@ $ python3 -m twine upload dist/*
7077
For local development:
7178

7279
```bash
73-
$ python3 setup.py develop
74-
```
75-
76-
## Testing
77-
78-
```bash
79-
$ pip3 install pytest
80-
$ pip3 install --editable .
81-
$ pytest
80+
$ python3 setup.py --editable .
8281
```
8382

8483
## Requirements

‎WORKSPACE‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
workspace(name = "knora_py")
2+
3+
# use bazel federation (set of rule versions known to work well together)
4+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
5+
http_archive(
6+
name = "bazel_federation",
7+
url = "https://github.com/bazelbuild/bazel-federation/releases/download/0.0.1/bazel_federation-0.0.1.tar.gz",
8+
sha256 = "506dfbfd74ade486ac077113f48d16835fdf6e343e1d4741552b450cfc2efb53",
9+
)
10+
11+
# load the initializer methods for all the rules we want to use in this workspace
12+
load("@bazel_federation//:repositories.bzl",
13+
"rules_python",
14+
)
15+
16+
# run any rule specific setups
17+
rules_python()
18+
load("@bazel_federation//setup:rules_python.bzl", "rules_python_setup")
19+
rules_python_setup()
20+
21+
# load py_repositories from rules_python
22+
load("@rules_python//python:repositories.bzl", "py_repositories")
23+
py_repositories()
24+
25+
# load pip_repositories from rules_python
26+
load("@rules_python//python:pip.bzl", "pip_repositories")
27+
pip_repositories()
28+
29+
# allows to use requirements.txt for loading the dependencies
30+
load("@rules_python//python:pip.bzl", "pip_import")
31+
32+
# This rule translates the specified requirements.txt into
33+
# @knora_py_deps//:requirements.bzl, which itself exposes a pip_install method.
34+
pip_import(
35+
name = "knora_py_deps",
36+
requirements = "//:requirements.txt",
37+
)
38+
39+
# Load the pip_install symbol for knora_py_deps, and create the dependencies'
40+
# repositories.
41+
load("@knora_py_deps//:requirements.bzl", "pip_install")
42+
pip_install()

‎knora/knora.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def __init__(self, server: str, prefixes: Dict[str, str] = None):
116116
"""
117117
self.server = server
118118
self.prefixes = prefixes
119+
self.token = None
119120

120121
def login(self, email: str, password: str):
121122
"""
@@ -142,12 +143,13 @@ def get_token(self):
142143
return self.token
143144

144145
def logout(self):
145-
req = requests.delete(
146-
self.server + '/v2/authentication',
147-
headers={'Authorization': 'Bearer ' + self.token}
148-
)
149-
self.on_api_error(req)
150-
self.token = None
146+
if self.token is not None:
147+
req = requests.delete(
148+
self.server + '/v2/authentication',
149+
headers={'Authorization': 'Bearer ' + self.token}
150+
)
151+
self.on_api_error(req)
152+
self.token = None
151153

152154
def __del__(self):
153155
self.logout()
@@ -245,7 +247,7 @@ def create_project(
245247
project['logo'] = logo
246248

247249
jsondata = json.dumps(project)
248-
print(jsondata)
250+
# print(jsondata)
249251

250252
req = requests.post(self.server + "/admin/projects",
251253
headers={'Content-Type': 'application/json; charset=UTF-8',

‎requirements.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ twine
66
pytest
77
mkdocs==1.0.4
88
mkdocs-material
9+
rdflib
10+
lxml
11+
validators
12+
requests
13+
jsonschema
14+
click
15+
rfc3987
16+
pprint

‎setup.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
'jsonschema',
2828
'click',
2929
'rfc3987',
30-
'pprint'
30+
'pprint',
3131
],
3232
entry_points={
3333
'console_scripts': [
3434
'knora-create-ontology=knora.create_ontology:main',
3535
'knora-xml-import=knora.xml2knora:main',
3636
'knora-reset-triplestore=knora.reset_triplestore:main',
37-
'knoractl=knoractl:main'
37+
'knoractl=knoractl:main',
3838
],
3939
},
4040
include_package_data=True,
41-
zip_safe=False
41+
zip_safe=False,
4242
)
File renamed without changes.

‎test/run.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Universal launcher for unit tests"""
2+
3+
import argparse
4+
import logging
5+
import os
6+
import sys
7+
import unittest
8+
9+
10+
def main():
11+
"""Parse args, collect tests and run them"""
12+
# Disable *.pyc files
13+
sys.dont_write_bytecode = True
14+
15+
# Add ".." to module search path
16+
cur_dir = os.path.dirname(os.path.realpath(__file__))
17+
top_dir = os.path.abspath(os.path.join(cur_dir, os.pardir))
18+
sys.path.append(top_dir)
19+
20+
# Parse command line arguments
21+
parser = argparse.ArgumentParser(description=__doc__)
22+
parser.add_argument("-v", "--verbose", action="count", default=0,
23+
help="verbosity level, use: [-v | -vv | -vvv]")
24+
parser.add_argument("-s", "--start-directory", default=None,
25+
help="directory to start discovery")
26+
parser.add_argument("-p", "--pattern", default="test*.py",
27+
help="pattern to match test files ('test*.py' default)")
28+
parser.add_argument("test", nargs="*",
29+
help="test specs (e.g. module.TestCase.test_func)")
30+
args = parser.parse_args()
31+
32+
if not args.start_directory:
33+
args.start_directory = cur_dir
34+
35+
if args.verbose > 2:
36+
logging.basicConfig(level=logging.DEBUG, format="DEBUG: %(message)s")
37+
38+
loader = unittest.TestLoader()
39+
if args.test:
40+
# Add particular tests
41+
for test in args.test:
42+
suite = unittest.TestSuite()
43+
suite.addTests(loader.loadTestsFromName(test))
44+
else:
45+
# Find all tests
46+
suite = loader.discover(args.start_directory, args.pattern)
47+
48+
runner = unittest.TextTestRunner(verbosity=args.verbose)
49+
result = runner.run(suite)
50+
return result.wasSuccessful()
51+
52+
53+
if __name__ == "__main__":
54+
# NOTE: True(success) -> 0, False(fail) -> 1
55+
exit(not main())

0 commit comments

Comments
 (0)