<commit_before># Copyright 2011 <NAME>
#
# This file is part of Ophot.
#
# Ophot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ophot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Ophot.  If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for the ophot module."""
# imports from built-in modules
import os
import sqlite3
import tempfile
import unittest
import uuid

# imports from third party modules
from werkzeug.datastructures import FileStorage
from werkzeug.datastructures import MultiDict

# imports from this application
from ophot import add_new_category
from ophot import app
from ophot import after_request
from ophot import before_request
from ophot import connect_db
from ophot import get_categories
from ophot import init_db

class OphotTestCase(unittest.TestCase):
    """Test class for the ophot module."""

    def setUp(self):
        """Connects the Flask application to a temporary database and creates a
        client for testing.

        """
        s = tempfile.mkstemp()
        self.db_fd, app.config['DATABASE'] = tempfile.mkstemp()
        self.app = app.test_client()
        init_db()

    def tearDown(self):
        """Closes and deletes the database."""
        os.close(self.db_fd)
        os.unlink(app.config['DATABASE'])

    def test_add_new_category(self):
        """Test for adding a new category to the database."""
        with app.test_request_context('/'):
            before_request()
            categories = get_categories()
            new_id = add_new_category('foobar')
            new_categories = get_categories()
            self.assertNotIn(new_id, categories)
            self.assertEqual(max(categories) + 1, new_id)
            self.assertIn('foobar', new_categories.values())

    def login(self, username=app.config['USERNAME'],
              password=app.config['PASSWORD']):
        """Makes a POST request to login to the Flask application with the
        specified username and password. If no username and password are
        specified, the ones from the configuration will be used.

        """<commit_msg>added some tests<commit_after># Copyright 2011 <NAME>
#
# This file is part of Ophot.
#
# Ophot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ophot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Ophot.  If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for the ophot module."""
# imports from built-in modules
import os
import sqlite3
import tempfile
import unittest
#import uuid

# imports from third party modules
#from werkzeug.datastructures import FileStorage
#from werkzeug.datastructures import MultiDict
from werkzeug.exceptions import Unauthorized

# imports from this application
from ophot import add_new_category
from ophot import app
from ophot import after_request
from ophot import before_request
from ophot import connect_db
from ophot import init_db
from ophot import get_categories
from ophot import get_last_display_position
from ophot import require_logged_in
from ophot import select_single


class temp_photos(object):
    """
    Context manager (for use in a "with" statement) which creates temporary
    files representing photos, connects to the database, adds photos to it,
    then on exit removes the files.

    """
    def __init__(self):
        """This method does nothing."""
        pass

    def __enter__(self):
        """Creates temporary photos and adds them to the database."""
        # mkstemp() returns a 2-tuple: (file_descriptor, filename)
        self.photos = [tempfile.mkstemp() for i in range(2)]
        self.conn = connect_db()
        for i in range(len(self.photos)):
            self.conn.execute('insert into photo (photofilename,'
                              ' photocategory, photodisplayposition)'
                              ' values (?, ?, ?)',
                              [self.photos[i][1], '1', str(i + 1)])
        self.conn.commit()

    def __exit__(self, exception_type, exception_value, traceback):
        """Unlinks the temporary files and closes the database connection."""
        self.conn.close()
        for f in self.photos:
            os.unlink(f[1])


class OphotTestCase(unittest.TestCase):
    """Test class for the ophot module."""

    def setUp(self):
        """Connects the Flask application to a temporary database and creates a
        client for testing.

        """
        s = tempfile.mkstemp()
        self.db_fd, app.config['DATABASE'] = tempfile.mkstemp()
        self.app = app.test_client()
        init_db()

    def tearDown(self):
        """Closes and deletes the database."""
        os.close(self.db_fd)
        os.unlink(app.config['DATABASE'])

    def test_add_new_category(self):
        """Test for adding a new category to the database."""
        with app.test_request_context('/'):
            before_request()
            categories = get_categories()
            new_id = add_new_category('foobar')
            new_categories = get_categories()
            self.assertNotIn(new_id, categories)
            self.assertEqual(max(categories) + 1, new_id)
            self.assertIn('foobar', new_categories.values())


    def test_get_categories(self):
        """Test for getting categories with their ID numbers."""
        with app.test_request_context('/'):
            before_request()
            categories = get_categories()
            self.assertIn((1, 'landscape'), categories.items())
            self.assertIn((2, 'personal'), categories.items())
            self.assertIn((3, 'portrait'), categories.items())


    def test_get_last_display_position(self):
        """
        Test for getting the display position of the photo which is to be
        displayed last.

        """
        with app.test_request_context('/'):
            before_request()
            with temp_photos():
                x = get_last_display_position(1)
                self.assertEqual(2, get_last_display_position(1))
                self.assertEqual(None, get_last_display_position(2))


    def test_require_logged_in(self):
        """
        Test for requiring that the user is marked as logged in on the session.

        """
        with app.test_request_context('/'):
            self.assertRaisesRegexp(Unauthorized, '401', require_logged_in)
            self.login()
            # here we expect no exception
            require_logged_in()


    def test_select_single(self):
        self.fail("Not yet implemented.")


    def login(self, username=app.config['USERNAME'],
              password=app.config['PASSWORD']):
        """Makes a POST request to login to the Flask application with the
        specified username and password. If no username and password are
        specified, the ones from the configuration will be used.

        """
{'fraction_non_alphanumeric': 0.08613309119211311, 'fraction_numerical': 0.0038915553249448697, 'mean_word_length': 2.8415545590433484, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '5416993', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2012, 'n_words': 755}