Spaces:
Sleeping
Sleeping
Clement Vachet
commited on
Commit
·
ca264b8
1
Parent(s):
9ecca49
Improve code based on pylint and black suggestions
Browse files- tests/test_classifier.py +13 -6
- tests/test_lambda.py +15 -6
tests/test_classifier.py
CHANGED
@@ -1,24 +1,31 @@
|
|
|
|
|
|
|
|
|
|
1 |
import pytest
|
2 |
from classification.classifier import Classifier
|
3 |
|
|
|
4 |
@pytest.fixture
|
5 |
def setup_pipeline():
|
|
|
6 |
pipeline = Classifier()
|
7 |
pipeline.train_and_save()
|
8 |
return pipeline
|
9 |
|
|
|
10 |
@pytest.fixture
|
11 |
def requests():
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
[6.1, 2.8, 4.7, 1.2]
|
16 |
-
]
|
17 |
-
}
|
18 |
|
19 |
@pytest.fixture
|
20 |
def response():
|
|
|
21 |
return ["virginica", "versicolor"]
|
22 |
|
|
|
23 |
def test_response(setup_pipeline, requests, response):
|
|
|
24 |
assert response == setup_pipeline.load_and_test(requests["features"])["predictions"]
|
|
|
1 |
+
"""
|
2 |
+
Testing Classifier module
|
3 |
+
"""
|
4 |
+
|
5 |
import pytest
|
6 |
from classification.classifier import Classifier
|
7 |
|
8 |
+
|
9 |
@pytest.fixture
|
10 |
def setup_pipeline():
|
11 |
+
"""Setup classifier pipeline - training classifier and saving model"""
|
12 |
pipeline = Classifier()
|
13 |
pipeline.train_and_save()
|
14 |
return pipeline
|
15 |
|
16 |
+
|
17 |
@pytest.fixture
|
18 |
def requests():
|
19 |
+
"""Example dataset"""
|
20 |
+
return {"features": [[6.5, 3.0, 5.8, 2.2], [6.1, 2.8, 4.7, 1.2]]}
|
21 |
+
|
|
|
|
|
|
|
22 |
|
23 |
@pytest.fixture
|
24 |
def response():
|
25 |
+
"""Ground truth response from classifier"""
|
26 |
return ["virginica", "versicolor"]
|
27 |
|
28 |
+
|
29 |
def test_response(setup_pipeline, requests, response):
|
30 |
+
"""Tests if classifier returns correct prediction"""
|
31 |
assert response == setup_pipeline.load_and_test(requests["features"])["predictions"]
|
tests/test_lambda.py
CHANGED
@@ -1,33 +1,42 @@
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
-
import pytest
|
4 |
import json
|
|
|
|
|
|
|
|
|
5 |
|
6 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
7 |
parent_dir = os.path.dirname(current_dir)
|
8 |
sys.path.insert(0, os.path.dirname(parent_dir))
|
9 |
|
10 |
-
from lambda_function import lambda_handler
|
11 |
|
12 |
@pytest.fixture
|
13 |
def event():
|
14 |
-
|
15 |
-
|
16 |
-
}
|
17 |
return json_event
|
18 |
|
|
|
19 |
@pytest.fixture
|
20 |
def context():
|
|
|
21 |
return None
|
22 |
|
|
|
23 |
@pytest.fixture
|
24 |
def response_prediction():
|
|
|
25 |
return ["virginica", "versicolor"]
|
26 |
|
27 |
|
28 |
def test_lambda_handler(event, context, response_prediction):
|
|
|
29 |
lambda_response = lambda_handler(event, context)
|
30 |
|
31 |
assert lambda_response["statusCode"] == 200
|
32 |
assert json.loads(lambda_response["body"])["predictions"] == response_prediction
|
33 |
-
|
|
|
1 |
+
"""
|
2 |
+
Testing Lambda handler
|
3 |
+
"""
|
4 |
+
|
5 |
import os
|
6 |
import sys
|
|
|
7 |
import json
|
8 |
+
import pytest
|
9 |
+
|
10 |
+
from lambda_function import lambda_handler
|
11 |
+
|
12 |
|
13 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
14 |
parent_dir = os.path.dirname(current_dir)
|
15 |
sys.path.insert(0, os.path.dirname(parent_dir))
|
16 |
|
|
|
17 |
|
18 |
@pytest.fixture
|
19 |
def event():
|
20 |
+
"""Example json event"""
|
21 |
+
json_event = {"features": [[6.5, 3.0, 5.8, 2.2], [6.1, 2.8, 4.7, 1.2]]}
|
|
|
22 |
return json_event
|
23 |
|
24 |
+
|
25 |
@pytest.fixture
|
26 |
def context():
|
27 |
+
"""Example context"""
|
28 |
return None
|
29 |
|
30 |
+
|
31 |
@pytest.fixture
|
32 |
def response_prediction():
|
33 |
+
"""Ground truth - API response"""
|
34 |
return ["virginica", "versicolor"]
|
35 |
|
36 |
|
37 |
def test_lambda_handler(event, context, response_prediction):
|
38 |
+
"""Tests lambda handler"""
|
39 |
lambda_response = lambda_handler(event, context)
|
40 |
|
41 |
assert lambda_response["statusCode"] == 200
|
42 |
assert json.loads(lambda_response["body"])["predictions"] == response_prediction
|
|