Skip to content
Snippets Groups Projects
Commit 3131d393 authored by Jan Bernoth's avatar Jan Bernoth
Browse files

test overview with or without user

parent ccf47022
No related branches found
No related tags found
1 merge request!122User centric api
......@@ -7,8 +7,8 @@ from django.db.models.signals import post_save
from django.test import TransactionTestCase
from articles.apps import ArticlesConfig
from articles.models import Article, MediumType, Medium, Files, User
from articles.serializers import MediumTypeSerializer, MediumSerializer, ArticleSerializer
from articles.models import Article, MediumType, Medium, Files, User, Tag
from articles.serializers import MediumTypeSerializer, MediumSerializer, ArticleSerializer, write_article_to_db
logger = logging.getLogger(__name__)
......@@ -109,10 +109,65 @@ class ArticleTest(TransactionTestCase):
class OverviewTest(TransactionTestCase):
""" test functions for REST API Overview"""
def setUp(self):
tag_1 = Tag(name="Test1")
tag_1.save()
tag_2 = Tag(name="Test2")
tag_2.save()
conf = ArticlesConfig.create('articles.apps.ArticlesConfig')
conf.ready()
with mock.patch('articles.signals.extract_information_from_file', autospec=True) as mocked_post_save:
post_save.connect(mocked_post_save, sender=Files, dispatch_uid='extract_information_from_file')
Files.objects.create(upload_file='testfile.pdf', upload_date='18.11.2021')
file = Files.objects.get(upload_file='testfile.pdf')
input_data = {
'medium_name': 'MAZ',
'medium_type': 'Zeitung',
'name': 'Testartikel',
'source_file': file,
'reach': 1000,
'date_of_publication': datetime.datetime.today(),
'text': 'Dies ist ein beispielhafter Beispieltext',
'pages': 2,
'wrongly_parsed': True,
'tags': [tag_1.name, tag_2.name]
}
write_article_to_db(**input_data)
def test_create_user_app_conf(self):
""" tests if user were created if config is ready"""
logger.info("started OverviewTest.test_create_user_app_conf")
conf = ArticlesConfig.create('articles.apps.ArticlesConfig')
conf.ready()
user = User.objects.get(id_tag=1)
self.assertTrue(user.activated)
self.assertEqual(user.favorite_tags.all().count(), 2)
def test_overview_api_wo_user(self):
""" tests the api without user id """
logger.info("started OverviewTest.test_overview_api_wo_user")
response = self.client.get('/articles/overview')
self.assertEqual(response.status_code, 200)
today_str = datetime.datetime.today().strftime('%Y-%m-%d')
self.assertJSONEqual(str(response.content, encoding='utf8'), [
{"name": "Test1", "article_name": "Testartikel", "medium_name": "maz",
"date_of_publication": today_str,
"reach": "1000"
},
{"name": "Test2", "article_name": "Testartikel", "medium_name": "maz",
"date_of_publication": today_str,
"reach": "1000"}])
def test_overview_api_with_user(self):
""" tests the overview with user id """
logger.info("started OverviewTest.test_overview_api_with_user")
today_str = datetime.datetime.today().strftime('%Y-%m-%d')
user = User(id_tag=2)
user.save()
tag2 = Tag.objects.get(name="Test2")
user.favorite_tags.add(tag2)
user.save()
response = self.client.get('/articles/overview?user=' + str(user.id_tag))
self.assertJSONEqual(str(response.content, encoding='utf8'), [
{"name": tag2.name, "article_name": "Testartikel", "medium_name": "maz",
"date_of_publication": today_str,
"reach": "1000"}
])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment