コード例 #1
0
def test_poll_repository_vote():
    repo = PollRepository()

    options = ['op1', 'op2']

    repo.add_poll("Name", options)

    assert repo.get_poll(1) == dict(id=1,
                                    name="Name",
                                    options=[{
                                        'value': "op1",
                                        'votes': 0
                                    }, {
                                        'value': "op2",
                                        'votes': 0
                                    }])

    repo.vote(1, 1)

    assert repo.get_poll(1)['options'][0]['votes'] == 1
    assert repo.get_poll(1)['options'][1]['votes'] == 0

    repo.vote(1, 1)

    assert repo.get_poll(1)['options'][0]['votes'] == 2
    assert repo.get_poll(1)['options'][1]['votes'] == 0

    repo.vote(1, 2)

    assert repo.get_poll(1)['options'][0]['votes'] == 2
    assert repo.get_poll(1)['options'][1]['votes'] == 1

    with pytest.raises(Exception):
        repo.vote(1, 3)
コード例 #2
0
def test_poll_repository_get():
    repo = PollRepository()

    repo.add_poll("Name", [])

    assert repo.get_poll(1) == dict(id=1, name="Name", options=[])

    repo.add_poll("Name2", ["option1"])

    assert repo.polls[1] == dict(id=2,
                                 name="Name2",
                                 options=[{
                                     'value': "option1",
                                     'votes': 0
                                 }])
コード例 #3
0
def test_poll_repository_init():
    repo = PollRepository()

    assert repo
    assert repo.polls == []
コード例 #4
0
# -*- coding: utf-8 -*-

from coopy.base import init_persistent_system
from flask import Flask, jsonify, render_template, request, flash

from loogica.domain import EmailRepository, ValidationError, PollRepository
from loogica.forms import UserForm

app = Flask(__name__)
user_repo = init_persistent_system(EmailRepository(),
                                   basedir="emailrepository")
poll_repo = init_persistent_system(PollRepository())


@app.route("/")
def main():
    return render_template('index.html')


@app.route("/participar", methods=['GET', 'POST'])
def create_user():
    if request.method == "GET":
        form = UserForm()
        return render_template('quero-participar.html', form=form)

    form = UserForm(request.form)
    errors = None

    if form.validate():
        try:
            user_repo.add_user(form.name.data, form.email.data)