Create script to add randomized test users

This commit is contained in:
Corder Guy 2018-04-24 15:11:50 -05:00
parent 00e470e45a
commit 01ac4264c9
2 changed files with 49 additions and 0 deletions

49
add_random_users.py Normal file
View file

@ -0,0 +1,49 @@
#!/usr/bin/env python3
import json
import random
import sqlite3
import sys
DATABASE = 'db.sqlite3'
def main():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
output = cursor.execute(
'SELECT user_id FROM users ORDER BY user_id DESC').fetchall()
existing_ids = [str(dict(row)['user_id']) for row in output]
if len(sys.argv) < 2:
n = 10
else:
n = int(sys.argv[1])
with open('/usr/share/dict/names', 'r') as name_file:
names = [row.strip('\n') for row in name_file]
for _ in range(n):
user_id = '1'
while user_id in existing_ids:
user_id = str(random.randint(1, 1000000))
existing_ids.append(user_id)
username = random.choice(names)
score = str(random.randint(0, 1000))
query = \
'INSERT INTO users (user_id, username, score) VALUES ({}, \'{}\', {})'\
.format(user_id, username, score)
print(query)
cursor.execute(query)
conn.commit()
conn.close()
if __name__ == '__main__':
main()

Binary file not shown.