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_new/buyercall/buyercall/tests/contacts/test_models.py
from sqlalchemy import func, or_, and_, extract
from buyercall.blueprints.contacts.models import Contact, ContactNotes
from buyercall.blueprints.user.models import User
from datetime import datetime
import pytest

class TestContact(object):

    def test_contact_name(self, contacts):
        contact_1 = Contact.query\
            .filter(and_(Contact.firstname == 'first_message', Contact.lastname == 'last_message'))\
            .first()

        contact_2 = Contact.query\
            .filter(and_(Contact.firstname == 'first_message', Contact.lastname == ''))\
            .first()

        contact_3 = Contact.query\
            .filter(Contact.caller_id == 'dude')\
            .first()

        contact_4 = Contact.query\
            .filter(
            and_(Contact.firstname == '', Contact.lastname == '', Contact.caller_id == ''))\
            .first()

        if contact_1.name == 'first_message last_message' \
                and contact_2.name == 'first_message' \
                and contact_3.name == 'dude' \
                and contact_4.name == 'Unknown':
            assert True
        else:
            assert False

    def test_contact_message(self, contacts):
        contact_1 = Contact.query \
            .filter(and_(Contact.firstname == 'first_message', Contact.lastname == 'last_message')) \
            .first()

        contact_2 = Contact.query \
            .filter(and_(Contact.firstname == 'first_message', Contact.lastname == '')) \
            .first()

        contact_3 = Contact.query \
            .filter(Contact.caller_id == 'dude') \
            .first()

        contact_4 = Contact.query \
            .filter(
            and_(Contact.firstname == '', Contact.lastname == '', Contact.caller_id == '')) \
            .first()

        if contact_1.message_name == 'first_message last_message' \
                and contact_2.message_name == 'first_message' \
                and contact_3.message_name == '1234567893' \
                and contact_4.message_name == 'Unknown':
            assert True
        else:
            assert False

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

    def test_contact_note_count(self, contacts):

        contact = Contact.query \
            .filter(and_(Contact.firstname == 'first_message', Contact.lastname == 'last_message')) \
            .first()

        if contact.note_count == 2:
            assert True
        else:
            assert False


class TestContactNote(object):

    def test_contact_note_create(self, contacts):
        user = User.query.filter(User.email == 'admin@localhost.com')\
            .first()

        contact = Contact.query\
            .filter(and_(Contact.firstname == 'first_message',
                         Contact.lastname == 'last_message',
                         Contact.caller_id == 'i dont know'))\
            .first()

        note_params = {
            'text': 'this is the best note ever',
            'contact_id': contact.id,
            'created_on': datetime.now(),
            'updated_on': datetime.now(),
            'user_id': int(user.id),
            'is_enabled': True,
            'partnership_account_id': int(contact.partnership_account_id)
        }

        result = ContactNotes.create(note_params)

        note = ContactNotes.query\
            .filter(and_(ContactNotes.partnership_account_id == contact.partnership_account_id,
                         ContactNotes.contact_id == contact.id,
                         ContactNotes.user_id == user.id,
                         ContactNotes.text == 'this is the best note ever'))\
            .first()

        if note is not None and result:
            assert True
        else:
            assert False

    def test_contact_note_update(self, contacts):
        contact_note_before = ContactNotes.query\
            .filter(ContactNotes.text == 'Text bro 1')\
            .first()

        result = ContactNotes.update(contact_note_before.id, 'tsek', '2019-12-31', contact_note_before.user_id, True)

        contact_note_after = ContactNotes.query\
            .filter(ContactNotes.id == contact_note_before.id)\
            .first()

        if contact_note_after.text == 'tsek' \
                and contact_note_after.date_edited == '2019-12-31 00:00:00' \
                and contact_note_after.is_enabled == True \
                and result == True:
            assert True
        else:
            assert False

    def test_contact_note_property(self, contacts):
        contact_note = ContactNotes.query\
            .filter(ContactNotes.text == 'Text bro 1')\
            .first()

        if contact_note.user_fullname == 'admin_firstname admin_lastname' \
                and contact_note.date_edited == '2019-10-16 00:00:00':
            assert True
        else:
            assert False