-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Tests for entity endpoints
- Loading branch information
Showing
22 changed files
with
970 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from brainzutils import cache | ||
|
||
import critiquebrainz.db.license as db_license | ||
import critiquebrainz.db.review as db_review | ||
import critiquebrainz.db.users as db_users | ||
from critiquebrainz.db.user import User | ||
from critiquebrainz.ws.testing import WebServiceTestCase | ||
|
||
|
||
class ArtistViewsTestCase(WebServiceTestCase): | ||
|
||
def setUp(self): | ||
super(ArtistViewsTestCase, self).setUp() | ||
|
||
self.artist_id1 = "f59c5520-5f46-4d2c-b2c4-822eabf53419" | ||
self.artist_id2 = "83d91898-7763-47d7-b03b-b92132375c47" | ||
|
||
self.user = User(db_users.get_or_create(1, "Tester", new_user_data={ | ||
"display_name": "test user", | ||
})) | ||
self.another_user = User(db_users.get_or_create(2, "Hacker!", new_user_data={ | ||
"display_name": "test hacker", | ||
})) | ||
self.license = db_license.create( | ||
id="CC BY-SA 3.0", | ||
full_name="Created so we can fill the form correctly.", | ||
) | ||
self.review = dict( | ||
entity_id=self.artist_id1, | ||
entity_type='artist', | ||
user_id=self.user.id, | ||
text="Testing! This text should be on the page.", | ||
rating=5, | ||
is_draft=False, | ||
license_id=self.license["id"], | ||
) | ||
self.review2 = dict( | ||
entity_id=self.artist_id2, | ||
entity_type='artist', | ||
user_id=self.user.id, | ||
text="Testing! This text should be on the page.", | ||
rating=5, | ||
is_draft=False, | ||
license_id=self.license["id"], | ||
) | ||
|
||
def create_dummy_review(self): | ||
return db_review.create(**self.review) | ||
|
||
def create_dummy_review2(self): | ||
return db_review.create(**self.review2) | ||
|
||
def test_artist_endpoint(self): | ||
review = self.create_dummy_review() | ||
response = self.client.get('/artist/f59c5520-5f46-4d2c-b2c4-822eabf53419') | ||
|
||
self.assert200(response) | ||
self.assertIn(review['text'], response.json['top_reviews'][0]['text']) | ||
|
||
self.assertEqual(5, response.json['average_rating']) | ||
self.assertEqual(1, response.json['reviews_count']) | ||
|
||
# Test for an artist which does not exist | ||
response = self.client.get('/artist/f59c5520-5f46-4d2c-b2c4-822eabf53417') | ||
self.assert404(response) | ||
|
||
def test_artist_user_reviews(self): | ||
review = self.create_dummy_review() | ||
response = self.client.get('/artist/f59c5520-5f46-4d2c-b2c4-822eabf53419?username=%s' % self.user.musicbrainz_username) | ||
|
||
self.assert200(response) | ||
self.assertIn(review['text'], response.json['user_review']['text']) | ||
|
||
def test_user_cache_tracking(self): | ||
track_key = cache.gen_key("entity_api", self.artist_id2, self.user.musicbrainz_username, "user_review") | ||
|
||
# Make sure the cache is empty | ||
self.client.get('/artist/83d91898-7763-47d7-b03b-b92132375c47?username=%s' % self.user.musicbrainz_username) | ||
cache_value = cache.get(track_key, namespace="Review") | ||
self.assertEqual([], cache_value) | ||
|
||
review = self.create_dummy_review2() | ||
|
||
# Check if the cache is populated after the request | ||
self.client.get('/artist/83d91898-7763-47d7-b03b-b92132375c47?username=%s' % self.user.musicbrainz_username) | ||
cache_value = cache.get(track_key, namespace="Review") | ||
self.assertTrue(cache_value is not None) | ||
|
||
self.assertIn(review['text'], cache_value['text']) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from brainzutils import cache | ||
|
||
import critiquebrainz.db.license as db_license | ||
import critiquebrainz.db.review as db_review | ||
import critiquebrainz.db.users as db_users | ||
from critiquebrainz.db.user import User | ||
from critiquebrainz.ws.testing import WebServiceTestCase | ||
|
||
|
||
class AuthorViewsTestCase(WebServiceTestCase): | ||
|
||
def setUp(self): | ||
super(AuthorViewsTestCase, self).setUp() | ||
|
||
self.author_id1 = "b8f9bd89-79d1-497e-811f-18576321111c" | ||
self.author_id2 = "b2507eee-1391-47c5-93e6-ca972bd8e0e0" | ||
|
||
self.user = User(db_users.get_or_create(1, "Tester", new_user_data={ | ||
"display_name": "test user", | ||
})) | ||
self.another_user = User(db_users.get_or_create(2, "Hacker!", new_user_data={ | ||
"display_name": "test hacker", | ||
})) | ||
self.license = db_license.create( | ||
id="CC BY-SA 3.0", | ||
full_name="Created so we can fill the form correctly.", | ||
) | ||
self.review = dict( | ||
entity_id=self.author_id1, | ||
entity_type='bb_author', | ||
user_id=self.user.id, | ||
text="Testing! This text should be on the page.", | ||
rating=5, | ||
is_draft=False, | ||
license_id=self.license["id"], | ||
) | ||
self.review2 = dict( | ||
entity_id=self.author_id2, | ||
entity_type='bb_author', | ||
user_id=self.user.id, | ||
text="Testing! This text should be on the page.", | ||
rating=5, | ||
is_draft=False, | ||
license_id=self.license["id"], | ||
) | ||
|
||
def create_dummy_review(self): | ||
return db_review.create(**self.review) | ||
|
||
def create_dummy_review2(self): | ||
return db_review.create(**self.review2) | ||
|
||
def test_author_endpoint(self): | ||
review = self.create_dummy_review() | ||
response = self.client.get('/author/b8f9bd89-79d1-497e-811f-18576321111c') | ||
|
||
self.assert200(response) | ||
self.assertIn(review['text'], response.json['top_reviews'][0]['text']) | ||
|
||
self.assertEqual(5, response.json['average_rating']) | ||
self.assertEqual(1, response.json['reviews_count']) | ||
|
||
# Test for an author which does not exist | ||
response = self.client.get('/author/b8f9bd89-79d1-497e-811f-18576321111g') | ||
self.assert404(response) | ||
|
||
def test_author_user_reviews(self): | ||
review = self.create_dummy_review() | ||
response = self.client.get('/author/b8f9bd89-79d1-497e-811f-18576321111c?username=%s' % self.user.musicbrainz_username) | ||
|
||
self.assert200(response) | ||
self.assertIn(review['text'], response.json['user_review']['text']) | ||
|
||
def test_user_cache_tracking(self): | ||
track_key = cache.gen_key("entity_api", self.author_id2, self.user.musicbrainz_username, "user_review") | ||
|
||
# Make sure the cache is empty | ||
self.client.get('/author/b2507eee-1391-47c5-93e6-ca972bd8e0e0?username=%s' % self.user.musicbrainz_username) | ||
cache_value = cache.get(track_key, namespace="Review") | ||
self.assertEqual([], cache_value) | ||
|
||
review = self.create_dummy_review2() | ||
|
||
# Check if the cache is populated after the request | ||
self.client.get('/author/b2507eee-1391-47c5-93e6-ca972bd8e0e0?username=%s' % self.user.musicbrainz_username) | ||
cache_value = cache.get(track_key, namespace="Review") | ||
|
||
self.assertIn(review['text'], cache_value['text']) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from brainzutils import cache | ||
|
||
import critiquebrainz.db.license as db_license | ||
import critiquebrainz.db.review as db_review | ||
import critiquebrainz.db.users as db_users | ||
from critiquebrainz.db.user import User | ||
from critiquebrainz.ws.testing import WebServiceTestCase | ||
|
||
|
||
class EditionGroupViewsTestCase(WebServiceTestCase): | ||
|
||
def setUp(self): | ||
super(EditionGroupViewsTestCase, self).setUp() | ||
|
||
self.edition_group_id1 = "ac4ae236-6bee-4335-9e04-21314a478f9f" | ||
self.edition_group_id2 = "ab87aa42-3cb7-478c-bba7-09192d04f252" | ||
|
||
self.user = User(db_users.get_or_create(1, "Tester", new_user_data={ | ||
"display_name": "test user", | ||
})) | ||
self.another_user = User(db_users.get_or_create(2, "Hacker!", new_user_data={ | ||
"display_name": "test hacker", | ||
})) | ||
self.license = db_license.create( | ||
id="CC BY-SA 3.0", | ||
full_name="Created so we can fill the form correctly.", | ||
) | ||
self.review = dict( | ||
entity_id=self.edition_group_id1, | ||
entity_type='bb_edition_group', | ||
user_id=self.user.id, | ||
text="Testing! This text should be on the page.", | ||
rating=5, | ||
is_draft=False, | ||
license_id=self.license["id"], | ||
) | ||
self.review2 = dict( | ||
entity_id=self.edition_group_id2, | ||
entity_type='bb_edition_group', | ||
user_id=self.user.id, | ||
text="Testing! This text should be on the page.", | ||
rating=5, | ||
is_draft=False, | ||
license_id=self.license["id"], | ||
) | ||
|
||
def create_dummy_review(self): | ||
return db_review.create(**self.review) | ||
|
||
def create_dummy_review2(self): | ||
return db_review.create(**self.review2) | ||
|
||
def test_edition_group_endpoint(self): | ||
review = self.create_dummy_review() | ||
response = self.client.get('/edition-group/ac4ae236-6bee-4335-9e04-21314a478f9f') | ||
|
||
self.assert200(response) | ||
self.assertIn(review['text'], response.json['top_reviews'][0]['text']) | ||
|
||
self.assertEqual(5, response.json['average_rating']) | ||
self.assertEqual(1, response.json['reviews_count']) | ||
|
||
# Test for an edition group that doesn't exist | ||
response = self.client.get('/edition-group/ac4ae236-6bee-4335-9e04-21314a478f9d') | ||
self.assert404(response) | ||
|
||
def test_edition_group_user_reviews(self): | ||
review = self.create_dummy_review() | ||
response = self.client.get('/edition-group/ac4ae236-6bee-4335-9e04-21314a478f9f?username=%s' % self.user.musicbrainz_username) | ||
|
||
self.assert200(response) | ||
self.assertIn(review['text'], response.json['user_review']['text']) | ||
|
||
def test_user_cache_tracking(self): | ||
track_key = cache.gen_key("entity_api", self.edition_group_id2, self.user.musicbrainz_username, "user_review") | ||
|
||
# Make sure the cache is empty | ||
self.client.get('/edition-group/ab87aa42-3cb7-478c-bba7-09192d04f252?username=%s' % self.user.musicbrainz_username) | ||
cache_value = cache.get(track_key, namespace="Review") | ||
self.assertEqual([], cache_value) | ||
|
||
review = self.create_dummy_review2() | ||
|
||
# Check if the cache is populated after the request | ||
self.client.get('/edition-group/ab87aa42-3cb7-478c-bba7-09192d04f252?username=%s' % self.user.musicbrainz_username) | ||
cache_value = cache.get(track_key, namespace="Review") | ||
|
||
self.assertIn(review['text'], cache_value['text']) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from brainzutils import cache | ||
|
||
import critiquebrainz.db.license as db_license | ||
import critiquebrainz.db.review as db_review | ||
import critiquebrainz.db.users as db_users | ||
from critiquebrainz.db.user import User | ||
from critiquebrainz.ws.testing import WebServiceTestCase | ||
|
||
|
||
class LiteraryWorkViewsTestCase(WebServiceTestCase): | ||
|
||
def setUp(self): | ||
super(LiteraryWorkViewsTestCase, self).setUp() | ||
|
||
self.literary_work_id1 = "56efa555-abd5-4ccb-89a6-ff9d9021971f" | ||
self.literary_work_id2 = "0e03bc2a-2867-4687-afee-e211ece30772" | ||
|
||
self.user = User(db_users.get_or_create(1, "Tester", new_user_data={ | ||
"display_name": "test user", | ||
})) | ||
self.another_user = User(db_users.get_or_create(2, "Hacker!", new_user_data={ | ||
"display_name": "test hacker", | ||
})) | ||
self.license = db_license.create( | ||
id="CC BY-SA 3.0", | ||
full_name="Created so we can fill the form correctly.", | ||
) | ||
self.review = dict( | ||
entity_id=self.literary_work_id1, | ||
entity_type='bb_literary_work', | ||
user_id=self.user.id, | ||
text="Testing! This text should be on the page.", | ||
rating=5, | ||
is_draft=False, | ||
license_id=self.license["id"], | ||
) | ||
self.review2 = dict( | ||
entity_id=self.literary_work_id2, | ||
entity_type='bb_literary_work', | ||
user_id=self.user.id, | ||
text="Testing! This text should be on the page.", | ||
rating=5, | ||
is_draft=False, | ||
license_id=self.license["id"], | ||
) | ||
|
||
def create_dummy_review(self): | ||
return db_review.create(**self.review) | ||
|
||
def create_dummy_review2(self): | ||
return db_review.create(**self.review2) | ||
|
||
def test_literary_work_endpoint(self): | ||
review = self.create_dummy_review() | ||
response = self.client.get('/literary-work/56efa555-abd5-4ccb-89a6-ff9d9021971f') | ||
|
||
self.assert200(response) | ||
self.assertIn(review['text'], response.json['top_reviews'][0]['text']) | ||
|
||
self.assertEqual(5, response.json['average_rating']) | ||
self.assertEqual(1, response.json['reviews_count']) | ||
|
||
# Test for a literary work that doesn't exist | ||
response = self.client.get('/literary-work/56efa555-abd5-4ccb-89a6-ff9d9021971g') | ||
self.assert404(response) | ||
|
||
def test_literary_work_user_reviews(self): | ||
review = self.create_dummy_review() | ||
response = self.client.get('/literary-work/56efa555-abd5-4ccb-89a6-ff9d9021971f?username=%s' % | ||
self.user.musicbrainz_username) | ||
|
||
self.assert200(response) | ||
self.assertIn(review['text'], response.json['user_review']['text']) | ||
|
||
def test_user_cache_tracking(self): | ||
track_key = cache.gen_key("entity_api", self.literary_work_id2, self.user.musicbrainz_username, "user_review") | ||
|
||
# Make sure the cache is empty | ||
self.client.get('/literary-work/0e03bc2a-2867-4687-afee-e211ece30772?username=%s' % self.user.musicbrainz_username) | ||
cache_value = cache.get(track_key, namespace="Review") | ||
self.assertEqual([], cache_value) | ||
|
||
review = self.create_dummy_review2() | ||
|
||
# Check if the cache is populated after the request | ||
self.client.get('/literary-work/0e03bc2a-2867-4687-afee-e211ece30772?username=%s' % self.user.musicbrainz_username) | ||
cache_value = cache.get(track_key, namespace="Review") | ||
|
||
self.assertIn(review['text'], cache_value['text']) |
Empty file.
Oops, something went wrong.