def test_details_view(self):
     """Details page displays all the fields Title, Date, Time Spent, What You Learned, Resources to Remember."""
     journal_entry_data = {
         'title':
         'testing new journal entries',
         'date':
         datetime.datetime.now().strftime('%Y-%m-%d'),
         'time_spent':
         '2',
         'what_i_learned':
         'how to test flask views',
         'resources_to_remember':
         'teamtreehouse.com, stackoverflow.com, flask.pocoo.org'
     }
     with test_database(TEST_DB, (User, JournalEntry)):
         UserModelTestCase.create_users(1)
         journal_entry_data['user'] = User.select().get()
         JournalEntry.create(**journal_entry_data)
         self.app.post('/login', data=USER_DATA)
         rv = self.app.get('/details/{}'.format(JournalEntry.get().id))
         self.assertIn(journal_entry_data['title'],
                       rv.get_data(as_text=True))
         self.assertIn(journal_entry_data['date'],
                       rv.get_data(as_text=True))
         self.assertIn(journal_entry_data['time_spent'],
                       rv.get_data(as_text=True))
         self.assertIn(journal_entry_data['what_i_learned'],
                       rv.get_data(as_text=True))
         self.assertIn(journal_entry_data['resources_to_remember'],
                       rv.get_data(as_text=True))
 def test_details_link_to_edit(self):
     """Details page includes a link to edit the entry"""
     journal_entry_data = {
         'title':
         'testing new journal entries',
         'date':
         datetime.datetime.now().strftime('%Y-%m-%d'),
         'time_spent':
         '2',
         'what_i_learned':
         'how to test flask views',
         'resources_to_remember':
         'teamtreehouse.com, stackoverflow.com, flask.pocoo.org'
     }
     with test_database(TEST_DB, (User, JournalEntry)):
         UserModelTestCase.create_users(1)
         journal_entry_data['user'] = User.select().get()
         JournalEntry.create(**journal_entry_data)
         self.app.post('/login', data=USER_DATA)
         rv = self.app.get('/details/{}'.format(JournalEntry.get().id))
         self.assertIn('href="/entry/', rv.get_data(as_text=True))
    def test_list_view_hyperlink(self):
        """The list view Title should be hyperlinked to the detail page for each journal entry."""
        journal_entry_data = {
            'title':
            'testing hyperlink',
            'date':
            datetime.datetime.now().strftime('%Y-%m-%d'),
            'time_spent':
            '4',
            'what_i_learned':
            'how to test flask views',
            'resources_to_remember':
            'teamtreehouse.com, stackoverflow.com, flask.pocoo.org'
        }
        with test_database(TEST_DB, (User, JournalEntry)):
            UserModelTestCase.create_users(1)
            user = User.select().get()
            journal_entry_data['user'] = user
            JournalEntry.create(**journal_entry_data)

            self.app.post('/login', data=USER_DATA)
            hyperlink = 'href="/details/{}"'.format(JournalEntry.get().id)
            rv = self.app.get('/entries')
            self.assertIn(hyperlink, rv.get_data(as_text=True))
    def test_many_to_many_relationships(self):
        """Make sure JournalEntries and Tags can have many to many relationships"""
        journal_entry_data_1 = {
            'title': 'entry 1',
            'date': datetime.datetime.now().strftime('%Y-%m-%d'),
            'time_spent': '111',
            'what_i_learned': 'many to many relationships',
            'resources_to_remember': 'docs.peewee-orm.com'
        }
        journal_entry_data_2 = {
            'title': 'entry 2',
            'date': datetime.datetime.now().strftime('%Y-%m-%d'),
            'time_spent': '222',
            'what_i_learned': 'many to many relationships',
            'resources_to_remember': 'docs.peewee-orm.com'
        }
        with test_database(TEST_DB,
                           (User, JournalEntry, Tag, JournalEntryTag)):
            # create the user
            UserModelTestCase.create_users(1)

            # create the journal entries
            journal_entry_data_1['user'] = User.select().get()
            journal_entry_data_2['user'] = User.select().get()
            JournalEntry.create(**journal_entry_data_1)
            JournalEntry.create(**journal_entry_data_2)
            entry1 = JournalEntry.get(JournalEntry.title == 'entry 1')
            entry2 = JournalEntry.get(JournalEntry.title == 'entry 2')

            # create the tags
            Tag.create_tag(tag='first')
            Tag.create_tag(tag='both')
            Tag.create_tag(tag='second')
            tag1 = Tag.get(Tag.tag == 'first')
            tag2 = Tag.get(Tag.tag == 'both')
            tag3 = Tag.get(Tag.tag == 'second')

            # tie tags to entries
            entry1.tags.add(
                [Tag.get(Tag.tag == 'first'),
                 Tag.get(Tag.tag == 'both')])

            entry2.tags.add(
                [Tag.get(Tag.tag == 'second'),
                 Tag.get(Tag.tag == 'both')])

            # assertions
            self.assertIn(tag1, entry1.tags)
            self.assertIn(tag2, entry1.tags)
            self.assertNotIn(tag3, entry1.tags)

            self.assertNotIn(tag1, entry2.tags)
            self.assertIn(tag2, entry2.tags)
            self.assertIn(tag3, entry2.tags)

            self.assertIn(entry1, tag1.journal_entries)
            self.assertNotIn(entry2, tag1.journal_entries)

            self.assertIn(entry1, tag2.journal_entries)
            self.assertIn(entry2, tag2.journal_entries)

            self.assertNotIn(entry1, tag3.journal_entries)
            self.assertIn(entry2, tag3.journal_entries)