Errors in Building Neo4j Applications with Python (Unique Email Addresses)

Can you fix the code in this section on the course page. It doesn't look like the answer is posted. Thanks.

Sorry, can you expand on the problem you're having?

@adam_cowley

I think the code under the Working Solution section (see below) is the same as the code already in the file so the test script fails.

def register(self, email, plain_password, name):
encrypted = bcrypt.hashpw(plain_password.encode("utf8"), bcrypt.gensalt()).decode('utf8')

def create_user(tx, email, encrypted, name):
    return tx.run(""" // (1)
        CREATE (u:User {
            userId: randomUuid(),
            email: $email,
            password: $encrypted,
            name: $name
        })
        RETURN u
    """,
    email=email, encrypted=encrypted, name=name # (2)
    ).single() # (3)

try:
    with self.driver.session() as session:
        result = session.execute_write(create_user, email, encrypted, name)

        user = result['u']

        payload = {
            "userId": user["userId"],
            "email":  user["email"],
            "name":  user["name"],
        }

        payload["token"] = self._generate_token(payload)

        return payload
except ConstraintError as err:
    # Pass error details through to a ValidationException
    raise ValidationException(err.message, {
        "email": err.message
    })

Test script gives this error:
======================================================================================== short test summary info ========================================================================================
FAILED tests/04_handle_constraint_errors__test.py::test_unique_constraint - assert None is not None
FAILED tests/04_handle_constraint_errors__test.py::test_validation_error - NameError: name 'neo4j' is not defined

code.txt (4.6 KB)
error.txt (17.7 KB)

See attached for the full error message and the full code.

Hey @john10,

It looks like this line is the problem.

        except neo4j.exceptions.ConstraintError as err:

The variable neo4j isn't defined in this context because you haven't imported neo4j. The error class is imported from the neo4j package rather than the package itself.

from neo4j.exceptions import ConstraintError

Try this line instead:

except ConstraintError:

Now I am getting this error message.

FAILED tests/04_handle_constraint_errors__test.py::test_validation_error - neo4j.exceptions.DatabaseError: {code: Neo.DatabaseError.Schema.ConstraintCreationFailed} {message: Unable to create Constr...

I can see that the constraint is not being created correctly. Do you have the whole error string?

I got it working eventually. Code attached.
neo4j authenticate user.txt (4.5 KB)

1 Like