-
Notifications
You must be signed in to change notification settings - Fork 2
/
seed_db.py
87 lines (59 loc) · 2.28 KB
/
seed_db.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Script to seed database."""
import os
import json
from random import choice, randint
from datetime import datetime
import crud
import model
import server
# os.system('dropdb testdb')
# os.system('createdb testdb')
model.connect_to_db(server.app)
# model.db.create_all() # <-- can un-comment later but good to get in the habit
# # of entering this into bash/terminal
# Create 10 test users
users_in_db = []
for n in range(10):
email = f'user{n}@test.com' # A unique email
username = f'user_{n}' # A unique username
password = 'testpwd'
user = crud.create_user(email, username, password)
users_in_db.append(user)
# Create 10 test landlords
landlords_in_db = []
for n in range(10):
landlord_name = f'Test Landlord {n}'
street_num = randint(10, 2500)
office_address = f'{street_num} Test Address Lane'
landlord = crud.create_landlord(landlord_name, office_address)
landlords_in_db.append(landlord)
# Create 5 test buildings
buildings_in_db = []
for n in range(10):
building_housenumber = randint(10, 2500)
building_streetname = "Renters Road"
building_postcode = randint(10100, 99999)
landlord_to_own = choice(landlords_in_db)
landlord_id=landlord_to_own.landlord_id
building = crud.create_building(building_housenumber, building_streetname,
building_postcode, landlord_id)
buildings_in_db.append(building)
# Create 10 test reviews
# Example review creation from terminal:
# test_review=Review(review_body="This is a test review.",
# created_on=datetime.datetime(2020, 5, 23),
# updated_at=datetime.datetime(2020, 5, 23),
# user_id=test_user.user_id,
# building_id=test_building.building_id)
for n in range(25):
review_body = f'This is review number {n}. This landlord...'
# create_yr = randint(2017, 2020)
# create_mth = randint(1, 12)
# create_day = randint(1, 28)
# created_on = datetime(create_yr, create_mth, create_day)
# updated_at = datetime.now()
user_to_write = choice(users_in_db)
user_id=user_to_write.user_id
building_to_write = choice(buildings_in_db)
building_id=building_to_write.building_id
review = crud.create_review(review_body, user, building_to_write)