Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe Lumia committed Nov 18, 2021
1 parent 3e130d8 commit 52615e7
Show file tree
Hide file tree
Showing 6 changed files with 520 additions and 2 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- Add coverage report generation <--- OK!
- Packaging (poetry!) <--- OK!
- Add linters (flake8, black, isort, ~~bandit~~, ~~safety~~) <--- OK!
- Add integration tests
- Add integration tests <--- OK!
- Write a decent readme - show what we can do with pylaprof :)
- Set-up CI/CD (Github Actions, package upload on PyPI).

Expand Down
436 changes: 435 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pytest = "^6.2.5"
coverage = {version = "^6.1.2", extras = ["toml"]}
freezegun = "^1.1.0"
boto3 = "^1.20.6"
moto = {extras = ["s3"], version = "^2.2.15"}

[tool.poetry.scripts]
pylaprof-merge = "scripts.merge:main"
Expand Down
Empty file added tests/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions tests/sleepy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Functions used in integration tests.
Be careful in adding stuff here as some tests rely on code appearing at certain line
numbers.
"""

import time

# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports
# reserved for future imports


def sleepy(t):
time.sleep(t)
55 changes: 55 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import re

import boto3
import pytest
from moto import mock_s3

from pylaprof import FS, S3, Profiler

from .sleepy import sleepy


def test_profiler_with_fs_storer(tmpcwd):
"""Check profiler with FS storer.
We set a period of 0.01s and run `sleepy` for 0.105s, so we should get a report with
10 hits of this function.
"""
with Profiler(period=0.01, storer=FS(path=lambda: "report.txt")):
sleepy(0.105)

regex = re.compile(r".*;sleepy \(\/[^)]+\/sleepy\.py:28\) 10")
with open("report.txt", "r") as fp:
report = fp.read()
for line in report.split("\n"):
if regex.match(line):
return # Ok!
else:
pytest.fail("something is wrong with the report:\n" + report)


@mock_s3
def test_profiler_with_s3_storer():
"""Check profiler with S3 storer.
We set a period of 0.01s and run `sleepy` for 0.105s, so we should get a report with
10 hits of this function.
"""
boto3.resource("s3").Bucket("pylaprof").create()

with Profiler(period=0.01, storer=S3(key=lambda: "report.txt")):
sleepy(0.105)

regex = re.compile(r".*;sleepy \(\/[^)]+\/sleepy\.py:28\) 10")
report = (
boto3.resource("s3")
.Object("pylaprof", "report.txt")
.get()["Body"]
.read()
.decode("utf-8")
)
for line in report.split("\n"):
if regex.match(line):
return # Ok!
else:
pytest.fail("something is wrong with the report:\n" + report)

0 comments on commit 52615e7

Please sign in to comment.