HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/tests/leads/test_models.py
from buyercall.blueprints.contacts.models import Contact
from buyercall.blueprints.user.models import User
from buyercall.blueprints.leads.models import LeadNotes, Lead
from datetime import date
import logging as log
from sqlalchemy import or_, extract, and_
from datetime import datetime, timedelta
import pytest


class TestLeadNoteModel(object):

    def test_lead_note_create(self, leads):

        lead = Lead.query.filter().first()
        user = User.query.filter().first()

        note = {
            'created_on': datetime.now(),
            'updated_on': datetime.now(),
            'text': 'The best note ever',
            'lead_id': lead.id,
            'user_id': user.id,
            'is_enabled': True
        }
        result = LeadNotes.create(note)

        if result:
            lead_note = LeadNotes.query.filter(LeadNotes.text == 'The best note ever').first()

            if lead_note and lead_note.lead_id == lead.id and lead_note.user_id == user.id and lead_note.is_enabled == True:
                assert True

    def test_lead_note_update_enable(self, leads):
        lead_note = LeadNotes.query.filter(LeadNotes.text == 'test note 1').first()
        user = User.query.filter().first()
        date_updated = datetime.now()
        result = LeadNotes.update(lead_note.id, 'Surely this is the best note', date_updated, user.id, True)

        if result:
            lead_note = LeadNotes.query.filter(LeadNotes.text == 'Surely this is the best note').first()

            if lead_note and lead_note.user_id == user.id and lead_note.updated_on == date_updated and lead_note.is_enabled == True:
                assert True

    def test_lead_note_update_disable(self, leads):
        lead_note = LeadNotes.query.filter(LeadNotes.text == 'test note 2').first()
        user = User.query.filter().first()
        date_updated = datetime.now()
        result = LeadNotes.update(lead_note.id, 'Surely this is the best note', date_updated, user.id, False)

        if result:
            lead_note = LeadNotes.query.filter(LeadNotes.text == 'test note 2').first()

            if lead_note and lead_note.user_id == user.id and lead_note.updated_on == date_updated and lead_note.is_enabled == False:
                assert True

    def test_lead_note_date_edited(self, leads):
        lead_note = LeadNotes.query.filter(LeadNotes.text == 'test note 2').first()

        if lead_note and lead_note.date_edited == lead_note.updated_on.strftime('%Y-%m-%d %H:%M:%S'):
            assert True

    def test_lead_note_user_fullname(self, leads):
        lead_note = LeadNotes.query.filter(LeadNotes.text == 'test note 1').first()

        if lead_note and lead_note.user_fullname == 'random_admin_fn random_admin_ln':
            assert True


class TestLeadModel(object):

    @pytest.mark.skip(reason="method not currently used in system")
    def test_search(self, leads):
        assert True

    @pytest.mark.skip(reason="accesses Twilio")
    def test_update_lead_twilio(self, leads):
        assert True

    @pytest.mark.skip(reason="warning relating to sql query")
    def test_get_last_known_name(self, leads):

        lead = Lead.query.filter(and_(Lead.firstname == 'Random FN', Lead.lastname == 'Random LN')).first()

        if lead:
            retrieved_name = Lead.get_last_known_name(lead.partnership_account_id, lead.phonenumber)

            if retrieved_name:
                assert True

    def test_assigned_full_name(self, leads):
        lead = Lead.query.filter(and_(Lead.firstname == 'Random FN', Lead.lastname == 'Random LN')).first()

        if lead and lead.assigned_full_name == 'no2 first name no2 last name':
            assert True
        else:
            assert False

    @pytest.mark.skip(reason="method not currently used in system")
    def test_full_name_set(self, leads):
        lead = Lead.query.filter(and_(Lead.firstname == 'Random FN', Lead.lastname == 'Random LN')).first()
        lead.full_name("first last")

        if lead.firstname == 'first' and lead.lastname == 'last':
            assert True

    def test_full_name_get(self, leads):
        lead = Lead.query.filter(and_(Lead.firstname == 'Random FN', Lead.lastname == 'Random LN')).first()

        if lead and lead.full_name == 'Random FN Random LN':
            assert True
        else:
            assert False

    @pytest.mark.skip(reason="method not currently used in system")
    def test_note_count(self, leads):
        lead = Lead.query.filter(and_(Lead.firstname == 'Random FN', Lead.lastname == 'Random LN')).first()

        if lead and lead.note_count == 2:
            assert True

    @pytest.mark.skip(reason="method not currently used in system")
    def test_interaction_count(self, leads):
        lead = Lead.query.filter(and_(Lead.firstname == 'Random FN', Lead.lastname == 'Random LN')).first()

        if lead and lead.interaction_count == 1:
            assert True

    def test_interaction_time(self, leads):
        lead = Lead.query.filter(and_(Lead.firstname == 'Random FN', Lead.lastname == 'Random LN')).first()

        if lead and lead.starttime.strftime('%Y-%m-%d %H:%M:%S') == lead.interaction_time:
            assert True
        else:
            assert False

    def test_agent_name(self, leads):
        lead = Lead.query.filter(and_(Lead.firstname == 'Random FN', Lead.lastname == 'Random LN')).first()

        if lead and lead.agent_name == 'tbd first name tbd last name':
            assert True
        else:
            assert False

    def test_source_phone(self, leads):
        lead = Lead.query.filter(and_(Lead.firstname == 'Random FN', Lead.lastname == 'Random LN')).first()

        if lead and lead.source == 'mobile message number':
            assert True
        else:
            assert False

    def test_source_widget(self, leads):
        lead = Lead.query.filter(and_(Lead.firstname == 'Widget FN', Lead.lastname == 'Widget LN')).first()

        if lead and lead.source == 'widget name':
            assert True
        else:
            assert False

    @pytest.mark.skip(reason="method not currently used in system")
    def test_source_id(self, leads):
        assert True