Introduction to MongoDB Schema Solution
Transcript from the "Schema Solution" Lesson
>> Scott Moss: Hope everyone had some fun and enough time to get some of those tests, if not all of those tests to pass. So I'm just gonna walk through all those solutions. And also you can check out to the master branch or the solution branch and see the solutions yourself.
[00:00:16] But I'm gonna walk through the same solutions that are gonna be on that branch right now. So the first thing that we're supposed to do after we install and do all that stuff is create connection logic. And this logic is very important because the test itself actually uses this logic.
[00:00:31] So it's basically gonna be exactly the same thing that I showed you all in the demonstration. It's just gonna use the mogoose.connect and then you pass the url. That's it. And once that's there, you're connected. Like I said, there are some other options you can pass here depending on what version you have.
[00:00:49] Like for me, I might put use new url parser true or something like that to get the deprecation warrant to go away. But it's not that important right now. It still connects. So once you add that url, like I said this is very important because the testing setup that I'm using is using your connection logic.
[00:01:07] So if this logic isn't working, then none of the tests will execute properly. So you would have had to have done this one first, which is why I put it first in the checklist. So once that's good, then let's head over to the user spec since Connor relies on user to be done.
[00:01:22] And I'll just open these up side-by-side so we can figure out what's going on. So user spec, and then userjs. All right, so the first test, let's get this to pass. It says first name must be required. So we'll head over to the user schema and to the first name declaration field here, and we'll just put required true.
[00:01:45] So we'll run that, and then we'll run our test.
>> Scott Moss: Get that command, I'm just gonna do a --watch. If you do --watch in JS, it will just keep the tests there and run them every time you change a file, so it doesn't have to rebuild every time.
[00:02:01] So it looks like that one passed. I got one that passed, and just to verify if it was that one, we'll scroll up and it was. First name must be required. So that one's good. The next one says, last name must be required. Very much the same thing.
[00:02:15] Just copy and past that required field down, and we're good. Test is re-running, looks like two pass now. And just to verify, last name must be required, so that one's good, okay? Keep going and if you want, just a quick tip with jes. You might see something that says expect.insertions1.
[00:02:35] This is because if your code didn't throw an error I wouldn't know. So I have to tell jes that you should expect at least one insertion. Because if this code didn't throw an error, this catch would never run. And therefore, there would be no expectations. And then we would never if your test or failed.
[00:02:54] We just wouldn't know. So this is captioning a failure that should be a failure, so that's why I'm telling you to do that. So for email, email must be required very much the same thing so I'm just gonna paste that required true in the email. And it looks like three is passing now, so if we verify that, we have,
>> Scott Moss: Email must be required so that passes. So, that's good. And then this is where it starts to get tricky. So email must be unique, okay. So we talked about this one a little bit, and it's just adding the unique field here to true. This is one way to do it, we will talk about other ways to do it when we talk about indexes.
[00:03:36] Remember unique is not a validation it is an index. If you don't know what index is that's fine we are going to talk about indexes later. But it is an index, it is not a validation, completely different thing. So unique true is going to add an index here and in a test you'll see me doing something like user.init.
[00:03:52] This is because indexes take time to build. And I need them to be built before I can actually use them, so user.init is telling mongoose, hey I'm just going to wait until you tell me when the index for the user is done because indexes have to be built.
[00:04:06] They are basically Data in a file, and that file has to be written. And I need it to be there so I can check for validations against that index. So wait for the indexes to be built, and then try to create two users with the same email, and we'll see what happens.
[00:04:21] So it looks like four tests are passing, before I go verify that. Email must be unique. So that one is good, good to go there. What else do we have in here? We have betaUser should default to false. So we didn't talk about defaults, but it looks like people were getting this one to pass because it's so intuitive, right?
[00:04:42] It's probably what you think it is. Like what if I just add a default here to false? If you did that then you'd be right because that's exactly what it is. If you add a default field here. And you can put whatever value you want, as long as the value's the same type as the type.
[00:04:57] So if we go look, we get 60 pounds here and that right there fixed the other test which needed other fields, too. So pass two tests just with that one fix.