From ead5260da8cf17e699dc805d1172d07640ac81ef Mon Sep 17 00:00:00 2001 From: Razvan Andrei Ionescu Date: Mon, 27 Jun 2016 11:36:41 +0300 Subject: [PATCH 1/6] Refactor tests, installed factory_boy. Github #22 --- requirements/razvansky.txt | 2 ++ requirements/test.txt | 1 + 2 files changed, 3 insertions(+) diff --git a/requirements/razvansky.txt b/requirements/razvansky.txt index 97e58f7..db7796c 100644 --- a/requirements/razvansky.txt +++ b/requirements/razvansky.txt @@ -9,3 +9,5 @@ coveralls==1.1 docopt==0.6.2 feedparser==5.2.1 + +factory_boy==2.7.0 diff --git a/requirements/test.txt b/requirements/test.txt index 401ce4f..34e6407 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -10,3 +10,4 @@ coveralls==1.1 docopt==0.6.2 feedparser==5.2.1 +factory_boy==2.7.0 From 24d039557c39d2b1ab58c9c2fc0d0fbfc9e2d4cc Mon Sep 17 00:00:00 2001 From: Razvan Andrei Ionescu Date: Mon, 27 Jun 2016 11:43:55 +0300 Subject: [PATCH 2/6] Refactor tests: Use factory_boy for Site, Github #22 --- src/blogengine/tests.py | 57 +++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/src/blogengine/tests.py b/src/blogengine/tests.py index c551213..30426c1 100644 --- a/src/blogengine/tests.py +++ b/src/blogengine/tests.py @@ -11,6 +11,18 @@ from blogengine.models import Post, Category, Tag import markdown2 as markdown import feedparser +import factory.django + +# Factories +class SiteFactory(factory.django.DjangoModelFactory): + class Meta: + model = Site + django_get_or_create = ( + 'name', + 'domain' + ) + name = 'test.com' + domain = 'test.com' # Create your tests here. class PostTest(TestCase): @@ -50,10 +62,7 @@ def test_create_tag(self): def test_create_post(self): # Create the site - site = Site() - site.name = 'test.com' - site.domain = 'test.com' - site.save() + site = SiteFactory() # Create the category category = Category() @@ -115,10 +124,7 @@ def test_create_post(self): def test_create_romanian_post(self): # Create the site - site = Site() - site.name = 'test.com' - site.domain = 'test.com' - site.save() + site = SiteFactory() # Create the tag tag = Tag() tag.name = u'răzvansky' @@ -373,10 +379,7 @@ def test_create_admin_post(self): def test_edit_post(self): # Create the site - site = Site() - site.name = 'edit.com' - site.domain = 'edit.com' - site.save() + site = SiteFactory() # Create the category category = Category() @@ -426,10 +429,7 @@ def test_edit_post(self): def test_delete_post(self): # Create the site - site = Site() - site.name = 'delete.com' - site.domain = 'delete.com' - site.save() + site = SiteFactory() # Create the category category = Category() @@ -475,10 +475,7 @@ def setUp(self): def test_index(self): # Create the site - site = Site() - site.name = 'test.com' - site.domain = 'test.com' - site.save() + site = SiteFactory() # Create the category category = Category() @@ -532,10 +529,7 @@ def test_index(self): def test_post_page(self): # Create the site - site = Site() - site.name = 'test.com' - site.domain = 'test.com' - site.save() + site = SiteFactory() # Create the category category = Category() category.name = 'python' @@ -597,10 +591,7 @@ def test_post_page(self): def test_category_page(self): # Create the site - site = Site() - site.name = 'test.com' - site.domain = 'test.com' - site.save() + site = SiteFactory() # Create the category category = Category() category.name = 'python' @@ -646,10 +637,7 @@ def test_category_page(self): def test_tag_page(self): # Create the site - site = Site() - site.name = 'test.com' - site.domain = 'test.com' - site.save() + site = SiteFactory() # Create the tag tag = Tag() tag.name = 'pythonsky' @@ -731,10 +719,7 @@ def test_create_flatpage(self): class FeedTest(BaseAcceptanceTest): def test_all_post_feed(self): # Create the site - site = Site() - site.name = 'test.com' - site.domain = 'test.com' - site.save() + site = SiteFactory() # Create the category category = Category() category.name = 'pythonsky' From d02c540fa523e921b80b1e908d3a0bbde1e76d7a Mon Sep 17 00:00:00 2001 From: Razvan Andrei Ionescu Date: Mon, 27 Jun 2016 12:10:38 +0300 Subject: [PATCH 3/6] Refactor tests: Use factory_boy for Category, Github #22 --- src/blogengine/models.py | 4 +-- src/blogengine/tests.py | 69 ++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 48 deletions(-) diff --git a/src/blogengine/models.py b/src/blogengine/models.py index d7d643f..0f96156 100644 --- a/src/blogengine/models.py +++ b/src/blogengine/models.py @@ -7,10 +7,10 @@ class Category(models.Model): name = models.CharField(max_length=100) description = models.TextField() slug = models.SlugField(max_length=40, unique=True, blank=True, null=True) - def save(self): + def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(unicode(self.name)) - super(Category, self).save() + super(Category, self).save(*args, **kwargs) def get_absolute_url(self): return "/blog/category/%s/" % (self.slug) def __unicode__(self): diff --git a/src/blogengine/tests.py b/src/blogengine/tests.py index 30426c1..a6c6f05 100644 --- a/src/blogengine/tests.py +++ b/src/blogengine/tests.py @@ -24,16 +24,23 @@ class Meta: name = 'test.com' domain = 'test.com' +class CategoryFactory(factory.django.DjangoModelFactory): + class Meta: + model = Category + django_get_or_create = ( + 'name', + 'description', + 'slug' + ) + name = 'python' + description = 'Python the programming language' + slug = 'python' + # Create your tests here. class PostTest(TestCase): def test_create_category(self): # Create the category - category = Category() - # Add attributes - category.name = 'python' - category.description = 'Python the programming language' - # Save it - category.save() + category = CategoryFactory() # Check if we can find it all_categories = Category.objects.all() self.assertEquals(len(all_categories), 1) @@ -65,10 +72,7 @@ def test_create_post(self): site = SiteFactory() # Create the category - category = Category() - category.name = 'python' - category.description = 'Python the programming language' - category.save() + category = CategoryFactory() # Create the tag tag = Tag() @@ -234,10 +238,7 @@ def test_create_category(self): self.assertEquals(len(all_categories), 1) def test_edit_category(self): # Create the category - category = Category() - category.name = 'python' - category.description = 'The Python programming language' - category.save() + category = CategoryFactory() # Log in self.client.login(username='testuser', password="test") # Edit the category @@ -256,10 +257,7 @@ def test_edit_category(self): self.assertEquals(only_category.description, 'The Perl programming language') def test_delete_category(self): # Create the category - category = Category() - category.name = 'python' - category.description = 'The Python programming language' - category.save() + category = CategoryFactory() # Log in self.client.login(username='testuser', password="test") # Delete the category @@ -336,10 +334,7 @@ def test_delete_tag(self): def test_create_admin_post(self): # Create the category - category = Category() - category.name = 'python' - category.description = 'The Python programming language' - category.save() + category = CategoryFactory() # Create the tag tag = Tag() @@ -382,10 +377,7 @@ def test_edit_post(self): site = SiteFactory() # Create the category - category = Category() - category.name = 'python' - category.description = 'The Python programming language' - category.save() + category = CategoryFactory() # Create the tag tag = Tag() @@ -432,10 +424,7 @@ def test_delete_post(self): site = SiteFactory() # Create the category - category = Category() - category.name = 'python' - category.description = 'The Python programming language' - category.save() + category = CategoryFactory() # Create the tag tag = Tag() @@ -478,10 +467,7 @@ def test_index(self): site = SiteFactory() # Create the category - category = Category() - category.name = 'python' - category.description = 'The Python programming language' - category.save() + category = CategoryFactory() # Create the tag tag = Tag() @@ -531,10 +517,7 @@ def test_post_page(self): # Create the site site = SiteFactory() # Create the category - category = Category() - category.name = 'python' - category.description = 'The Python programming language' - category.save() + category = CategoryFactory() # Create the tag tag = Tag() tag.name = 'pythonsky' @@ -593,10 +576,7 @@ def test_category_page(self): # Create the site site = SiteFactory() # Create the category - category = Category() - category.name = 'python' - category.description = 'The Python programming language' - category.save() + category = CategoryFactory() # Create the post post = Post() post.title = 'My first post' @@ -721,10 +701,7 @@ def test_all_post_feed(self): # Create the site site = SiteFactory() # Create the category - category = Category() - category.name = 'pythonsky' - category.description = 'The pythonic programming language' - category.save() + category = CategoryFactory() #Create the tag tag = Tag() tag.name = 'python' From 1a6d7e281c6aa44e5dc58c3fca77e5c840c9613f Mon Sep 17 00:00:00 2001 From: Razvan Andrei Ionescu Date: Mon, 27 Jun 2016 12:19:23 +0300 Subject: [PATCH 4/6] Refactor tests: Use factory_boy for Tag, Github #22 --- src/blogengine/models.py | 4 +-- src/blogengine/tests.py | 75 ++++++++++++++-------------------------- 2 files changed, 27 insertions(+), 52 deletions(-) diff --git a/src/blogengine/models.py b/src/blogengine/models.py index 0f96156..f14a243 100644 --- a/src/blogengine/models.py +++ b/src/blogengine/models.py @@ -22,10 +22,10 @@ class Tag(models.Model): name = models.CharField(max_length=100) description = models.TextField() slug = models.SlugField(max_length=40, unique=True, blank=True, null=True) - def save(self): + def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(unicode(self.name)) - super(Tag, self).save() + super(Tag, self).save(*args, **kwargs) def get_absolute_url(self): return "/blog/tag/%s/" % (self.slug) def __unicode__(self): diff --git a/src/blogengine/tests.py b/src/blogengine/tests.py index a6c6f05..70e98f4 100644 --- a/src/blogengine/tests.py +++ b/src/blogengine/tests.py @@ -36,6 +36,18 @@ class Meta: description = 'Python the programming language' slug = 'python' +class TagFactory(factory.django.DjangoModelFactory): + class Meta: + model = Tag + django_get_or_create = ( + 'name', + 'description', + 'slug' + ) + name = 'pythonsky' + description = 'Pythonsky the programming language' + slug = 'pythonsky' + # Create your tests here. class PostTest(TestCase): def test_create_category(self): @@ -52,12 +64,7 @@ def test_create_category(self): def test_create_tag(self): # Create the tag - tag = Tag() - # Add attributes - tag.name = 'pythonsky' - tag.description = 'Pythonsky the programming language' - # Save it - tag.save() + tag = TagFactory() # Check if we can find it all_tags = Tag.objects.all() self.assertEquals(len(all_tags), 1) @@ -75,10 +82,7 @@ def test_create_post(self): category = CategoryFactory() # Create the tag - tag = Tag() - tag.name = 'pythonsky' - tag.description = 'Pythonsky the programming language' - tag.save() + tag = TagFactory() # Create the post post = Post() @@ -130,10 +134,7 @@ def test_create_romanian_post(self): # Create the site site = SiteFactory() # Create the tag - tag = Tag() - tag.name = u'răzvansky' - tag.description = u'Răzvan the programming language' - tag.save() + tag = TagFactory(name = u'răzvansky', description = u'Răzvan the programming language', slug = 'razvansky') # Create the post post = Post() # Set attributes including romanian characters "diacritice" @@ -171,6 +172,7 @@ def test_create_romanian_post(self): self.assertEquals(only_post_tag, tag) self.assertEquals(only_post_tag.name, u'răzvansky') self.assertEquals(only_post_tag.description, u'Răzvan the programming language') + self.assertEquals(only_post_tag.slug, u'razvansky') class BaseAcceptanceTest(LiveServerTestCase): def setUp(self): @@ -292,10 +294,7 @@ def test_create_tag(self): self.assertEquals(len(all_tags), 1) def test_edit_tag(self): # Create the tag - tag = Tag() - tag.name = 'pythonsky' - tag.description = 'The Pythonsky programming language' - tag.save() + tag = TagFactory() # Log in self.client.login(username='testuser', password='test') # Edit the tag @@ -314,10 +313,7 @@ def test_edit_tag(self): self.assertEquals(only_tag.description, 'The Perlsky programming language') def test_delete_tag(self): # Create the tag - tag = Tag() - tag.name = 'python' - tag.description = 'The Python programming language' - tag.save() + tag = TagFactory() # Log in self.client.login(username='testuser', password='test') # Delete the tag @@ -337,10 +333,7 @@ def test_create_admin_post(self): category = CategoryFactory() # Create the tag - tag = Tag() - tag.name = 'pythonsky' - tag.description = 'The Pythonsky programming language' - tag.save() + tag = TagFactory() # Log in self.client.login(username='testuser', password='test') @@ -380,10 +373,7 @@ def test_edit_post(self): category = CategoryFactory() # Create the tag - tag = Tag() - tag.name = 'pythonsky' - tag.description = 'The Pythonsky programming language' - tag.save() + tag = TagFactory() # Log in self.client.login(username='testuser', password='test') @@ -427,10 +417,7 @@ def test_delete_post(self): category = CategoryFactory() # Create the tag - tag = Tag() - tag.name = 'pythonsky' - tag.description = 'The Pythonsky programming language' - tag.save() + tag = TagFactory() # Create the post post = Post() @@ -470,10 +457,7 @@ def test_index(self): category = CategoryFactory() # Create the tag - tag = Tag() - tag.name = 'pythonsky' - tag.description = 'The Pythonsky programming language' - tag.save() + tag = TagFactory() # Create the post post = Post() @@ -519,10 +503,7 @@ def test_post_page(self): # Create the category category = CategoryFactory() # Create the tag - tag = Tag() - tag.name = 'pythonsky' - tag.description = 'The Pythonsky programming language' - tag.save() + tag = TagFactory() # Create the post post = Post() post.title = 'My first post' @@ -619,10 +600,7 @@ def test_tag_page(self): # Create the site site = SiteFactory() # Create the tag - tag = Tag() - tag.name = 'pythonsky' - tag.description = 'The Pythonsky programming language' - tag.save() + tag = TagFactory() # Create the post post = Post() post.title = 'My tagged post' @@ -703,10 +681,7 @@ def test_all_post_feed(self): # Create the category category = CategoryFactory() #Create the tag - tag = Tag() - tag.name = 'python' - tag.description = 'The python programming language' - tag.save() + tag = TagFactory() # Create a post post = Post() post.title = 'My first post' From e67cc0b501256593c333f1bb8f4f57204653fcc0 Mon Sep 17 00:00:00 2001 From: Razvan Andrei Ionescu Date: Mon, 27 Jun 2016 12:24:47 +0300 Subject: [PATCH 5/6] Refactor tests: Use factory_boy for FlatPage, Github #22 --- src/blogengine/tests.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/blogengine/tests.py b/src/blogengine/tests.py index 70e98f4..bdcfe51 100644 --- a/src/blogengine/tests.py +++ b/src/blogengine/tests.py @@ -48,6 +48,18 @@ class Meta: description = 'Pythonsky the programming language' slug = 'pythonsky' +class FlatPageFactory(factory.django.DjangoModelFactory): + class Meta: + model = FlatPage + django_get_or_create = ( + 'url', + 'title', + 'content' + ) + url = '/about/' + title = 'About Me' + content = 'All about me. Well almost ...' + # Create your tests here. class PostTest(TestCase): def test_create_category(self): @@ -641,11 +653,7 @@ def test_tag_page(self): class FlatPageViewTest(BaseAcceptanceTest): def test_create_flatpage(self): # Create FlatPage - page = FlatPage() - page.url = '/about/' - page.title = 'About Me' - page.content = 'All about me. Well almost ...' - page.save() + page = FlatPageFactory() # Add the site page.sites.add(Site.objects.all()[0]) From ae0a4e135bdbed9fe4bbb8ba3cd18ee4b98f1345 Mon Sep 17 00:00:00 2001 From: Razvan Andrei Ionescu Date: Mon, 27 Jun 2016 12:48:46 +0300 Subject: [PATCH 6/6] Refactor tests: Use factory_boy for Post, Github #22 --- src/blogengine/tests.py | 115 +++++++++++----------------------------- 1 file changed, 30 insertions(+), 85 deletions(-) diff --git a/src/blogengine/tests.py b/src/blogengine/tests.py index bdcfe51..b214e7e 100644 --- a/src/blogengine/tests.py +++ b/src/blogengine/tests.py @@ -60,6 +60,22 @@ class Meta: title = 'About Me' content = 'All about me. Well almost ...' +class PostFactory(factory.django.DjangoModelFactory): + class Meta: + model = Post + django_get_or_create = ( + 'title', + 'text', + 'slug', + 'pub_date' + ) + title = 'My test post' + text = 'This is my test blog post' + slug = 'my-test-post' + pub_date = timezone.now() + site = factory.SubFactory(SiteFactory) + category = factory.SubFactory(CategoryFactory) + # Create your tests here. class PostTest(TestCase): def test_create_category(self): @@ -97,20 +113,10 @@ def test_create_post(self): tag = TagFactory() # Create the post - post = Post() - # Set the attributes - post.title = 'My test post' - post.text = 'This is my test blog post' - post.slug = 'my-test-post' - post.pub_date = timezone.now() - post.site = site - post.category = category - # Save it - post.save() + 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() @@ -147,15 +153,8 @@ def test_create_romanian_post(self): site = SiteFactory() # Create the tag tag = TagFactory(name = u'răzvansky', description = u'Răzvan the programming language', slug = 'razvansky') - # Create the post - post = Post() - # Set attributes including romanian characters "diacritice" - post.title = 'Testul cu șțăîâ' - post.text = 'Ăsta este textul de test cu ăîșțâ' - post.slug = 'testul-cu-staia' # testing prepopulated fields in admin is out of scope now. So we pass. - post.pub_date = timezone.now() - post.site = site - post.save() + # Create the post & Set attributes including romanian characters "diacritice" + post = PostFactory(title = 'Testul cu șțăîâ', text = 'Ăsta este textul de test cu ăîșțâ', slug = 'testul-cu-staia') # Add the tag only after creating post post.tags.add(tag) post.save() @@ -390,13 +389,7 @@ def test_edit_post(self): # Log in self.client.login(username='testuser', password='test') # Create the post - blogpost = Post() - blogpost.title = 'My editable post' - blogpost.text = 'This is my first editable blog post' - blogpost.slug = 'my-editable-post' - blogpost.pub_date = timezone.now() - blogpost.site = site - blogpost.save() + blogpost = PostFactory() # Edit the post response = self.client.post('/administrare/blogengine/post/' + str(blogpost.pk) + '/', { 'title': 'My EDITED post', @@ -422,24 +415,16 @@ def test_edit_post(self): self.assertEquals(only_post.text, 'This is my EDITED editable blog post') def test_delete_post(self): + # Create the post + post = PostFactory() # Create the site site = SiteFactory() - # Create the category category = CategoryFactory() - # Create the tag tag = TagFactory() + post.tags.add(tag) - # Create the post - post = Post() - post.title = 'My deletable post' - post.text = 'This is my first deletable post' - post.slug = 'my-deletable-post' - post.pub_date = timezone.now() - post.site = site - post.category = category - post.save() # Check new post saved all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) @@ -464,22 +449,12 @@ def setUp(self): def test_index(self): # Create the site site = SiteFactory() - # Create the category category = CategoryFactory() - # Create the tag tag = TagFactory() - # Create the post - post = Post() - post.title = 'My first test post for View' - post.text = 'This the first test post for view. And [markdown blog](http://127.0.0.1:8000/)' - post.slug = 'my-first-test-post-for-view' - post.pub_date = timezone.now() - post.site = site - post.category = category - post.save() + post = PostFactory(title = 'My first test post for View', text = 'This the first test post for view. And [markdown blog](http://127.0.0.1:8000/)', slug = 'my-first-test-post-for-view') post.tags.add(tag) # Check post saved all_posts = Post.objects.all() @@ -494,7 +469,7 @@ def test_index(self): #print "%s" %response self.assertTrue(markdown.markdown(post.text) in response.content.decode('utf-8')) # Check the post category is in the response - self.assertTrue(post.category.name in response.content) + self.assertTrue(post.category.name in response.content.decode('utf-8')) # Check the post tag is in the response post_tag = all_posts[0].tags.all()[0] self.assertTrue(post_tag.name in response.content.decode('utf-8')) @@ -517,16 +492,8 @@ def test_post_page(self): # Create the tag tag = TagFactory() # 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.site = site - post.category = category - post.save() + post = PostFactory(text = 'This is [my first blog post](http://127.0.0.1:8000/)') post.tags.add(tag) - post.save() # Check new post saved all_posts = Post.objects.all() @@ -547,7 +514,7 @@ def test_post_page(self): self.assertTrue(post.title in response.content) # Check the post category is in the response - self.assertTrue(post.category.name in response.content) + self.assertTrue(post.category.name in response.content.decode('utf-8')) # Check the post category is in the response post_tag = all_posts[0].tags.all()[0] @@ -571,14 +538,7 @@ def test_category_page(self): # Create the category category = CategoryFactory() # 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.site = site - post.category = category - post.save() + post = PostFactory(text = 'This is [my first blog post](http://127.0.0.1:8000/)') # Check new post saved all_posts = Post.objects.all() @@ -614,13 +574,7 @@ def test_tag_page(self): # Create the tag tag = TagFactory() # Create the post - post = Post() - post.title = 'My tagged post' - post.text = 'This is [my tagged blog post](http://127.0.0.1:8000/)' - post.slug = 'my-tagged-post' - post.pub_date = timezone.now() - post.site = site - post.save() + post = PostFactory(text = 'This is [my tagged blog post](http://127.0.0.1:8000/)') post.tags.add(tag) # Check new post saved all_posts = Post.objects.all() @@ -691,18 +645,9 @@ def test_all_post_feed(self): #Create the tag tag = TagFactory() # 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.site = site - post.category = category - # Save it - post.save() + post = PostFactory() # 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)