This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- The Parts of a Room Database 3:45
- Creating the PizzaTopping Entity 5:50
- Choosing Queries 1:47
- Creating the First Dao 3:45
- Finishing the Daos 4:17
- Room Beginnings 6 questions
- Creating the RoomDatabase 4:10
- Testing the Database 6:05
- Saving a Test Pizza 4:54
- Saving a Pizza's Toppings 5:16
- Room Endings 5 questions
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we'll see how to save which Toppings belong on which Pizza into the PizzaTopping table!
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've just tested our
ability to save a pizza.
0:00
But what about saving its toppings?
0:03
Let's add some space at
the bottom of the class and
0:06
add a function to help test saving
data to the pizza topping table.
0:08
Let's start by adding
the @Test annotation, and
0:14
then declaring our test function.
0:18
Fun, and let's call it pizzaToppingTest.
0:20
Just like our other test, we'll want
to start by getting access to our
0:25
database and then clearing all the tables.
0:29
Since we already have access to our
database and the pizzaTest function,
0:32
let's start by moving the appContext and
0:36
db variables out of the pizzaTest
function and into the class itself.
0:39
Then, back in pizzaToppingTest,
let's add a call to
0:50
db.clearAllTables, and
get on with writing our test.
0:54
We'll need to insert a row for
each topping on our pizza.
0:59
So let's call testToppingIds.forEach and
then for
1:04
each topping,
let's create a new pizza topping
1:08
object combining our
testPizzaId with the toppingId.
1:12
So val pizzaTopping is
going to equal a new pizza
1:17
topping object where we'll
pass in testPizza.id and it.
1:23
Remember that it represents the topping
ID we're currently looping on.
1:31
Once we've got the pizza topping,
let's insert it with a call to
1:41
db.pizzaToppingDao.insert while
passing in the pizza topping.
1:45
Now that we've inserted the data,
1:53
we need to test that we can retrieve
which toppings belong to which pizza.
1:55
Let's add a new line at
the bottom of the function and
2:01
then create a new val
called returnedToppingIDs.
2:04
And let's set it equal to
2:08
db.pizzaToppingDao.getToppingIdsForPizzaId
and
2:11
pass in testPizza.Id for the id.
2:18
Then we just need to assert that
these are the same topping ids
2:23
from our test topping ids list.
2:26
Let's type assertEquals, and
then for the expected value,
2:29
let's pass in test ToppingIds, and for the
result let's pass in returnedToppingIds.
2:34
Awesome, that takes care of that test.
2:41
Let's click over here on the left
to run it and see how we're doing.
2:44
Oops, looks like we have
a foreign key error.
2:58
Thanks to our foreign key constraints,
we're not allowed to insert any pizzas or
3:01
toppings that don't exist
into our pizza topping table.
3:06
So before we insert any pizza toppings,
we'll need to make sure we've already
3:10
inserted the pizzas and
toppings into their respective tables.
3:15
To help us rewrite our test, let's break
it out into the three sections we learned
3:21
about in the testing course,
arrange, act and assert.
3:25
To arrange the test, we'll clear
all the tables in the database.
3:30
Then for the Act section, we'll do all
the inserting and retrieving of data.
3:36
And for the Assert section,
we'll have the Assert.
3:43
Now, to finish the test, we just
need to add inserting the pizza and
3:47
all of the toppings to
the Arrange section.
3:51
Let's add a line below the call to clear
all tables, and then insert the testPizza
3:54
just like we did above, db.pizzaDau.insert
and pass in the testPizza.
3:58
Last but not least,
we just need to insert all the toppings.
4:06
Remember, the toppings array
is created in our app file and
4:10
is available throughout the whole project.
4:13
So let's type toppings.forEach and
then for each topping,
4:16
let's insert it into the database,
db.toppingDao.insert,
4:25
and pass in it to be the topping.
4:31
Now when we try to insert our new
pizza topping both the pizza and
4:35
the topping we'll be
inserting will both exist.
4:40
Let's run our test again and
hope for the best.
4:43
Perfect.
4:52
Testing is a great way to
make sure something works
4:54
before you start trying to
weave it into your code base.
4:57
Not only that, it might save you some
time by letting you work with just a test
4:59
instead of the whole app.
5:04
Okay, our tests are written, and
5:06
we're pretty sure our data
layer is ready for action.
5:08
Coming up we'll dive back into the UI,
and connect it to our database.
5:11
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up