Skip to content

Commit

Permalink
Proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
EfronC committed Jan 14, 2025
1 parent 3c13881 commit b9aacb9
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 0 deletions.
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.
53 changes: 53 additions & 0 deletions tests/python/factories/archivefactory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import factory
import os
import zipfile

from factories.models.archives import Archive

def generate_file(obj):
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

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
33 changes: 33 additions & 0 deletions tests/python/test_archive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
import requests
import redis
import shutil
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

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

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

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

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

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:

0 comments on commit b9aacb9

Please sign in to comment.