def test_modify_firstname(app, db, check_ui): if len(db.get_contact_list()) == 0: app.contact.create(Contact(firstname="Vlad", lastname="hater")) old_contacts = db.get_contact_list() index = randrange(len(old_contacts)) contact = Contact(firstname="New firstname") contact.id = old_contacts[index].id app.contact.modify_contact_by_id(contact.id, contact) new_contacts = db.get_contact_list() assert len(old_contacts) == len(new_contacts) assert old_contacts == new_contacts assert sorted(old_contacts, key=Contact.id_or_max) == sorted(new_contacts, key=Contact.id_or_max) if check_ui: assert sorted(new_contacts, key=Contact.id_or_max) == sorted(app.group.get_contact_list(), key=Contact.id_or_max)
def get_contact_list(self): list = [] cursor = self.connection.cursor() try: cursor.execute( "select id, firstname, lastname, home, mobile, work, " "phone2, address, email, email2,email3 " "from addressbook where deprecated='0000-00-00 00:00:00'") for row in cursor: (id, firstname, lastname, home, mobile, work, phone2, address, email, email2, email3) = row list.append( Contact(id=str(id), firstname=firstname, lastname=lastname, homephone=home, mobilephone=mobile, workphone=work, secondaryphone=phone2, address=address, email=email, email2=email2, email3=email3)) finally: cursor.close() return list
def get_contacts_not_in_group(self): list = [] cursor = self.connection.cursor() try: cursor.execute( "select id, firstname, lastname, address, home, mobile, work, email, email2, email3, " "phone2 from addressbook where deprecated = '0000-00-00 00:00:00'" "and id not in (select id from address_in_groups)") for row in cursor: (id, firstname, lastname, address, home, mobile, work, email, email2, email3, phone2) = row list.append( Contact(id=str(id), firstname=firstname, lastname=lastname, address=address, homephone=home, mobilephone=mobile, workphone=work, email=email, email2=email2, email3=email3, secondaryphone=phone2)) finally: cursor.close() return list
def get_contact_from_edit_page(self, index): wd = self.app.wd self.open_contact_to_edit_by_index(index) firstname = wd.find_element_by_name("firstname").get_attribute("value") lastname = wd.find_element_by_name("lastname").get_attribute("value") id = wd.find_element_by_name("id").get_attribute("value") homephone = wd.find_element_by_name("home").get_attribute("value") mobilephone = wd.find_element_by_name("mobile").get_attribute("value") workphone = wd.find_element_by_name("work").get_attribute("value") secondaryphone = wd.find_element_by_name("phone2").get_attribute( "value") address = wd.find_element_by_name("address").get_attribute("value") email = wd.find_element_by_name("email").get_attribute("value") email2 = wd.find_element_by_name("email2").get_attribute("value") email3 = wd.find_element_by_name("email3").get_attribute("value") return Contact(firstname=firstname, lastname=lastname, id=id, homephone=homephone, mobilephone=mobilephone, workphone=workphone, secondaryphone=secondaryphone, address=address, email=email, email2=email2, email3=email3)
def test_list_users_within_distance(self, mock_verify_id_token): """There must not be recommended users because anotehr users contacts contain my number.""" mock_time = pendulum.datetime(2020, 5, 21, 12) pendulum.set_test_now(mock_time) # insert user_1 mock_verify_id_token.return_value = dict(uid=mock_user_1["uid"]) response_1 = create_user_1(self.app) self.assertEqual(response_1.status_code, 200) # insert user_2 mock_verify_id_token.return_value = dict(uid=mock_user_2["uid"]) response_2 = create_user_2(self.app) self.assertEqual(response_2.status_code, 200) User.objects(uid=mock_user_1["uid"]).update(available=True) User.objects(uid=mock_user_2["uid"]).update(available=True) user_1 = User.objects(uid=mock_user_1["uid"]).first() user_2 = User.objects(uid=mock_user_2["uid"]).first() Contact(owner=user_2, phones=[user_1.phone], last_updated_at=pendulum.now().int_timestamp).save() response = self.app.get("/users/{user_id}/distance/10".format( user_id=response_1.get_json().get("_id")), headers=dict(uid=mock_user_1["uid"]), content_type="application/json") self.assertEqual(response.status_code, 500) recommendation = Recommendation.objects.first() self.assertEqual(len(recommendation.user_ids), 0)
def get_contacts_in_group_by_group_id(self, group_id): list = [] cursor = self.connection.cursor() sql = """select id, firstname, lastname, address, home, mobile, work, email, email2, email3, phone2 from addressbook where deprecated = '0000-00-00 00:00:00' and id in (select id from address_in_groups where group_id=%s)""" try: cursor.execute(sql, (int(group_id), )) for row in cursor: (id, firstname, lastname, address, home, mobile, work, email, email2, email3, phone2) = row list.append( Contact(id=str(id), firstname=firstname, lastname=lastname, address=address, homephone=home, mobilephone=mobile, workphone=work, email=email, email2=email2, email3=email3, secondaryphone=phone2)) finally: cursor.close() return list
def test_contacts_info_on_home_page_vs_db(app, db): if len(db.get_contact_list()) == 0: app.contact.create(Contact(firstname="Vlad", lastname="hater")) contact_from_homepage = sorted(app.contact.get_contact_list(), key=Contact.id_or_max) contact_from_db = sorted(db.get_contact_list(), key=Contact.id_or_max) for index in range(len(db.get_contact_list())): assert contact_from_homepage[index].firstname == contact_from_db[index].firstname assert contact_from_homepage[index].lastname == contact_from_db[index].lastname assert contact_from_homepage[index].address == contact_from_db[index].address assert contact_from_homepage[index].all_phones_from_home_page == merge_phones_like_on_home_page(contact_from_db[index]) assert contact_from_homepage[index].all_emails_from_home_page == merge_emails_like_on_home_page(contact_from_db[index])
def test_phone_query_performance(self): user = User.objects(uid=dldyddn0624_uid).first() start = pendulum.now().int_timestamp contact = Contact.objects(owner=user).first() phone_users = User.objects(phone__in=contact.phones).only("id").all() user_ids = set([str(user.id) for user in phone_users]) end = pendulum.now().int_timestamp elapsed = end - start print(elapsed) self.assertEqual(elapsed < 1, True) self.assertEqual(len(user_ids), 505)
def get_contact_from_view_page(self, index): wd = self.app.wd self.open_contact_view_by_index(index) text = wd.find_element_by_id("content").text homephone = re.search("H: (.*)", text).group(1) mobilephone = re.search("M: (.*)", text).group(1) workphone = re.search("W: (.*)", text).group(1) secondaryphone = re.search("P: (.*)", text).group(1) return Contact(homephone=homephone, mobilephone=mobilephone, workphone=workphone, secondaryphone=secondaryphone)
def test_delete_some_contact(app, db, check_ui): if len(db.get_contact_list()) == 0: app.contact.create(Contact(firstname="Vlad", lastname="hater")) old_contacts = db.get_contact_list() contact = random.choice(old_contacts) app.contact.delete_contact_by_id(contact.id) new_contacts = db.get_contact_list() old_contacts.remove(contact) assert old_contacts == new_contacts if check_ui: assert sorted(new_contacts, key=Contact.id_or_max) == sorted( app.group.get_contact_list(), key=Contact.id_or_max)
def test_add_contact_in_group(app, db, check_ui): if len(db.get_contact_list()) == 0: app.contact.create(Contact(firstname="Vlad", lastname="hater")) if len(db.get_group_list()) == 0: app.group.create(Group(name="test")) if len(db.get_contacts_not_in_group()) == 0: app.contact.create(Contact(firstname="Vlad", lastname="hater")) group = app.group.get_group_list() groups = random.choice(group) group_id = groups.id contacts = db.get_contacts_not_in_group() contact = random.choice(contacts) contact_id = contact.id print(db.get_contacts_in_group()) old_contact = db.get_contacts_in_group() app.contact.add_contact_to_group(contact_id, group_id) new_contacts = db.get_contacts_in_group() assert len(old_contact) + 1 == len(new_contacts) contact_ui = db.get_contacts_in_group() if check_ui: assert sorted(contact_ui, key=Contact.id_or_max) == sorted( app.group.get_group_list(), key=Contact.id_or_max)
def test_del_contact_in_group(app, db): if len(db.get_contact_list()) == 0: app.contact.create(Contact(firstname="Vlad", lastname="hater")) if len(db.get_group_list()) == 0: app.group.create(Group(name="test")) groups = db.get_groups_with_contacts() group = random.choice(groups) group_id = group.id contacts = db.get_contacts_in_group() contact = random.choice(contacts) contact_id = contact.id if len(db.get_contacts_in_group()) == 0: app.contact.add_contact_to_group(contact_id, group_id) old_contacts = db.get_contacts_in_group() app.contact.del_contact_in_group(contact_id, group_id) new_contacts = db.get_contacts_in_group() assert len(old_contacts) - 1 == len(new_contacts)
def get_contact_list(self): if self.contact_cache is None: wd = self.app.wd self.return_home_page() self.contact_cache = [] for row in wd.find_elements_by_name("entry"): cells = row.find_elements_by_tag_name("td") lastname = cells[1].text firstname = cells[2].text address = cells[3].text id = cells[0].find_element_by_name("selected[]").get_attribute( "value") all_phones = cells[5].text all_emails = cells[4].text self.contact_cache.append( Contact(firstname=firstname, lastname=lastname, address=address, id=id, all_phones_from_home_page=all_phones, all_emails_from_home_page=all_emails)) return list(self.contact_cache)
def test_does_know_each_other2(self, mock_verify_id_token): """Should be True at least when one person has the others phone number.""" mock_time = pendulum.datetime(2020, 5, 21, 12) pendulum.set_test_now(mock_time) # insert user_1 mock_verify_id_token.return_value = dict(uid=mock_user_1["uid"]) response_1 = create_user_1(self.app) self.assertEqual(response_1.status_code, 200) # insert user_2 mock_verify_id_token.return_value = dict(uid=mock_user_2["uid"]) response_2 = create_user_2(self.app) self.assertEqual(response_2.status_code, 200) user_1 = User.objects(uid=mock_user_1["uid"]).first() user_2 = User.objects(uid=mock_user_2["uid"]).first() Contact(owner=user_1, phones=[user_2.phone], last_updated_at=pendulum.now().int_timestamp).save() does_know_1 = user_1.does_know_each_other(user_2) does_know_2 = user_2.does_know_each_other(user_1) self.assertEqual(does_know_1, True) self.assertEqual(does_know_2, True)
def convert(contact): return Contact(id=str(contact.id), firstname=contact.fristname, lastname=contact.lastname)
def clean(contact): return Contact(id=contact.id, firstname=contact.firstname.strip())
from model.models import Contact testdata = [ Contact(firstname="firstname1", middlename="middlename1", lastname="lastname1", company="company1", addreswork="addreswork1", homephone="homephone1", mobilephone="mobilephone1", workphone="workphone1", email="email1", email2="email21", email3="email31", bday="30", bmonth="August", byear="1994", address="address1", address2="address21", secondaryphone="secondaryphone1", notes="notes1"), Contact(firstname="firstname2", middlename="middlename2", lastname="lastname2", company="company2", addreswork="addreswork2", homephone="homephone2", mobilephone="mobilephone2", workphone="workphone2", email="email2", email2="email22",
def random_string(prefix, maxlen): symbols = string.ascii_letters + string.digits + string.punctuation + "" * 10 return prefix + "".join( [random.choice(symbols) for i in range(random.randrange(maxlen))]) testdata = [ Contact(firstname=random_string("firstname", 20), middlename=random_string("middlename", 20), lastname=random_string("lastname", 20), company=random_string("company", 10), addreswork=random_string("addreswork", 20), homephone=random_phone_number(), mobilephone=random_phone_number(), workphone=random_phone_number(), email=random_email(10), email2=random_email(10), email3=random_email(8), bday="30", bmonth="August", byear="1994", address=random_string("address", 10), address2="vampilov", secondaryphone=random_phone_number(), notes=random_string("notes", 50)) for i in range(5) ] file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", f) with open(file, "w") as out: jsonpickle.set_encoder_options("json", indent=2) out.write(jsonpickle.encode(testdata))