-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathseed_data.py
44 lines (38 loc) · 1.12 KB
/
seed_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from faker import Faker
from sqlalchemy.orm import Session
from models import User, Book, Author
from crud import create_user, create_book, create_author
from database import SessionLocal
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
fake = Faker()
# Seed fake data into the database
def seed_data(db: Session):
# Seed users
for _ in range(3):
user = User(
username=fake.user_name(),
hashed_password=pwd_context.hash("password123"),
is_active=True
)
db.add(user)
# Seed authors
authors = []
for _ in range(5):
author = Author(name=fake.name())
db.add(author)
authors.append(author)
# Seed books
for _ in range(10):
book = Book(
title=fake.catch_phrase(),
description=fake.text(),
author_id=fake.random_element(elements=[author.id for author in authors])
)
db.add(book)
db.commit()
if __name__ == "__main__":
db = SessionLocal()
seed_data(db)
db.close()
print("Database seeded successfully.")