|
| 1 | +""" |
| 2 | +Tests for views that are used to modify the compendium. |
| 3 | +""" |
| 4 | + |
| 5 | +import datetime |
| 6 | +import os |
| 7 | +import random |
| 8 | + |
| 9 | +from django.urls import reverse |
| 10 | +from django.test import tag |
| 11 | +from entries.models import CompendiumEntry, CompendiumEntryTag, Author |
| 12 | +from utils.test_utils import UnitTest, random_username |
| 13 | + |
| 14 | + |
| 15 | +@tag("compendium-modification") |
| 16 | +class AddNewCompendiumEntryTestCase(UnitTest): |
| 17 | + """ |
| 18 | + Test the view that presents a form for manually creating a new compendium |
| 19 | + entry. |
| 20 | + """ |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + super().setUp(preauth=True) |
| 24 | + self.new_entry_page = reverse("research new article") |
| 25 | + |
| 26 | + # Add three tags for testing purposes |
| 27 | + for tagname in ("test_tag_A", "test_tag_B", "test_tag_C"): |
| 28 | + if not CompendiumEntryTag.objects.filter(tagname=tagname).exists(): |
| 29 | + CompendiumEntryTag.objects.create(tagname=tagname) |
| 30 | + |
| 31 | + def test_page_templates(self): |
| 32 | + # Ensure that the page uses the correct templates in its response |
| 33 | + response = self.client.get(self.new_entry_page) |
| 34 | + self.assertTemplateUsed("dashboard_base.html") |
| 35 | + self.assertTemplateUsed("new_article.html") |
| 36 | + |
| 37 | + @tag("tags") |
| 38 | + def test_add_new_compendium_entry(self): |
| 39 | + self.assertEqual(len(CompendiumEntry.objects.all()), 0) |
| 40 | + |
| 41 | + # Retrieve the tag ID for "test_tag_B" |
| 42 | + tag_id = CompendiumEntryTag.objects.get(tagname="test_tag_B").id |
| 43 | + |
| 44 | + # publisher information |
| 45 | + publisher = random_username(self.rd) |
| 46 | + |
| 47 | + # published date |
| 48 | + year = random.randrange(1900, datetime.date.today().year) |
| 49 | + month = random.randrange(1, 12) |
| 50 | + day = random.randrange(1, 31) |
| 51 | + |
| 52 | + # Send POST data to the URL to create a new entry and ensure that |
| 53 | + # the entry was created correctly. |
| 54 | + data = { |
| 55 | + "title": "New compendium entry", |
| 56 | + "abstract": "Abstract for new entry", |
| 57 | + "url": "https://example.com", |
| 58 | + "tags": [tag_id], |
| 59 | + "publisher_text": publisher, |
| 60 | + "year": year, |
| 61 | + "month": month, |
| 62 | + "day": day, |
| 63 | + "edit-entry": "", |
| 64 | + } |
| 65 | + response = self.client.post(self.new_entry_page, data) |
| 66 | + self.assertTrue(len(CompendiumEntry.objects.all()), 1) |
| 67 | + |
| 68 | + entry = CompendiumEntry.objects.get(title="New compendium entry") |
| 69 | + self.assertEqual(entry.owner, self.user) |
| 70 | + self.assertEqual(entry.title, "New compendium entry") |
| 71 | + self.assertEqual(entry.abstract, "Abstract for new entry") |
| 72 | + self.assertEqual(entry.url, "https://example.com") |
| 73 | + self.assertEqual(len(entry.tags.all()), 1) |
| 74 | + self.assertEqual(entry.tags.get().tagname, "test_tag_B") |
| 75 | + self.assertEqual(entry.publisher.publishername, publisher) |
| 76 | + self.assertEqual(entry.year, year) |
| 77 | + self.assertEqual(entry.month, month) |
| 78 | + self.assertEqual(entry.day, day) |
| 79 | + |
| 80 | + @tag("tags") |
| 81 | + def test_add_compendium_entry_with_multiple_tags(self): |
| 82 | + """Create a CompendiumEntry with multiple tags""" |
| 83 | + self.assertEqual(len(CompendiumEntry.objects.all()), 0) |
| 84 | + |
| 85 | + # Retrieve tag IDs for "test_tag_B" and "test_tag_C" |
| 86 | + id_A = CompendiumEntryTag.objects.get(tagname="test_tag_A").id |
| 87 | + id_C = CompendiumEntryTag.objects.get(tagname="test_tag_C").id |
| 88 | + |
| 89 | + # Send POST data with multiple tag IDs |
| 90 | + data = { |
| 91 | + "title": "New compendium entry", |
| 92 | + "tags": [id_A, id_C], |
| 93 | + "edit-entry": "", |
| 94 | + } |
| 95 | + self.client.post(self.new_entry_page, data) |
| 96 | + self.assertEqual(len(CompendiumEntry.objects.all()), 1) |
| 97 | + |
| 98 | + entry = CompendiumEntry.objects.get(title="New compendium entry") |
| 99 | + self.assertEqual(len(entry.tags.all()), 2) |
| 100 | + self.assertTrue(entry.tags.filter(tagname="test_tag_A").exists()) |
| 101 | + self.assertFalse(entry.tags.filter(tagname="test_tag_B").exists()) |
| 102 | + self.assertTrue(entry.tags.filter(tagname="test_tag_C").exists()) |
| 103 | + |
| 104 | + def test_attempt_to_create_entry_with_empty_title(self): |
| 105 | + """New entries must have a title""" |
| 106 | + self.assertEqual(len(CompendiumEntry.objects.all()), 0) |
| 107 | + tag_id = CompendiumEntryTag.objects.get(tagname="test_tag_A").id |
| 108 | + |
| 109 | + ### Try to create an entry without a title |
| 110 | + data = { |
| 111 | + "tags": [tag_id], |
| 112 | + } |
| 113 | + response = self.client.post(self.new_entry_page, data) |
| 114 | + self.assertEqual(len(CompendiumEntry.objects.all()), 0) |
| 115 | + |
| 116 | + ### Try to create an entry with an empty title |
| 117 | + data = { |
| 118 | + "title": "", |
| 119 | + "tags": [tag_id], |
| 120 | + } |
| 121 | + response = self.client.post(self.new_entry_page, data) |
| 122 | + self.assertEqual(len(CompendiumEntry.objects.all()), 0) |
| 123 | + |
| 124 | + def test_attempt_to_create_entry_without_tags(self): |
| 125 | + """New entries must have _at least_ one tag""" |
| 126 | + self.assertEqual(len(CompendiumEntry.objects.all()), 0) |
| 127 | + |
| 128 | + data = { |
| 129 | + "title": "New compendium entry", |
| 130 | + } |
| 131 | + self.client.post(self.new_entry_page, data) |
| 132 | + self.assertEqual(len(CompendiumEntry.objects.all()), 0) |
| 133 | + |
| 134 | + |
| 135 | +### TODO |
| 136 | + |
| 137 | + |
| 138 | +@tag("compendium-modification") |
| 139 | +class UploadBibTexViewTestCase(UnitTest): |
| 140 | + """ |
| 141 | + Test the view that's involved in handling BibTeX uploads to the site for |
| 142 | + adding new compendium entries. |
| 143 | + """ |
| 144 | + |
| 145 | + def setUp(self): |
| 146 | + super().setUp(preauth=True) |
| 147 | + |
| 148 | + # Load in a test .bib file |
| 149 | + # self.test_filename = os. |
0 commit comments