コード例 #1
0
def test_invalid_date():
    '''
    This test should still update Brad and create John, but should return
    a single error because the salary field for Brad isn't a number
    '''

    body = '''Name,Email,Manager,Salary,Hire Date
Bradley Jones,[email protected],,100000,NOT A DATE
John Smith,[email protected],[email protected],80000,07/16/2018
'''

    response = handle_csv_upload(body, {})
    assert (response["statusCode"] == 200)
    body = json.loads(response["body"])

    # Check the response counts
    assert (body["numCreated"] == 1)
    assert (body["numUpdated"] == 1)
    assert (len(body["errors"]) == 1)

    # Check that we added the correct number of users
    assert (db.user.count() == 3)
    assert (db.chain_of_command.count() == 3)

    # Check that Brad's hire date was not updated, but other fields were
    brad = db.user.find_one({"normalized_email": "*****@*****.**"})
    assert (brad["hire_date"] == datetime.datetime(2010, 2, 10))
    assert (brad["salary"] == 100000)
    assert (brad["name"] == "Bradley Jones")
コード例 #2
0
def test_invalid_number():
    '''
    This test should still update Brad and create John, but should return
    a single error because the salary field for Brad isn't a number
    '''

    body = '''Name,Email,Manager,Salary,Hire Date
Bradley Jones,[email protected],,NOT A NUMBER,02/10/2010
John Smith,[email protected],[email protected],80000,07/16/2018
'''

    response = handle_csv_upload(body, {})
    assert (response["statusCode"] == 200)
    body = json.loads(response["body"])

    # Check the response counts
    assert (body["numCreated"] == 1)
    assert (body["numUpdated"] == 1)
    assert (len(body["errors"]) == 1)

    # Check that we added the correct number of users
    assert (db.user.count() == 3)
    assert (db.chain_of_command.count() == 3)

    # Check that Brad's salary was updated
    brad = db.user.find_one({"normalized_email": "*****@*****.**"})
    assert (brad["salary"] == 90000)
    assert (brad["name"] == "Bradley Jones")

    # Check that Brad's chain of command is still empty
    brad_chain_of_command = db.chain_of_command.find_one(
        {"user_id": brad["_id"]})
    assert (len(brad_chain_of_command["chain_of_command"]) == 0)

    # Check that John's data was inserted correctly
    john = db.user.find_one({"normalized_email": "*****@*****.**"})
    assert (john["name"] == "John Smith")
    assert (john["salary"] == 80000)
    assert (john["manager_id"] == brad["_id"])
    assert (john["hire_date"] == datetime.datetime(2018, 7, 16))

    # Check that Brad is in John's chain of command
    john_chain_of_command = db.chain_of_command.find_one(
        {"user_id": john["_id"]})
    assert (len(john_chain_of_command["chain_of_command"]) == 1)
    assert (john_chain_of_command["chain_of_command"][0] == brad["_id"])
コード例 #3
0
def test_simple_csv():
    '''
    This should successfully update one user and create one user,
    also updating their chain of commands appropriately
    '''

    body = '''Name,Email,Manager,Salary,Hire Date
Brad Jones,[email protected],,100000,02/10/2010
John Smith,[email protected],[email protected],80000,07/16/2018
'''

    response = handle_csv_upload(body, {})
    assert (response["statusCode"] == 200)
    body = json.loads(response["body"])

    # Check the response counts
    assert (body["numCreated"] == 1)
    assert (body["numUpdated"] == 1)
    assert (len(body["errors"]) == 0)

    # Check that we added the correct number of users
    assert (db.user.count() == 3)
    assert (db.chain_of_command.count() == 3)

    # Check that Brad's salary was updated
    brad = db.user.find_one({"normalized_email": "*****@*****.**"})
    assert (brad["salary"] == 100000)

    # Check that Brad's chain of command is still empty
    brad_chain_of_command = db.chain_of_command.find_one(
        {"user_id": brad["_id"]})
    assert (len(brad_chain_of_command["chain_of_command"]) == 0)

    # Check that John's data was inserted correctly
    john = db.user.find_one({"normalized_email": "*****@*****.**"})
    assert (john["name"] == "John Smith")
    assert (john["salary"] == 80000)
    assert (john["manager_id"] == brad["_id"])
    assert (john["hire_date"] == datetime.datetime(2018, 7, 16))

    # Check that Brad is in John's chain of command
    john_chain_of_command = db.chain_of_command.find_one(
        {"user_id": john["_id"]})
    assert (len(john_chain_of_command["chain_of_command"]) == 1)
    assert (john_chain_of_command["chain_of_command"][0] == brad["_id"])
コード例 #4
0
def test_chain_of_command_multi_level():
    '''
    This test should create a fourth employee, Stephen, who works
    under John Smith, to demonstrate the multi level chain of command
    '''

    body = '''Name,Email,Manager,Salary,Hire Date
Bradley Jones,[email protected],,100000,NOT A DATE
John Smith,[email protected],[email protected],80000,07/16/2018
Stephen White,[email protected],[email protected],99999,10/27/2019
'''

    response = handle_csv_upload(body, {})
    assert (response["statusCode"] == 200)
    body = json.loads(response["body"])

    # Check the response counts
    assert (body["numCreated"] == 2)
    assert (body["numUpdated"] == 1)
    assert (len(body["errors"]) == 1)

    # Check that we added the correct number of users
    assert (db.user.count() == 4)
    assert (db.chain_of_command.count() == 4)

    # Check that Stephen's record was created
    steve = db.user.find_one({"normalized_email": "*****@*****.**"})
    assert (steve["hire_date"] == datetime.datetime(2019, 10, 27))
    assert (steve["salary"] == 99999)
    assert (steve["name"] == "Stephen White")

    # Check that Stephen's manager and skiplevel are John and Brad respectively
    john = db.user.find_one({"normalized_email": "*****@*****.**"})
    brad = db.user.find_one({"normalized_email": "*****@*****.**"})
    steve_chain_of_command = db.chain_of_command.find_one(
        {"user_id": steve["_id"]})
    assert (len(steve_chain_of_command["chain_of_command"]) == 2)
    assert (steve_chain_of_command["chain_of_command"][0] == john["_id"])
    assert (steve_chain_of_command["chain_of_command"][1] == brad["_id"])