<commit_before>from datetime import datetime, timedelta
from nose.tools import *  # noqa

from website.models import User
from tests.base import OsfTestCase
from tests.factories import AuthUserFactory
from scripts.analytics.user_count import count


class TestUserCount(OsfTestCase):
    def setUp(self):
        self.today = datetime.today()
        self.a_while_ago = self.today - timedelta(2)
        super(TestUserCount, self).setUp()
        for i in range(0, 3):
            u = AuthUserFactory()
            u.is_registered = True
            u.password = '<PASSWORD>' + str(i)
            u.date_confirmed = self.today
            u.save()
        u = AuthUserFactory()
        u.is_registered = True
        u.password = '<PASSWORD>'
        u.date_confirmed = self.a_while_ago
        u.save()
        for i in range(0, 2):
            u = AuthUserFactory()
            u.date_confirmed = None
            u.save()
        u = AuthUserFactory()
        u.date_disabled = self.today
        u.save()
        u = AuthUserFactory()
        u.date_disabled = self.a_while_ago
        u.save()

    def tearDown(self):
        User.remove()

    def test_gets_users(self):
        data = count(self.today + timedelta(1))
        assert_equal(data['active_users'], 4)
        assert_equal(data['unconfirmed_users'], 2)
        assert_equal(data['deactivated_users'], 2)

    def test_gets_only_users_from_given_date(self):
        data = count(self.today - timedelta(1))
        assert_equal(data['active_users'], 1)
        assert_equal(data['unconfirmed_users'], 0)
        assert_equal(data['deactivated_users'], 1)<commit_msg>Fix user summary tests, manually set user registered dates in mmongo<commit_after>from datetime import datetime, timedelta
from nose.tools import *  # noqa
from framework.mongo import database
from framework.transactions.context import TokuTransaction

from website.models import User
from tests.base import OsfTestCase
from tests.factories import AuthUserFactory
from scripts.analytics.user_summary import UserSummary


def modify_user_dates_in_mongo(new_date):
    with TokuTransaction():
        for user in database.user.find():
            database['user'].find_and_modify(
                {'_id': user['_id']},
                {'$set': {
                    'date_registered': new_date
                }}
            )

class TestUserCount(OsfTestCase):
    def setUp(self):
        self.yesterday = datetime.today() - timedelta(1)
        self.a_while_ago = datetime.today() - timedelta(2)
        super(TestUserCount, self).setUp()
        for i in range(0, 3):
            u = AuthUserFactory()
            u.is_registered = True
            u.password = '<PASSWORD>' + str(i)
            u.date_confirmed = self.yesterday
            u.save()
        u = AuthUserFactory()
        u.is_registered = True
        u.password = '<PASSWORD>'
        u.date_confirmed = self.a_while_ago
        u.save()
        for i in range(0, 2):
            u = AuthUserFactory()
            u.date_confirmed = None
            u.save()
        u = AuthUserFactory()
        u.date_disabled = self.yesterday
        u.save()
        u = AuthUserFactory()
        u.date_disabled = self.a_while_ago
        u.save()

        modify_user_dates_in_mongo(self.yesterday)

    def tearDown(self):
        User.remove()

    def test_gets_users(self):
        data = UserSummary().get_events(self.yesterday.date())[0]
        assert_equal(data['status']['active'], 4)
        assert_equal(data['status']['unconfirmed'], 2)
        assert_equal(data['status']['deactivated'], 2)

    def test_gets_only_users_from_given_date(self):
        data = UserSummary().get_events(self.a_while_ago.date())[0]
        assert_equal(data['status']['active'], 1)
        assert_equal(data['status']['unconfirmed'], 0)
        assert_equal(data['status']['deactivated'], 1)
{'fraction_non_alphanumeric': 0.11162670791441093, 'fraction_numerical': 0.0069605568445475635, 'mean_word_length': 2.4581105169340463, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '910102', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1133, 'n_words': 241}