<commit_before># -*- coding: UTF-8 -*-

import os
import os.path
import json
import platform
import tempfile
import logging

if platform.python_version() < '2.7':
    import unittest2 as unittest
else:
    import unittest

import rogue_scores.web
from rogue_scores.web import init_scores, sanitize_score, \
        sanitize_scores, merge_scores, index, scores_upload

class FakeRequest(object):
    scores = '[]'

    def __init__(self, *args, **kwargs):
        self.form = {'scores': FakeRequest.scores}
        self.headers = {}

rogue_scores.web.app.logger.handlers = [logging.FileHandler('/dev/null')]

class TestRogueWeb(unittest.TestCase):

    def setUp(self):
        self._scores = rogue_scores.web.app.config['SCORES']
        self.tmp = tempfile.NamedTemporaryFile(delete=False)
        rogue_scores.web.app.config['SCORES'] = self.tmp.name

    def tearDown(self):
        rogue_scores.web.app.config['SCORES'] = self._scores
        if os.path.isfile(self.tmp.name):
            os.unlink(self.tmp.name)

    # == .init_scores == #

    def test_init_scores(self):
        os.unlink(self.tmp.name)
        self.assertFalse(os.path.isfile(self.tmp.name))
        init_scores()
        self.assertTrue(os.path.isfile(self.tmp.name))
        with open(self.tmp.name) as f:
            self.assertEquals('[]', f.read())
<commit_msg>two more tests for web.init<commit_after># -*- coding: UTF-8 -*-

import os
import os.path
import json
import platform
import tempfile
import logging

if platform.python_version() < '2.7':
    import unittest2 as unittest
else:
    import unittest

import rogue_scores.web
from rogue_scores.web import init_scores, sanitize_score, \
        sanitize_scores, merge_scores, index, scores_upload

class FakeRequest(object):
    scores = '[]'

    def __init__(self, *args, **kwargs):
        self.form = {'scores': FakeRequest.scores}
        self.headers = {}

rogue_scores.web.app.logger.handlers = [logging.FileHandler('/dev/null')]

class TestRogueWeb(unittest.TestCase):

    def setUp(self):
        self._scores = rogue_scores.web.app.config['SCORES']
        self.tmp = tempfile.NamedTemporaryFile(delete=False)
        rogue_scores.web.app.config['SCORES'] = self.tmp.name

    def tearDown(self):
        rogue_scores.web.app.config['SCORES'] = self._scores
        if os.path.isfile(self.tmp.name):
            os.unlink(self.tmp.name)

    # == .init_scores == #

    def test_init_scores_with_dir_without_file(self):
        os.unlink(self.tmp.name)
        self.assertFalse(os.path.isfile(self.tmp.name))
        init_scores()
        self.assertTrue(os.path.isfile(self.tmp.name))
        with open(self.tmp.name) as f:
            self.assertEquals('[]', f.read())

    def test_init_scores_with_dir_with_file(self):
        s = 's$@%r9'
        with open(self.tmp.name, 'w') as f:
            f.write(s)
        init_scores()
        with open(self.tmp.name) as f:
            self.assertEquals(s, f.read())

    def test_init_scores_without_dir(self):
        path = tempfile.mkdtemp()
        os.rmdir(path)
        self.assertFalse(os.path.exists(path))
        rogue_scores.web.app.config['SCORES'] = name = '%s/scores' % path
        init_scores()
        self.assertTrue(os.path.isdir(path))
        self.assertTrue(os.path.isfile(name))
        with open(name) as f:
            self.assertEquals('[]', f.read())

{'fraction_non_alphanumeric': 0.13447867298578198, 'fraction_numerical': 0.002665876777251185, 'mean_word_length': 2.868270332187858, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4405225', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1117, 'n_words': 198}