<commit_before># -*- coding: utf-8 -*-
"""
engine-groups/tests.py
"""

import os

from django.contrib.auth.models import User
from mongoengine.connection import _get_db as get_db
from mongoengine.django.tests import MongoTestCase

from depot.models import Resource
from engine_groups.models import Account, Collection, Membership, \
    MEMBER_ROLE, ADMIN_ROLE

SEP = '**************'

# import json
# from mongoengine import base as mongobase
# from pymongo import json_util

def _dump_collections(collection_names=None):
    if collection_names is None:
        collection_names = [coll for coll in get_db().collection_names() if coll != 'system.indexes']
    for coll in collection_names:
        os.system('mongoexport -d {0} -c {1} -o output_{1}.json'.format(get_db().name, coll))

def _load_collections(collection_names=None, drop='--drop'):
    if collection_names is None:
        collection_names = [coll for coll in get_db().collection_names() if coll != 'system.indexes']
    for coll in collection_names:
        os.system('mongoimport -d {0} -c {1} --file output_{1}.json {2}'.format(get_db().name, coll, drop))

def _print_db_info():
    """docstring for _print_db_info"""
    print SEP
    print 'Account: ', Account.objects.count()
    print 'Membership: ', Membership.objects.count()
    print 'Collection: ', Collection.objects.count()
    print SEP

def _make_user(name):
    return User.objects.create_user(name, email="%<EMAIL>" % name, password='password')

class AccountsBaseTest(MongoTestCase):
    def setUp(self):
        # _print_db_info()
        self.user_bob = _make_user('bob')
        self.user_humph = _make_user('humph')
        self.user_jorph = _make_user('jorph')
        self.user_group = _make_user('group')

        self.bob = Account.objects.create(name="<NAME>", email="<EMAIL>", local_id=str(self.user_bob.id))
        self.humph = Account.objects.create(name="<NAME>", email="<EMAIL>", local_id=str(self.user_humph.id))
        self.jorph = Account.objects.create(name="<NAME>", email="<EMAIL>", local_id=str(self.user_jorph.id))
        self.group = Account.objects.create(
            name="<NAME>", 
            email="<EMAIL>", 
            local_id=str(self.user_group.id),
            )
        self.group.add_member(self.humph)
        self.group.add_member(self.jorph, role=ADMIN_ROLE)

    def tearDown(self):
        # _print_db_info()
        pass

class CollectionsTest(AccountsBaseTest):

    def test_creation(self):

        coll1 = Collection.objects.create(name='Test Collection', owner=self.bob)
        coll2 = Collection.objects.create(name='Test Collection 2', owner=self.humph)
        self.assertEqual(1, Collection.objects(owner=self.bob).count())

        coll1.add_accounts([self.humph, self.jorph])
        coll1.add_accounts([self.humph])
        coll1.add_accounts([self.jorph])
        self.assertEqual(2, len(coll1.accounts))

        self.assertEqual(1, len(self.humph.collections))
        self.assertEqual(self.humph.collections[0].name, 'Test Collection')

        coll2.add_accounts([self.jorph])
        self.assertEqual(1, len(coll2.accounts))

        self.assertEqual(2, len(self.jorph.collections))

class AccountsTest(AccountsBaseTest):
    def test_account(self):
        self.assertEqual(Account.objects.count(), 4)
        _dump_collections()

    def test_load_accounts(self):
        """docstring for test_load_accounts"""
        _load_collections()
        self.assertEqual(Account.objects.count(), 4)

        arthur = _make_user('arthur')
        acct1 = Account.objects.create(name="<NAME>", email="<EMAIL>", local_id=str(arthur.id))

        self.group.add_member(acct1)
        self.group.save()
        self.assertEqual(len(self.group.members), 3)

        
        
        
        <commit_msg>changed to use subprocess in tests.<commit_after># -*- coding: utf-8 -*-
"""
engine-groups/tests.py
"""

import subprocess

