Last active
September 21, 2015 03:05
-
-
Save berlotto/ad289033dedec4b05af0 to your computer and use it in GitHub Desktop.
Script para criação do banco de dados RethinkDB para testes da API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
# RESTFul api test for blog post | |
# Author: Sérgio Berlotto <[email protected]> | |
# Site: http://blog.programadorlivre.com | |
# restapi-db.py | |
TOTAL_FRASES = 1000 | |
DBNAME = "restapi" | |
TABLENAME = "frases" | |
HOST = "localhost" | |
PORT = 28015 | |
def main(): | |
import rethinkdb as r | |
from rethinkdb.errors import RqlRuntimeError | |
#Lib para auxilio na inserção de dados de teste | |
from faker import Factory | |
fake = Factory.create('pt_BR') | |
# Conecta ao banco local | |
r.connect(HOST, PORT).repl() | |
try: | |
r.db_drop(DBNAME).run() | |
except RqlRuntimeError: | |
pass | |
# Cria o banco de dados | |
r.db_create(DBNAME).run() | |
# Cria a tabela | |
r.db(DBNAME).table_create(TABLENAME).run() | |
# Insere os registros na tabela | |
for x in range(TOTAL_FRASES): | |
reg = { | |
'id' : x, | |
'frase': fake.text(), | |
'autor': fake.name() | |
} | |
r.db(DBNAME).table(TABLENAME).insert(reg).run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment