Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make family more liberal #815

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions backend/coreapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,61 @@ def test_family_etag(self) -> None:
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertNotEqual(etag, response.headers.get("Etag"))

def test_family_checks_hash_only(self) -> None:
"""
Ensure that scratches with the same target_asm hash belong to the same family, even if their Assembly instances differ somehow
"""

scratch1_dict = {
"compiler": compilers.DUMMY.id,
"platform": platforms.DUMMY.id,
"context": "",
"target_asm": "jr $ra\nnop\n",
}
scratch2_dict = {
"compiler": compilers.DUMMY.id,
"platform": platforms.DUMMY.id,
"context": "",
"target_asm": "jr $ra\nnop\n",
}

scratch1 = self.create_scratch(scratch1_dict)
scratch2 = self.create_scratch(scratch2_dict)

assembly_2: Assembly = scratch1.target_assembly
assembly_2.hash = 0
assembly_2.pk = None
assembly_2.save()
scratch2.target_assembly = assembly_2
scratch2.save()

response = self.client.get(reverse("scratch-family", args=[scratch1.slug]))
self.assertEqual(len(response.json()), 2)

def test_family_checks_hash_only_empty_asm(self) -> None:
"""
Ensure that scratches with empty asm do not have a family, even if their asm is the same
"""

scratch1_dict = {
"compiler": compilers.DUMMY.id,
"platform": platforms.DUMMY.id,
"context": "",
"target_asm": " ",
}
scratch2_dict = {
"compiler": compilers.DUMMY.id,
"platform": platforms.DUMMY.id,
"context": "",
"target_asm": " ",
}

scratch1 = self.create_scratch(scratch1_dict)
scratch2 = self.create_scratch(scratch2_dict)

response = self.client.get(reverse("scratch-family", args=[scratch1.slug]))
self.assertEqual(len(response.json()), 1)


@dataclass
class MockRepository:
Expand Down
18 changes: 12 additions & 6 deletions backend/coreapp/views/scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,12 @@ def scratch_etag(request: Request, pk: Optional[str] = None) -> Optional[str]:
def family_etag(request: Request, pk: Optional[str] = None) -> Optional[str]:
scratch: Optional[Scratch] = Scratch.objects.filter(slug=pk).first()
if scratch:
family = Scratch.objects.filter(
target_assembly=scratch.target_assembly,
)
if scratch.target_assembly.source_asm.data.strip() == "":
family = Scratch.objects.filter(slug=scratch.slug).order_by("creation_time")
else:
family = Scratch.objects.filter(
target_assembly__source_asm__hash=scratch.target_assembly.source_asm.hash,
)

return str(hash((family, request.headers.get("Accept"))))
else:
Expand Down Expand Up @@ -492,9 +495,12 @@ def export(self, request: Request, pk: str) -> HttpResponse:
def family(self, request: Request, pk: str) -> Response:
scratch: Scratch = self.get_object()

family = Scratch.objects.filter(
target_assembly=scratch.target_assembly,
).order_by("creation_time")
if scratch.target_assembly.source_asm.data.strip() == "":
family = Scratch.objects.filter(slug=scratch.slug).order_by("creation_time")
else:
family = Scratch.objects.filter(
target_assembly__source_asm__hash=scratch.target_assembly.source_asm.hash,
).order_by("creation_time")

return Response(
TerseScratchSerializer(family, many=True, context={"request": request}).data
Expand Down