def test_speaker_page_lists_speeches(self): # Add a speaker speaker = Speaker.objects.create( name='Steve', instance=self.instance, summary='A froody dude', image='http://example.com/image.jpg') self.speakers.append(speaker) id = ('%s' % speaker.person_ptr_id).encode() self.assertEqual(speaker.colour, hashlib.sha1(id).hexdigest()[:6]) # Call the speaker's page resp = self.client.get('/speaker/%s' % speaker.slug) assertRegex(self, resp.content.decode(), r'background-color: #[0-9a-f]{6}(?i)') self.assertSequenceEqual("Steve", resp.context['speaker'].name) self.assertSequenceEqual("A froody dude", resp.context['speaker'].summary) self.assertSequenceEqual("http://example.com/image.jpg", resp.context['speaker'].image) # Assert no speeches self.assertSequenceEqual([], resp.context['speech_list']) self.assertSequenceEqual([], resp.context['page_obj']) # Add a speech speech = Speech.objects.create( text="A test speech", speaker=speaker, instance=self.instance, start_date=date(2014, 9, 17), ) # Call the speaker's page again resp = self.client.get('/speaker/%s' % speaker.slug) self.assertSequenceEqual([speech], resp.context['speech_list']) self.assertSequenceEqual([speech], resp.context['page_obj']) # Add another speech on the same date and check that both dates are displayed speech = Speech.objects.create( text="Another speech", speaker=speaker, instance=self.instance, start_date=date(2014, 9, 17), ) # Check that a search which returns more than one speech on the same # date displays a date for each. resp = self.client.get('/speaker/%s' % speaker.slug) self.assertEqual( len(re.findall(r'<span class="breadcrumbs__date">\s*17 Sep 2014\s*</span>', resp.content.decode())), 2) # Add another speech on a different date and check the order speech = Speech.objects.create( text="A third speech", speaker=speaker, instance=self.instance, start_date=date(2014, 9, 18), ) resp = self.client.get('/speaker/%s' % speaker.slug) self.assertEqual(resp.context['speech_list'][0].start_date, date(2014, 9, 18))
def test_user_not_logged_in_without_next(self): """ Test when ?next= is not present in URL. Rendered form should have hidden input with name next and value '' """ response = self.client.get(self.login_url) self.assertEqual(response.status_code, 200) six.assertRegex(self, response.content.decode('utf-8'), r'name=[\'"]next[\'"].*?value=[\'"]{2}')
def test_file_truncation(self): # Given the max_length is limited, when multiple files get uploaded # under the same name, then the filename get truncated in order to fit # in _(7 random chars). When most of the max_length is taken by # dirname + extension and there are not enough characters in the # filename to truncate, an exception should be raised. objs = [Storage() for i in range(2)] filename = 'filename.ext' for o in objs: o.limited_length.save(filename, ContentFile('Same Content')) try: # Testing truncation. names = [o.limited_length.name for o in objs] self.assertEqual(names[0], 'tests/%s' % filename) six.assertRegex(self, names[1], 'tests/fi_%s.ext' % FILE_SUFFIX_REGEX) # Testing exception is raised when filename is too short to truncate. filename = 'short.longext' objs[0].limited_length.save(filename, ContentFile('Same Content')) self.assertRaisesMessage( SuspiciousFileOperation, 'Storage can not find an available filename', objs[1].limited_length.save, *(filename, ContentFile('Same Content')) ) finally: for o in objs: o.delete()
def test_runner_deprecation_verbosity_default(self): args = ['test', '--settings=test_project.settings', 'test_runner_deprecation_app'] out, err = self.run_django_admin(args) self.assertIn("Ran 1 test", force_text(err)) # change "Deprecation" to "RemovedInDjango\d+" in Django 1.11. six.assertRegex(self, err, r"DeprecationWarning: warning from test") six.assertRegex(self, err, r"DeprecationWarning: module-level warning from deprecation_app")
def test_create_speaker_with_long_image_url(self): sixtyfive = '1234567890' * 6 + '12345' ninetynine = '1234567890' * 9 + '123456789' long_image_url = 'http://example.com/image%%E2%%97%%8F%s.jpg' % ninetynine s1 = Speaker.objects.create( name='Long Image URL', instance=self.instance, image=long_image_url, ) s2 = Speaker.objects.create( name='Duplicate long image URL', instance=self.instance, image=long_image_url, ) self.speakers.extend((s1, s2)) # Note the filename in image_cache has been truncated. self.assertEqual( s1.image_cache, u'speakers/default/image\u25cf%s.jpg' % sixtyfive, ) # The truncated filename for the second speaker has some random stuff at the end. # If this get fails it might well mean you need a Django security update # https://www.djangoproject.com/weblog/2014/aug/20/security/ assertRegex( self, smart_text(s2.image_cache), u'^speakers/default/image\u25cf%s_.{7}\.jpg$' % sixtyfive, )
def test_image_to_html_no_classnames(self): self.format.classnames = None result = self.format.image_to_html(self.image, 'test alt text') six.assertRegex(self, result, '<img src="[^"]+" width="1" height="1" alt="test alt text">' ) self.format.classnames = 'test classnames'
def testSyncUsersCommandOutput(self): out = StringIO() if six.PY3 else BytesIO() call_command("ldap_sync_users", verbosity=1, stdout=out) rows = out.getvalue().split("\n")[:-1] self.assertEqual(len(rows), User.objects.count()) for row in rows: six.assertRegex(self, row, r"^Synced [^\s]+$")
def test_jsonp_and_pretty(self): response = self.client.get(self.url, {'callback': 'abcdefghijklmnopqrstuvwxyz.ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\\|;:\'",<>/?', 'pretty': 1}) self.assertResponse(response) content = response.content.decode('utf-8') self.assertJSONEqual(content[66:-2], self.json) assertRegex(self, content, jsonp_re) assertRegex(self, response.content.decode('utf-8'), pretty_re)
def test_speaker_headshots_in_speeches_section(self): # Test that headshots vs default image work OK speaker1 = Speaker.objects.create( name='Marilyn', instance=self.instance, summary='movie star', image=u'http://example.com/imag%C3%A9.jpg') self.speakers.append(speaker1) speaker2 = Speaker.objects.create(name='Salinger', instance=self.instance) section = Section.objects.create(heading='Test Section', instance=self.instance) speech1 = Speech.objects.create( text="A girl doesn't need anyone that doesn't need her.", speaker=speaker1, section=section, instance=self.instance, public=True) speech2 = Speech.objects.create( text="I'm sick of not having the courage to be an absolute nobody.", speaker=speaker2, section=section, instance=self.instance, public=True) resp = self.client.get('/sections/' + str(section.id)) assertRegex(self, resp.content.decode(), r'(?s)<img src="/uploads/speakers/default/imag%%C3%%A9.jpg.96x96_q85_crop-smart_face_upscale.jpg".*?<a href="/speaker/%s">\s*' % (speaker1.slug)) assertRegex(self, resp.content.decode(), r'(?s)<img src="\s*/static/speeches/i/a.png\s*".*?<a href="/speaker/%s">\s*' % (speaker2.slug))
def test_race_condition(self): self.thread.start() self.save_file('conflict') self.thread.join() files = sorted(os.listdir(self.storage_dir)) self.assertEqual(files[0], 'conflict') six.assertRegex(self, files[1], 'conflict_%s' % FILE_SUFFIX_REGEX)
def test_image_to_editor_html(self): result = self.format.image_to_editor_html(self.image, "test alt text") six.assertRegex( self, result, '<img data-embedtype="image" data-id="0" data-format="test name" data-alt="test alt text" class="test classnames" src="[^"]+" width="1" height="1" alt="test alt text">', )
def test_template_comments(self): """Template comment tags on the same line of other constructs (#19552)""" os.chdir(self.test_dir) # Test detection/end user reporting of old, incorrect templates # translator comments syntax with warnings.catch_warnings(record=True) as ws: warnings.simplefilter('always') management.call_command('makemessages', locale=[LOCALE], extensions=['thtml'], verbosity=0) self.assertEqual(len(ws), 3) for w in ws: self.assertTrue(issubclass(w.category, TranslatorCommentWarning)) six.assertRegex( self, str(ws[0].message), r"The translator-targeted comment 'Translators: ignored i18n comment #1' \(file templates[/\\]comments.thtml, line 4\) was ignored, because it wasn't the last item on the line\." ) six.assertRegex( self, str(ws[1].message), r"The translator-targeted comment 'Translators: ignored i18n comment #3' \(file templates[/\\]comments.thtml, line 6\) was ignored, because it wasn't the last item on the line\." ) six.assertRegex( self, str(ws[2].message), r"The translator-targeted comment 'Translators: ignored i18n comment #4' \(file templates[/\\]comments.thtml, line 8\) was ignored, because it wasn't the last item on the line\." ) # Now test .po file contents self.assertTrue(os.path.exists(self.PO_FILE)) with open(self.PO_FILE, 'r') as fp: po_contents = force_text(fp.read()) self.assertMsgId('Translatable literal #9a', po_contents) self.assertNotIn('ignored comment #1', po_contents) self.assertNotIn('Translators: ignored i18n comment #1', po_contents) self.assertMsgId("Translatable literal #9b", po_contents) self.assertNotIn('ignored i18n comment #2', po_contents) self.assertNotIn('ignored comment #2', po_contents) self.assertMsgId('Translatable literal #9c', po_contents) self.assertNotIn('ignored comment #3', po_contents) self.assertNotIn('ignored i18n comment #3', po_contents) self.assertMsgId('Translatable literal #9d', po_contents) self.assertNotIn('ignored comment #4', po_contents) self.assertMsgId('Translatable literal #9e', po_contents) self.assertNotIn('ignored comment #5', po_contents) self.assertNotIn('ignored i18n comment #4', po_contents) self.assertMsgId('Translatable literal #9f', po_contents) self.assertIn('#. Translators: valid i18n comment #5', po_contents) self.assertMsgId('Translatable literal #9g', po_contents) self.assertIn('#. Translators: valid i18n comment #6', po_contents) self.assertMsgId('Translatable literal #9h', po_contents) self.assertIn('#. Translators: valid i18n comment #7', po_contents) self.assertMsgId('Translatable literal #9i', po_contents) six.assertRegex(self, po_contents, r'#\..+Translators: valid i18n comment #8') six.assertRegex(self, po_contents, r'#\..+Translators: valid i18n comment #9') self.assertMsgId("Translatable literal #9j", po_contents)
def test_contains_tuple_not_url_instance(self): result = check_url_config(None) warning = result[0] self.assertEqual(warning.id, 'urls.E004') six.assertRegex(self, warning.msg, ( r"^Your URL pattern \('\^tuple/\$', <function <lambda> at 0x(\w+)>\) is " r"invalid. Ensure that urlpatterns is a list of url\(\) instances.$" ))
def test_sql_delete(self): app = app_cache.get_app_config("commands_sql").models_module output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS]) drop_tables = [o for o in output if o.startswith("DROP TABLE")] self.assertEqual(len(drop_tables), 3) # Lower so that Oracle's upper case tbl names wont break sql = drop_tables[-1].lower() six.assertRegex(self, sql, r"^drop table .commands_sql_comment.*")
def test_erroneous_dump_has_error_messages(self): url = reverse('dump-app-data', kwargs={'app_label': 'flatpages'}) response = self.client.get(url, follow=True) response_messages = list(response.context['messages']) self.assertEqual(1, len(response_messages)) self.assertEqual(messages.ERROR, response_messages[0].level) assertRegex(self, response_messages[0].message, r'An exception occurred while dumping data:.*flatpages.*')
def test_sql_create(self): app = app_cache.get_app_config('commands_sql').models_module output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS]) create_tables = [o for o in output if o.startswith('CREATE TABLE')] self.assertEqual(len(create_tables), 3) # Lower so that Oracle's upper case tbl names wont break sql = create_tables[-1].lower() six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
def test_sql_delete(self): app_config = apps.get_app_config('commands_sql') output = sql_delete(app_config, no_style(), connections[DEFAULT_DB_ALIAS], close_connection=False) drop_tables = [o for o in output if o.startswith('DROP TABLE')] self.assertEqual(len(drop_tables), 3) # Lower so that Oracle's upper case tbl names wont break sql = drop_tables[-1].lower() six.assertRegex(self, sql, r'^drop table .commands_sql_comment.*')
def test_sql_delete(self): app = models.get_app("commands_sql") output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS]) # Oracle produces DROP SEQUENCE and DROP TABLE for this command. if connections[DEFAULT_DB_ALIAS].vendor == "oracle": sql = output[1].lower() else: sql = output[0].lower() six.assertRegex(self, sql, r"^drop table .commands_sql_book.*")
def test_get_available_name_existing(self): self.create_file('test.txt', CONTENT) filename = self.storage.get_available_name('test.txt') six.assertRegex(self, filename, r'^test_\w+\.txt$') if self.use_fs: self.assertAnyServerLogIs([]) else: self.assertAnyServerLogIs([('HEAD', '/test.txt', 200), ('HEAD', '/' + filename, 404)])
def test_master_detail_relationship(self): """ Verify that Contact is a master-detail relationship of Account, but Opportunity is not. """ line = self.match_line(' account = ', classes_texts['Contact']) assertRegex(self, line, r'#.* Master Detail Relationship \*') line = self.match_line(' created_by = ', classes_texts['Opportunity']) self.assertNotIn('Master Detail Relationship', line)
def test_extraction_error(self): os.chdir(self.test_dir) self.assertRaises(SyntaxError, management.call_command, 'makemessages', locale=LOCALE, extensions=['tpl'], verbosity=0) with self.assertRaises(SyntaxError) as context_manager: management.call_command('makemessages', locale=LOCALE, extensions=['tpl'], verbosity=0) six.assertRegex(self, str(context_manager.exception), r'Translation blocks must not include other block tags: blocktrans \(file templates[/\\]template_with_error\.tpl, line 3\)' ) # Check that the temporary file was cleaned up self.assertFalse(os.path.exists('./templates/template_with_error.tpl.py'))
def test_handle_garbage_upload(self): f = open(p('..', 'smuggler_fixtures', 'garbage', 'garbage.json'), mode='rb') response = self.c.post(self.url, { 'uploads': f }, follow=True) response_messages = list(response.context['messages']) self.assertEqual(1, len(response_messages)) self.assertEqual(messages.ERROR, response_messages[0].level) assertRegex(self, response_messages[0].message, ' No JSON object could be decoded')
def test_repr(self): # First, for an unevaluated SimpleLazyObject obj = self.lazy_wrap(42) # __repr__ contains __repr__ of setup function and does not evaluate # the SimpleLazyObject six.assertRegex(self, repr(obj), '^<SimpleLazyObject:') self.assertIs(obj._wrapped, empty) # make sure evaluation hasn't been triggered self.assertEqual(obj, 42) # evaluate the lazy object self.assertIsInstance(obj._wrapped, int) self.assertEqual(repr(obj), '<SimpleLazyObject: 42>')
def test_handle_integrity_error(self): f = open(p('..', 'smuggler_fixtures', 'garbage', 'invalid_page_dump.json'), mode='rb') response = self.client.post(self.url, { 'uploads': f }, follow=True) response_messages = list(response.context['messages']) self.assertEqual(1, len(response_messages)) self.assertEqual(messages.ERROR, response_messages[0].level) assertRegex(self, response_messages[0].message, r'(?i)An exception occurred while loading data:.*unique.*')
def test_handle_garbage_upload(self): f = open(p('..', 'smuggler_fixtures', 'garbage', 'garbage.json'), mode='rb') response = self.client.post(self.url, { 'uploads': f }, follow=True) response_messages = list(response.context['messages']) self.assertEqual(1, len(response_messages)) self.assertEqual(messages.ERROR, response_messages[0].level) assertRegex(self, response_messages[0].message, 'An exception occurred while loading data: ' 'Problem installing fixture .*')
def test_duplicate_filename(self): # Multiple files with the same name get _(7 random chars) appended to them. objs = [Storage() for i in range(2)] for o in objs: o.normal.save("multiple_files.txt", ContentFile("Same Content")) try: names = [o.normal.name for o in objs] self.assertEqual(names[0], "tests/multiple_files.txt") six.assertRegex(self, names[1], "tests/multiple_files_%s.txt" % FILE_SUFFIX_REGEX) finally: for o in objs: o.delete()
def test_first_character_dot(self): """ File names with a dot as their first character don't have an extension, and the underscore should get added to the end. """ self.storage.save('dotted.path/.test', ContentFile("1")) self.storage.save('dotted.path/.test', ContentFile("2")) files = sorted(os.listdir(os.path.join(self.storage_dir, 'dotted.path'))) self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path'))) self.assertEqual(files[0], '.test') six.assertRegex(self, files[1], '.test_%s' % FILE_SUFFIX_REGEX)
def test_save_existing(self): self.create_file('test.txt', CONTENT) filename = self.storage.save('test.txt', ContentFile(CONTENT * 2)) # a new file is generated six.assertRegex(self, filename, r'^test_\w+\.txt$') self.assertEqual(self.get_file(filename), CONTENT * 2) if self.use_fs: self.assertEachServerLogIs([('PUT', '/' + filename, 201)]) else: self.assertAnyServerLogIs([('HEAD', '/test.txt', 200), ('HEAD', '/' + filename, 404)] + [('PUT', '/' + filename, 201)] * len(self.storage.hosts))
def test_first_character_dot(self): """ File names with a dot as their first character don't have an extension, and the underscore should get added to the end. """ self.storage.save('dotted.path/.test', ContentFile("1")) self.storage.save('dotted.path/.test', ContentFile("2")) files = sorted( os.listdir(os.path.join(self.storage_dir, 'dotted.path'))) self.assertFalse( os.path.exists(os.path.join(self.storage_dir, 'dotted_.path'))) self.assertEqual(files[0], '.test') six.assertRegex(self, files[1], '.test_%s' % FILE_SUFFIX_REGEX)
def test_runner_deprecation_verbosity_default(self): args = [ 'test', '--settings=test_project.settings', 'test_runner_deprecation_app' ] out, err = self.run_django_admin(args) self.assertIn("Ran 1 test", force_text(err)) # change "NextVersion" to "RemovedInDjango\d+" in Django 1.11. six.assertRegex(self, err, r"RemovedInNextVersionWarning: warning from test") six.assertRegex( self, err, r"RemovedInNextVersionWarning: module-level warning from deprecation_app" )
def test_directory_with_dot(self): """Regression test for #9610. If the directory name contains a dot and the file name doesn't, make sure we still mangle the file name instead of the directory name. """ self.storage.save('dotted.path/test', ContentFile("1")) self.storage.save('dotted.path/test', ContentFile("2")) files = sorted(os.listdir(os.path.join(self.storage_dir, 'dotted.path'))) self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path'))) self.assertEqual(files[0], 'test') six.assertRegex(self, files[1], 'test_%s' % FILE_SUFFIX_REGEX)
def test_speaker_page_lists_speeches(self): # Add a speaker speaker = Speaker.objects.create( name='Steve', instance=self.instance, summary='A froody dude', image='http://example.com/image.jpg') self.speakers.append(speaker) id = ('%s' % speaker.person_ptr_id).encode() self.assertEqual(speaker.colour, hashlib.sha1(id).hexdigest()[:6]) # Call the speaker's page resp = self.client.get('/speaker/%s' % speaker.slug) assertRegex(self, resp.content.decode(), r'background-color: #[0-9a-f]{6}(?i)') self.assertSequenceEqual("Steve", resp.context['speaker'].name) self.assertSequenceEqual("A froody dude", resp.context['speaker'].summary) self.assertSequenceEqual("http://example.com/image.jpg", resp.context['speaker'].image) # Assert no speeches self.assertSequenceEqual([], resp.context['speech_list']) self.assertSequenceEqual([], resp.context['page_obj']) # Add a speech speech = Speech.objects.create( text="A test speech", speaker=speaker, instance=self.instance, start_date=date(2014, 9, 17), ) # Call the speaker's page again resp = self.client.get('/speaker/%s' % speaker.slug) self.assertSequenceEqual([speech], resp.context['speech_list']) self.assertSequenceEqual([speech], resp.context['page_obj']) # Add another speech on the same date and check that both dates are displayed speech = Speech.objects.create( text="Another speech", speaker=speaker, instance=self.instance, start_date=date(2014, 9, 17), ) # Check that a search which returns more than one speech on the same # date displays a date for each. resp = self.client.get('/speaker/%s' % speaker.slug) self.assertEqual( len(re.findall(r'<span class="breadcrumbs__date">\s*17 Sep 2014\s*</span>', resp.content.decode())), 2)
def _assertPoLocComment(self, assert_presence, po_filename, line_number, *comment_parts): with open(po_filename, "r") as fp: po_contents = force_text(fp.read()) if os.name == "nt": # #: .\path\to\file.html:123 cwd_prefix = "%s%s" % (os.curdir, os.sep) else: # #: path/to/file.html:123 cwd_prefix = "" path = os.path.join(cwd_prefix, *comment_parts) parts = [path] if isinstance(line_number, six.string_types): line_number = self._get_token_line_number(path, line_number) if line_number is not None: parts.append(":%d" % line_number) needle = "".join(parts) pattern = re.compile(r"^\#\:.*" + re.escape(needle), re.MULTILINE) if assert_presence: return six.assertRegex(self, po_contents, pattern, '"%s" not found in final .po file.' % needle) else: if six.PY3: return self.assertNotRegex(po_contents, pattern, '"%s" shouldn\'t be in final .po file.' % needle) else: return self.assertNotRegexpMatches( po_contents, pattern, '"%s" shouldn\'t be in final .po file.' % needle )
def _assertPoLocComment(self, assert_presence, po_filename, line_number, *comment_parts): with open(po_filename, 'r') as fp: po_contents = force_text(fp.read()) if os.name == 'nt': # #: .\path\to\file.html:123 cwd_prefix = '%s%s' % (os.curdir, os.sep) else: # #: path/to/file.html:123 cwd_prefix = '' path = os.path.join(cwd_prefix, *comment_parts) parts = [path] if isinstance(line_number, six.string_types): line_number = self._get_token_line_number(path, line_number) if line_number is not None: parts.append(':%d' % line_number) needle = ''.join(parts) pattern = re.compile(r'^\#\:.*' + re.escape(needle), re.MULTILINE) if assert_presence: return six.assertRegex( self, po_contents, pattern, '"%s" not found in final .po file.' % needle) else: if six.PY3: return self.assertNotRegex( po_contents, pattern, '"%s" shouldn\'t be in final .po file.' % needle) else: return self.assertNotRegexpMatches( po_contents, pattern, '"%s" shouldn\'t be in final .po file.' % needle)
def test_files(self): # Attempting to access a FileField from the class raises a descriptive # error self.assertRaises(AttributeError, lambda: Storage.normal) # An object without a file has limited functionality. obj1 = Storage() self.assertEqual(obj1.normal.name, "") self.assertRaises(ValueError, lambda: obj1.normal.size) # Saving a file enables full functionality. obj1.normal.save("django_test.txt", ContentFile("content")) self.assertEqual(obj1.normal.name, "tests/django_test.txt") self.assertEqual(obj1.normal.size, 7) self.assertEqual(obj1.normal.read(), b"content") obj1.normal.close() # File objects can be assigned to FileField attributes, but shouldn't # get committed until the model it's attached to is saved. obj1.normal = SimpleUploadedFile("assignment.txt", b"content") dirs, files = temp_storage.listdir("tests") self.assertEqual(dirs, []) self.assertNotIn("assignment.txt", files) obj1.save() dirs, files = temp_storage.listdir("tests") self.assertEqual(sorted(files), ["assignment.txt", "django_test.txt"]) # Save another file with the same name. obj2 = Storage() obj2.normal.save("django_test.txt", ContentFile("more content")) obj2_name = obj2.normal.name six.assertRegex(self, obj2_name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX) self.assertEqual(obj2.normal.size, 12) obj2.normal.close() # Deleting an object does not delete the file it uses. obj2.delete() obj2.normal.save("django_test.txt", ContentFile("more content")) self.assertNotEqual(obj2_name, obj2.normal.name) six.assertRegex(self, obj2.normal.name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX) obj2.normal.close()
def test01_retrieve(self): "Testing retrieval of SpatialRefSys model objects." for sd in test_srs: srs = SpatialRefSys.objects.get(srid=sd['srid']) self.assertEqual(sd['srid'], srs.srid) # Some of the authority names are borked on Oracle, e.g., SRID=32140. # also, Oracle Spatial seems to add extraneous info to fields, hence the # the testing with the 'startswith' flag. auth_name, oracle_flag = sd['auth_name'] if postgis or (oracle and oracle_flag): self.assertEqual(True, srs.auth_name.startswith(auth_name)) self.assertEqual(sd['auth_srid'], srs.auth_srid) # No proj.4 and different srtext on oracle backends :( if postgis: self.assertTrue(srs.wkt.startswith(sd['srtext'])) six.assertRegex(self, srs.proj4text, sd['proj4_re'])
def assert_opengraph_matches(self, response, graph): """Check that the response matches the graph. The graph should be passed in as a dictionary of opengraph property name to a content test. The test can be either * A string - this must be equal to the content of that key, or * A compliled regular expression - which must be found in the content for that OpenGraph key in a regex search. For example: { 'title': 'OpenGraph title of the page', 'url:': re.compile('http://testing.example.com:8000/images/*.jpg'), } Extra items in the graph in resp will be ignored - we're only checking that everything in graph appears correctly. """ # Keys that should be URLs if they exist url_keys = set(( 'url', 'image', 'audio', 'video', 'image:url', 'image:secure_url', 'video:secure_url', 'audio:secure_url')) parser = lxml.html.HTMLParser(encoding='utf-8') root = lxml.html.fromstring(response.content, parser=parser) for key, test in graph.items(): content = root.xpath(".//meta[@property='og:%s']/@content" % key) self.assertEqual(len(content), 1) content = content[0] if hasattr(test, 'pattern'): assertRegex(self, content, test) else: self.assertEqual(content, test) if key in url_keys: # Check that the url is absolute. self.assertTrue( urllib.parse.urlsplit(content).netloc, 'og:%s must be an absolute URL, not %s' % (key, content) )
def test_speaker_headshots_in_speeches_section(self): # Test that headshots vs default image work OK speaker1 = Speaker.objects.create( name='Marilyn', instance=self.instance, summary='movie star', image=u'http://example.com/imag%C3%A9.jpg') self.speakers.append(speaker1) speaker2 = Speaker.objects.create(name='Salinger', instance=self.instance) section = Section.objects.create(heading='Test Section', instance=self.instance) Speech.objects.create( text="A girl doesn't need anyone that doesn't need her.", speaker=speaker1, section=section, instance=self.instance, public=True) Speech.objects.create( text="I'm sick of not having the courage to be an absolute nobody.", speaker=speaker2, section=section, instance=self.instance, public=True) resp = self.client.get('/sections/' + str(section.id)) # Prior to Django 1.5, override_settings didn't sort out MEDIA_ROOT # properly, see https://code.djangoproject.com/ticket/17744". # So best to skip the following assertion. if django.VERSION[:2] >= (1, 5): assertRegex( self, resp.content.decode(), r'(?s)<img src="/uploads/speakers/default/imag%%C3%%A9.jpg.96x96_q85_crop-smart_face_upscale.jpg"' r'.*?<a href="/speaker/%s">\s*' % (speaker1.slug)) assertRegex( self, resp.content.decode(), r'(?s)<img src="\s*/static/speeches/i/a.png\s*".*?<a href="/speaker/%s">\s*' % (speaker2.slug))
def testManagementCollectstatic(self): call_command("collectstatic", interactive=False, stdout=StringIO()) url = staticfiles_storage.url("foo.css") try: # The non-hashed name should have the default cache control. meta = staticfiles_storage.meta("foo.css") self.assertEqual(meta["CacheControl"], "public,max-age=3600") # The URL should not contain query string authentication. self.assertFalse(urlsplit(url).query) # The URL should contain an MD5 hash. assertRegex(self, url, "foo\.[0-9a-f]{12}\.css$") # The hashed name should be accessible and have a huge cache control. response = requests.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, b"* { display: none; }\n") self.assertEqual(response.headers["cache-control"], "public,max-age=31536000") finally: staticfiles_storage.delete("staticfiles.json") staticfiles_storage.delete("foo.css") staticfiles_storage.delete(posixpath.basename(url))
def test_extraction_error(self): os.chdir(self.test_dir) self.assertRaises(SyntaxError, management.call_command, 'makemessages', locale=[LOCALE], extensions=['tpl'], verbosity=0) with self.assertRaises(SyntaxError) as context_manager: management.call_command('makemessages', locale=LOCALE, extensions=['tpl'], verbosity=0) six.assertRegex( self, str(context_manager.exception), r'Translation blocks must not include other block tags: blocktrans \(file templates[/\\]template_with_error\.tpl, line 3\)' ) # Check that the temporary file was cleaned up self.assertFalse( os.path.exists('./templates/template_with_error.tpl.py'))
def test02_osr(self): "Testing getting OSR objects from SpatialRefSys model objects." for sd in test_srs: sr = SpatialRefSys.objects.get(srid=sd['srid']) self.assertEqual(True, sr.spheroid.startswith(sd['spheroid'])) self.assertEqual(sd['geographic'], sr.geographic) self.assertEqual(sd['projected'], sr.projected) if not (spatialite and not sd['spatialite']): # Can't get 'NAD83 / Texas South Central' from PROJ.4 string # on SpatiaLite self.assertEqual(True, sr.name.startswith(sd['name'])) # Testing the SpatialReference object directly. if postgis or spatialite: srs = sr.srs six.assertRegex(self, srs.proj4, sd['proj4_re']) # No `srtext` field in the `spatial_ref_sys` table in SpatiaLite if not spatialite: self.assertTrue(srs.wkt.startswith(sd['srtext']))
def test_server_formatter_default_format(self): server_time = '2016-09-25 10:20:30' log_msg = 'log message' logger = logging.getLogger('django.server') @contextmanager def patch_django_server_logger(): old_stream = logger.handlers[0].stream new_stream = six.StringIO() logger.handlers[0].stream = new_stream yield new_stream logger.handlers[0].stream = old_stream with patch_django_server_logger() as logger_output: logger.info(log_msg, extra={'server_time': server_time}) self.assertEqual('[%s] %s\n' % (server_time, log_msg), logger_output.getvalue()) with patch_django_server_logger() as logger_output: logger.info(log_msg) six.assertRegex(self, logger_output.getvalue(), r'^\[[-:,.\s\d]+\] %s' % log_msg)
def assertSingleRevision(self, objects, user=None, comment="", meta_names=(), date_created=None, using=None, model_db=None): revision = Version.objects.using(using).get_for_object(objects[0], model_db=model_db).get().revision self.assertEqual(revision.user, user) if hasattr(comment, 'pattern'): assertRegex(self, revision.get_comment(), comment) elif comment is not None: # Allow a wildcard comment. self.assertEqual(revision.get_comment(), comment) self.assertAlmostEqual(revision.date_created, date_created or timezone.now(), delta=timedelta(seconds=1)) # Check meta. self.assertEqual(revision.testmeta_set.count(), len(meta_names)) for meta_name in meta_names: self.assertTrue(revision.testmeta_set.filter(name=meta_name).exists()) # Check objects. self.assertEqual(revision.version_set.count(), len(objects)) for obj in objects: self.assertTrue(Version.objects.using(using).get_for_object( obj, model_db=model_db, ).filter( revision=revision, ).exists())
def test_speaker_headshots_in_speeches_section(self): # Test that headshots vs default image work OK speaker1 = Speaker.objects.create( name='Marilyn', instance=self.instance, summary='movie star', image=u'http://example.com/imag%C3%A9.jpg') self.speakers.append(speaker1) speaker2 = Speaker.objects.create(name='Salinger', instance=self.instance) section = Section.objects.create(heading='Test Section', instance=self.instance) Speech.objects.create( text="A girl doesn't need anyone that doesn't need her.", speaker=speaker1, section=section, instance=self.instance, public=True) Speech.objects.create( text="I'm sick of not having the courage to be an absolute nobody.", speaker=speaker2, section=section, instance=self.instance, public=True) resp = self.client.get('/section/' + str(section.id)) assertRegex( self, resp.content.decode(), r'(?s)<img src="/uploads/speakers/default/imag%%C3%%A9.jpg.96x96_q85_crop-smart_face_upscale.jpg"' r'.*?<a href="/speaker/%s">\s*' % (speaker1.slug)) assertRegex( self, resp.content.decode(), r'(?s)<img src="\s*/static/speeches/i/a.png\s*".*?<a href="/speaker/%s">\s*' % (speaker2.slug))
def test_pretty(self): response = self.client.get(self.url, {'pretty': 1}) self.assertResponse(response) self.assertEqual(load_response(response), self.json) assertRegex(self, response.content.decode('utf-8'), pretty_re)
def test_files(self): temp_storage.save('tests/default.txt', ContentFile('default content')) # Attempting to access a FileField from the class raises a descriptive # error self.assertRaises(AttributeError, lambda: Storage.normal) # An object without a file has limited functionality. obj1 = Storage() self.assertEqual(obj1.normal.name, "") self.assertRaises(ValueError, lambda: obj1.normal.size) # Saving a file enables full functionality. obj1.normal.save("django_test.txt", ContentFile("content")) self.assertEqual(obj1.normal.name, "tests/django_test.txt") self.assertEqual(obj1.normal.size, 7) self.assertEqual(obj1.normal.read(), b"content") obj1.normal.close() # File objects can be assigned to FileField attributes, but shouldn't # get committed until the model it's attached to is saved. obj1.normal = SimpleUploadedFile("assignment.txt", b"content") dirs, files = temp_storage.listdir("tests") self.assertEqual(dirs, []) self.assertEqual(sorted(files), ["default.txt", "django_test.txt"]) obj1.save() dirs, files = temp_storage.listdir("tests") self.assertEqual( sorted(files), ["assignment.txt", "default.txt", "django_test.txt"] ) # Files can be read in a little at a time, if necessary. obj1.normal.open() self.assertEqual(obj1.normal.read(3), b"con") self.assertEqual(obj1.normal.read(), b"tent") self.assertEqual(list(obj1.normal.chunks(chunk_size=2)), [b"co", b"nt", b"en", b"t"]) obj1.normal.close() # Save another file with the same name. obj2 = Storage() obj2.normal.save("django_test.txt", ContentFile("more content")) obj2_name = obj2.normal.name six.assertRegex(self, obj2_name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX) self.assertEqual(obj2.normal.size, 12) # Push the objects into the cache to make sure they pickle properly cache.set("obj1", obj1) cache.set("obj2", obj2) six.assertRegex(self, cache.get("obj2").normal.name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX) # Deleting an object does not delete the file it uses. obj2.delete() obj2.normal.save("django_test.txt", ContentFile("more content")) self.assertNotEqual(obj2_name, obj2.normal.name) six.assertRegex(self, obj2.normal.name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX) # Multiple files with the same name get _N appended to them. objs = [Storage() for i in range(2)] for o in objs: o.normal.save("multiple_files.txt", ContentFile("Same Content")) names = [o.normal.name for o in objs] self.assertEqual(names[0], "tests/multiple_files.txt") six.assertRegex(self, names[1], "tests/multiple_files_%s.txt" % FILE_SUFFIX_REGEX) for o in objs: o.delete() # Default values allow an object to access a single file. obj3 = Storage.objects.create() self.assertEqual(obj3.default.name, "tests/default.txt") self.assertEqual(obj3.default.read(), b"default content") obj3.default.close() # But it shouldn't be deleted, even if there are no more objects using # it. obj3.delete() obj3 = Storage() self.assertEqual(obj3.default.read(), b"default content") obj3.default.close() # Verify the fix for #5655, making sure the directory is only # determined once. obj4 = Storage() obj4.random.save("random_file", ContentFile("random content")) self.assertTrue(obj4.random.name.endswith("/random_file"))
def match_line(self, pattern, text): """requires the pattern and finds the line""" assertRegex(self, text, pattern) (ret,) = [line for line in text.split('\n') if re.match(pattern, line)] return ret
def test_template_comments(self): """Template comment tags on the same line of other constructs (#19552)""" os.chdir(self.test_dir) # Test detection/end user reporting of old, incorrect templates # translator comments syntax with warnings.catch_warnings(record=True) as ws: warnings.simplefilter('always') management.call_command('makemessages', locale=[LOCALE], extensions=['thtml'], verbosity=0) self.assertEqual(len(ws), 3) for w in ws: self.assertTrue( issubclass(w.category, TranslatorCommentWarning)) six.assertRegex( self, str(ws[0].message), r"The translator-targeted comment 'Translators: ignored i18n comment #1' \(file templates[/\\]comments.thtml, line 4\) was ignored, because it wasn't the last item on the line\." ) six.assertRegex( self, str(ws[1].message), r"The translator-targeted comment 'Translators: ignored i18n comment #3' \(file templates[/\\]comments.thtml, line 6\) was ignored, because it wasn't the last item on the line\." ) six.assertRegex( self, str(ws[2].message), r"The translator-targeted comment 'Translators: ignored i18n comment #4' \(file templates[/\\]comments.thtml, line 8\) was ignored, because it wasn't the last item on the line\." ) # Now test .po file contents self.assertTrue(os.path.exists(self.PO_FILE)) with open(self.PO_FILE, 'r') as fp: po_contents = force_text(fp.read()) self.assertMsgId('Translatable literal #9a', po_contents) self.assertFalse('ignored comment #1' in po_contents) self.assertFalse( 'Translators: ignored i18n comment #1' in po_contents) self.assertMsgId("Translatable literal #9b", po_contents) self.assertFalse('ignored i18n comment #2' in po_contents) self.assertFalse('ignored comment #2' in po_contents) self.assertMsgId('Translatable literal #9c', po_contents) self.assertFalse('ignored comment #3' in po_contents) self.assertFalse('ignored i18n comment #3' in po_contents) self.assertMsgId('Translatable literal #9d', po_contents) self.assertFalse('ignored comment #4' in po_contents) self.assertMsgId('Translatable literal #9e', po_contents) self.assertFalse('ignored comment #5' in po_contents) self.assertFalse('ignored i18n comment #4' in po_contents) self.assertMsgId('Translatable literal #9f', po_contents) self.assertTrue( '#. Translators: valid i18n comment #5' in po_contents) self.assertMsgId('Translatable literal #9g', po_contents) self.assertTrue( '#. Translators: valid i18n comment #6' in po_contents) self.assertMsgId('Translatable literal #9h', po_contents) self.assertTrue( '#. Translators: valid i18n comment #7' in po_contents) self.assertMsgId('Translatable literal #9i', po_contents) six.assertRegex(self, po_contents, r'#\..+Translators: valid i18n comment #8') six.assertRegex(self, po_contents, r'#\..+Translators: valid i18n comment #9') self.assertMsgId("Translatable literal #9j", po_contents)
def test_area_admin_page(self): admin_url = reverse("admin:mapit_area_add") resp = self.client.get(admin_url) assertRegex(self, resp.content.decode('utf-8'), '<input([^>]*(id="id_name"|name="name"|type="text")){3}') self.assertEqual(resp.status_code, 200)
def test_rs_name_repr(self): self.assertEqual(self.rs_path, self.rs.name) six.assertRegex(self, repr(self.rs), "<Raster object at 0x\w+>")
def test_runner_deprecation_verbosity_default(self): args = ['test', '--settings=test_project.settings', 'test_runner_deprecation_app'] out, err = self.run_django_admin(args) self.assertIn("Ran 1 test", err) six.assertRegex(self, err, r"RemovedInDjango\d\dWarning: warning from test") six.assertRegex(self, err, r"RemovedInDjango\d\dWarning: module-level warning from deprecation_app")
def test_sql_delete(self): app = models.get_app('commands_sql') output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS]) six.assertRegex(self, output[0], r'^DROP TABLE .commands_sql_book.*')
def test_transform_multipolygon(self): geometry = Geometry(OGRGeometry('MULTIPOLYGON (((0 0,0 5,5 5,0 0)))')).transform(SpatialReference(26917)) self.assertIsInstance(geometry, Geometry) self.assertEqual(geometry.geometry.geom_name, 'MULTIPOLYGON') assertRegex(self, geometry.wkt, r'MULTIPOLYGON \(\(\(-85.488743884\d{6} 0.0,-85.488743884\d{6} 0.000045096\d{6},-85.488699089\d{6} 0.000045096\d{6},-85.488743884\d{6} 0.0\)\)\)')
def test_development(self): ver_tuple = (1, 4, 0, 'alpha', 0) # This will return a different result when it's run within or outside # of a git clone: 1.4.devYYYYMMDDHHMMSS or 1.4. ver_string = get_version(ver_tuple) six.assertRegex(self, ver_string, r'1\.4(\.dev\d+)?')
def test_if_changed(self): resp = self.client.get('/') assertRegex(self, resp.content, '1\s+2\s+3')
def test_speaker_list_ordering(self): Speaker.objects.create(name='alice', instance=self.instance) Speaker.objects.create(name='Bob', instance=self.instance) Speaker.objects.create(name='Eve', sort_name='AAA FIRST', instance=self.instance) resp = self.client.get('/speakers') assertRegex(self, resp.content.decode(), u'Eve.*alice.*Bob(?s)')
def assertRegex(self, *args, **kwargs): six.assertRegex(self, *args, **kwargs)
def test_sql_create(self): app = models.get_app('commands_sql') output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS]) # Lower so that Oracle's upper case tbl names wont break sql = output[0].lower() six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
def testURLs(self): c = Client() # Test Archive URL response = c.get('/blog/') self.assertTrue(isinstance( response.context['view'], blogviews.ArchiveIndexView)) self.assertEqual(len(response.context['object_list']), 2) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Entry 1') self.assertContains(response, 'Eintrag 1') # Test year archive response = c.get('/blog/2012/') self.assertTrue(isinstance( response.context['view'], blogviews.YearArchiveView)) self.assertEqual(len(response.context['object_list']), 2) self.assertEqual(response.context['view'].get_template_names(), ['elephantblog/entry_archive.html']) self.assertEqual(response.status_code, 200) self.assertContains(response, 'News for 2012') self.assertContains(response, 'Entry 1') self.assertContains(response, 'Eintrag 1') # No entries in 2011: response = c.get('/blog/2011/') self.assertEqual(response.status_code, 404) # Test month archive response = c.get('/blog/2012/10/') self.assertTrue(isinstance( response.context['view'], blogviews.MonthArchiveView)) self.assertEqual(len(response.context['object_list']), 1) self.assertEqual(response.status_code, 200) six.assertRegex( self, response.content.decode('utf-8'), r'News\s+for October 2012') self.assertContains(response, 'Eintrag 1') response = c.get('/blog/2012/08/') self.assertEqual(len(response.context['object_list']), 1) six.assertRegex( self, response.content.decode('utf-8'), r'News\s+for August 2012') self.assertContains(response, 'Entry 1') response = c.get('/blog/2012/06/') self.assertEqual(response.status_code, 404) # Test day archive response = c.get('/blog/2012/10/12/') self.assertTrue(isinstance( response.context['view'], blogviews.DayArchiveView)) self.assertEqual(len(response.context['object_list']), 1) self.assertEqual(response.status_code, 200) six.assertRegex( self, response.content.decode('utf-8'), 'News\s+for Oct. 12, 2012') self.assertContains(response, 'Eintrag 1') response = c.get('/blog/2012/08/12/') self.assertEqual(len(response.context['object_list']), 1) six.assertRegex( self, response.content.decode('utf-8'), 'News\s+for Aug. 12, 2012') self.assertContains(response, 'Entry 1') # No entries in 2011: response = c.get('/blog/2012/10/13/') self.assertEqual(response.status_code, 404) # Test category archive # assign a category to the entry category1 = create_category('Category 1') category2 = create_category('Category 2') entry = Entry.objects.get(slug='entry-1') entry.categories.add(category1) entry.categories.add(category2) entry = Entry.objects.get(slug='eintrag-1') entry.categories.add(category2) response = c.get('/blog/category/category-1/') self.assertEqual(response.status_code, 200) self.assertTrue( isinstance(response.context['view'], blogviews.CategoryArchiveIndexView)) self.assertEqual(len(response.context['object_list']), 1) self.assertContains(response, 'Entry 1') self.assertNotContains(response, 'Eintrag 1') response = c.get('/blog/category/category-2/') self.assertEqual(response.status_code, 200) self.assertTrue( isinstance(response.context['view'], blogviews.CategoryArchiveIndexView)) self.assertEqual(len(response.context['object_list']), 2) self.assertContains(response, 'Entry 1') self.assertContains(response, 'Eintrag 1')