__author__ = 'adeksandrcernov'

from model.contact import Contact
from random import randrange

def test_edit_contact_lastname(app,db):
        if app.contacts.count() == 0:
                app.contacts.add_new_contact(Contact(firstname="test",address="test"))
        old_contacts = db.get_contacts_list()
        index = randrange (len(old_contacts))
        contact = Contact(firstname="Newname")
        contact.id = old_contacts[index].id
        app.contacts.edit_contact_by_index(index, contact)
        new_contacts = db.get_contacts_list()
        assert len(old_contacts) == len(new_contacts)
        old_contacts[index] = contact
        assert sorted(old_contacts, key=Contact.id_or_max) == sorted (new_contacts,key=Contact.id_or_max)

#def test_edit_contact_address(app):
#        if app.contacts.count() == 0:
#                app.contacts.add_new_contact(Contact(firstname="test", address="test"))
#        old_contacts = app.contacts.get_contacts_list()
#        contact = Contact(address="New address")
#        contact.id = old_contacts[0].id
#        app.contacts.edit_first_contact(contact)
#        new_contacts = app.contacts.get_contacts_list()
#        assert len(old_contacts) == len(new_contacts)
#        old_contacts[0] = contact
#        assert sorted(old_contacts, key=Contact.id_or_max) == sorted (new_contacts,key=Contact.id_or_max)