/
Askoline
/
Homework03
Обзор
Документация
Войти
/
Askoline
/
Homework03
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
create_fixtures.py
180 строк
6 KB
Askoline
first_commit
05 авг 2026, 14:29
05 авг 2026, 14:29
a97374b
Код
Авторство
О чём код?
# -*- coding: utf-8 -*- # create_fixtures.py # Script to generate sample fixture files import json import random from datetime import datetime, timedelta from pathlib import Path from config import FIXTURES_DIR def create_publishers(): """Create publishers fixture""" publishers = [ {"name": "Pushkin"}, {"name": "Tolstoy"}, {"name": "Dostoevsky"}, {"name": "Chekhov"}, {"name": "Gogol"}, {"name": "Bulgakov"}, {"name": "Turgenev"}, {"name": "Solzhenitsyn"}, {"name": "Nabokov"}, ] save_fixture('publishers.json', publishers) def create_shops(): """Create shops fixture""" shops = [ {"name": "Bookvoed"}, {"name": "Labyrinth"}, {"name": "Book House"}, {"name": "Read City"}, {"name": "Book World"}, {"name": "Readers' Paradise"}, {"name": "Litera"}, ] save_fixture('shops.json', shops) def create_books(): """Create books fixture""" books = [ {"title": "Captain's Daughter", "publisher_name": "Pushkin"}, {"title": "Ruslan and Ludmila", "publisher_name": "Pushkin"}, {"title": "Eugene Onegin", "publisher_name": "Pushkin"}, {"title": "The Bronze Horseman", "publisher_name": "Pushkin"}, {"title": "War and Peace", "publisher_name": "Tolstoy"}, {"title": "Anna Karenina", "publisher_name": "Tolstoy"}, {"title": "Resurrection", "publisher_name": "Tolstoy"}, {"title": "Crime and Punishment", "publisher_name": "Dostoevsky"}, {"title": "The Idiot", "publisher_name": "Dostoevsky"}, {"title": "The Brothers Karamazov", "publisher_name": "Dostoevsky"}, {"title": "The Cherry Orchard", "publisher_name": "Chekhov"}, {"title": "Uncle Vanya", "publisher_name": "Chekhov"}, {"title": "The Seagull", "publisher_name": "Chekhov"}, {"title": "Dead Souls", "publisher_name": "Gogol"}, {"title": "The Government Inspector", "publisher_name": "Gogol"}, {"title": "The Overcoat", "publisher_name": "Gogol"}, {"title": "The Master and Margarita", "publisher_name": "Bulgakov"}, {"title": "Heart of a Dog", "publisher_name": "Bulgakov"}, {"title": "Fathers and Sons", "publisher_name": "Turgenev"}, {"title": "A Nest of the Gentry", "publisher_name": "Turgenev"}, ] save_fixture('books.json', books) def create_stock(): """Create stock fixture""" books = [ ("Captain's Daughter", ["Bookvoed", "Labyrinth"]), ("Ruslan and Ludmila", ["Bookvoed", "Book House"]), ("Eugene Onegin", ["Book House", "Book World"]), ("The Bronze Horseman", ["Read City"]), ("War and Peace", ["Bookvoed", "Labyrinth"]), ("Anna Karenina", ["Book House"]), ("Crime and Punishment", ["Book World", "Bookvoed"]), ("The Idiot", ["Labyrinth"]), ("The Master and Margarita", ["Book House", "Read City"]), ("Fathers and Sons", ["Book World"]), ] stock = [] for book_title, shops in books: for shop in shops: stock.append({ "book_title": book_title, "shop_name": shop, "count": random.randint(20, 100) }) save_fixture('stock.json', stock) def create_sales(): """Create sales fixture""" sales = [] base_date = datetime.now() - timedelta(days=30) sales_data = [ ("Captain's Daughter", "Bookvoed", 2, 600.00, "2026-07-15 14:30:00"), ("Captain's Daughter", "Bookvoed", 3, 590.00, "2026-07-20 11:15:00"), ("Captain's Daughter", "Labyrinth", 1, 580.00, "2026-07-18 16:45:00"), ("Ruslan and Ludmila", "Bookvoed", 1, 500.00, "2026-07-22 10:00:00"), ("Ruslan and Ludmila", "Book House", 2, 480.00, "2026-07-19 13:20:00"), ("Eugene Onegin", "Book House", 1, 490.00, "2026-07-21 09:30:00"), ("Eugene Onegin", "Book World", 2, 485.00, "2026-07-17 15:10:00"), ("War and Peace", "Bookvoed", 1, 1200.00, "2026-07-16 12:00:00"), ("War and Peace", "Labyrinth", 2, 1150.00, "2026-07-14 11:30:00"), ("Anna Karenina", "Book House", 1, 950.00, "2026-07-13 14:00:00"), ("Crime and Punishment", "Book World", 3, 800.00, "2026-07-12 10:45:00"), ("Crime and Punishment", "Bookvoed", 1, 820.00, "2026-07-11 16:20:00"), ("The Master and Margarita", "Book House", 2, 750.00, "2026-07-10 13:50:00"), ("The Master and Margarita", "Read City", 1, 780.00, "2026-07-09 11:15:00"), ("Fathers and Sons", "Book World", 2, 650.00, "2026-07-08 15:40:00"), ] for title, shop, qty, price, date in sales_data: sales.append({ "book_title": title, "shop_name": shop, "quantity": qty, "price": price, "date": date }) # Generate some random additional sales for i in range(20): book_title = random.choice([ "Captain's Daughter", "Ruslan and Ludmila", "Eugene Onegin", "War and Peace", "Anna Karenina", "Crime and Punishment", "The Master and Margarita", "Fathers and Sons" ]) shop_name = random.choice(["Bookvoed", "Labyrinth", "Book House", "Read City", "Book World"]) quantity = random.randint(1, 4) price = round(random.uniform(400, 1500), 2) sale_date = base_date + timedelta(days=random.randint(0, 30)) date_str = sale_date.strftime("%Y-%m-%d %H:%M:%S") sales.append({ "book_title": book_title, "shop_name": shop_name, "quantity": quantity, "price": price, "date": date_str }) save_fixture('sales.json', sales) def save_fixture(filename, data): """Save data to JSON file""" filepath = FIXTURES_DIR / filename with open(filepath, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) print(f"Created: {filepath}") def main(): """Generate all fixtures""" print("Generating fixture files...") print("="*40) FIXTURES_DIR.mkdir(exist_ok=True) create_publishers() create_shops() create_books() create_stock() create_sales() print("="*40) print("All fixtures generated successfully!") if __name__ == "__main__": main()