Для текущего школьного проекта я и мои друзья разработали небольшое приложение для поиска книг в Python с Django. Фактически мы почти закончили, но есть проблемы с написанием тестов. Будем рады услышать, как вы подойдете к тестам. Они могут быть максимально простыми.
book.py:
from django.db import models
import pdb
class Book:
def __init__(self, book_element):
self.book_information = self.create_book(book_element)
# some instance variabless are missing try fixing it maybe use forms
def create_book(self, book_element):
info = book_element["volumeInfo"]
title = info.get("title")
publisher = info.get("publisher, Nicht vorhanden")
author = info.get("authors, Nicht vorhanden.")
imageTrue = info["readingModes"].get("image")
if imageTrue == True:
image = info["imageLinks"].get("thumbnail")
else:
image = "Nicht vorhanden"
published_Date = info.get("publishedDate, Nicht vorhanden.")
page_Count = info.get("pageCount, Nicht vorhanden.")
categorie = info.get("categories, Nicht vorhanden")
book_information = {
"title": title,
"author": author,
"publisher": publisher,
"publishedDate": published_Date,
"pageCount": page_Count,
"image": image,
"categorie": categorie
}
return book_information
book_api.py:
from django.db import models
from .book import Book
import requests
import pdb
class Book_Api:
def __init__(self, search):
self.show_books = self.process_data(search)
# try using private methods and what about class methods
def process_data(self, search):
json_respose = self.__fetch_data(search)
books_list = []
for book in json_respose["items"]:
book_obj = Book(book)
books_list.append(book_obj)
return books_list
def __fetch_data(self, book_search):
key = "AIzaSyA02bYtu7X4pdwRlmOlp0HrouGGwue0qaY"
filter = "partial"
printType = "books"
language = "de"
url = "https://www.googleapis.com/books/v1/volumes?q=intitle:{}&filter={}&printType={}&maxResults=10&projection=full&langRestrict={}&keyes&key={}"
data = requests.get(url.format(
book_search, filter, printType, language, key)).json()
return data