Skip to content

Commit 52615e7

Browse files
author
Giuseppe Lumia
committed
Add integration tests
1 parent 3e130d8 commit 52615e7

File tree

6 files changed

+520
-2
lines changed

6 files changed

+520
-2
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- Add coverage report generation <--- OK!
1919
- Packaging (poetry!) <--- OK!
2020
- Add linters (flake8, black, isort, ~~bandit~~, ~~safety~~) <--- OK!
21-
- Add integration tests
21+
- Add integration tests <--- OK!
2222
- Write a decent readme - show what we can do with pylaprof :)
2323
- Set-up CI/CD (Github Actions, package upload on PyPI).
2424

poetry.lock

Lines changed: 435 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pytest = "^6.2.5"
2323
coverage = {version = "^6.1.2", extras = ["toml"]}
2424
freezegun = "^1.1.0"
2525
boto3 = "^1.20.6"
26+
moto = {extras = ["s3"], version = "^2.2.15"}
2627

2728
[tool.poetry.scripts]
2829
pylaprof-merge = "scripts.merge:main"

tests/__init__.py

Whitespace-only changes.

tests/sleepy.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Functions used in integration tests.
2+
3+
Be careful in adding stuff here as some tests rely on code appearing at certain line
4+
numbers.
5+
"""
6+
7+
import time
8+
9+
# reserved for future imports
10+
# reserved for future imports
11+
# reserved for future imports
12+
# reserved for future imports
13+
# reserved for future imports
14+
# reserved for future imports
15+
# reserved for future imports
16+
# reserved for future imports
17+
# reserved for future imports
18+
# reserved for future imports
19+
# reserved for future imports
20+
# reserved for future imports
21+
# reserved for future imports
22+
# reserved for future imports
23+
# reserved for future imports
24+
# reserved for future imports
25+
26+
27+
def sleepy(t):
28+
time.sleep(t)

tests/test_integration.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import re
2+
3+
import boto3
4+
import pytest
5+
from moto import mock_s3
6+
7+
from pylaprof import FS, S3, Profiler
8+
9+
from .sleepy import sleepy
10+
11+
12+
def test_profiler_with_fs_storer(tmpcwd):
13+
"""Check profiler with FS storer.
14+
15+
We set a period of 0.01s and run `sleepy` for 0.105s, so we should get a report with
16+
10 hits of this function.
17+
"""
18+
with Profiler(period=0.01, storer=FS(path=lambda: "report.txt")):
19+
sleepy(0.105)
20+
21+
regex = re.compile(r".*;sleepy \(\/[^)]+\/sleepy\.py:28\) 10")
22+
with open("report.txt", "r") as fp:
23+
report = fp.read()
24+
for line in report.split("\n"):
25+
if regex.match(line):
26+
return # Ok!
27+
else:
28+
pytest.fail("something is wrong with the report:\n" + report)
29+
30+
31+
@mock_s3
32+
def test_profiler_with_s3_storer():
33+
"""Check profiler with S3 storer.
34+
35+
We set a period of 0.01s and run `sleepy` for 0.105s, so we should get a report with
36+
10 hits of this function.
37+
"""
38+
boto3.resource("s3").Bucket("pylaprof").create()
39+
40+
with Profiler(period=0.01, storer=S3(key=lambda: "report.txt")):
41+
sleepy(0.105)
42+
43+
regex = re.compile(r".*;sleepy \(\/[^)]+\/sleepy\.py:28\) 10")
44+
report = (
45+
boto3.resource("s3")
46+
.Object("pylaprof", "report.txt")
47+
.get()["Body"]
48+
.read()
49+
.decode("utf-8")
50+
)
51+
for line in report.split("\n"):
52+
if regex.match(line):
53+
return # Ok!
54+
else:
55+
pytest.fail("something is wrong with the report:\n" + report)

0 commit comments

Comments
 (0)