Skip to content

Commit

Permalink
Improve documentation, fix unit tests to clear cache before each test
Browse files Browse the repository at this point in the history
  • Loading branch information
Matoking committed Jun 7, 2015
1 parent a742f8a commit 3a769a3
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 14 deletions.
10 changes: 7 additions & 3 deletions INSTALL.md
Expand Up @@ -39,9 +39,13 @@ After this is done, run the following command in the root of your virtualenv env

python manage.py syncdb

After you have let Django sync its own database tables, you can run the SQL queries in sql/create_tables.sql to create the rest of the tables.
Configuring the Redis instance
--
pastebin-django uses a data structure server to both to store persistent data that wouldn't be a good fit for a relational database (eg. paste hit counts). You can install Redis on Debian or a derivative using the following command.

sudo apt-get install redis-server

psql DATABASE_NAME < sql/create_tables.sql
By default Redis runs on port 6379 and saves its data regularly. However, pastebin-django's default settings assume a persistent Redis storage runs on the port 6380, so you may need to change the port for the 'persistent' cache in settings.py to match this port. You can also run a non-persistent Redis server under the port 6379 and a persistent Redis server under port 6380, which matches the default settings.

Running the unit tests
--
Expand All @@ -57,4 +61,4 @@ We are now ready to start the development web server. Run the following command

python manage.py runserver 127.0.0.1:8000

Now, try opening http://127.0.0.1:8000 in your web browser. If everything worked out correctly, you should now be able to use the web application as normal.
Now, try opening http://127.0.0.1:8000 in your web browser. If everything worked out correctly, you should now be able to use the web application as normal.
Binary file removed doc/components.odt
Binary file not shown.
Binary file removed doc/diagrams.odt
Binary file not shown.
Binary file removed doc/documentation.pdf
Binary file not shown.
Binary file removed doc/install.odt
Binary file not shown.
Binary file removed doc/introduction.odt
Binary file not shown.
Binary file removed doc/relation_diagram.png
Binary file not shown.
Binary file removed doc/structure.odt
Binary file not shown.
Binary file removed doc/use_cases.odt
Binary file not shown.
Binary file removed doc/user_manual.odt
Binary file not shown.
2 changes: 2 additions & 0 deletions home/tests.py
Expand Up @@ -65,11 +65,13 @@ def test_latest_pastes_doesnt_show_expired_pastes(self):
"visibility": "public"},
follow=True)

self.clearCache()
response = self.client.get(reverse("home:home"))

self.assertContains(response, "Paste paste")

with freeze_time("2015-01-01 13:00:01"):
self.clearCache()
response = self.client.get(reverse("home:home"))

self.assertContains(response, "No pastes have been submitted yet")
Expand Down
10 changes: 7 additions & 3 deletions home/views.py
Expand Up @@ -23,8 +23,12 @@ def home(request):
"""
paste_form = SubmitPasteForm(request.POST or None, request=request)

latest_pastes = Paste.objects.get_pastes(include_expired=False, include_hidden=False,
count=15)
latest_pastes = cache.get("home_latest_pastes")

if latest_pastes == None:
latest_pastes = Paste.objects.get_pastes(include_expired=False, include_hidden=False,
count=15)
cache.set("home_latest_pastes", latest_pastes, 5)

languages = highlighting.settings.LANGUAGES

Expand Down Expand Up @@ -75,7 +79,7 @@ def latest_pastes(request, page=1):

if pastes == None:
pastes = Paste.objects.get_pastes(count=PASTES_PER_PAGE, offset=offset, include_hidden=False)
cache.set("latest_pastes:%s" % page, pastes, 10)
cache.set("latest_pastes:%s" % page, pastes, 5)

pages = Paginator.get_pages(page, PASTES_PER_PAGE, total_paste_count)
total_pages = math.ceil(float(total_paste_count) / float(PASTES_PER_PAGE))
Expand Down
16 changes: 12 additions & 4 deletions pastebin/testcase.py
Expand Up @@ -6,12 +6,20 @@

class CacheAwareTestCase(TestCase):
"""
Cache-aware TestCase that clears both Redis servers on startup
Cache-aware TestCase that clears the Redis storage and cache on startup
"""
def setUp(self):
super(CacheAwareTestCase, self).setUp()
def clearCache(self):
"""
Clears the cache
Can be invoked manually if the unit test requires it
"""
cache.clear()
con = get_redis_connection("persistent")

con.flushall()
con.flushall()

def setUp(self):
super(CacheAwareTestCase, self).setUp()

self.clearCache()
4 changes: 2 additions & 2 deletions pastes/models.py
Expand Up @@ -279,7 +279,7 @@ def add_paste(self, text, user=None, title="Untitled", expiration=None, visibili
hash=self.hash,
format=self.format)
first_version.save()
cache.set("paste_version:%s:1" % (char_id), first_version)
cache.set("paste_version:%s:1" % (self.char_id), first_version)

if not self.encrypted and not self.hidden and self.expiration_datetime == None:
con = get_redis_connection("persistent")
Expand Down Expand Up @@ -325,7 +325,7 @@ def update_paste(self, text="", title="", visibility=None, format="text", encryp
hash=self.hash,
format=self.format)
new_version.save()
cache.set("paste_version:%s:%s" % (char_id, self.version), new_version)
cache.set("paste_version:%s:%s" % (self.char_id, self.version), new_version)

def remove_paste(self, type=ADMIN_REMOVAL, reason=""):
"""
Expand Down
4 changes: 3 additions & 1 deletion pastes/tests.py
Expand Up @@ -2,6 +2,8 @@
from django.core.urlresolvers import reverse
from django.utils.html import escape

from pastebin.testcase import CacheAwareTestCase

from freezegun import freeze_time

from pastes.models import Paste
Expand Down Expand Up @@ -40,7 +42,7 @@ def upload_test_paste(test_case, username="TestUser"):
title="Test paste")

@freeze_time("2015-01-01")
class PasteTests(TestCase):
class PasteTests(CacheAwareTestCase):
def test_upload_paste(self):
"""
Check that paste is submitted correctly and can be viewed
Expand Down
4 changes: 3 additions & 1 deletion users/tests.py
Expand Up @@ -2,6 +2,8 @@
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User

from pastebin.testcase import CacheAwareTestCase

from pastes.models import Paste

from freezegun import freeze_time
Expand Down Expand Up @@ -42,7 +44,7 @@ def upload_test_paste(test_case, username="TestUser"):
title="Test paste")

@freeze_time("2015-01-01")
class UserTests(TestCase):
class UserTests(CacheAwareTestCase):
def test_user_can_register(self):
"""
Register with valid details
Expand Down
3 changes: 3 additions & 0 deletions users/views.py
Expand Up @@ -90,7 +90,10 @@ def profile(request, username, tab="home", page=1):
if profile_user == None:
profile_user = User.objects.get(username=username)
cache.set("user:%s" % username, profile_user)
elif profile_user == False:
return render(request, "users/profile/profile_error.html", {"reason": "not_found"}, status=404)
except ObjectDoesNotExist:
cache.set("user:%s" % username, False)
return render(request, "users/profile/profile_error.html", {"reason": "not_found"}, status=404)

if not profile_user.is_active:
Expand Down

0 comments on commit 3a769a3

Please sign in to comment.