Skip to content

Backend Testing Strategies

Enestst edited this page Dec 3, 2024 · 1 revision

Backend Testing Documentation

Testing Strategies

This document outlines the testing strategies implemented for the backend of the music app. Below are the key areas tested and their corresponding examples.


1. Unit Tests

Unit tests verify individual components of the backend for correct functionality.

User Management

  • Registration Test

    • Description: Tests if a new user can register successfully with valid data.
    • Example Test Code: python def test_register(self): response = self.client.post(reverse('register'), data=json.dumps({ 'username': 'newuser', 'email': '[email protected]', 'password': 'newpassword', 'name': 'New', 'surname': 'User', 'labels': ['Listener'] }), content_type='application/json') self.assertEqual(response.status_code, 200) self.assertContains(response, 'Registration successful')
  • Login Test

    • Description: Verifies user login with valid and invalid credentials.
    • Example Test Code: python def test_login(self): response = self.client.post(reverse('login'), data=json.dumps({ 'username': 'testuser', 'password': 'testpassword' }), content_type='application/json') self.assertEqual(response.status_code, 200) self.assertContains(response, 'Login successful')

2. Domain-Specific Features

Tests for features unique to the music app, such as "Now Playing" and search functionality.

Now Playing

  • Save Now Playing

    • Description: Tests saving "Now Playing" data with valid and invalid inputs.
    • Example Test Code: python def test_save_now_playing_success(self): payload = { "link": "https://open.spotify.com/track/7eBo7oeGFWJ9GlL1Me6NuV", "latitude": 37.7749, "longitude": -122.4194 } response = self.client.post(self.url, data=json.dumps(payload), content_type="application/json") self.assertEqual(response.status_code, 201) self.assertEqual(NowPlaying.objects.count(), 1)
  • Invalid Input

    • Description: Ensures appropriate error handling for missing fields.
    • Example Test Code: python def test_save_now_playing_missing_fields(self): payload = {"link": "https://open.spotify.com/track/7eBo7oeGFWJ9GlL1Me6NuV"} response = self.client.post(self.url, data=json.dumps(payload), content_type="application/json") self.assertEqual(response.status_code, 400) self.assertJSONEqual(response.content, {"error": "link, latitude, and longitude are required."})

Search Functionality

  • Search with Query

    • Description: Tests searching content using specific keywords.
    • Example Test Code: python def test_search_with_query(self): response = self.client.get(reverse('search'), {'search': 'First'}) self.assertEqual(response.status_code, 200) data = response.json() self.assertEqual(data['total_results'], 1) self.assertEqual(data['contents'][0]['description'], 'First content description')
  • Pagination

    • Description: Verifies paginated search results.
    • Example Test Code: python def test_search_pagination(self): response = self.client.get(reverse('search'), {'page': 1, 'page_size': 2}) self.assertEqual(response.status_code, 200) data = response.json() self.assertEqual(len(data['contents']), 2)

3. API and Documentation

Tests for API endpoints ensure they meet expected input and output criteria.

API Endpoints

  • Save Now Playing Endpoint

    • Endpoint: /api/save-now-playing/
    • Inputs:
      • link (string, required): URL of the track.
      • latitude (float, required): Latitude coordinate.
      • longitude (float, required): Longitude coordinate.
    • Outputs:
      • Success: {"message": "Now playing data saved successfully.", "id": }
      • Error: {"error": "Invalid JSON."} or {"error": "link, latitude, and longitude are required."}
  • Search Endpoint

    • Endpoint: /api/search/
    • Inputs:
      • search (string, optional): Keyword for searching content.
      • page (int, optional): Page number for pagination.
      • page_size (int, optional): Number of results per page.
    • Outputs:
      • Success: JSON response with matching content and metadata.
      • Error: Pagination or query format errors.

4. Testing Tools and Standards

  • Tools:

    • Django TestCase: Framework for structuring and running tests.
    • Client: Simulates HTTP requests to API endpoints.
    • Reverse: Dynamically generates URLs for test consistency.
  • Standards:

    • Follows PEP 8 guidelines for code readability.
    • Ensures consistent naming conventions for test cases (e.g., test_* format).

5. Edge Cases

  • Handling Invalid Data

    • Tests for malformed requests (e.g., missing fields, invalid JSON).
    • Example: test_save_now_playing_invalid_json.
  • No Results

    • Tests for search queries that return no matches.
    • Example: test_search_no_matches.
  • Special Characters

    • Tests for handling unusual inputs in search (e.g., "&").
    • Example: test_search_with_special_characters.

Conclusion

This documentation provides a structured approach to backend testing for the music app. The test cases ensure that all core functionalities and edge cases are covered while adhering to standards for reliability and maintainability.

Clone this wiki locally