Skip to content

Commit

Permalink
Blog search by filter
Browse files Browse the repository at this point in the history
Added in the ability in the blog get method, now we can filter what blogs a person will look at. Example, by game, by title, and they can be ordered by most recent, oldest, and by likes. Also made it so to delete/update a blog or comment, the user must be the same user who created the blog/comment
  • Loading branch information
Zen-Kristalovich authored and Zen-Kristalovich committed Mar 11, 2024
1 parent e39a5a7 commit 0618925
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 40 deletions.
329 changes: 302 additions & 27 deletions lvlgg_backend/blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,170 @@ def test_get_blogs(self):
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = "/blog/getlist/"
get_url = "/blog/get_blog/"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)

# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is not empty
self.assertTrue(blogs)

# Check the structure of each blog in the list
for blog in blogs:
self.assertIn('id', blog)
self.assertIn('title', blog)

first_blog = blogs[0]
self.assertEqual(first_blog['title'], payload['title'])

def test_get_blogs_byId(self):
"""
Test getting all blogs
"""
# Retrieve list of blogs
get_url = f"/blog/get_blog/?id={self.blog_pk}"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)

# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is not empty
self.assertTrue(blogs)

# Check the structure of each blog in the list
for blog in blogs:
self.assertIn('id', blog)
self.assertIn('title', blog)

first_blog = blogs[0]
self.assertEqual(first_blog['id'], self.blog_pk)

def test_get_blogs_byTitle(self):
"""
Test getting all blogs
"""
# Create a blog first
payload = {
"content": "Stuff for a bloggggg",
"title": "Sample Title Test",
"author": self.user_pk,
"game" : "Minecraft"
}
create_url = "/blog/create_blog/"
response = self.client.post(create_url, payload, content_type="application/json")
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = f"/blog/get_blog/?title=Sample"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)
# Parse JSON response
data = json.loads(response.content.decode('utf-8'))
# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)
# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is not empty
self.assertEqual( len(blogs), 2)

# Retrieve list of blogs
get_url = f"/blog/get_blog/?title=Sample Title Test"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)
# Parse JSON response
data = json.loads(response.content.decode('utf-8'))
# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)
# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is not empty
self.assertEqual( len(blogs), 1)


def test_get_blogs_byGame(self):
"""
Test getting all blogs
"""
# Create a blog first
payload = {
"content": "Stuff for a bloggggg",
"title": "My Title",
"author": self.user_pk,
"game" : "Minecraft"
}
create_url = "/blog/create_blog/"
response = self.client.post(create_url, payload, content_type="application/json")
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = f"/blog/get_blog/?game=Minecraft"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)

# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is not empty
self.assertTrue(blogs)

# Check the structure of each blog in the list
for blog in blogs:
self.assertIn('id', blog)
self.assertIn('title', blog)

first_blog = blogs[0]
self.assertEqual(first_blog['title'], payload['title'])

def test_get_blogs_byAuthor(self):
"""
Test getting all blogs
"""
# Create a blog first
payload = {
"content": "Stuff for a bloggggg",
"title": "My Title",
"author": self.user_pk,
"game" : "Minecraft"
}
create_url = "/blog/create_blog/"
response = self.client.post(create_url, payload, content_type="application/json")
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = f"/blog/get_blog/?author={self.user_pk}"
response = self.client.get(get_url)

# Check response status code
Expand All @@ -114,17 +277,151 @@ def test_get_blogs(self):
first_blog = blogs[0]
self.assertEqual(first_blog['title'], payload['title'])

def test_get_blogs_byOrderOld(self):
"""
Test getting all blogs
"""
# Create a blog first
payload = {
"content": "Stuff for a bloggggg",
"title": "My Title",
"author": self.user_pk,
"game" : "Minecraft"
}
create_url = "/blog/create_blog/"
response = self.client.post(create_url, payload, content_type="application/json")
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = f"/blog/get_blog/?order=old"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)

# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is not empty
self.assertTrue(blogs)

# Check the structure of each blog in the list
for blog in blogs:
self.assertIn('id', blog)
self.assertIn('title', blog)

first_blog = blogs[1]
self.assertEqual(first_blog['title'], payload['title'])

def test_get_blogs_byOrderLikes(self):
"""
Test getting all blogs
"""
# Create a blog first
payload = {
"content": "Stuff for a bloggggg",
"title": "My Title",
"author": self.user_pk,
"game" : "Minecraft"
}
create_url = "/blog/create_blog/"
response = self.client.post(create_url, payload, content_type="application/json")
self.assertEqual(response.status_code, 200)

#Update Likes

update_url = f"/blog/likes/{self.blog_pk}/"

update = {
"action": "like",
"value": 1,
}

response = self.client.put(update_url, update, content_type="application/json")
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = f"/blog/get_blog/?order=likes"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)

# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is not empty
self.assertTrue(blogs)

# Check the structure of each blog in the list
for blog in blogs:
self.assertIn('id', blog)
self.assertIn('title', blog)

first_blog = blogs[0]
self.assertEqual(first_blog['title'], "Sample Blog Post" )

def test_get_blogs_multiple_filters(self):
"""
Test getting all blogs
"""
# Create a blog first
payload = {
"content": "Stuff for a bloggggg",
"title": "My Title",
"author": self.user_pk,
"game" : "Minecraft"
}
create_url = "/blog/create_blog/"
response = self.client.post(create_url, payload, content_type="application/json")
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = f"/blog/get_blog/?order=old&title=My Title&game=Minecraft"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)

# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is not empty
self.assertTrue(blogs)

# Check the structure of each blog in the list
for blog in blogs:
self.assertIn('id', blog)
self.assertIn('title', blog)

first_blog = blogs[0]
self.assertEqual(first_blog['title'], payload['title'])

def test_get_blogs_empty(self):
"""
Test getting all blogs when there are no blogs
"""
url = f"/blog/delete_blog/{self.blog_pk}/"
response = self.client.delete(url)
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = "/blog/getlist/"
get_url = "/blog/get_blog/?game=NOTAGAME"
response = self.client.get(get_url)

# Check response status code
Expand Down Expand Up @@ -206,28 +503,6 @@ def test_update_likes(self):
response = self.client.put(update_url, update, content_type="application/json")
self.assertEqual(response.status_code, 200)

def test_specific_get(self):
"""
Test getting a specific blog
"""
get_url = f"/blog/get_blog/{self.blog_pk}/"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

self.assertEqual(data['content'], "This is a sample blog post content.")
self.assertEqual(data['title'], "Sample Blog Post")

get_url = "/blog/get_blog/99/"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 500)

def test_delete(self):
"""
Test deleting a specific blog
Expand Down
2 changes: 1 addition & 1 deletion lvlgg_backend/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
path("create_blog/", Blogs.as_view()),
path("getlist/", Blogs.as_view()),
path("delete_blog/<int:pk>/", Blogs.as_view()),
path("get_blog/<int:pk>/", GetBlogs.as_view()),
path("get_blog/", GetBlogs.as_view()),
path("update_blog/<int:pk>/", Blogs.as_view()),
path("likes/<int:pk>/", GetBlogs.as_view())
]
Loading

0 comments on commit 0618925

Please sign in to comment.