def setUp(self): """ Initialize the objects to use in tests for Location endpoints. In stations app. """ self.user = UserFactory() self.user_token = TokenFactory(user=self.user) self.client.credentials( HTTP_AUTHORIZATION="Urbvan {}".format(self.user_token.key)) self.location = LocationFactory(owner=self.user) self.url = reverse("api_v1_stations:v1_retrieve_location", kwargs={'pk': self.location.id})
def test_list(self): LocationFactory() response = self.client.get(self.url) response = response.json() self.assertEquals(response['body'].get('count'), 1)
def test_create_successfully(self): location = LocationFactory() data = {"location": location.id, "order": 20, "is_active": True} response = self.client.post(self.url, data, format='json') self.assertEquals(response.status_code, 201)
def test_get_all_locations(self): """Test api can all locations.""" LocationFactory(owner=self.user) response = self.client.get(self.url) response = response.json() self.assertEquals(response['body'].get('count'), 1)
def test_retrieve_successfully(self): """ Retrieve test for Location Model API """ st = LocationFactory() url = reverse("locations:v1_detail_location", kwargs={'pk': st.id}) response = self.client.get(url) self.assertEquals(response.status_code, 200) self.assertEquals(response.data['id'], st.id)
def test_retrieve(self): location = LocationFactory() url = reverse("locations:v1_location_manage", kwargs={'pk': location.id}) response = self.client.get(url, format='json') self.assertEquals(response.status_code, 200) self.assertEquals(response.data['id'], location.id)
def test_create_forbidden(self): self.user = UserFactory() self.user_token = TokenFactory(user=self.user) self.client.credentials( HTTP_AUTHORIZATION="Urbvan {}".format(self.user_token.key)) location = LocationFactory() data = {"order": 1, "is_active": True, "location": location.id} response = self.client.post(self.url_create, data, format="json") self.assertEquals(response.status_code, 403)
def setUp(self): """ Initialize the objects to use in tests for Station endpoints. In stations app. """ self.user = UserFactory() self.user_token = TokenFactory(user=self.user) self.location = LocationFactory(owner=self.user) self.client.credentials( HTTP_AUTHORIZATION="Urbvan {}".format(self.user_token.key))
def test_destroy_successfully(self): """ Update unit test for Location Model API """ st = LocationFactory() url = reverse("locations:v1_detail_location", kwargs={'pk': st.id}) response = self.client.get(url) self.assertEquals(response.status_code, 200) response = self.client.delete(url) self.assertEquals(response.status_code, 204) response = self.client.get(url) self.assertEquals(response.status_code, 404)
def test_delete(self): location = LocationFactory() url = reverse("locations:v1_location_manage", kwargs={'pk': location.id}) response = self.client.delete(url, format='json') self.assertEquals(response.status_code, 204) deleted = self.client.get(url, format='json') self.assertEquals(deleted.status_code, 404)
def test_update(self): location = LocationFactory() data = {"name": "socks"} url = reverse("locations:v1_location_manage", kwargs={'pk': location.id}) response = self.client.patch(url, data, format='json') self.assertEquals(response.status_code, 200) self.assertEquals(response.data['name'], "socks")
def test_delete_a_station(self): """Test api can delete a station.""" StationFactory(location=LocationFactory()) stations = Station.objects.all() station_id = self.station.id response = self.client.delete(self.url, format='json') deleted_station = Station.objects.filter(id=station_id).exists() self.assertEquals(response.status_code, status.HTTP_204_NO_CONTENT) self.assertTrue(len(stations) == 1) self.assertFalse(deleted_station)
def test_put_a_station(self): """Test api can update a station.""" data = { "location": LocationFactory().id, "order": 12, "is_active": False, "owner": self.user.id } response = self.client.put(self.url, data, format='json') new_order_station = response.data['body']['results'][0]['order'] self.station.refresh_from_db() self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(new_order_station, 12) self.assertEquals(self.station.order, new_order_station)
def test_update_successfully(self): """ Update unit test for Location Model API """ st = LocationFactory() url = reverse("locations:v1_detail_location", kwargs={'pk': st.id}) response = self.client.get(url) self.assertEquals(response.status_code, 200) self.assertEquals(Decimal(response.data['latitude']), st.latitude) data = { "id": st.id, "name": st.name, "latitude": st.latitude + 10, "longitude": st.longitude } response = self.client.put(url, data=data) self.assertEquals(response.status_code, 200) self.assertEquals(response.data['id'], st.id) self.assertEquals(Decimal(response.data['latitude']), st.latitude + 10)
class LocationDetailEndpointTest(APITestCase): def setUp(self): """ Initialize the objects to use in tests for Location endpoints. In stations app. """ self.user = UserFactory() self.user_token = TokenFactory(user=self.user) self.client.credentials( HTTP_AUTHORIZATION="Urbvan {}".format(self.user_token.key)) self.location = LocationFactory(owner=self.user) self.url = reverse("api_v1_stations:v1_retrieve_location", kwargs={'pk': self.location.id}) def test_get_a_location(self): """Test api get a location.""" response = self.client.get(self.url, format='json') self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertContains(response, self.location) def test_put_a_location(self): """Test api can update a location.""" data = { "name": "My new location", "latitude": "1.120000000000003", "longitude": "1.0000000000000004", "owner": self.user.id } response = self.client.put(self.url, data, format='json') new_name_location = response.data['body']['results'][0]['name'] self.location.refresh_from_db() self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(new_name_location, 'My new location') self.assertEquals(self.location.name, new_name_location) def test_patch_a_location(self): """Test api can partial update a location.""" data = { "latitude": "0.0000000000000300", } response = self.client.patch(self.url, data, format='json') new_latitude_location = '{0:f}'.format( response.data['body']['results'][0]['latitude']) self.location.refresh_from_db() self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(new_latitude_location, '0.0000000000000300') self.assertEquals('{0:f}'.format(self.location.latitude), new_latitude_location) def test_delete_a_location(self): """Test api can delete a location.""" LocationFactory(owner=self.user) locations = Location.objects.all() location_id = self.location.id response = self.client.delete(self.url, format='json') deleted_location = Location.objects.filter(id=location_id).exists() self.assertEquals(response.status_code, status.HTTP_204_NO_CONTENT) self.assertTrue(len(locations) == 1) self.assertFalse(deleted_location)