def test_set_history_doc_refs_as_deleted_on_uuid_0():
    """Marking an entity with dealroom_uuid = 0, should raise an error"""
    db = fc.new_connection(project=TEST_PROJECT)
    FINAL_URL = "foo77.bar"
    fc.set_history_doc_refs(db, {
        "dealroom_uuid": uuid(),
        "final_url": FINAL_URL
    })
    res = fc.set_history_doc_refs(db, {"dealroom_uuid": "0"}, FINAL_URL)

    assert res == StatusCode.ERROR
    doc_ref = fc.get_history_doc_refs(db, final_url=FINAL_URL)["final_url"][0]
    doc_ref.delete()
def test_set_history_doc_refs_existing_by_url_with_new_dealroom_id():
    """Update a new document, using a valid but already used final_url (with another dealroom_id=-1), should be ok"""
    db = fc.new_connection(project=TEST_PROJECT)
    new_dr_id = randint(1e5, 1e8)
    fc.set_history_doc_refs(db, {"final_url": "foo9.bar", "dealroom_id": -1})
    res = fc.set_history_doc_refs(db, {
        "final_url": "foo9.bar",
        "dealroom_id": new_dr_id
    }, new_dr_id)
    assert res == StatusCode.UPDATED
    doc_ref = fc.get_history_doc_refs(db,
                                      dealroom_id=new_dr_id)["dealroom_id"][0]
    doc_ref.delete()
def test_set_history_doc_refs_as_deleted_on_uuid():
    """Marking an entity as deleted (dealroom_uuid = -2), should be ok"""
    db = fc.new_connection(project=TEST_PROJECT)
    FINAL_URL = "foo77.bar"
    fc.set_history_doc_refs(db, {
        "dealroom_uuid": uuid(),
        "final_url": FINAL_URL
    })
    res = fc.set_history_doc_refs(db, {"dealroom_uuid": -2}, FINAL_URL)

    assert res == StatusCode.UPDATED
    doc_ref = fc.get_history_doc_refs(db, final_url=FINAL_URL)["final_url"][0]
    doc_ref.delete()
def test_set_history_doc_refs_existing_by_url_using_payload_w_id():
    """Update an existing document with dealroom_id=-1, using the final_url from the payload"""
    db = fc.new_connection(project=TEST_PROJECT)
    fc.set_history_doc_refs(db, {"final_url": "foo5.bar", "dealroom_id": -1})
    dealroom_id = randint(1e5, 1e8)
    res = fc.set_history_doc_refs(db, {
        "final_url": "foo5.bar",
        "dealroom_id": dealroom_id
    })

    assert res == StatusCode.UPDATED
    doc_ref = fc.get_history_doc_refs(
        db, dealroom_id=dealroom_id)["dealroom_id"][0]
    doc_ref.delete()
def test_set_history_doc_refs_for_deleted_company_w_uuid():
    """Create a document for a new company that appears previously as deleted should be ok."""
    # Tests this bug https://dealroom.atlassian.net/browse/DS2-154
    db = fc.new_connection(project=TEST_PROJECT)
    dealroom_uuid = uuid()
    print(dealroom_uuid)
    res = fc.set_history_doc_refs(db, {
        "final_url": "foo66.bar",
        "dealroom_uuid": dealroom_uuid
    }, dealroom_uuid)
    doc_ref = fc.get_history_doc_refs(
        db, dealroom_id=dealroom_uuid)["dealroom_uuid"][0]
    doc_ref.delete()
    # NOTE: if the call doesn't have the dealroom_id as a parameter this fails. Since we removed
    # the logic to extract the dealroom_id from the payload here: https://dealroom.atlassian.net/browse/DS2-104
    assert res == StatusCode.CREATED