-
Notifications
You must be signed in to change notification settings - Fork 2
/
testdata.py
61 lines (48 loc) · 1.65 KB
/
testdata.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
"""
testdata.py
import dummy data into database.
created on 13/June/2014
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from catalog_app.api.util import encrypt_password, check_password
from catalog_app.api.models import Base, User, Category, Item
from settings import config
# Get engine and session for dummy data importing
engine = create_engine(
config.DATABASE_URI
)
Base.metadata.create_all(engine)
DBSession = sessionmaker(bind=engine)
session = DBSession()
# Create dummy users(10 users)
# Example:
# Username: user1@email.com ~ user10@email.com
# Password: user1password ~ user10@password
for i in range(10):
password = "user{}password".format(i + 1)
enc, salt = encrypt_password(password)
user = User(name="user{}".format(i + 1),
email="user{}@email.com".format(i + 1),
password=enc, salt=salt)
session.add(user)
session.commit()
# Create dummy categories and items(10 categories, 100 items)
# Example:
# Category: category1 ~ category10
# Item: item1_c1 ~ item10_c10
for c in range(10):
category = Category(name="category{}".format(c + 1))
session.add(category)
session.commit()
# 10 items in each category
for i in range(10):
item = Item(title="item{}_c{}".format(i + 1, c + 1),
category_id=category.id, user_id=(i % 10 + 1))
item.description = "This is a description of category: \
{} and item: {}. This item is created by {}"\
.format(i + 1, c + 1, "user{}".format(i % 10 + 1))
session.add(item)
session.commit()
print "inserting rows done!"