<commit_before><commit_msg>Added required (numberic) weight and optional reason to relationship<commit_after>from django.test import SimpleTestCase
from django.test.client import RequestFactory

import crits.domains.views as views
from crits.relationships.handlers import forge_relationship, update_relationship_reasons, update_relationship_weights
from crits.core.user import CRITsUser
from crits.campaigns.campaign import Campaign

TUSER_NAME = "test_user"
TUSER_PASS = <PASSWORD>"
TUSER_EMAIL = "<EMAIL>"
TUSER2_NAME = "second_testUser"
TUSER2_PASS = <PASSWORD>"
TUSER2_EMAIL = "<EMAIL>"
TCAMPAIGN1 = "Test_Campain1"
TCAMPAIGN2 = "Test_Campain2"
TRELATIONSHIP_TYPE = "Allocated"
TRELATIONSHIP_WEIGHT = 8
TRELATIONSHIP_NEW_WEIGHT = 2
TRELATIONSHIP_NEW_REASON = "Because I Said So"

def prep_db():
    """
    Prep database for test.
    """

    clean_db()
    # Add User
    user = CRITsUser.create_user(
                          username=TUSER_NAME,
                          password=<PASSWORD>,
                          email=TUSER_EMAIL,
                          )
    user.save()
    user2 = CRITsUser.create_user(
                          username=TUSER2_NAME,
                          password=<PASSWORD>,
                          email=TUSER2_EMAIL,
                          )
    user2.save()
    campaign1 = Campaign(name=TCAMPAIGN1)
    campaign1.save(username=user.username)
    campaign2 = Campaign(name=TCAMPAIGN2)
    campaign2.save(username=user.username)

def clean_db():
    """
    Clean database for test.
    """

    user = CRITsUser.objects(username=TUSER_NAME).first()
    if user:
        user.delete()
    user2 = CRITsUser.objects(username=TUSER2_NAME).first()
    if user2:
        user2.delete()
    campaign1 = Campaign.objects(name=TCAMPAIGN1).first()
    if campaign1:
        campaign1.delete()
    campaign2 = Campaign.objects(name=TCAMPAIGN2).first()
    if campaign2:
        campaign2.delete()


class RelationshipWeightAndReasonTests(SimpleTestCase):
    """
    Test Domain Handlers
    """

    def setUp(self):
        prep_db()
        self.user = CRITsUser.objects(username=TUSER_NAME).first()
        self.user2 = CRITsUser.objects(username=TUSER2_NAME).first()
        self.campaign1 = Campaign.objects(name=TCAMPAIGN1).first()
        self.campaign2 = Campaign.objects(name=TCAMPAIGN2).first()
        relationship = forge_relationship(left_class=self.campaign1,
                                          right_class=self.campaign2,
                                          rel_type=TRELATIONSHIP_TYPE,
                                          analyst=self.user.username, 
                                          rel_weight=TRELATIONSHIP_WEIGHT)

    def tearDown(self):
        clean_db()
        
    def testCreateRelationship(self):
        relationship1 = self.campaign1.relationships[0]
        relationship2 = self.campaign2.relationships[0]
        self.assertEqual(relationship1.rel_weight, TRELATIONSHIP_WEIGHT)
        self.assertEqual(relationship2.rel_weight, TRELATIONSHIP_WEIGHT)
        self.assertEqual(relationship1.analyst, self.user.username)
        self.assertEqual(relationship2.analyst, self.user.username)

    def testChangingReason(self):
        relationship1 = self.campaign1.relationships[0]
        relationship2 = self.campaign2.relationships[0]
        
        self.assertEqual(relationship1.rel_reason, "N/A")
        self.assertEqual(relationship2.rel_reason, "N/A")
        
        update_relationship_reasons(left_class=self.campaign1,
                                    right_class=self.campaign2,
                                    rel_type=TRELATIONSHIP_TYPE,
                                    analyst=self.user2.username, 
                                    new_reason=TRELATIONSHIP_NEW_REASON)
        
        campaign1 = Campaign.objects.get(id=self.campaign1.id)
        campaign2 = Campaign.objects.get(id=self.campaign2.id)
        relationship1 = campaign1.relationships[0]
        relationship2 = campaign2.relationships[0]
        
        self.assertEqual(relationship1.rel_reason, TRELATIONSHIP_NEW_REASON)
        self.assertEqual(relationship2.rel_reason, TRELATIONSHIP_NEW_REASON)
        
    def testChangingWeight(self):
        relationship1 = self.campaign1.relationships[0]
        relationship2 = self.campaign2.relationships[0]
        
        self.assertEqual(relationship1.rel_weight, TRELATIONSHIP_WEIGHT)
        self.assertEqual(relationship2.rel_weight, TRELATIONSHIP_WEIGHT)
        
        update_relationship_weights(left_class=self.campaign1,
                                    right_class=self.campaign2,
                                    rel_type=TRELATIONSHIP_TYPE,
                                    analyst=self.user2.username, 
                                    new_weight=TRELATIONSHIP_NEW_WEIGHT)
        
        self.assertEqual(relationship1.rel_weight, TRELATIONSHIP_NEW_WEIGHT)
        self.assertEqual(relationship2.rel_weight, TRELATIONSHIP_NEW_WEIGHT)
{'fraction_non_alphanumeric': 0.08215353938185443, 'fraction_numerical': 0.01694915254237288, 'mean_word_length': 2.302172481895984, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6787167', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1424, 'n_words': 199}