<commit_before># -*- coding: utf-8 -*-
"""
    Simblin Test Views
    ~~~~~~~~~~~~~~~~~~

    Test the different views of the blogging application.

    :copyright: (c) 2010 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import datetime
import flask

from nose.tools import assert_equal

from simblin import create_app
from simblin.extensions import db
from simblin.models import Post, Tag, Category, post_tags, post_categories

# TODO: Test Models separately (see danjac)
# TODO: Test archive view, Test month view

# Configuration

#: Inmemory database
SQLALCHEMY_DATABASE_URI = 'sqlite://'

# Globals
#: The application
app = None
#: The test client   
client = None
#: The context of the application
ctx = None
    
    
def setUp():
    """Initalize application and temporary database"""
    global app, client, ctx
    app = create_app(__name__)
    client = app.test_client()
    ctx = app.test_request_context()
    ctx.push()
    db.init_app(app)
    db.create_all()


def teardown():
    """Get rid of the context"""
    ctx.pop()
    
    
def clear_db():
    """Remove all rows inside the database"""
    ctx.push() # If I remove this test_registering won't work?
    db.drop_all()
    db.create_all()
    
    
# Helper functions

class faked_request:
    """Use with the `with` statement. Simulates the `before_request` and 
    `after_request` actions."""
    
    def __enter__(self):
        """Initialize db connections, g, request etc."""
        ctx.push()
        app.preprocess_request()
        
    def __exit__(self, type, value, traceback):
        """Close db connections, g, request etc."""
        app.process_response(app.response_class())
        ctx.pop()


def register(username, password, password2='', email=''):
    """Helper function to register a user"""
    return client.post('/register', data=dict(
        username=username,
        password=password,
        password2=password2,
        email=email,
    ), follow_redirects=True)


def login(username, password):
    """Helper function to login"""
    return client.post('/login', data=dict(
        username=username,
        password=password
    ), follow_redirects=True)
    
    
def register_and_login(username, password):
    """Register and login in one go"""
    register(username, password, password)
    login(username, password)


def logout():
    """Helper function to logout"""
    return client.get('/logout', follow_redirects=True)


def add_post(title, markup, tags=None, categories=[]):
    """Helper functions to create a blog post"""
    data=dict(
        title=title,
        markup=markup,
        tags=tags,
        action='Publish',
    )
    # Mimic select form fields
    for i, category_id in enumerate(categories):
        data['category-%d' % i] = category_id
    return client.post('/compose', data=data, follow_redirects=True)


def update_post(slug, title, markup, tags=None, categories=[]):
    """Helper functions to create a blog post"""
    data=dict(
        title=title,
        markup=markup,
        tags=tags,
        action='Update',
    )
    # Mimic select form fields
    for i, category_id in enumerate(categories):
        data['category-%d' % i] = category_id
    return client.post('/update/%s' % slug, data=data, follow_redirects=True)
    

def delete_post(slug):
    """Helper function to delete a blog post"""
    return client.post('/_delete/%s' % slug, data=dict(next=''), 
        follow_redirects=True)
        
        
def add_category(name):
    """Register category in the database and return its id"""
    return flask.json.loads(
        client.post('/_add_category', data=dict(name=name)).data)['id']
    
        
        
class TestRegistration:
    
    def test_registering(self):
        """Test form validation and successful registering"""
        clear_db()
        rv = register('', 'password')
        assert 'You have to enter a username' in rv.data
        rv = register('britney spears', '')
        assert 'You have to enter a password' in rv.data<commit_msg>cleaned<commit_after># -*- coding: utf-8 -*-
"""
    Simblin Test Views
    ~~~~~~~~~~~~~~~~~~

    Test the different views of the blogging application.

    :copyright: (c) 2010 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import datetime
import flask

from nose.tools import assert_equal

from simblin import create_app
from simblin.extensions import db
from simblin.models import Post, Tag, Category, post_tags, post_categories

# TODO: Test archive view, Test month view

# Configuration

#: Inmemory database
SQLALCHEMY_DATABASE_URI = 'sqlite://'

# Globals
#: The application
app = None
#: The test client   
client = None
#: The context of the application
ctx = None
    
    
def setUp():
    """Initalize application and temporary database"""
    global app, client, ctx
    app = create_app(__name__)
    client = app.test_client()
    ctx = app.test_request_context()
    ctx.push()
    db.init_app(app)
    db.create_all()


def teardown():
    """Get rid of the context"""
    ctx.pop()
    
    
def clear_db():
    """Remove all rows inside the database"""
    ctx.push() # If I remove this test_registering won't work?
    db.session.remove()
    db.drop_all()
    db.create_all()
    
    
# Helper functions

class faked_request:
    """Use with the `with` statement. Simulates the `before_request` and 
    `after_request` actions."""
    
    def __enter__(self):
        """Initialize db connections, g, request etc."""
        ctx.push()
        app.preprocess_request()
        
    def __exit__(self, type, value, traceback):
        """Close db connections, g, request etc."""
        app.process_response(app.response_class())
        ctx.pop()


def register(username, password, password2='', email=''):
    """Helper function to register a user"""
    return client.post('/register', data=dict(
        username=username,
        password=password,
        password2=<PASSWORD>,
        email=email,
    ), follow_redirects=True)


def login(username, password):
    """Helper function to login"""
    return client.post('/login', data=dict(
        username=username,
        password=password
    ), follow_redirects=True)
    
    
def register_and_login(username, password):
    """Register and login in one go"""
    register(username, password, password)
    login(username, password)


def logout():
    """Helper function to logout"""
    return client.get('/logout', follow_redirects=True)


def add_post(title, markup, tags=None, categories=[]):
    """Helper functions to create a blog post"""
    data=dict(
        title=title,
        markup=markup,
        tags=tags,
        action='Publish',
    )
    # Mimic select form fields
    for i, category_id in enumerate(categories):
        data['category-%d' % i] = category_id
    return client.post('/compose', data=data, follow_redirects=True)


def update_post(slug, title, markup, tags=None, categories=[]):
    """Helper functions to create a blog post"""
    data=dict(
        title=title,
        markup=markup,
        tags=tags,
        action='Update',
    )
    # Mimic select form fields
    for i, category_id in enumerate(categories):
        data['category-%d' % i] = category_id
    return client.post('/update/%s' % slug, data=data, follow_redirects=True)
    

def delete_post(slug):
    """Helper function to delete a blog post"""
    return client.post('/_delete/%s' % slug, data=dict(next=''), 
        follow_redirects=True)
        
        
def add_category(name):
    """Register category in the database and return its id"""
    return flask.json.loads(
        client.post('/_add_category', data=dict(name=name)).data)['id']
    

# Tests
        
class TestRegistration:
    
    def test_registering(self):
        """Test form validation and successful registering"""
        clear_db()
        rv = register('', 'password')
        assert 'You have to enter a username' in rv.data
        rv = register('britney spears', '')
        assert 'You have to enter a password' in rv.data
{'fraction_non_alphanumeric': 0.12256164887743835, 'fraction_numerical': 0.001840264998159735, 'mean_word_length': 2.9824132877381535, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, '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': '5767941', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2344, 'n_words': 715}