Skip to content

Commit

Permalink
test: add basic integration test (#1)
Browse files Browse the repository at this point in the history
* chore: run integration tests on ci

* chore: update env for python development

* test: add initial integration test

* chore: update gitignore

* test: use subprocess

* test: use temporary directory for saving files

* test: creste test utils file

* chore: specify python version for ci

* test: simplify temp directory creation

* chore: order imports
  • Loading branch information
michalczmiel authored Jan 23, 2024
1 parent 1eec08a commit 2eefaab
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ trim_trailing_whitespace = true
indent_style = tab
indent_size = 4
trim_trailing_whitespace = true

[*.py]
indent_style = space
indent_size = 4
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
- name: Unit tests
run: go test -v ./...
- name: Integration tests
run: python -m unittest discover -v -s test
181 changes: 178 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Created by https://www.toptal.com/developers/gitignore/api/macos,visualstudiocode,go
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,visualstudiocode,go
# Created by https://www.toptal.com/developers/gitignore/api/go,macos,visualstudiocode,python
# Edit at https://www.toptal.com/developers/gitignore?templates=go,macos,visualstudiocode,python

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
Expand Down Expand Up @@ -57,6 +57,177 @@ Temporary Items
# iCloud generated files
*.icloud

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

### Python Patch ###
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
poetry.toml

# ruff
.ruff_cache/

# LSP config files
pyrightconfig.json

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
Expand All @@ -76,4 +247,8 @@ Temporary Items
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/macos,visualstudiocode,go
# Custom

images/

# End of https://www.toptal.com/developers/gitignore/api/go,macos,visualstudiocode,python
44 changes: 44 additions & 0 deletions test/test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import subprocess
import unittest
from tempfile import TemporaryDirectory

from utils import Server, does_directory_contains_file


class BigIntegrationTest(unittest.TestCase):
server: Server
directory: TemporaryDirectory

def setUp(self) -> None:
self.server = Server()
self.server.start()
self.directory = TemporaryDirectory()

def test_download_images_from_html_and_save_to_directory(self):
# when the server is running, run the program to download images
subprocess.run(
[
"go",
"run",
"main.go",
"html",
self.server.url,
"-d",
self.directory.name,
],
check=True,
)

# then check if the images were downloaded to the file system
self.assertTrue(does_directory_contains_file(self.directory.name, "300.jpeg"))
self.assertTrue(does_directory_contains_file(self.directory.name, "800.jpeg"))
self.assertTrue(does_directory_contains_file(self.directory.name, "1350.jpeg"))


def tearDown(self) -> None:
self.server.stop()
self.directory.cleanup()


if __name__ == "__main__":
unittest.main()
13 changes: 13 additions & 0 deletions test/testdata/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Big Image Getter Static Site</title>
</head>
<body>
<img src="https://picsum.photos/200/300" />
<img src="https://picsum.photos/1200/800" />
<img src="https://picsum.photos/2400/1350" />
</body>
</html>
41 changes: 41 additions & 0 deletions test/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import http.server
import os
from threading import Thread
from typing import Final

PORT: Final[int] = 8080
DIRECTORY: Final[str] = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "testdata/"
)


def does_directory_contains_file(path: str, file: str) -> bool:
"""
Checks if the given directory contains the given file
"""
return os.path.isfile(os.path.join(path, file))


class Server:
"""
A simple HTTP server that serves files from the given directory
"""

class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)

def __init__(self):
self.server = http.server.HTTPServer(("", PORT), self.Handler)

def start(self) -> None:
thread = Thread(target=self.server.serve_forever)
thread.start()

def stop(self) -> None:
thread = Thread(target=self.server.shutdown)
thread.start()

@property
def url(self) -> str:
return f"http://localhost:{self.server.server_port}"

0 comments on commit 2eefaab

Please sign in to comment.