Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python test environment #1165

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ config.log
autobackup.json
Makefile
oshino

__pycache__/
.pytest_cache/
*.py[cod]
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pytest
pytest-redis
requests
factory_boy
Empty file.
38 changes: 38 additions & 0 deletions tests/python/factories/archivefactory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import factory
import os
import zipfile

from factories.models.archives import Archive

class ArchiveFactory(factory.Factory):
class Meta:
model = Archive

name = factory.Faker("first_name")
tags = "date_added:1736124197"
summary = ""
arcsize = "16532135"
file = factory.LazyAttribute(lambda p: '/home/koyomi/lanraragi/content/test/{}.zip'.format(p.name))
title = factory.LazyAttribute(lambda p: p.name)
isnew = "true"
pagecount = "30"
thumbhash = "ec2a0ca3a3da67a9390889f0910fe494241faa9a"

@factory.post_generation
def generate_file(obj, create, extracted, **kwargs):
if not create:
return

logo_path = "/lanraragi/public/img/logo.png"
content_path = "/lanraragi/content/test"

if not os.path.exists(content_path):
os.makedirs(content_path)

zip_path = os.path.join(content_path, f"{obj.name}.zip")

with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
file_name = os.path.basename(logo_path)
zipf.write(logo_path, file_name)

return "/home/koyomi"+zip_path
Empty file.
13 changes: 13 additions & 0 deletions tests/python/factories/models/archives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import dataclass

@dataclass
class Archive:
name: str
tags: str
summary: str
arcsize: str
file: str
title: str
isnew: str
pagecount: str
thumbhash: str
47 changes: 47 additions & 0 deletions tests/python/test_archive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import unittest
import requests
import redis
import shutil
import base64
import os
from utils.utils import generate_archive_id
from factories.archivefactory import ArchiveFactory

class ArchiveAPITestCase(unittest.TestCase):
def setUp(self):
self.redis_client = redis.Redis(host='redis', port=6379)
self.archivefactory = ArchiveFactory

# Set Apikey
self.redis_client.select(2)
self.redis_client.hset("LRR_CONFIG", "apikey", "lanraragi")
self.redis_client.select(0)

def tearDown(self):
if os.path.exists('/lanraragi/content/test'):
shutil.rmtree('/lanraragi/content/test')
self.redis_client.flushall()

def test_archives_list(self):
arcid = "28697b96f0ac5858be2614ed10ca47742c9522fd" #generate_archive_id()
archive = self.archivefactory.create()
self.redis_client.hset(arcid, mapping=archive.__dict__)

response = requests.get('http://lanraragi:3000/api/archives')
data = response.json()

self.assertEqual(response.status_code, 200)
self.assertEqual(data[0]["arcid"], arcid)
self.assertEqual(data[0]["filename"], archive.name)

def test_plugins_list(self):
bearer = base64.b64encode("lanraragi".encode(encoding='utf-8')).decode('utf-8')
header = {'accept':'application/json', 'authorization': 'Bearer '+bearer}
response = requests.get('http://lanraragi:3000/api/plugins/login', headers=header)
data = response.json()

self.assertEqual(response.status_code, 200)
self.assertEqual(data[0]["author"], "Difegue")

if __name__ == '__main__':
unittest.main()
Empty file added tests/python/utils/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/python/utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import uuid
import hashlib

def generate_archive_id():
base_id = uuid.uuid4().hex
hashid = hashlib.sha1(base_id.encode()).hexdigest()

return hashid
6 changes: 6 additions & 0 deletions tools/build/docker/Dockerfile-python
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3.8

COPY requirements.txt /tests/
RUN pip install -r /tests/requirements.txt

WORKDIR /tests
43 changes: 43 additions & 0 deletions tools/build/docker/docker-compose-testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
# Docker compose file for dev environments.
services:
lanraragi:
build:
dockerfile: tools/build/docker/Dockerfile-dev
context: ../../..
volumes:
- ../../../:/home/koyomi/lanraragi
ports:
- "3000:3000"
environment:
- "LRR_REDIS_ADDRESS=redis:6379"
networks:
- lrr

redis:
image: "docker.io/redis:7"
volumes:
- redis_test_data:/data
networks:
- lrr

python-tests:
build:
dockerfile: tools/build/docker/Dockerfile-python
context: ../../..
volumes:
- ../../../tests/python:/tests
- ../../../:/lanraragi
working_dir: /tests
networks:
- lrr
depends_on:
- lanraragi
- redis
command: ["tail", "-F", "Anything"]

networks:
lrr:

volumes:
redis_test_data:
Loading