import datetime os from mercurial import ui hg from mercurial repo imp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import datetime, os
from mercurial import ui, hg
from mercurial.repo import RepoError
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from dumps.utils import format
from dumps.models import Code, Lexer
from dumps import vars
input = """
from django.db import models
from django.contrib.auth.models import User
from tagging.fields import TagField
"""
output = u"""<table class="highlighttable"><tr><td class="linenos"><pre>1
2
3</pre></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span>
<span class="kn">from</span> <span class="nn">tagging.fields</span> <span class="kn">import</span> <span class="n">TagField</span>
</pre></div>
</td></tr></table>"""
class SimpleTest(TestCase):
def test_formated_text(self):
lexer = "python"
formated_output = format(lexer, input)
self.failUnlessEqual(output, formated_output)
class ModelTests(TestCase):
def setUp(self):
lexer = Lexer(title="Python", slug="python")
lexer.save()
self.lexer = lexer
text_lexer = Lexer(title="Text", slug="text")
text_lexer.save()
self.client = Client()
user = User(username="test", email="admin@muzx.net")
user.set_password("test")
user.save()
self.user = user
self.upload_url = reverse("upload-code")
def test_lexer(self):
lexer = Lexer.objects.get(slug="python")
self.failUnlessEqual(lexer.title, "Python")
def test_code(self):
code = Code(content="print 'hello world'", lexer=self.lexer, date=datetime.datetime.now())
code.save()
self.failUnlessEqual(code.content_formated, format("python", code.content))
url = reverse("show-code", args=[code.id])
response = self.client.get(url)
self.failUnlessEqual(response.status_code, 200)
def test_anonymous_upload(self):
response = self.client.post(self.upload_url, {'content': input, 'lexer': 'python', 'filename': 'script.py'})
code = Code.objects.latest("date")
self.failUnlessEqual(code.content_formated, format("python", input))
self.failUnlessEqual(response.status_code, 200)
def test_auth_upload(self):
response = self.client.post(self.upload_url, {'content': input, 'lexer': 'python',
'email': 'admin@muzx.net', 'password': 'test', 'filename': 'script.py'})
code = Code.objects.latest("date")
self.failUnlessEqual(code.user, self.user)
self.failUnlessEqual(response.status_code, 200)
def test_unknown_upload_lexer(self):
response = self.client.post(self.upload_url, {'content': input, 'lexer': 'bug', 'filename': 'script.py'})
code = Code.objects.latest("date")
self.failUnlessEqual(code.lexer.slug, "python")
self.failUnlessEqual(response.status_code, 200)
def test_without_upload_lexer(self):
response = self.client.post(self.upload_url, {'content': input, 'filename': 'script.py'})
code = Code.objects.latest("date")
self.failUnlessEqual(code.lexer.slug, "python")
self.failUnlessEqual(response.status_code, 200)
def test_with_buggy_upload_lexer(self):
response = self.client.post(self.upload_url, {'content': 'hello world', 'filename': 'script.pdy'})
code = Code.objects.latest("date")
self.failUnlessEqual(code.lexer.slug, "text")
self.failUnlessEqual(response.status_code, 200)
def test_guess_by_filename(self):
response = self.client.post(self.upload_url, {'content': input, 'filename': 'script.py'})
code = Code.objects.latest("date")
self.failUnlessEqual(code.lexer.slug, "python")
self.failUnlessEqual(response.status_code, 200)
class HistorizeTests(TestCase):
def setUp(self):
lexer = Lexer(title="Python", slug="python")
lexer.save()
self.lexer = lexer
text_lexer = Lexer(title="Text", slug="text")
text_lexer.save()
self.client = Client()
user = User(username="test", email="admin@muzx.net")
user.set_password("test")
user.save()
self.user = user
self.upload_url = reverse("upload-code")
self.u = ui.ui()
try:
self.repo = hg.repository(self.u, vars.REV_DIR)
except RepoError:
self.repo = hg.repository(self.u, vars.REV_DIR, create=True)
def test_changes(self):
code = Code(content="print 'hello world'", lexer=self.lexer, date=datetime.datetime.now())
code.save()
code.content = "#!/usr/bin/python\nimport mercurial\nprint 'hello world'"
code.save()
dir_path = os.path.join(code.key[0], code.key)
full_path = os.path.join(vars.REV_DIR, dir_path)
self.failUnlessEqual(os.path.isfile(full_path), True)
code.content = "#!/usr/bin/python\nimport mercurial\nprint 'hello world\n'"
code.save()
revisions = code.get_revisions()
self.failUnlessEqual(type(revisions), type([]))
self.failUnlessEqual(code.get_latest_revision(), 1)
def test_whitout_key(self):
code = Code(content="print 'hello world'", lexer=self.lexer, date=datetime.datetime.now())
code.save()
revisions = code.get_revisions()
self.failUnlessEqual(revisions, None)
self.failUnlessEqual(code.get_latest_revision(), None)