<commit_before>import unittest
from pyramid.config import Configurator
from pyramid import testing

from raggregate import queries
from raggregate.models import User
from raggregate.models import Epistle
from raggregate.models import Submission

import re

def _initTestingDB():
    from sqlalchemy import create_engine
    from raggregate.models import initialize_sql_test
    session = initialize_sql_test(create_engine('sqlite://'))
    return session

class BaseTest(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()
        self.dbsession = _initTestingDB()

    def tearDown(self):
        testing.tearDown()

class TestUsers(BaseTest):
    def test_create_user(self, username = None):
        if not username:
            username = 'test'
        u = queries.create_user(username = username, password = username,)
        res = self.dbsession.query(User).filter(User.name == username).first()
        self.assertEqual(res.id, u.id)
        self.assertEqual(res.name, u.name)

    def test_get_user_by_name(self):
        u = queries.create_user(username = 'test', password='<PASSWORD>')
        res = queries.get_user_by_name(u.name)
        self.assertEqual(u.id, res.id)

    def test_find_by_id_user(self):
        # depends on functional test_create_user
        u = queries.create_user(username = 'test', password='<PASSWORD>')
        res = queries.find_by_id(u.id)
        self.assertEqual(res.id, u.id)
        self.assertEqual(res.name, u.name)

class TestEpistles(BaseTest):
    def test_send_epistle_by_id(self):
        #@TODO: as this test illustrates, we should make a "send_epistle" function in queries
        u1 = queries.create_user(username = 't1', password='<PASSWORD>')
        u2 = queries.create_user(username = 't2', password='<PASSWORD>')
        ep = Epistle(u1.id, u2.id, u'test epistle', subject = u'a simple test')
        self.dbsession.add(ep)
        self.dbsession.flush()
        epd = queries.get_epistle_by_recipient_id(u1.id)[0]
        self.assertEqual(ep.id, epd.id)

class TestSubmissions(BaseTest):
    def test_create_submission(self):
        #@TODO: another function that should be split out of the view for easy repitition.
        # if the view code changes substantially, this test will not keep up
        user = queries.create_user(username = 'test', password = '<PASSWORD>')
        url = 'http://google.com'
        title = 'test'
        description = 'test'

        if url != '' and url is not None:
            url = queries.strip_all_html(url)
            if not re.match(r'http[s]*:\/\/', url):
                url = 'http://' + url
        else:
            # set to None so that NULL goes into the database
            url = None

        sub = Submission(title, description, url, user.id)
        self.dbsession.add(sub)
        self.dbsession.flush()
        s = queries.get_story_by_id(sub.id)
        self.assertEqual(s.id, sub.id)<commit_msg>Add tests for domain parsing<commit_after>import unittest
from pyramid.config import Configurator
from pyramid import testing

from raggregate import queries
from raggregate.models import User
from raggregate.models import Epistle
from raggregate.models import Submission

import re

def _initTestingDB():
    from sqlalchemy import create_engine
    from raggregate.models import initialize_sql_test
    session = initialize_sql_test(create_engine('sqlite://'))
    return session

class BaseTest(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()
        self.dbsession = _initTestingDB()

    def tearDown(self):
        testing.tearDown()

class TestUsers(BaseTest):
    def test_create_user(self, username = None):
        if not username:
            username = 'test'
        u = queries.create_user(username = username, password = username,)
        res = self.dbsession.query(User).filter(User.name == username).first()
        self.assertEqual(res.id, u.id)
        self.assertEqual(res.name, u.name)

    def test_get_user_by_name(self):
        u = queries.create_user(username = 'test', password='<PASSWORD>')
        res = queries.get_user_by_name(u.name)
        self.assertEqual(u.id, res.id)

    def test_find_by_id_user(self):
        # depends on functional test_create_user
        u = queries.create_user(username = 'test', password='<PASSWORD>')
        res = queries.find_by_id(u.id)
        self.assertEqual(res.id, u.id)
        self.assertEqual(res.name, u.name)

class TestEpistles(BaseTest):
    def test_send_epistle_by_id(self):
        #@TODO: as this test illustrates, we should make a "send_epistle" function in queries
        u1 = queries.create_user(username = 't1', password='<PASSWORD>')
        u2 = queries.create_user(username = 't2', password='<PASSWORD>')
        ep = Epistle(u1.id, u2.id, u'test epistle', subject = u'a simple test')
        self.dbsession.add(ep)
        self.dbsession.flush()
        epd = queries.get_epistle_by_recipient_id(u1.id)[0]
        self.assertEqual(ep.id, epd.id)

class TestSubmissions(BaseTest):
    def test_create_submission(self):
        #@TODO: another function that should be split out of the view for easy repitition.
        # if the view code changes substantially, this test will not keep up
        user = queries.create_user(username = 'test', password = '<PASSWORD>')
        url = 'http://google.com'
        title = 'test'
        description = 'test'

        if url != '' and url is not None:
            url = queries.strip_all_html(url)
            if not re.match(r'http[s]*:\/\/', url):
                url = 'http://' + url
        else:
            # set to None so that NULL goes into the database
            url = None

        sub = Submission(title, description, url, user.id)
        self.dbsession.add(sub)
        self.dbsession.flush()
        s = queries.get_story_by_id(sub.id)
        self.assertEqual(s.id, sub.id)

    def test_domain_parse(self):
        title = 'test'
        description = 'test'
        #@TODO: we should make this accept a fake user id in test mode at least
        # so that we don't have huge cascading failures if create_user is broken
        user = queries.create_user(username='test', password='<PASSWORD>')

        sub = Submission(title, description, 'http://google.com', user.id)
        self.assertEqual('google.com', sub.get_domain_name())

        sub = Submission(title, description, 'http://googlewww.com', user.id)
        self.assertEqual('googlewww.com', sub.get_domain_name())

        sub = Submission(title, description, 'https://google.com', user.id)
        self.assertEqual('google.com', sub.get_domain_name())
{'fraction_non_alphanumeric': 0.11077664057796509, 'fraction_numerical': 0.002408187838651415, 'mean_word_length': 2.8701223063482817, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '2534764', 'n_tokens_mistral': 2072, 'n_tokens_neox': 2007, 'n_words': 506}