Testing with Instance Model

Here is the instance model that should now be in our graph:

Current instance model

In this challenge, you will test each use case against the graph by executing Cypher queries. The query values will be specific to the data we know is in the graph, but you can imagine that a fully-populated graph would satisfy a broader range of query parameters.

Follow the instructions below to test each use case for our application.

Use case #1: What people acted in a movie?

Run this Cypher code to test this use case using the movie, Sleepless in Seattle.

cypher
MATCH (p:Person)-[:ACTED_IN]-(m:Movie)
WHERE m.title = 'Sleepless in Seattle'
RETURN p.name AS Actor

It should return two names of actors, Tom Hanks, and Meg Ryan.

Use case #2: What person directed a movie?

Run this Cypher code to test this use case using the movie, Hoffa.

cypher
MATCH (p:Person)-[:DIRECTED]-(m:Movie)
WHERE m.title = 'Hoffa'
RETURN  p.name AS Director

It should return Danny DeVito.

Use case #3: What movies did a person act in?

Run this Cypher code to test this use case using the person, Tom Hanks.

cypher
MATCH (p:Person)-[:ACTED_IN]-(m:Movie)
WHERE p.name = 'Tom Hanks'
RETURN m.title AS Movie

It should return the movies Apollo 13 and Sleepless in Seattle.

Use case #4: How many users rated a movie?

Run this Cypher code to test this use case using the movie, Apollo 13.

cypher
MATCH (u:User)-[:RATED]-(m:Movie)
WHERE m.title = 'Apollo 13'
RETURN count(*) AS `Number of reviewers`

It should return 2.

Use case #5: Who was the youngest person to act in a movie?

Run this Cypher code to test this use case using the movie, Hoffa.

cypher
MATCH (p:Person)-[:ACTED_IN]-(m:Movie)
WHERE m.title = 'Hoffa'
RETURN  p.name AS Actor, p.born as `Year Born` ORDER BY p.born DESC LIMIT 1

It should return Danny DeVito with his birth year.

Use case #6: What role did a person play in a movie?

Run this Cypher code to test this use case using the movie, Sleepless in Seattle and the person, Meg Ryan.

cypher
MATCH (p:Person)-[r:ACTED_IN]-(m:Movie)
WHERE m.title = 'Sleepless in Seattle' AND
p.name = 'Meg Ryan'
RETURN  r.role AS Role

It should return Annie Reed.

Use case #7: What is the highest rated movie in a particular year according to imDB?

Run this Cypher code to test this use case using movies in the year 1995.

cypher
MATCH (m:Movie)
WHERE m.released STARTS WITH '1995'
RETURN  m.title as Movie, m.imdbRating as Rating ORDER BY m.imdbRating DESC LIMIT 1

Our test data has only one movie released in 1995, but if you were to add another movie to the graph that was released in 1995, this query would be answered correctly. In this case, you must add more data to the graph to fully test this query.

Run the Cypher code to add another Movie node and its director to the graph:

cypher
MERGE (casino:Movie {title: 'Casino', tmdbId: 524, released: '1995-11-22', imdbRating: 8.2, genres: ['Drama','Crime']})
MERGE (martin:Person {name: 'Martin Scorsese', tmdbId: 1032})
MERGE (martin)-[:DIRECTED]->(casino)

You can rerun the query above to test the use case and receive a different result.

The new instance model for this graph is now:

Testing addition

Use case #8: What drama movies did an actor act in?

Run this Cypher code to test this use case using the person, Tom Hanks.

cypher
MATCH (p:Person)-[:ACTED_IN]-(m:Movie)
WHERE p.name = 'Tom Hanks' AND
'Drama' IN m.genres
RETURN m.title AS Movie

If you were to change 'Drama' to 'Comedy' in this query, it would return a different result.

Use case #9: What users gave a movie a rating of 5?

Run this Cypher code to test this use case using the movie, Apollo 13.

cypher
MATCH (u:User)-[r:RATED]-(m:Movie)
WHERE m.title = 'Apollo 13' AND
r.rating = 5
RETURN u.name as Reviewer

It should return Sandy Jones.

This query returns everything in the graph. Run this query and answer the following questions about the instance model:

cypher
MATCH (n) RETURN n

Node & Relationship Counts

After you have tested all the use cases and added the additional node for Martin Scorsese, use the Sandbox window to answer the questions below about the graph thus far.

1. Person Node Count

How many Person nodes are in the graph?

  • ✓ 5

Hint

You can either run a query to retrieve all nodes in the graph and count the number of nodes in the visualization by running the following query:

cypher
Get All Nodes
MATCH (n) RETURN n

Warning: Don’t run this on a large dataset as it may crash your Browser window.

Alternatively, you could run the following query to get a count of :Person nodes in the graph:

cypher
Get Node Count by Label
MATCH (:Person) RETURN count(*)

Solution

You should have 5 Person nodes in your Graph.

2. Movie Node Count

How many Movie nodes are in the graph?

  • ✓ 4

Hint

You can run the following query to get a count of :Movie nodes in the graph:

cypher
Get Node Count by Label
MATCH (:Movie) RETURN count(*)

Solution

You should have 4 Movie nodes in your Graph.

3. User Node Count

How many User nodes are in the graph?

  • ✓ 2

Hint

You can run the following query to get a count of :User nodes in the graph:

cypher
Get Node Count by Label
MATCH (:User) RETURN count(*)

Solution

You should have 2 User nodes in your Graph.

4. Acted In Relationship Count

How many ACTED_IN relationships are in the graph?

  • ✓ 5

Hint

You can run the following query to get a count of :ACTED_IN relationships in the graph:

cypher
Get Node Count by Label
MATCH ()-[:ACTED_IN]->() RETURN count(*)

Solution

You should have 5 :ACTED_IN relationships in your Graph.

5. Directed Relationship Count

How many DIRECTED relationships are in the graph?

  • ✓ 2

Hint

You can run the following query to get a count of :DIRECTED relationships in the graph:

cypher
Get Node Count by Label
MATCH ()-[:DIRECTED]->() RETURN count(*)

Solution

You should have 2 :DIRECTED relationship in your Graph.

6. Ratings Count

How many RATED relationships are in the graph?

  • ✓ 5

Hint

You can run the following query to get a count of :RATED relationships in the graph:

cypher
Get Node Count by Label
MATCH ()-[:RATED]->() RETURN count(*)

Solution

You should have 5 :RATED relationships in your Graph.

Summary

In this challenge, you tested the use cases against your instance model.

In the next module, you will learn why and how to refactor your data model.