Skip to content

Commit

Permalink
Add status field to test. Also run black.
Browse files Browse the repository at this point in the history
  • Loading branch information
aapris committed Sep 19, 2023
1 parent a472b02 commit e07a86d
Showing 1 changed file with 96 additions and 97 deletions.
193 changes: 96 additions & 97 deletions django_server/feedback_map/rest/tests/map_data_points_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class MapDataPointsTests(FVHAPITestCase):
def create_and_login_reviewer(self):
user = User.objects.create(username='reviewer', first_name='Regina', last_name='Reviewer')
user = User.objects.create(username="reviewer", first_name="Regina", last_name="Reviewer")
user.groups.add(Group.objects.get_or_create(name=REVIEWER_GROUP)[0])
self.client.force_login(user)
return user
Expand All @@ -23,14 +23,14 @@ def test_save_map_data_point(self):
user = self.create_and_login_user()

# When requesting to save an Map Data Point over ReST
url = reverse('mapdatapoint-list')
url = reverse("mapdatapoint-list")
fields = {
'lat': '60.16134701761975',
'lon': '24.944593941327188',
'comment': 'Nice view',
'tags': ['Entrance', 'Steps']
"lat": "60.16134701761975",
"lon": "24.944593941327188",
"comment": "Nice view",
"tags": ["Entrance", "Steps"],
}
response = self.client.post(url, data=fields, format='json')
response = self.client.post(url, data=fields, format="json")

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Expand All @@ -43,33 +43,33 @@ def test_save_map_data_point(self):
self.assertEqual(note.modified_by_id, user.id)

# And it creates any passed tags:
self.assertSetEqual(set(note.tags), set(fields['tags']))
self.assertSetEqual(set(note.tags), set(fields["tags"]))

# And when subsequently requesting to attach an image to the note
with open(os.path.join(os.path.dirname(__file__), 'test_image.png'), 'rb') as file:
with open(os.path.join(os.path.dirname(__file__), "test_image.png"), "rb") as file:
file_content = file.read()
uploaded_file = SimpleUploadedFile("image.png", file_content, content_type="image/png")
url = reverse('mapdatapoint-detail', kwargs={'pk': note.id})
response = self.client.patch(url, data={'image': uploaded_file}, format='multipart')
url = reverse("mapdatapoint-detail", kwargs={"pk": note.id})
response = self.client.patch(url, data={"image": uploaded_file}, format="multipart")

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_200_OK)

# And a note is updated in db:
note = models.MapDataPoint.objects.get()
self.assertEqual(note.image.name, f'map_data_points/{note.id}/image.png')
self.assertEqual(note.image.name, f"map_data_points/{note.id}/image.png")

def test_save_anonymous_map_data_point(self):
# Given that no user is signed in
# When requesting to save an Map Data Point over ReST
url = reverse('mapdatapoint-list')
url = reverse("mapdatapoint-list")
fields = {
'lat': '60.16134701761975',
'lon': '24.944593941327188',
'comment': 'Nice view',
'tags': ['Entrance', 'Steps']
"lat": "60.16134701761975",
"lon": "24.944593941327188",
"comment": "Nice view",
"tags": ["Entrance", "Steps"],
}
response = self.client.post(url, data=fields, format='json')
response = self.client.post(url, data=fields, format="json")

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Expand All @@ -82,36 +82,36 @@ def test_save_anonymous_map_data_point(self):
self.assertEqual(note.modified_by_id, None)

# And it creates any passed tags:
self.assertSetEqual(set(note.tags), set(fields['tags']))
self.assertSetEqual(set(note.tags), set(fields["tags"]))

