-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add basic integration test (#1)
* 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
1 parent
1eec08a
commit 2eefaab
Showing
6 changed files
with
285 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |