<commit_before>from datetime import datetime
from pathlib import Path
from pytest import fixture
import sqlite3

import anketa

# BTW: http://flask.pocoo.org/docs/0.12/testing/


@fixture
def test_client():
    return anketa.app.test_client()


def test_render_index(test_client):
    rv = test_client.get('/')
    assert b'Anketa' in rv.data


@fixture
def temp_dir(tmpdir):
    # I like standard pathlib.Path more than py.path :)
    return Path(str(tmpdir))


def test_smoke_sqlite3(temp_dir):
    # just an example of sqlite3 usage from Python docs:
    # https://docs.python.org/3.6/library/sqlite3.html
    conn = sqlite3.connect(str(temp_dir / 'example.db'))
    c = conn.cursor()
    c.execute('''CREATE TABLE stocks
                 (date text, trans text, symbol text, qty real, price real)''')
    c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
    conn.commit()
    conn.close()


def test_workflow_prototype(temp_dir):
    conn = sqlite3.connect(str(temp_dir / 'workflow.db'))
    c = conn.cursor()
    c.execute('''
        CREATE TABLE suggestions (
            id INTEGER PRIMARY KEY,
            title TEXT,
            date DATE,
            cookie TEXT
        )
    ''')
    c.execute('''
        CREATE TABLE votes (
            id INTEGER PRIMARY KEY,
            suggestion_id TEXT,
            date DATE,
            cookie TEXT,
            value INTEGER
        )
    ''')
    c.execute(
        "INSERT INTO suggestions (title, date, cookie) VALUES (?, ?, ?)",
        ('Tvorba webu', datetime(2017, 3, 15, 12, 0).isoformat(), 'cookie1'))
    sug1_id = c.lastrowid
    c.execute(
        "INSERT INTO suggestions (title, date, cookie) VALUES (?, ?, ?)",
        ('Statistika', datetime(2017, 3, 15, 12, 10).isoformat(), 'cookie1'))
    # add some upvotes
    c.execute(
        "INSERT INTO votes (suggestion_id, date, cookie, value) VALUES (?, ?, ?, ?)",
        (sug1_id, datetime(2017, 3, 15, 13, 0).isoformat(), 'cookie1', 1))
    c.execute(
        "INSERT INTO votes (suggestion_id, date, cookie, value) VALUES (?, ?, ?, ?)",
        (sug1_id, datetime(2017, 3, 15, 13, 0).isoformat(), 'cookie2', 1))
    # add some downvote
    c.execute(
        "INSERT INTO votes (suggestion_id, date, cookie, value) VALUES (?, ?, ?, ?)",
        (sug1_id, datetime(2017, 3, 15, 13, 0).isoformat(), 'cookie3', -1))

    # the most important query :)
    c.execute('''
        SELECT
            suggestions.title,
            COALESCE(SUM(votes.value), 0) AS vote_count
        FROM suggestions
        LEFT JOIN votes ON (votes.suggestion_id = suggestions.id)
        GROUP BY suggestions.id
        ORDER BY vote_count DESC
    ''')
    rows = c.fetchall()
    assert rows == [
        ('Tvorba webu', 1),
        ('Statistika', 0),
    ]
<commit_msg>rozdeleni prototypu do funkci<commit_after>from datetime import datetime
from pathlib import Path
from pytest import fixture
import sqlite3

import anketa

# BTW: http://flask.pocoo.org/docs/0.12/testing/


@fixture
def test_client():
    return anketa.app.test_client()


def test_render_index(test_client):
    rv = test_client.get('/')
    assert b'Anketa' in rv.data


@fixture
def temp_dir(tmpdir):
    # I like standard pathlib.Path more than py.path :)
    return Path(str(tmpdir))


def test_smoke_sqlite3(temp_dir):
    # just an example of sqlite3 usage from Python docs:
    # https://docs.python.org/3.6/library/sqlite3.html
    conn = sqlite3.connect(str(temp_dir / 'example.db'))
    c = conn.cursor()
    c.execute('''CREATE TABLE stocks
                 (date text, trans text, symbol text, qty real, price real)''')
    c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
    conn.commit()
    conn.close()


def prepare_schema(conn):
    c = conn.cursor()
    c.execute('''
        CREATE TABLE suggestions (
            id INTEGER PRIMARY KEY,
            title TEXT,
            date DATE,
            cookie TEXT
        )
    ''')
    c.execute('''
        CREATE TABLE votes (
            id INTEGER PRIMARY KEY,
            suggestion_id TEXT,
            date DATE,
            cookie TEXT,
            value INTEGER
        )
    ''')


def test_workflow_prototype(temp_dir):
    conn = sqlite3.connect(str(temp_dir / 'workflow.db'))
    prepare_schema(conn)
    sug1_id = insert_suggestion(
        conn, '<NAME>', 'cookie1', date=datetime(2017, 3, 15, 12, 0))
    sug2_id = insert_suggestion(
        conn, 'Statistika', 'cookie1', date=datetime(2017, 3, 15, 13, 0))
    # add some upvotes
    insert_upvote(conn, sug1_id, 'cookie1', upvote=True, date=datetime(2017, 3, 15, 13, 0))
    insert_upvote(conn, sug1_id, 'cookie2', upvote=True, date=datetime(2017, 3, 15, 13, 0))
    insert_upvote(conn, sug1_id, 'cookie3', upvote=False, date=datetime(2017, 3, 15, 13, 0))
    # the most important query :)
    rows = list_suggestions(conn)
    assert rows == [
        {'title': 'Tvorba webu', 'vote_count': 1},
        {'title': 'Statistika',  'vote_count': 0},
    ]


def insert_suggestion(conn, title, cookie, date=None):
    date = date or datetime.utcnow()
    c = conn.cursor()
    c.execute(
        "INSERT INTO suggestions (title, date, cookie) VALUES (?, ?, ?)",
        (title, date.isoformat(), cookie))
    return c.lastrowid


def insert_upvote(conn, suggestion_id, cookie, upvote, date=None):
    date = date or datetime.utcnow()
    c = conn.cursor()
    c.execute(
        "INSERT INTO votes (suggestion_id, date, cookie, value) "
        "VALUES (?, ?, ?, ?)", (
            int(suggestion_id),
            date.isoformat(),
            cookie,
            1 if upvote else -1,
        ))


def list_suggestions(conn):
    c = conn.cursor()
    c.execute('''
        SELECT
            suggestions.title,
            COALESCE(SUM(votes.value), 0) AS vote_count
        FROM suggestions
        LEFT JOIN votes ON (votes.suggestion_id = suggestions.id)
        GROUP BY suggestions.id
        ORDER BY vote_count DESC
    ''')
    suggestions = []
    for row in c:
        suggestions.append({
            'title': row[0],
            'vote_count': row[1],
        })
    return suggestions

{'fraction_non_alphanumeric': 0.13374452199318293, 'fraction_numerical': 0.030027592923226747, 'mean_word_length': 2.471549295774648, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4272284', 'n_tokens_mistral': 2190, 'n_tokens_neox': 1979, 'n_words': 523}