# And when subsequently requesting to attach an image to the note
with open(os.path.join(os.path.dirname(__file__), 'test_image.png'), 'rb') as file:
with open(os.path.join(os.path.dirname(__file__), "test_image.png"), "rb") as file:
file_content = file.read()
uploaded_file = SimpleUploadedFile("image.png", file_content, content_type="image/png")
url = reverse('mapdatapoint-detail', kwargs={'pk': note.id})
response = self.client.patch(url, data={'image': uploaded_file}, format='multipart')
url = reverse("mapdatapoint-detail", kwargs={"pk": note.id})
response = self.client.patch(url, data={"image": uploaded_file}, format="multipart")

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_200_OK)

# And a note is updated in db:
note = models.MapDataPoint.objects.get()
self.assertEqual(note.image.name, f'map_data_points/{note.id}/image.png')
self.assertEqual(note.image.name, f"map_data_points/{note.id}/image.png")

def test_save_anonymous_map_data_point_with_button_position(self):
# Given that no user is signed in
# And given a published Tag with button position field defined
models.Tag.objects.create(tag='Smelly', button_position=1, published=timezone.now())
models.Tag.objects.create(tag="Smelly", button_position=1, published=timezone.now())

# When requesting to save an Map Data Point over ReST, giving a button position
url = reverse('mapdatapoint-list')
url = reverse("mapdatapoint-list")
fields = {
'lat': '60.16134701761975',
'lon': '24.944593941327188',
'device_id': 'dev_1234',
'button_position': 1
"lat": "60.16134701761975",
"lon": "24.944593941327188",
"device_id": "dev_1234",
"button_position": 1,
}
response = self.client.post(url, data=fields, format='json')
response = self.client.post(url, data=fields, format="json")

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Expand All @@ -120,49 +120,48 @@ def test_save_anonymous_map_data_point_with_button_position(self):
note = models.MapDataPoint.objects.get()

# And it creates tags based on given button position:
self.assertSetEqual(set(note.tags), set(['Smelly']))
self.assertEqual(note.device_id, 'dev_1234')
self.assertSetEqual(set(note.tags), set(["Smelly"]))
self.assertEqual(note.device_id, "dev_1234")

def test_update_map_data_point_tags(self):
# Given that a user is signed in
user = self.create_and_login_user()

# And given a successfully created Map Data Point
note = models.MapDataPoint.objects.create(lat='60.16134701761975', lon='24.944593941327188',
created_by=user)
note = models.MapDataPoint.objects.create(lat="60.16134701761975", lon="24.944593941327188", created_by=user)

# When requesting to update an Map Data Point over ReST, giving a list of tags to add
url = reverse('mapdatapoint-detail', kwargs={'pk': note.id})
fields = {'tags': ['Entrance', 'Steps']}
response = self.client.patch(url, data=fields, format='json')
url = reverse("mapdatapoint-detail", kwargs={"pk": note.id})
fields = {"tags": ["Entrance", "Steps"]}
response = self.client.patch(url, data=fields, format="json")

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_200_OK)

# And the passed tags are created:
note = models.MapDataPoint.objects.get()
self.assertSetEqual(set(note.tags), set(fields['tags']))
self.assertSetEqual(set(note.tags), set(fields["tags"]))

# And when subsequently requesting to update the note, giving another list of tags
fields = {'tags': ['Entrance']}
response = self.client.patch(url, data=fields, format='json')
fields = {"tags": ["Entrance"]}
response = self.client.patch(url, data=fields, format="json")

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_200_OK)

# And the tags have been changed:
note = models.MapDataPoint.objects.get()
self.assertSetEqual(set(note.tags), set(fields['tags']))
self.assertSetEqual(set(note.tags), set(fields["tags"]))

def test_review_features(self):
# Given that a reviewer user is signed in
user = self.create_and_login_reviewer()

# And given a successfully created Map Data Point
note = models.MapDataPoint.objects.create(lat='60.16134701761975', lon='24.944593941327188')
note = models.MapDataPoint.objects.create(lat="60.16134701761975", lon="24.944593941327188")

