Homework: H6 Model Validations and Tests
For this homework, you will continue to build upon the Rails app from the previous homework by adding model validations to prevent bad data from being saved to the database and by writing model tests to verify that the model code you create works correctly. Additionally, you will practice using the basic version control features of Git.
The tasks you will perform draw heavily from this demonstration:
The specific tasks are detailed below, and each one refers to a model class from the last homework. For each task:
- Implement the specified model validations.
- Create valid test fixtures for the model class as specified.
- Create a model test that verifies that the model fixtures are all valid with respect to the model validations.
- Create model tests (as specified) that use the fixtures to verify that the model validations were declared correctly.
- Once you have completed the task, commit the current version of the code to your local repo, and push the changes to the remote GitHub repo.
Task 1: Book Model
- Figure 1.1 depicts the model class for which you must create validations.
- Table 1.1 specifies the constraints that the validations should enforce.
Attribute | Validation |
---|---|
title |
Must not exceed 100 characters; must be present |
publisher |
Must not exceed 50 characters |
isbn |
Must match be a valid ISBN-10 code as per the standard |
genre |
Must be one of the following: Comedy, Dystopia, Fantasy, Mystery, Romance, Science Fiction, Thriller, Western |
- Table 1.2 lists the valid test fixtures you must create.
title | publisher | publication_date | isbn | genre |
---|---|---|---|---|
American Gods | William Morrow, Headline | 19 June 2001 | 0-380-97365-0 | Fantasy |
Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch | Gollancz (UK) / Workman (US) | 1 May 1990 | 0-575-04800-X | Comedy |
The Colour of Magic | Colin Smythe | 24 November 1983 | 0-86140-324-X | Fantasy |
- The model tests to create are as follows (use each exact string below as the name of a test):
- fixtures should be valid
- title with more than 100 characters should be invalid
- title that is missing or empty should be invalid
- publisher with more than 50 characters should be invalid
- isbn that does not match the ISBN-10 standard should be invalid
- genre that is not one of the specified genres should be invalid
Task 2: Author Model
- Figure 2.1 depicts the model class for which you must create validations.
- Table 2.1 specifies the constraints that the validations should enforce.
Attribute | Validation |
---|---|
first_name |
Must not exceed 50 characters |
last_name |
Must not exceed 50 characters; must be present |
webpage |
Must match be a valid URL as per the standard |
dob |
Must be before today; must be present |
dod |
Must be after the dob; must be before tomorrow |
- Table 2.2 lists the valid test fixtures you must create.
first_name | last_name | webpage | dob | dod |
---|---|---|---|---|
Terry | Pratchett | nil | 28 April 1948 | 12 March 2015 |
Neil | Gaiman | http://neilgaiman.com/ | 10 November 1960 | nil |
J. K. | Rowling | http://jkrowling.com/ | 31 July 1965 | nil |
- The model tests to create are as follows (use each exact string below as the name of a test):
- fixtures should be valid
- first name with more than 50 characters should be invalid
- last name with more than 50 characters should be invalid
- last name that is missing or empty should be invalid
- webpage that does not fit the URL standard should be invalid
- dob that is after today should be invalid
- dob that is missing should be invalid
- dod that is before the dob should be invalid
- dod that is after today should be invalid
Task 3: Review Model
- Figure 3.1 depicts the model class for which you must create validations.
- Table 3.1 specifies the constraints that the validations should enforce.
Attribute | Validation |
---|---|
score |
Must be between 0 and 5; must be present |
summary |
Must not exceed 200 characters; must be present |
full_text |
Must not exceed 10,000 characters |
- Table 3.2 lists the valid test fixtures you must create.
score | summary | full_text |
---|---|---|
5 | This grand tale of ancient gods struggling to survive in today's USA has a chilly setting but provides a great summer escape. |
Beaches, like books, offer a dislocation from reality, and when taken together the effect is multiplied many times. I love getting lost in a narrative then being jarred out of it by a noisy fellow holidaymaker and looking up, blinking, to remember I'm lying awkwardly on a towel on some sun-kissed shore, miles from home. Like the terra firma of a beach bordering the seemingly endless sea spooling out and away from the land, Neil Gaiman's American Gods offers that same sense of being anchored to reality but at the same time being merely a bulwark against something massive and frightening and ultimately unknowable. It seems a little odd now, given his ubiquity, but when American Gods was published in 2001 Gaiman wasn't a household name, unless your household happened to be filled with long-boxes of the comic books on which he had built his career. It wasn't his first, but American Gods feels like his first proper novel; previous outings had been Neverwhere, which was essentially a novelisation of his own screenplay for a BBC TV production whose budget never quite matched his imagination, and Stardust, a slight (though satisfying) fairytale originally conceived as an illustrated book with the artist Charles Vess. |
4 | Gaiman's storytelling is the furthest thing from clumsy — extremely well researched, superbly clever, and endlessly fun — so American Gods is a con worth falling for. |
Gaiman's cast of "old gods" includes gods from all over the world. There's Odin, of course, along with a number of others from Norse mythology. There's also Anansi from African folklore and Bilquis, the Bible's Queen of Sheba, and many, many more. Gaiman handles them all brilliantly, but what's really genius is how he gives them a back story. The novel's epigraph poses the question of "what happens to demonic beings when immigrants move from their homelands”; Gaiman takes this question and runs with it. His answer is that these "demonic beings” don't just appear on American soil. They migrate here, carried from other continents in the minds of their believers. These days, life ain't easy for the old gods. They face stiff competition in the "new gods” — media and technology and drugs. People no longer believe in the old gods, so they no longer make sacrifices to them, so the old gods are fading away. The old gods must take a stand against the new gods or face elimination. We follow along as Wednesday and Shadow round up the troops in preparation for battle. |
2 | There is so much going on, so many little details to keep track of, and yet it still manages to come together quite nicely to form a great story about what it means to be human. |
Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch is a contemporary and comedic fantasy written by Neil Gaiman and Terry Pratchett in 1990. The collaboration between two of the finest fantasy authors of our generation is nothing short of brilliant, and while they both shared everything equally and did it for fun, Pratchett has said they wouldn't do it again for a big clock. The antichrist has been born, the four bikers of apocalypse have gathered, its the end of the world as we know it and its all happening in the small english suburb of Lower Tadfield. This is not the news the angel Aziraphale and the demon Crowley had been hoping for as they have become quite accustomed to their lives on earth and are starting to have second thoughts about the whole Armageddon thing. Despite being mortal enemies they decide to work together to help raise the antichrist in the hope that when it comes time for the apocalypse, the antichrist might just decide not to carry through with it. If only those meddling satanic nuns hadn't got to him first... |
- The model tests to create are as follows (use each exact string below as the name of a test):
- fixtures should be valid
- score less than 0 should be invalid
- score greater than 5 should be invalid
- score that is missing should be invalid
- summary with over 200 characters should be invalid
- summary that is missing or empty should be invalid
- full text that is over 10,000 characters should be invalid
Task 4: User Model
- Figure 4.1 depicts the model class for which you must create validations.
- Table 4.1 specifies the constraints that the validations should enforce.
Attribute | Validation |
---|---|
first_name |
Must not exceed 50 characters |
last_name |
Must not exceed 50 characters; must be present |
email |
Must match be a valid email address as per the standard; must be present; must be unique |
- Table 4.2 lists the valid test fixtures you must create.
first_name | last_name | |
---|---|---|
Homer | Simpson | homer@email.com |
Lisa | Simpson | lsimp@hotmail.com |
Bart | Simpson | bsmpson@memphis.edu |
- The model tests to create are as follows (use each exact string below as the name of a test):
- fixtures should be valid
- first name with more than 50 characters should be invalid
- last name with more than 50 characters should be invalid
- last name that is missing or empty should be invalid
- email that does not fit the email address standard should be invalid
- email that is missing or empty should be invalid
- email that is not unique should be invalid