def test_edit_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() # Log in self.client.login(username='******', password='******') # Edit the post response = self.client.post('/admin/blogengine/post/1/', { 'title': 'My second post', 'text': 'This is my second blog post', 'pub_date_0': '2013-12-28', 'pub_date_1': '22:00:04' }, follow = True ) # self.assertEquals(response.status_code, 200) # Check changed successfully # self.assertTrue('changed succesfully' in response.content) # Check post amended all_post = Post.objects.all() self.assertEquals(len(all_post), 1) only_post = all_post[0] self.assertEquals(only_post.title, 'My second post') self.assertEquals(only_post.text, 'This is my second blog post')
def test_index(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() post.title = 'My first post' post.text = 'This is [my first blog post](http://127.0.0.1:8000/)' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category post.save() # Check new post saved all_posts = Post.objects.all() self.assertEqual(len(all_posts), 1) # Fetch the index response = self.client.get('/') self.assertEqual(response.status_code, 200)
def test_tag_page(self): tag = Tag() tag.name = 'python' tag.description = 'python' tag.save() author = User.objects.create_user('testuser','*****@*****.**','password') author.save() post = Post() post.title = 'first post' post.text = 'fir post' post.slug = 'first-post' post.pub_date = timezone.now() post.author = author post.save() post.tags.add(tag) post.save() self.assertEquals(len(Post.objects.all()),1) self.assertEquals(Post.objects.all()[0],post) r = self.client.get(post.tags.all()[0].get_absolute_url(),follow=True) self.assertEquals(r.status_code,200) self.assertTrue(tag.name in r.content) self.assertTrue(markdown.markdown(post.text) in r.content)
def test_category_page(self): #create category cat = Category() cat.name = 'python' cat.description = 'python' cat.save() author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() post = Post() post.title = 'first post' post.text = 'text' post.slug = 'first-post' post.pub_date = timezone.now() post.author = author post.category = cat post.save() self.assertEquals(Post.objects.all()[0], post) self.assertEquals(len(Post.objects.all()), 1) cat_url = post.category.get_absolute_url() r = self.client.get(cat_url) self.assertEquals(r.status_code, 200) self.assertTrue(post.category.name in r.content) self.assertTrue(markdown.markdown(post.text) in r.content)
def test_post_page(self): # Create the category category = Category() category.name = "python" category.description = "The Python programming language" category.save() # Create the author author = User.objects.create_user("testuser", "*****@*****.**", "password") author.save() # Create the site site = Site() site.name = "example.com" site.domain = "example.com" site.save() # Create the post post = Post() post.title = "My first post" post.text = "This is [my first blog post](http://127.0.0.1:8000/)" post.slug = "my-first-post" post.pub_date = timezone.now() post.author = author post.site = site post.category = category post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Get the post URL post_url = only_post.get_absolute_url() # Fetch the post response = self.client.get(post_url) self.assertEquals(response.status_code, 200) # Check the post title is in the response self.assertTrue(post.title in response.content) # Check the post category is in the response self.assertTrue(post.category.name in response.content) # Check the post text is in the response self.assertTrue(markdown.markdown(post.text) in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime("%b") in response.content) self.assertTrue(str(post.pub_date.day) in response.content) # Check the link is marked up properly self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_tag_page(self): # Create the tag tag = CreateTag() # Create the post post = Post() post.title = 'My first post' post.text = 'This is [my first blog post](http://127.0.0.1:8000/)' post.slug = 'my-first-post' post.pub_date = timezone.now() post.save() post.tags.add(tag) post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Get the tag URL tag_url = post.tags.all()[0].get_absolute_url() # Fetch the tag response = self.client.get(tag_url) self.assertEquals(response.status_code, 200) # Check the tag name is in the response self.assertTrue(post.tags.all()[0].name in response.content) # Check the post text is in the response self.assertTrue(markdown.markdown(post.text) in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) # Check the link is marked up properly self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_delete_post(self): # create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # create a site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # create post post = Post() post.title = 'Delete Test' post.content = 'Test post for delete test' post.author = author post.pub_date = timezone.now() post.slug = 'delete-test' post.site = site post.save() # check the post is saved all_post = Post.objects.all() self.assertEquals(len(all_post), 1) only_post = all_post[0] # login self.client.login(username='******', password='******') # delete post response = self.client.post('/admin/blogengine/post/1/delete/', dict(post='yes'), follow=True) self.assertEquals(response.status_code, 200) self.assertTrue('deleted successfully' in response.content) all_post = Post.objects.all() self.assertNotEquals(len(all_post), 1)
def test_edit_post(self): # create author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # create site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # create post post = Post() post.title = 'My first post', post.text = 'This is a test post checking from the test.py', post.pub_date = timezone.now() post.slug = 'my-first-post' post.author = author post.site = site post.save() # login self.client.login(username='******', password='******') # edit the post new_post = { 'title': 'My second post', 'text': 'This is a test post checking the test.py', 'pub_date': timezone.now(), 'slug': 'my-second-post', 'site': 1, } response = self.client.post('/admin/blogengine/post/1/', new_post, follow=True)
def test_category_page(self): #create category cat = Category() cat.name = 'python' cat.description = 'python' cat.save() author = User.objects.create_user('testuser','*****@*****.**','password') author.save() post = Post() post.title = 'first post' post.text = 'text' post.slug = 'first-post' post.pub_date = timezone.now() post.author = author post.category = cat post.save() self.assertEquals(Post.objects.all()[0],post) self.assertEquals(len(Post.objects.all()),1) cat_url = post.category.get_absolute_url() r = self.client.get(cat_url) self.assertEquals(r.status_code,200) self.assertTrue( post.category.name in r.content ) self.assertTrue( markdown.markdown(post.text) in r.content)
def test_category_page(self): category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.slug = 'my-first-post' post.site = site post.category = category post.save() all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) category_url = post.category.get_absolute_url() # Fetch the category response = self.client.get(category_url) self.assertEquals(response.status_code, 200) self.assertTrue(post.category.name in response.content) self.assertTrue(post.title in response.content)
def test_create_post(self): category = CreateCategory() tag = CreateTag() post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.category = category post.save() post.tags.add(tag) post.save() all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) self.assertEquals(only_post, post) self.assertEquals(only_post.title, 'My first post') self.assertEquals(only_post.text, 'This is my first blog post') self.assertEquals(only_post.pub_date.day, post.pub_date.day) self.assertEquals(only_post.pub_date.month, post.pub_date.month) self.assertEquals(only_post.pub_date.year, post.pub_date.year) self.assertEquals(only_post.pub_date.hour, post.pub_date.hour) self.assertEquals(only_post.pub_date.minute, post.pub_date.minute) self.assertEquals(only_post.pub_date.second, post.pub_date.second) self.assertEquals(only_post.category.name, 'Robots')
def test_tag_page(self): # Create the tag tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() post.title = 'My first post' post.text = 'This is [my first blog post](http://127.0.0.1:8000/)' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.save() post.tags.add(tag) # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Get the tag URL tag_url = post.tags.all()[0].get_absolute_url() # Fetch the tag response = self.client.get(tag_url) self.assertEquals(response.status_code, 200) # Check the tag name is in the response self.assertTrue(post.tags.all()[0].name in response.content) # Check the post text is in the response self.assertTrue(markdown.markdown(post.text) in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) # Check the link is marked up properly self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_create_post(self): # Create the category category = Category() category.name = "python" category.description = "The Python programming language" category.save() # Create the author author = User.objects.create_user("testuser", "*****@*****.**", "password") author.save() # Create the site site = Site() site.name = "example.com" site.domain = "example.com" site.save() # Create the post post = Post() # Set the attributes post.title = "My first post" post.text = "This is my first blog post" post.slug = "my-first-post" post.pub_date = timezone.now() post.author = author post.site = site post.category = category # Save it post.save() # Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Check attributes self.assertEquals(only_post.title, "My first post") self.assertEquals(only_post.text, "This is my first blog post") self.assertEquals(only_post.slug, "my-first-post") self.assertEquals(only_post.site.name, "example.com") self.assertEquals(only_post.site.domain, "example.com") self.assertEquals(only_post.pub_date.day, post.pub_date.day) self.assertEquals(only_post.pub_date.month, post.pub_date.month) self.assertEquals(only_post.pub_date.year, post.pub_date.year) self.assertEquals(only_post.pub_date.hour, post.pub_date.hour) self.assertEquals(only_post.pub_date.minute, post.pub_date.minute) self.assertEquals(only_post.pub_date.second, post.pub_date.second) self.assertEquals(only_post.author.username, "testuser") self.assertEquals(only_post.author.email, "*****@*****.**") self.assertEquals(only_post.category.name, "python") self.assertEquals(only_post.category.description, "The Python programming language")
def test_create_post(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() # Set the attributes post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category # Save it post.save() # Check we can find it all_posts = Post.objects.all() self.assertEqual(len(all_posts), 1) only_post = all_posts[0] self.assertEqual(only_post, post) # Check attributes self.assertEqual(only_post.title, 'My first post') self.assertEqual(only_post.text, 'This is my first blog post') self.assertEqual(only_post.slug, 'my-first-post') self.assertEqual(only_post.pub_date.day, post.pub_date.day) self.assertEqual(only_post.pub_date.month, post.pub_date.month) self.assertEqual(only_post.pub_date.year, post.pub_date.year) self.assertEqual(only_post.pub_date.hour, post.pub_date.hour) self.assertEqual(only_post.pub_date.minute, post.pub_date.minute) self.assertEqual(only_post.pub_date.second, post.pub_date.second) self.assertEqual(only_post.author.username, 'testuser') self.assertEqual(only_post.author.email, '*****@*****.**') self.assertEqual(only_post.category.name, 'python') self.assertEqual(only_post.category.description, 'The Python programming language')
def test_index(self): # Create the category category = Category() category.name = 'perl' category.description = 'The Perl programming language' category.save # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() post.title = 'My first post' post.text = 'This is [my first blog post](http://127.0.0.1:8000/)' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Fetch the index response = self.client.get('/') self.assertEquals(response.status_code, 200) # Check the post title is in the response self.assertTrue(post.title in response.content) # Check the post text is in the response self.assertTrue(markdown.markdown(post.text) in response.content) # Check the post category is in the response self.assertTrue(post.category.name in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) # Check the link is marked up properly self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_create_post_(self): author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.save() all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) self.assertEquals(only_post.title, 'My first post') self.assertEquals(only_post.text, 'This is my first blog post') self.assertEquals(only_post.slug, 'my-first-post') self.assertEquals(only_post.pub_date.day, post.pub_date.day) self.assertEquals(only_post.pub_date.month, post.pub_date.month) self.assertEquals(only_post.pub_date.year, post.pub_date.year) self.assertEquals(only_post.pub_date.hour, post.pub_date.hour) self.assertEquals(only_post.pub_date.minute, post.pub_date.minute) self.assertEquals(only_post.pub_date.second, post.pub_date.second) self.assertEquals(only_post.author.username, 'testuser') self.assertEquals(only_post.author.email, '*****@*****.**')
def test_post_page(self): author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() post = Post() post.title = "My first post" post.text = 'This is [my first blog post](http://127.0.0.1:8000/)' post.pub_date = timezone.now() post.slug = 'my-first-post' post.author = author post.save() all_posts = Post.objects.all() self.assertEquals(len(all_posts),1) only_post = all_posts[0] self.assertEquals(only_post, post) post_url = only_post.get_absolute_url() response = self.client.get(post_url) self.assertEquals(response.status_code, 200) self.assertTrue(post.title in response.content) self.assertTrue(markdown.markdown(post.text) in response.content) self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_delete_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.save() #Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Log in self.client.login(username='******', password="******") #Delete the post response = self.client.post('/admin/blogengine/post/%s/delete/' % (post.id), {'post': 'yes'}, follow=True) self.assertEquals(response.status_code, 200) #Check deleted successfully self.assertTrue('deleted successfully' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 0)
def test_edit_post(self): category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.slug = 'my-first-post' post.site = site post.save() post.tags.add(tag) post.save() self.client.login(username='******', password="******") # Edit post response = self.client.post('/admin/blogengine/post/1/', { 'title': 'My second post', 'text': 'This is my second blog post', 'pub_date_0': '2013-12-28', 'pub_date_1': '22:00:04', 'slug': 'my-second-post', 'site': '1', 'category': '1', 'tags': '1', }, follow=True ) self.assertEquals(response.status_code, 200) self.assertTrue('changed successfully' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post.title, 'My second post') self.assertEquals(only_post.text, 'This is my second blog post')
def test_index(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is [my first blog post](http://127.0.0.1:8000/)' post.slug = 'my-first-post' post.pub_date = timezone.now() post.save() #Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) #Fetch the index response = self.client.get('/') self.assertEquals(response.status_code, 200) #Check the post title is in the response self.assertTrue(post.title in response.content) #Check the post text is in the response self.assertTrue(markdown.markdown(post.text) in response.content) #Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) # Check the linke is mared up properly self.assertTrue( '<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_create_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.save() #Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Check attributes self.assertEquals(only_post.title, 'My first post') self.assertEquals(only_post.text, 'This is my first blog post') self.assertEquals(only_post.slug, 'my-first-post') self.assertEquals(only_post.pub_date.day, post.pub_date.day) self.assertEquals(only_post.pub_date.month, post.pub_date.month) self.assertEquals(only_post.pub_date.year, post.pub_date.year) self.assertEquals(only_post.pub_date.hour, post.pub_date.hour) self.assertEquals(only_post.pub_date.minute, post.pub_date.minute) self.assertEquals(only_post.pub_date.second, post.pub_date.second)
def test_edit_post(self): CreateCategory() tag = CreateTag() post = Post() post.title = 'My first post' post.text = 'Hello World' post.pub_date = timezone.now() post.save() post.tags.add(tag) post.save() self.client.login(username='******', password='******') # Edit the post response = self.client.post( '/admin/blogengine/post/1/', { 'title': 'My second post', 'text': 'Hello world part 2', 'pub_date_0': '2014-07-12', 'pub_date_1': '22:00:00', 'category': '1', 'tags': '1'}, follow=True ) self.assertEquals(response.status_code, 200) # Check changed successfully self.assertTrue('changed successfully' in response.content) all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post.title, 'My second post') self.assertEqual(only_post.text, 'Hello world part 2')
def test_index(self): # create author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # create a site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # create post post = Post() post.title = 'My first post' post.text = 'This is [my first blog post](http://127.0.0.1:8000/)', post.pub_date = timezone.now() post.author = author post.site = site post.save() all_post = Post.objects.all() self.assertEquals(len(all_post), 1) # fetch the index response = self.client.get('/') self.assertEqual(response.status_code, 200) # check the post title in response self.assertTrue(post.title in response.content) # check the post date year in response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(str(post.pub_date.day) in response.content) # check the link is markedup as properly self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_create_post(self): # create a author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # create site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # create a post post = Post() post.title = 'First test post' post.text = 'Fists test post body' post.pub_date = timezone.now() post.author = author post.site = site # save post post.save() all_post = Post.objects.all() self.assertEquals(len(all_post), 1) only_post = all_post[0] self.assertEquals(only_post, post) self.assertEqual(only_post.author.username, post.author.username) self.assertEquals(only_post.author.email, post.author.email)
def test_edit_post(self): # Create the category category = Category() category.name = "python" category.description = "The Python programming language" category.save() # Create the author author = User.objects.create_user("testuser", "*****@*****.**", "password") author.save() # Create the site site = Site() site.name = "example.com" site.domain = "example.com" site.save() # Create the post post = Post() post.title = "My first post" post.text = "This is my first blog post" post.slug = "my-first-post" post.pub_date = timezone.now() post.author = author post.site = site post.save() # Log in self.client.login(username="******", password="******") # Edit the post response = self.client.post( "/admin/blogengine/post/1/", { "title": "My second post", "text": "This is my second blog post", "pub_date_0": "2013-12-28", "pub_date_1": "22:00:04", "slug": "my-second-post", "site": "1", "category": "1", }, follow=True, ) self.assertEquals(response.status_code, 200) # Check changed successfully self.assertTrue("changed successfully" in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post.title, "My second post") self.assertEquals(only_post.text, "This is my second blog post")
def test_edit_post(self): #create category category = Category() category.name = 'python' category.descritption = 'python' category.save() #create author author = User.objects.create_user('testuser','*****@*****.**','password') author.save() #create tag tag = Tag() tag.name = 'python' tag.description = 'python' tag.save() post = Post() post.title = 'My first Post' post.text = 'first blog post' post.pub_date = timezone.now() post.author = author post.category = category post.save() post.tags.add(tag) post.save() self.client.login(username='******',password='******') url = '/admin/blogengine/post/%d/' % Post.objects.all()[0].id response = self.client.post(url,{ 'title':'my second post', 'text':'this is my second post', 'pub_date_0':'2014-10-9', 'pub_date_1':'22:00:04', 'slug':'my-first-test', 'category': Category.objects.all()[0].id, 'tags' : Tag.objects.all()[0].id, },follow=True) self.assertEquals(response.status_code,200) self.assertTrue('changed successfully' in response.content) all_posts = Post.objects.all() self.assertEquals(len(all_posts),1) self.assertEquals(all_posts[0].title,'my second post'), self.assertEquals(all_posts[0].text,'this is my second post')
def test_delete_post(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category post.save() # Check new post saved all_posts = Post.objects.all() self.assertEqual(len(all_posts), 1) # Log in self.client.login(username='******', password="******") # Delete the post response = self.client.post('/admin/blogengine/post/1/delete/', { 'post': 'yes' }, follow=True) self.assertEqual(response.status_code, 200) # Check deleted successfully # self.assertTrue('deleted successfully' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEqual(len(all_posts), 0)
def test_delete_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Log in self.client.login(username='******', password="******") # Delete the post response = self.client.post('/admin/blogengine/post/1/delete/', { 'post': 'yes' }, follow=True) self.assertEquals(response.status_code, 200) # Check deleted successfully self.assertTrue('успешно удален' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 0)
def test_edit_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.save() # Log in self.client.login(username='******', password="******") # Edit the post response = self.client.post('/admin/blogengine/post/1/', { 'title': 'My second post', 'text': 'This is my second blog post', 'pub_date_0': '2013-12-28', 'pub_date_1': '22:00:04', 'slug': 'my-second-post' }, follow=True ) self.assertEquals(response.status_code, 200) # Check changed successfully self.assertTrue('успешно изменен' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post.title, 'My second post') self.assertEquals(only_post.text, 'This is my second blog post')
def test_create_post(self): # Create the post post = Post() # Set the attributes post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() # Save it post.save() # Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Check attributes self.assertEquals(only_post.title, 'My first post') self.assertEquals(only_post.text, 'This is my first blog post') self.assertEquals(only_post.pub_date.day, post.pub_date.day) self.assertEquals(only_post.pub_date.month, post.pub_date.month) self.assertEquals(only_post.pub_date.year, post.pub_date.year) self.assertEquals(only_post.pub_date.hour, post.pub_date.hour) self.assertEquals(only_post.pub_date.minute, post.pub_date.minute) self.assertEquals(only_post.pub_date.second, post.pub_date.second)
def test_edit_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.save() # Log in self.client.login(username='******', password="******") # Edit the post response = self.client.post('/admin/blogengine/post/1/', { 'title': 'My second post', 'text': 'This is my second blog post', 'pub_date_0': '2013-12-28', 'pub_date_1': '22:00:04' }, follow=True) self.assertEquals(response.status_code, 200) # Check changed successfully self.assertTrue('changed successfully' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post.title, 'My second post') self.assertEquals(only_post.text, 'This is my second blog post')
def test_delete_post(self): # Create the category category = Category() category.name = "python" category.description = "The Python programming language" category.save() # Create the author author = User.objects.create_user("testuser", "*****@*****.**", "password") author.save() # Create the site site = Site() site.name = "example.com" site.domain = "example.com" site.save() # Create the post post = Post() post.title = "My first post" post.text = "This is my first blog post" post.slug = "my-first-post" post.pub_date = timezone.now() post.site = site post.author = author post.category = category post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Log in self.client.login(username="******", password="******") # Delete the post response = self.client.post("/admin/blogengine/post/1/delete/", {"post": "yes"}, follow=True) self.assertEquals(response.status_code, 200) # Check deleted successfully self.assertTrue("deleted successfully" in response.content) # Check post deleted all_posts = Post.objects.all() self.assertEquals(len(all_posts), 0)
def test_index(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is [my first blog post](http://127.0.0.1:8000/)' post.slug = 'my-first-post' post.pub_date = timezone.now() post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Fetch the index response = self.client.get('/') self.assertEquals(response.status_code, 200) # Check the post title is in the response self.assertTrue(post.title in response.content) # Check the post text is in the response self.assertTrue(markdown2.markdown(post.text) in force_unicode(response.content)) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(str(post.pub_date.day) in response.content) # Check the link is marked up properly self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)
def test_index(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Fetch the index response = self.client.get('/') self.assertEquals(response.status_code, 200) # Check the post title is in the response self.assertTrue(post.title in response.content) # Check the post text is in the response self.assertTrue(post.text in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content)
def newpost(request): ''' create new post''' if request.POST == {}: form = NewPostForm() return render(request,'newpost.html', {'form': form}) print request form = NewPostForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['text'] author_id = 1 slug = slugify(title) p = Post( title = title, text = content, author_id = author_id, slug = slug) p.save() message = 'sucess' else: message = 'You submitted an empty form.' return HttpResponse(message)
def test_index(self): #create category category = Category() category.name = 'python' category.descritption = 'python' category.save() #create tag tag = Tag() tag.name = 'perl' tag.description = 'perl' tag.save() author = User.objects.create_user('test','*****@*****.**','test') author.save() post = Post() post.title = 'first post' post.text = 'this is [my first blog post](http://localhost:8000/)' post.pub_date = timezone.now() post.author = author post.category = category post.save() post.tags.add(tag) post.save() self.assertEqual(len(Post.objects.all()),1) r = self.client.get('/') self.assertEquals(r.status_code,200) self.assertTrue( post.category.name in r.content) self.assertTrue( Tag.objects.all()[0].name in r.content) self.assertTrue( post.title in r.content) self.assertTrue( markdown.markdown(post.text) in r.content) self.assertTrue( str(post.pub_date.year) in r.content) self.assertTrue( str(post.pub_date.day) in r.content) #Check the link is makred down properly self.assertTrue('<a href="http://localhost:8000/">my first blog post</a>' in r.content)
def test_edit_post(self): # Create the post post = Post(title='My first post', text='This is my first blog post', slug='my-first-post', pub_date=timezone.now()) post.save() # Log in self.client.login(username='******', password="******") # Getting ID of post since we might now know what this is all_posts = Post.objects.all() post_id = all_posts[0].id # Edit the post response = self.client.post("/admin/blogengine/post/%s/" % (post_id), { 'title': 'My second post', 'text': 'This is my second blog post', 'pub_date_0': '2013-12-28', 'pub_date_1': '22:00:04', 'slug': 'my-second-past' }, follow=True) self.assertEquals(response.status_code, 200) # Check changed successfully self.assertTrue('changed successfully' in response.content) # Check post amended self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post.title, 'My second post') self.assertEquals(only_post.text, 'This is my second blog post')
def test_duplicate_slug(self): # Create the site site = SiteFactory() # Create the category category = CategoryFactory() # Create the tag tag = TagFactory() # Create the post post = PostFactory() # Add the tag only after creating post post.tags.add(tag) post.save() # Check if we can find it all_posts = Post.objects.all() print("%s" % all_posts) self.assertEquals(len(all_posts), 1) only_post = all_posts[0] print("post slug: %s" % post.slug) # A bit nasty here but I need to move on for now, make it work site = Site.objects.get( pk=str(site.pk) ) # get(pk=1) will work on sqlit but not on posgtres - diff- id for pk category = Category.objects.get(pk=str(category.pk)) post_dup = Post( title='My test post', text='Whatever but title already exists in DB', pub_date=timezone.now(), site=site, category=category, ) post_dup.save() all_posts = Post.objects.all() print("%s" % all_posts) # Check if inded 2nd post with same title was added and the slug autogenerated self.assertEquals(len(all_posts), 2) print("2nd post slug: %s" % post_dup.slug)
def test_create_post(self): post = Post() post.title = "Test post" post.text = "Hello World!" post.pub_date = timezone.now() post.save() all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts.first() self.assertEquals(only_post, post) self.assertEquals(only_post.title, "Test post") self.assertEquals(only_post.text, "Hello World!")
def test_tag_page(self): tag = Tag() tag.name = 'python' tag.description = 'python' tag.save() author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() post = Post() post.title = 'first post' post.text = 'fir post' post.slug = 'first-post' post.pub_date = timezone.now() post.author = author post.save() post.tags.add(tag) post.save() self.assertEquals(len(Post.objects.all()), 1) self.assertEquals(Post.objects.all()[0], post) r = self.client.get(post.tags.all()[0].get_absolute_url(), follow=True) self.assertEquals(r.status_code, 200) self.assertTrue(tag.name in r.content) self.assertTrue(markdown.markdown(post.text) in r.content)
def test_index(self): #create category category = Category() category.name = 'python' category.descritption = 'python' category.save() #create tag tag = Tag() tag.name = 'perl' tag.description = 'perl' tag.save() author = User.objects.create_user('test', '*****@*****.**', 'test') author.save() post = Post() post.title = 'first post' post.text = 'this is [my first blog post](http://localhost:8000/)' post.pub_date = timezone.now() post.author = author post.category = category post.save() post.tags.add(tag) post.save() self.assertEqual(len(Post.objects.all()), 1) r = self.client.get('/') self.assertEquals(r.status_code, 200) self.assertTrue(post.category.name in r.content) self.assertTrue(Tag.objects.all()[0].name in r.content) self.assertTrue(post.title in r.content) self.assertTrue(markdown.markdown(post.text) in r.content) self.assertTrue(str(post.pub_date.year) in r.content) self.assertTrue(str(post.pub_date.day) in r.content) #Check the link is makred down properly self.assertTrue( '<a href="http://localhost:8000/">my first blog post</a>' in r.content)
def test_edit_post(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the tag tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.save() post.tags.add(tag) post.save() # Log in self.client.login(username='******', password="******") # Edit the post response = self.client.post('/admin/blogengine/post/1/', { 'title': 'My second post', 'text': 'This is my second blog post', 'pub_date_0': '2013-12-28', 'pub_date_1': '22:00:04', 'slug': 'my-second-post', 'site': '1', 'category': '1', 'tags': '1' }, follow=True ) self.assertEquals(response.status_code, 200) # Check changed successfully self.assertTrue('changed successfully' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post.title, 'My second post') self.assertEquals(only_post.text, 'This is my second blog post')
def test_create_post(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the tag tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() # Set the attributes post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category # Save it post.save() # Add the tag post.tags.add(tag) post.save() # Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Check attributes self.assertEquals(only_post.title, 'My first post') self.assertEquals(only_post.text, 'This is my first blog post') self.assertEquals(only_post.slug, 'my-first-post') self.assertEquals(only_post.site.name, 'example.com') self.assertEquals(only_post.site.domain, 'example.com') self.assertEquals(only_post.pub_date.day, post.pub_date.day) self.assertEquals(only_post.pub_date.month, post.pub_date.month) self.assertEquals(only_post.pub_date.year, post.pub_date.year) self.assertEquals(only_post.pub_date.hour, post.pub_date.hour) self.assertEquals(only_post.pub_date.minute, post.pub_date.minute) self.assertEquals(only_post.pub_date.second, post.pub_date.second) self.assertEquals(only_post.author.username, 'testuser') self.assertEquals(only_post.author.email, '*****@*****.**') self.assertEquals(only_post.category.name, 'python') self.assertEquals(only_post.category.description, 'The Python programming language') # Check tags post_tags = only_post.tags.all() self.assertEquals(len(post_tags), 1) only_post_tag = post_tags[0] self.assertEquals(only_post_tag, tag) self.assertEquals(only_post_tag.name, 'python') self.assertEquals(only_post_tag.description, 'The Python programming language')
def test_delete_post(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the tag tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.site = site post.author = author post.category = category post.save() post.tags.add(tag) post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Log in self.client.login(username='******', password="******") # Delete the post response = self.client.post('/admin/blogengine/post/1/delete/', { 'post': 'yes' }, follow=True) self.assertEquals(response.status_code, 200) # Check deleted successfully self.assertTrue('deleted successfully' in response.content) # Check post deleted all_posts = Post.objects.all() self.assertEquals(len(all_posts), 0)
def test_index(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Fetch the index response = self.client.get('/') self.assertEquals(response.status_code, 200) # Check the post title is in the response self.assertTrue(post.title in response.content) # Check the post text is in the response self.assertTrue(post.text in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) #Here we create the post, and assert that it is the sole post object. We then fetch the index page, # and assert that the HTTP status code is 200 (ie. the page exists and is returned). # We then verify that the response contains the post title, text and publication date. #Note that for the month, we need to do a bit of jiggery-pokery to get the month name. By # default Django will return short month names (eg Jan, Feb etc), but Python stores months as # numbers, so we need to format it as a short month name using %b. #If you run this, you will get an error because the index route isn’t implemented. So let’s # fix that. Open up the existing django_tutorial_blog_ng/urls.py file and amend it to look like this: from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'django_tutorial_blog_ng.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)), # Blog URLs url(r'^.*$', include('blogengine.urls')), ) #Then, create a new file at blogengine/urls.py and edit it as follows: from django.conf.urls import patterns, url from django.views.generic import ListView from blogengine.models import Post urlpatterns = patterns('', # Index url('^$', ListView.as_view( model=Post, )), ) #A little explanation is called for. The project has its own urls.py file that handles routing # throughout the project. However, because Django encourages you to make your apps reusable, we # want to keep the routes in the individual apps as far as possible. So, in the project file, we # include the blogengine/urls.py file. #In the app-specific urls.py, we import the Post model and the ListView generic view. # We then define a route for the index page - the regular expression ^$ will match only # an empty string, so that page will be the index. For this route, we then call the # as_view() method of the ListView object, and set the model as Post. #Now, if you either run the tests, or run the development server and visit the index page, # you’ll see that it isn’t working yet - you should see the error TemplateDoesNotExist: # blogengine/post_list.html. This tells us that we need to create a template called # blogengine/post_list.html, so let’s do that. First of all, add the following at the end of # django_tutorial_blog_ng/settings.py: # Template directory TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] #Next, create the folders for the templates, and a blank post_list.html file: $ mkdir templates $ mkdir templates/blogengine $ touch templates/blogengine/post_list.html #Now, run your tests again, and you’ll see that the template now exists, but a new error is showing up: Creating test database for alias 'default'... ......F #====================================================================== #FAIL: test_index (blogengine.tests.PostViewTest) #---------------------------------------------------------------------- #Traceback (most recent call last): #File "/Users/matthewdaly/Projects/django_tutorial_blog_ng/blogengine/tests.py", line 189, in test_index #self.assertTrue(post.title in response.content) #AssertionError: False is not true #---------------------------------------------------------------------- #Ran 7 tests in 2.162s #FAILED (failures=1) #Destroying test database for alias 'default'... #To fix this, we make sure the template shows the data we want. Open up # templates/blogengine/post_list.html and enter the following: #<html> #<head> #<title>My Django Blog</title> #</head> #<body> #{% for post in object_list %} #<h1>{{ post.title }}</h1> #<h3>{{ post.pub_date }}</h3> #{{ post.text }} #{% endfor %} #</body> #</html> #This is only a very basic template, and we’ll expand upon it in future. #With that done, you can run python manage.py test, and it should pass. Well done! Don’t forget to commit your changes: $ git add django_tutorial_blog_ng/ templates/ blogengine/ $ git commit -m 'Implemented list view for posts' #And that’s all for this lesson! We’ve done a hell of a lot in this lesson - set up our project, # created a comprehensive test suite for it, and implemented the basic functionality. Next time # Make it a bit prettier using Twitter Bootstrap, as well as implementing more of the # basic functionality for the blog. $ python manage.py migrate #This won’t actually make any changes, but it will ensure that all future changes to your models for the # blogengine app are handled by South. Let’s commit our app skeleton: $ git add django_tutorial_blog_ng/settings.py blogengine/ $ git commit -m 'Added blogengine app skeleton'
def test_delete_post(self): #create category category = Category() category.name = 'python' category.descritption = 'python' category.save() #create user author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() #creat tag tag = Tag() tag.name = 'python' tag.description = 'python' tag.save() post = Post() post.title = 'first' post.text = 'first text' post.pub_date = timezone.now() post.author = author post.category = category post.save() post.tags.add(tag) post.save() self.assertEquals(len(Post.objects.all()), 1) self.client.login(username='******', password='******') url = '/admin/blogengine/post/%d/delete/' % Post.objects.all()[0].id r = self.client.post(url, {'post': 'yes'}, follow=True) self.assertEquals(r.status_code, 200) self.assertEqual(len(Post.objects.all()), 0)
def test_edit_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.save() # Log in self.client.login(username='******', password="******") # Edit the post response = self.client.post('/admin/blogengine/post/1/', { 'title': 'My second post', 'text': 'This is my second blog post', 'pub_date_0': '2013-12-28', 'pub_date_1': '22:00:04' }, follow=True ) self.assertEquals(response.status_code, 200) # Check changed successfully self.assertTrue('changed successfully' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post.title, 'My second post') self.assertEquals(only_post.text, 'This is my second blog post') #Here we create a new blog post, then verify we can edit it by resubmitting it with # different values, and checking that we get the expected response, and that the data in # the database has been updated. Run python manage.py test, and this should pass. #Finally, we’ll set up a test for deleting posts: def test_delete_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Log in self.client.login(username='******', password="******") # Delete the post response = self.client.post('/admin/blogengine/post/1/delete/', { 'post': 'yes' }, follow=True) self.assertEquals(response.status_code, 200) # Check deleted successfully self.assertTrue('deleted successfully' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 0) #Again, this is pretty similar to what we did before. We create a new post, verify that # it is the sole post in the database, and log into the admin. Then we delete the post via # the admin, and confirm that the admin interface confirmed it has been deleted, and the # post is gone from the database. #I think it’s now time to commit again: $ git add blogengine/ $ git commit -m 'Post admin tests in place' #So we now know that we can create, edit and delete posts, and we have tests in place to # confirm this. So our next task is to be able to display our posts. #For now, to keep things simple, we’re only going to implement the index view - in other words, # all the posts in reverse chronological order. We’ll use Django’s generic views to keep things really easy. #Django’s generic views are another really handy feature. As mentioned earlier, a view is a # function or class that describes how a specific route should render an object. Now, there are # many tasks that recur in web development. For instance, many web pages you may have seen may be a # list of objects - in this case, the index page for a blog is a list of blog posts. For that reason, # Django has the ListView generic view, which makes it easy to render a list of objects. #Now, like before, we want to have a test in place. Open up blogengine/tests.py and add the # following class at the end of the file: class PostViewTest(LiveServerTestCase): def setUp(self): self.client = Client() def test_index(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Fetch the index response = self.client.get('/') self.assertEquals(response.status_code, 200) # Check the post title is in the response self.assertTrue(post.title in response.content) # Check the post text is in the response self.assertTrue(post.text in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) #Here we create the post, and assert that it is the sole post object. We then fetch the index page, # and assert that the HTTP status code is 200 (ie. the page exists and is returned). # We then verify that the response contains the post title, text and publication date. #Note that for the month, we need to do a bit of jiggery-pokery to get the month name. By # default Django will return short month names (eg Jan, Feb etc), but Python stores months as # numbers, so we need to format it as a short month name using %b. #If you run this, you will get an error because the index route isn’t implemented. So let’s # fix that. Open up the existing django_tutorial_blog_ng/urls.py file and amend it to look like this: from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'django_tutorial_blog_ng.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)), # Blog URLs url(r'^.*$', include('blogengine.urls')), ) #Then, create a new file at blogengine/urls.py and edit it as follows: from django.conf.urls import patterns, url from django.views.generic import ListView from blogengine.models import Post urlpatterns = patterns('', # Index url('^$', ListView.as_view( model=Post, )), ) #A little explanation is called for. The project has its own urls.py file that handles routing # throughout the project. However, because Django encourages you to make your apps reusable, we # want to keep the routes in the individual apps as far as possible. So, in the project file, we # include the blogengine/urls.py file. #In the app-specific urls.py, we import the Post model and the ListView generic view. # We then define a route for the index page - the regular expression ^$ will match only # an empty string, so that page will be the index. For this route, we then call the # as_view() method of the ListView object, and set the model as Post. #Now, if you either run the tests, or run the development server and visit the index page, # you’ll see that it isn’t working yet - you should see the error TemplateDoesNotExist: # blogengine/post_list.html. This tells us that we need to create a template called # blogengine/post_list.html, so let’s do that. First of all, add the following at the end of # django_tutorial_blog_ng/settings.py: # Template directory TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] #Next, create the folders for the templates, and a blank post_list.html file: $ mkdir templates $ mkdir templates/blogengine $ touch templates/blogengine/post_list.html #Now, run your tests again, and you’ll see that the template now exists, but a new error is showing up: Creating test database for alias 'default'... ......F #====================================================================== #FAIL: test_index (blogengine.tests.PostViewTest) #---------------------------------------------------------------------- #Traceback (most recent call last): #File "/Users/matthewdaly/Projects/django_tutorial_blog_ng/blogengine/tests.py", line 189, in test_index #self.assertTrue(post.title in response.content) #AssertionError: False is not true #---------------------------------------------------------------------- #Ran 7 tests in 2.162s #FAILED (failures=1) #Destroying test database for alias 'default'... #To fix this, we make sure the template shows the data we want. Open up # templates/blogengine/post_list.html and enter the following: #<html> #<head> #<title>My Django Blog</title> #</head> #<body> #{% for post in object_list %} #<h1>{{ post.title }}</h1> #<h3>{{ post.pub_date }}</h3> #{{ post.text }} #{% endfor %} #</body> #</html> #This is only a very basic template, and we’ll expand upon it in future. #With that done, you can run python manage.py test, and it should pass. Well done! Don’t forget to commit your changes: $ git add django_tutorial_blog_ng/ templates/ blogengine/ $ git commit -m 'Implemented list view for posts' #And that’s all for this lesson! We’ve done a hell of a lot in this lesson - set up our project, # created a comprehensive test suite for it, and implemented the basic functionality. Next time # Make it a bit prettier using Twitter Bootstrap, as well as implementing more of the # basic functionality for the blog. $ python manage.py migrate #This won’t actually make any changes, but it will ensure that all future changes to your models for the # blogengine app are handled by South. Let’s commit our app skeleton: $ git add django_tutorial_blog_ng/settings.py blogengine/ $ git commit -m 'Added blogengine app skeleton'
def test_create_post(self): # Create the post post = Post() # Set the attributes post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() # Save it post.save() # Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Check attributes self.assertEquals(only_post.title, 'My first post') self.assertEquals(only_post.text, 'This is my first blog post') self.assertEquals(only_post.pub_date.day, post.pub_date.day) self.assertEquals(only_post.pub_date.month, post.pub_date.month) self.assertEquals(only_post.pub_date.year, post.pub_date.year) self.assertEquals(only_post.pub_date.hour, post.pub_date.hour) self.assertEquals(only_post.pub_date.minute, post.pub_date.minute) self.assertEquals(only_post.pub_date.second, post.pub_date.second) class AdminTest(LiveServerTestCase): def test_login(self): # Create client c = Client() # Get login page response = c.get('/admin/') # Check response code self.assertEquals(response.status_code, 200) # Check 'Log in' in response self.assertTrue('Log in' in response.content) # Log the user in c.login(username='******', password="******") # Check response code response = c.get('/admin/') self.assertEquals(response.status_code, 200) # Check 'Log out' in response self.assertTrue('Log out' in response.content) #Used to log in don’t exist. Let’s resolve that. #Now, you could put your own credentials in there, but that’s not a good idea because it’s a # security risk. Instead, we’ll create a fixture for the test user that will be loaded when the tests are run. Run the following command: $ python manage.py createsuperuser #Give the username as bobsmith, the email address as [email protected], and the password as password. # Once that’s done, run these commands to dump the existing users to a fixture: $ mkdir blogengine/fixtures $ python manage.py dumpdata auth.User --indent=2 > blogengine/fixtures/users.json #This will dump all of the existing users to blogengine/fixtures/users.json. You may wish to edit this #file to remove your own superuser account and leave only the newly created one in there. #Next we need to amend our test to load this fixture: class AdminTest(LiveServerTestCase): fixtures = ['users.json'] def test_login(self): # Create client c = Client() # Get login page response = c.get('/admin/') # Check response code self.assertEquals(response.status_code, 200) # Check 'Log in' in response self.assertTrue('Log in' in response.content) # Log the user in c.login(username='******', password="******") # Check response code response = c.get('/admin/') self.assertEquals(response.status_code, 200) # Check 'Log out' in response self.assertTrue('Log out' in response.content) #Now, if you run python manage.py test, you should find that the test passes. Next, we’ll test that we can log out: class AdminTest(LiveServerTestCase): fixtures = ['users.json'] def test_login(self): # Create client c = Client() # Get login page response = c.get('/admin/') # Check response code self.assertEquals(response.status_code, 200) # Check 'Log in' in response self.assertTrue('Log in' in response.content) # Log the user in c.login(username='******', password="******") # Check response code response = c.get('/admin/') self.assertEquals(response.status_code, 200) # Check 'Log out' in response self.assertTrue('Log out' in response.content) def test_logout(self): # Create client c = Client() # Log in c.login(username='******', password="******") # Check response code response = c.get('/admin/') self.assertEquals(response.status_code, 200) # Check 'Log out' in response self.assertTrue('Log out' in response.content) # Log out c.logout() # Check response code response = c.get('/admin/') self.assertEquals(response.status_code, 200) # Check 'Log in' in response self.assertTrue('Log in' in response.content) #This test works along very similar lines. We log in, verify that ‘Log out’ is in the response, # then we log out, and verify that ‘Log in’ is in the response. Run the tests again, and they should pass. # Assuming they do, let’s commit our changes again: $ git add blogengine/ $ git commit -m 'Added tests for admin auth' #This code is a little repetitive. We create the client twice, when we could do so only once. Amend the AdminTest class as follows: class AdminTest(LiveServerTestCase): fixtures = ['users.json'] def setUp(self): self.client = Client() def test_login(self): # Get login page response = self.client.get('/admin/') # Check response code self.assertEquals(response.status_code, 200) # Check 'Log in' in response self.assertTrue('Log in' in response.content) # Log the user in self.client.login(username='******', password="******") # Check response code response = self.client.get('/admin/') self.assertEquals(response.status_code, 200) # Check 'Log out' in response self.assertTrue('Log out' in response.content) def test_logout(self): # Log in self.client.login(username='******', password="******") # Check response code response = self.client.get('/admin/') self.assertEquals(response.status_code, 200) # Check 'Log out' in response self.assertTrue('Log out' in response.content) # Log out self.client.logout() # Check response code response = self.client.get('/admin/') self.assertEquals(response.status_code, 200) # Check 'Log in' in response self.assertTrue('Log in' in response.content) #The setUp() method is automatically run when the test runs, and ensures we only need to start up the # client once. Run your tests to make sure they pass, then commit your changes: $ git add blogengine/ $ git commit -m 'Refactored admin test' #Now, we’ll implement a test for creating a new post. The admin interface implements URLs for # creating new instances of a model in a consistent format of /admin/app_name/model_name/add/, so the # URL for adding a new post will be /admin/blogengine/post/add/. #Add this method to the AdminTest class: def test_create_post(self): # Log in self.client.login(username='******', password="******") # Check response code response = self.client.get('/admin/blogengine/post/add/') self.assertEquals(response.status_code, 200) #Try running it and this will fail, because we haven’t registered the Post model in the Django admin. # So we need to do that. To do so, open a new file at blogengine/admin.py and add the following code: import models from django.contrib import admin admin.site.register(models.Post) #Now, run python manage.py test and the test should pass. If you want to confirm that the post model # appears in the admin, run python manage.py runserver and click here. #So now we can reach the page for adding a post, but we haven’t yet tested that we can submit one. Let’s remedy that: def test_create_post(self): # Log in self.client.login(username='******', password="******") # Check response code response = self.client.get('/admin/blogengine/post/add/') self.assertEquals(response.status_code, 200) # Create the new post response = self.client.post('/admin/blogengine/post/add/', { 'title': 'My first post', 'text': 'This is my first post', 'pub_date_0': '2013-12-28', 'pub_date_1': '22:00:04' }, follow=True ) self.assertEquals(response.status_code, 200) # Check added successfully self.assertTrue('added successfully' in response.content) # Check new post now in database all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) #Here we submit the new post via HTTP POST, with all the data passed through. This mirrors the # form created by the Django admin interface - if you take a look at the HTML generated by the # admin, you’ll see that the inputs are given names that match these. Note that the pub_date field, # because it represents a datetime object, is split up into a separate date and time field. Also # note the parameter follow=True - this denotes that the test client should follow any HTTP redirect. #We confirm that the POST request responded with a 200 code, denoting success. We also confirm that # the response included the phrase ‘added successfully’. Finally we confirm that there is now a # single Post object in the database. Don’t worry about any existing content - Django creates a # dedicated test database and destroys it after the tests are done, so you can be sure that no # posts are present unless you explicitly load them from a fixture. #We can now test creating a post, but we also need to ensure we can test editing and deleting them. # First we’ll add a test for editing posts: def test_edit_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.save() # Log in self.client.login(username='******', password="******") # Edit the post response = self.client.post('/admin/blogengine/post/1/', { 'title': 'My second post', 'text': 'This is my second blog post', 'pub_date_0': '2013-12-28', 'pub_date_1': '22:00:04' }, follow=True ) self.assertEquals(response.status_code, 200) # Check changed successfully self.assertTrue('changed successfully' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post.title, 'My second post') self.assertEquals(only_post.text, 'This is my second blog post') #Here we create a new blog post, then verify we can edit it by resubmitting it with # different values, and checking that we get the expected response, and that the data in # the database has been updated. Run python manage.py test, and this should pass. #Finally, we’ll set up a test for deleting posts: def test_delete_post(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Log in self.client.login(username='******', password="******") # Delete the post response = self.client.post('/admin/blogengine/post/1/delete/', { 'post': 'yes' }, follow=True) self.assertEquals(response.status_code, 200) # Check deleted successfully self.assertTrue('deleted successfully' in response.content) # Check post amended all_posts = Post.objects.all() self.assertEquals(len(all_posts), 0) #Again, this is pretty similar to what we did before. We create a new post, verify that # it is the sole post in the database, and log into the admin. Then we delete the post via # the admin, and confirm that the admin interface confirmed it has been deleted, and the # post is gone from the database. #I think it’s now time to commit again: $ git add blogengine/ $ git commit -m 'Post admin tests in place' #So we now know that we can create, edit and delete posts, and we have tests in place to # confirm this. So our next task is to be able to display our posts. #For now, to keep things simple, we’re only going to implement the index view - in other words, # all the posts in reverse chronological order. We’ll use Django’s generic views to keep things really easy. #Django’s generic views are another really handy feature. As mentioned earlier, a view is a # function or class that describes how a specific route should render an object. Now, there are # many tasks that recur in web development. For instance, many web pages you may have seen may be a # list of objects - in this case, the index page for a blog is a list of blog posts. For that reason, # Django has the ListView generic view, which makes it easy to render a list of objects. #Now, like before, we want to have a test in place. Open up blogengine/tests.py and add the # following class at the end of the file: class PostViewTest(LiveServerTestCase): def setUp(self): self.client = Client() def test_index(self): # Create the post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.pub_date = timezone.now() post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) # Fetch the index response = self.client.get('/') self.assertEquals(response.status_code, 200) # Check the post title is in the response self.assertTrue(post.title in response.content) # Check the post text is in the response self.assertTrue(post.text in response.content) # Check the post date is in the response self.assertTrue(str(post.pub_date.year) in response.content) self.assertTrue(post.pub_date.strftime('%b') in response.content) self.assertTrue(str(post.pub_date.day) in response.content) #Here we create the post, and assert that it is the sole post object. We then fetch the index page, # and assert that the HTTP status code is 200 (ie. the page exists and is returned). # We then verify that the response contains the post title, text and publication date. #Note that for the month, we need to do a bit of jiggery-pokery to get the month name. By # default Django will return short month names (eg Jan, Feb etc), but Python stores months as # numbers, so we need to format it as a short month name using %b. #If you run this, you will get an error because the index route isn’t implemented. So let’s # fix that. Open up the existing django_tutorial_blog_ng/urls.py file and amend it to look like this: from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'django_tutorial_blog_ng.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)), # Blog URLs url(r'^.*$', include('blogengine.urls')), ) #Then, create a new file at blogengine/urls.py and edit it as follows: from django.conf.urls import patterns, url from django.views.generic import ListView from blogengine.models import Post urlpatterns = patterns('', # Index url('^$', ListView.as_view( model=Post, )), ) #A little explanation is called for. The project has its own urls.py file that handles routing # throughout the project. However, because Django encourages you to make your apps reusable, we # want to keep the routes in the individual apps as far as possible. So, in the project file, we # include the blogengine/urls.py file. #In the app-specific urls.py, we import the Post model and the ListView generic view. # We then define a route for the index page - the regular expression ^$ will match only # an empty string, so that page will be the index. For this route, we then call the # as_view() method of the ListView object, and set the model as Post. #Now, if you either run the tests, or run the development server and visit the index page, # you’ll see that it isn’t working yet - you should see the error TemplateDoesNotExist: # blogengine/post_list.html. This tells us that we need to create a template called # blogengine/post_list.html, so let’s do that. First of all, add the following at the end of # django_tutorial_blog_ng/settings.py: # Template directory TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] #Next, create the folders for the templates, and a blank post_list.html file: $ mkdir templates $ mkdir templates/blogengine $ touch templates/blogengine/post_list.html #Now, run your tests again, and you’ll see that the template now exists, but a new error is showing up: Creating test database for alias 'default'... ......F #====================================================================== #FAIL: test_index (blogengine.tests.PostViewTest) #---------------------------------------------------------------------- #Traceback (most recent call last): #File "/Users/matthewdaly/Projects/django_tutorial_blog_ng/blogengine/tests.py", line 189, in test_index #self.assertTrue(post.title in response.content) #AssertionError: False is not true #---------------------------------------------------------------------- #Ran 7 tests in 2.162s #FAILED (failures=1) #Destroying test database for alias 'default'... #To fix this, we make sure the template shows the data we want. Open up # templates/blogengine/post_list.html and enter the following: #<html> #<head> #<title>My Django Blog</title> #</head> #<body> #{% for post in object_list %} #<h1>{{ post.title }}</h1> #<h3>{{ post.pub_date }}</h3> #{{ post.text }} #{% endfor %} #</body> #</html> #This is only a very basic template, and we’ll expand upon it in future. #With that done, you can run python manage.py test, and it should pass. Well done! Don’t forget to commit your changes: $ git add django_tutorial_blog_ng/ templates/ blogengine/ $ git commit -m 'Implemented list view for posts' #And that’s all for this lesson! We’ve done a hell of a lot in this lesson - set up our project, # created a comprehensive test suite for it, and implemented the basic functionality. Next time # Make it a bit prettier using Twitter Bootstrap, as well as implementing more of the # basic functionality for the blog. $ python manage.py migrate #This won’t actually make any changes, but it will ensure that all future changes to your models for the # blogengine app are handled by South. Let’s commit our app skeleton: $ git add django_tutorial_blog_ng/settings.py blogengine/ $ git commit -m 'Added blogengine app skeleton'
def test_all_post_feed(self): cat = Category() cat.name = 'python' cat.description = 'python' cat.save() tag = Tag() tag.name = 'python' tag.description = 'python' tag.save() author = User.objects.create_user('example', '*****@*****.**', 'password') author.save() post = Post() post.title = 'first post' post.text = 'first post text' post.slug = 'first-text' post.pub_date = timezone.now() post.author = author post.category = cat post.save() post.tags.add(tag) post.save() self.assertEquals(len(Post.objects.all()), 1) self.assertEquals(Post.objects.all()[0], post) r = self.client.get('/feeds/posts/') self.assertEqual(r.status_code, 200) feed = feedparser.parse(r.content) self.assertEqual(len(feed.entries), 1) self.assertEquals(feed.entries[0].title, post.title) self.assertEquals(feed.entries[0].description, post.text)
def test_post_page(self): #create category category = Category() category.name = 'python' category.descritption = 'python' category.save() #tag tag = Tag() tag.name = 'python' tag.description = 'not perl' tag.save() #create author author = User.objects.create_user('adsf', '*****@*****.**', 'asdf') author.save() post = Post() post.title = 'my first page' post.text = 'This is my [first blog post](http://localhost:8000/)' post.pub_date = timezone.now() post.slug = 'my-first-post' post.author = author post.category = category post.save() post.tags.add(tag) post.save() self.assertEquals(len(Post.objects.all()), 1) self.assertEqual(Post.objects.all()[0], post) p = Post.objects.all()[0] p_url = p.get_absolute_url() r = self.client.get(p_url) self.assertEquals(r.status_code, 200) self.assertTrue(post.title in r.content) self.assertTrue(post.category.name in r.content) self.assertTrue(markdown.markdown(post.text) in r.content) self.assertTrue(str(post.pub_date.year) in r.content) self.assertTrue(str(post.pub_date.day) in r.content) self.assertTrue(tag.name in r.content) self.assertTrue('<a href="http://localhost:8000/">first blog post</a>' in r.content)
def test_create_post(self): #create author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() #create Category cat = Category() cat.name = 'python' cat.description = 'python' cat.save() #create tag tag = Tag() tag.name = 'python' tag.description = 'python' tag.save() post = Post() title = "first blog" pub_date = timezone.now() text = "first blog text" post.text = text post.pub_date = pub_date post.title = title post.slug = 'first-post' post.author = author post.category = cat post.save() post.tags.add(tag) post.save() posts = Post.objects.all() self.assertEquals(len(posts), 1) p = posts[0] self.assertEquals(p, post) self.assertEqual(p.title, title) self.assertEqual(p.text, text) self.assertEqual(p.pub_date, pub_date) self.assertEquals(p.author.username, author.username) self.assertEquals(p.author.email, author.email) self.assertEquals(p.category.name, cat.name) self.assertEquals(p.category.description, cat.description) #check tags post_tags = p.tags.all() self.assertEqual(len(post_tags), 1) t = post_tags[0] self.assertEqual(t, tag) self.assertEqual(t.name, tag.name) self.assertEqual(t.description, tag.description)
def test_all_post_feed(self): # Create the category category = Category() category.name = 'python' category.description = 'The Python programming language' category.save() # Create the tag tag = Tag() tag.name = 'python' tag.description = 'The Python programming language' tag.save() # Create the author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() # Create the site site = Site() site.name = 'example.com' site.domain = 'example.com' site.save() # Create a post post = Post() post.title = 'My first post' post.text = 'This is my first blog post' post.slug = 'my-first-post' post.pub_date = timezone.now() post.author = author post.site = site post.category = category # Save it post.save() # Add the tag post.tags.add(tag) post.save() # Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Fetch the feed response = self.client.get('/feeds/posts/') self.assertEquals(response.status_code, 200) # Parse the feed feed = feedparser.parse(response.content) # Check length self.assertEquals(len(feed.entries), 1) # Check post retrieved is the correct one feed_post = feed.entries[0] self.assertEquals(feed_post.title, post.title) self.assertEquals(feed_post.description, post.text)
def test_edit_post(self): #create category category = Category() category.name = 'python' category.descritption = 'python' category.save() #create author author = User.objects.create_user('testuser', '*****@*****.**', 'password') author.save() #create tag tag = Tag() tag.name = 'python' tag.description = 'python' tag.save() post = Post() post.title = 'My first Post' post.text = 'first blog post' post.pub_date = timezone.now() post.author = author post.category = category post.save() post.tags.add(tag) post.save() self.client.login(username='******', password='******') url = '/admin/blogengine/post/%d/' % Post.objects.all()[0].id response = self.client.post(url, { 'title': 'my second post', 'text': 'this is my second post', 'pub_date_0': '2014-10-9', 'pub_date_1': '22:00:04', 'slug': 'my-first-test', 'category': Category.objects.all()[0].id, 'tags': Tag.objects.all()[0].id, }, follow=True) self.assertEquals(response.status_code, 200) self.assertTrue('changed successfully' in response.content) all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) self.assertEquals(all_posts[0].title, 'my second post'), self.assertEquals(all_posts[0].text, 'this is my second post')