# When requesting to mark the Map Data Point as processed over ReST
url = reverse('mapdatapoint-mark-processed', kwargs={'pk': note.id})
url = reverse("mapdatapoint-mark-processed", kwargs={"pk": note.id})
response = self.client.put(url)

# Then an OK response is received:
Expand All @@ -177,35 +176,35 @@ def test_hide_features(self):
user = self.create_and_login_reviewer()

# And given a successfully created Map Data Point
note = models.MapDataPoint.objects.create(lat='60.16134701761975', lon='24.944593941327188')
note = models.MapDataPoint.objects.create(lat="60.16134701761975", lon="24.944593941327188")

# When requesting to mark the Map Data Point as hidden over ReST
url = reverse('mapdatapoint-hide-note', kwargs={'pk': note.id})
response = self.client.put(url, data={'hidden_reason': 'Too ugly.'})
url = reverse("mapdatapoint-hide-note", kwargs={"pk": note.id})
response = self.client.put(url, data={"hidden_reason": "Too ugly."})

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_200_OK)

# And a note is updated in db:
note = models.MapDataPoint.objects.get()
self.assertEqual(note.processed_by_id, user.id)
self.assertEqual(note.hidden_reason, 'Too ugly.')
self.assertEqual(note.hidden_reason, "Too ugly.")

# And the note is not shown in further requests to list Map Data Points
url = reverse('mapdatapoint-list')
url = reverse("mapdatapoint-list")
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'], [])
self.assertEqual(response.data["results"], [])

def test_vote_on_map_data_point(self):
# Given that a user is signed in
user = self.create_and_login_user()

# And given a successfully created Map Data Point
note = models.MapDataPoint.objects.create(lat='60.16134701761975', lon='24.944593941327188')
note = models.MapDataPoint.objects.create(lat="60.16134701761975", lon="24.944593941327188")

# When requesting to upvote the Map Data Point over ReST
url = reverse('mapdatapoint-upvote', kwargs={'pk': note.id})
url = reverse("mapdatapoint-upvote", kwargs={"pk": note.id})
response = self.client.put(url)

# Then an OK response is received:
Expand All @@ -214,109 +213,109 @@ def test_vote_on_map_data_point(self):
# And the upvote is created:
note = models.MapDataPoint.objects.get()
# self.assertSetEqual(set(response.json()['upvotes']), set([user.id]))
self.assertSetEqual(set(note.upvotes.values_list('user_id', flat=True)), set([user.id]))
self.assertSetEqual(set(note.upvotes.values_list("user_id", flat=True)), set([user.id]))

# And when subsequently requesting to downvote the note
url = reverse('mapdatapoint-downvote', kwargs={'pk': note.id})
url = reverse("mapdatapoint-downvote", kwargs={"pk": note.id})
response = self.client.put(url)

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_200_OK)

# And the votes have been changed:
note = models.MapDataPoint.objects.get()
self.assertSetEqual(set(note.upvotes.values_list('user_id', flat=True)), set())
self.assertSetEqual(set(note.upvotes.values_list("user_id", flat=True)), set())
# self.assertSetEqual(set(response.json()['downvotes']), set([user.id]))
self.assertSetEqual(set(note.downvotes.values_list('user_id', flat=True)), set([user.id]))
self.assertSetEqual(set(note.downvotes.values_list("user_id", flat=True)), set([user.id]))

def test_comment_on_map_data_point(self):
# Given that a user is signed in
user = self.create_and_login_user()
self.create_and_login_user()

# And given a successfully created Map Data Point
user2 = User.objects.create(username='other_user')
note = models.MapDataPoint.objects.create(lat='60.16134701761975', lon='24.944593941327188', created_by=user2)
user2 = User.objects.create(username="other_user")
note = models.MapDataPoint.objects.create(lat="60.16134701761975", lon="24.944593941327188", created_by=user2)

