Skip to content

Commit

Permalink
fix algorithm order in AlgorithmList view and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DrCBeatz committed Apr 1, 2024
1 parent 57dbeef commit f1b46a7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
48 changes: 47 additions & 1 deletion mastertheorem/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,50 @@ def test_algorithm_list_view():
assert response.data[0]['python_code'].startswith('def binary_search') == True
assert response.data[1]['name'] == "Merge Sort"
assert response.data[1]['description'] == "Merge Sort Description"
assert response.data[1]['python_code'].startswith('def merge_sort') == True
assert response.data[1]['python_code'].startswith('def merge_sort') == True

@pytest.mark.django_db
def test_algorithm_list_view_order():
client = APIClient()
# Ensure the algorithms are created in a certain order
first_algorithm = Algorithm.objects.create(name="Algorithm A", description="First Algorithm", python_code="def algorithm_a():pass")
second_algorithm = Algorithm.objects.create(name="Algorithm B", description="Second Algorithm", python_code="def algorithm_b():pass")

response = client.get('/api/algorithms/')
assert response.status_code == 200
assert len(response.data) == 2

# Verify th order by ID or any other attribute that reflects creation order
assert response.data[0]['id'] == first_algorithm.id
assert response.data[1]['id'] == second_algorithm.id

@pytest.mark.django_db
def test_algorithm_order_after_modification():
client = APIClient()
# Create two algorithms
first_algorithm = Algorithm.objects.create(name="Algorithm C", description="C Algorithm", python_code="def algorithm_c(): pass")
second_algorithm = Algorithm.objects.create(name="Algorithm D", description="D Algorithm", python_code="def algorithm_d(): pass")

# Modify the first algorithm
Algorithm.objects.filter(id=first_algorithm.id).update(name="Algorithm C Updated")

response = client.get('/api/algorithms/')
assert response.status_code == 200
assert len(response.data) == 2

# Check if the order is still by creation time (ID)
assert response.data[0]['id'] == first_algorithm.id
assert response.data[0]['name'] == "Algorithm C Updated"
assert response.data[1]['id'] == second_algorithm.id

# Modify the second algorithm
Algorithm.objects.filter(id=second_algorithm.id).update(name="Algorithm D Updated")

response = client.get('/api/algorithms/')
assert response.status_code == 200
assert len(response.data) == 2

# Check if the order is by ID again
assert response.data[1]['id'] == second_algorithm.id
assert response.data[1]['name'] == "Algorithm D Updated"
assert response.data[0]['id'] == first_algorithm.id
2 changes: 1 addition & 1 deletion mastertheorem/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ def post(self, request, *args, **kwargs):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class AlgorithmList(generics.ListAPIView):
queryset = Algorithm.objects.all()
queryset = Algorithm.objects.all().order_by('id')
serializer_class = AlgorithmSerializer

0 comments on commit f1b46a7

Please sign in to comment.