from django.contrib.auth.models import User
from mongoengine.connection import _get_db as get_db
from mongoengine.django.tests import MongoTestCase

from depot.models import Resource
from engine_groups.models import Account, Collection, Membership, \
    MEMBER_ROLE, ADMIN_ROLE

SEP = '**************'

# import json
# from mongoengine import base as mongobase
# from pymongo import json_util

def _dump_collections(collection_names=None):
    if collection_names is None:
        collection_names = [coll for coll in get_db().collection_names() if coll != 'system.indexes']
    for coll in collection_names:
        subprocess.call([
            'mongoexport', 
            '-d', '%s' % get_db().name, 
            '-c', '%s' % coll, 
            '-o', 'output_%s.json' % coll])

def _load_collections(collection_names=None, drop='--drop'):
    if collection_names is None:
        collection_names = [coll for coll in get_db().collection_names() if coll != 'system.indexes']
    for coll in collection_names:
        subprocess.call([
            'mongoimport', 
            '-d', '%s' % get_db().name, 
            '-c', '%s' % coll, 
            '--file', 'output_%s.json' % coll, 
            '%s' % drop])

def _print_db_info():
    """docstring for _print_db_info"""
    print SEP
    print 'Account: ', Account.objects.count()
    print 'Membership: ', Membership.objects.count()
    print 'Collection: ', Collection.objects.count()
    print SEP

def _make_user(name):
    return User.objects.create_user(name, email="<EMAIL>" % name, password='password')

class AccountsBaseTest(MongoTestCase):
    def setUp(self):
        # _print_db_info()
        self.user_bob = _make_user('bob')
        self.user_humph = _make_user('humph')
        self.user_jorph = _make_user('jorph')
        self.user_group = _make_user('group')

        self.bob = Account.objects.create(name="<NAME>", email="<EMAIL>", local_id=str(self.user_bob.id))
        self.humph = Account.objects.create(name="<NAME>", email="<EMAIL>", local_id=str(self.user_humph.id))
        self.jorph = Account.objects.create(name="<NAME>", email="<EMAIL>", local_id=str(self.user_jorph.id))
        self.group = Account.objects.create(
            name="<NAME>", 
            email="<EMAIL>", 
            local_id=str(self.user_group.id),
            )
        self.group.add_member(self.humph)
        self.group.add_member(self.jorph, role=ADMIN_ROLE)

    def tearDown(self):
        # _print_db_info()
        pass

class CollectionsTest(AccountsBaseTest):

    def test_creation(self):

        coll1 = Collection.objects.create(name='Test Collection', owner=self.bob)
        coll2 = Collection.objects.create(name='Test Collection 2', owner=self.humph)
        self.assertEqual(1, Collection.objects(owner=self.bob).count())

        coll1.add_accounts([self.humph, self.jorph])
        coll1.add_accounts([self.humph])
        coll1.add_accounts([self.jorph])
        self.assertEqual(2, len(coll1.accounts))

        self.assertEqual(1, len(self.humph.collections))
        self.assertEqual(self.humph.collections[0].name, 'Test Collection')

        coll2.add_accounts([self.jorph])
        self.assertEqual(1, len(coll2.accounts))

        self.assertEqual(2, len(self.jorph.collections))

class AccountsTest(AccountsBaseTest):
    def test_account(self):
        self.assertEqual(Account.objects.count(), 4)
        _dump_collections()

    def test_load_accounts(self):
        """docstring for test_load_accounts"""
        _load_collections()
        self.assertEqual(Account.objects.count(), 4)

        arthur = _make_user('arthur')
        acct1 = Account.objects.create(name="<NAME>", email="<EMAIL>", local_id=str(arthur.id))

        self.group.add_member(acct1)
        self.group.save()
        self.assertEqual(len(self.group.members), 3)

        
        
        
        
{'fraction_non_alphanumeric': 0.13942739761201695, 'fraction_numerical': 0.006290923096674798, 'mean_word_length': 3.172469201928227, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '1741444', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2449, 'n_words': 451}