# When requesting to comment the Map Data Point over ReST
url = reverse('mapdatapointcomment-list')
response = self.client.post(url, {'map_data_point': note.id, 'comment': 'nice!'})
url = reverse("mapdatapointcomment-list")
response = self.client.post(url, {"map_data_point": note.id, "comment": "nice!"})

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

# And the comment is created:
note = models.MapDataPoint.objects.get()
self.assertSetEqual(set(note.comments.values_list('comment', flat=True)), set(['nice!']))
self.assertSetEqual(set(note.comments.values_list("comment", flat=True)), set(["nice!"]))

# And it is included with the note when fetched over ReST
url = reverse('mapdatapoint-detail', kwargs={'pk': note.id})
url = reverse("mapdatapoint-detail", kwargs={"pk": note.id})
response = self.client.get(url)
self.assertEqual(response.json()['comments'][0]['comment'], 'nice!')
self.assertEqual(response.json()["comments"][0]["comment"], "nice!")

# And a notification of the comment is created for the note creator:
self.assertEqual(user2.notifications.count(), 1)

# And when subsequently requesting to delete the note
url = reverse('mapdatapointcomment-detail', kwargs={'pk': note.comments.first().id})
url = reverse("mapdatapointcomment-detail", kwargs={"pk": note.comments.first().id})
response = self.client.delete(url)

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

# And the comment is deleted:
note = models.MapDataPoint.objects.get()
self.assertSetEqual(set(note.comments.values_list('comment', flat=True)), set([]))
self.assertSetEqual(set(note.comments.values_list("comment", flat=True)), set([]))

def test_map_data_points_as_geojson(self):
# Given that there are some Map Data Points in the db
note = models.MapDataPoint.objects.create(**{
'lat': '60.16134701761975',
'lon': '24.944593941327188',
'comment': 'Nice view'})
note = models.MapDataPoint.objects.create(
**{"lat": "60.16134701761975", "lon": "24.944593941327188", "comment": "Nice view"}
)

# When requesting the notes as geojson
url = reverse('map_data_points_geojson')
url = reverse("map_data_points_geojson")
response = self.client.get(url)

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_200_OK)

# And it contains the notes as geojson:
props = response.json()['features'][0]['properties']
self.assertDictEqual(response.json(), {
'type': 'FeatureCollection',
'features': [{
'type': 'Feature',
'geometry': {'type': 'Point', 'coordinates': [24.94459394, 60.16134702]},
'properties': {
'id': note.id,
'comment': 'Nice view',
'lat': '60.16134702',
'lon': '24.94459394',
'is_processed': False,
'created_at': props['created_at'],
'modified_at': props['modified_at'],
}
}]
})
props = response.json()["features"][0]["properties"]
self.assertDictEqual(
response.json(),
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [24.94459394, 60.16134702]},
"properties": {
"id": note.id,
"comment": "Nice view",
"status": "NEW",
"lat": "60.16134702",
"lon": "24.94459394",
"is_processed": False,
"created_at": props["created_at"],
"modified_at": props["modified_at"],
},
}
],
},
)

def test_rejected_map_data_points_not_in_geojson(self):
# And given that there are some Map Data Points in the db marked as not visible
note = models.MapDataPoint.objects.create(**{
'lat': '60.16134701761975',
'lon': '24.944593941327188',
'comment': 'Nice view',
'visible': False})
_ = models.MapDataPoint.objects.create(
**{"lat": "60.16134701761975", "lon": "24.944593941327188", "comment": "Nice view", "visible": False}
)

# When requesting the notes as geojson
url = reverse('map_data_points_geojson')
url = reverse("map_data_points_geojson")
response = self.client.get(url)

# Then an OK response is received:
self.assertEqual(response.status_code, status.HTTP_200_OK)

# And it does not contain the invisible notes:
self.assertDictEqual(response.json(), {
'type': 'FeatureCollection',
'features': []
})
self.assertDictEqual(response.json(), {"type": "FeatureCollection", "features": []})

0 comments on commit e07a86d

Please sign in to comment.