<commit_before>from flask import Response

from changes.api.base import APIView
from changes.api.serializer import Serializer
from changes.constants import Status
from changes.models import Build, Author, Test


class TestWithBuildSerializer(Serializer):
    def serialize(self, instance):
        return {
            'id': instance.id.hex,
            'name': instance.name,
            'package': instance.package,
            'result': instance.result,
            'duration': instance.duration,
            'message': instance.message,
            'link': '/tests/%s/' % (instance.id.hex,),
            'dateCreated': instance.date_created,
            'build': instance.build,
        }


class TestDetailsAPIView(APIView):
    def get(self, test_id):
        test = Test.query.get(test_id)
        if test is None:
            return Response(status=404)

        # find other builds this test has run in
        # TODO(dcramer): ideally this would only query for builds which
        # are previous in vcs tree (not based on simply date created)
        previous_runs = Test.query.join(Build).outerjoin(Author).filter(
            Test.group_sha == test.group_sha,
            Test.label_sha == test.label_sha,
            Build.date_created < test.build.date_created,
            Build.status == Status.finished
        ).order_by(Build.date_created.desc())[:25]

        first_run = Test.query.join(Build).outerjoin(Author).filter(
            Test.group_sha == test.group_sha,
            Test.label_sha == test.label_sha,
            Test.id != test.id,
            Build.status == Status.finished
        ).order_by(Build.date_created.asc()).first()

        extended_serializers = {
            Test: TestWithBuildSerializer(),
        }

        context = {
            'build': test.build,
            'test': test,
            'previousRuns': self.serialize(previous_runs, extended_serializers),
            'firstRun': self.serialize(first_run, extended_serializers),<commit_msg>Correctly handle first run being self<commit_after>from flask import Response

from changes.api.base import APIView
from changes.api.serializer import Serializer
from changes.constants import Status
from changes.models import Build, Author, Test


class TestWithBuildSerializer(Serializer):
    def serialize(self, instance):
        return {
            'id': instance.id.hex,
            'name': instance.name,
            'package': instance.package,
            'result': instance.result,
            'duration': instance.duration,
            'message': instance.message,
            'link': '/tests/%s/' % (instance.id.hex,),
            'dateCreated': instance.date_created,
            'build': instance.build,
        }


class TestDetailsAPIView(APIView):
    def get(self, test_id):
        test = Test.query.get(test_id)
        if test is None:
            return Response(status=404)

        # find other builds this test has run in
        # TODO(dcramer): ideally this would only query for builds which
        # are previous in vcs tree (not based on simply date created)
        previous_runs = Test.query.join(Build).outerjoin(Author).filter(
            Test.group_sha == test.group_sha,
            Test.label_sha == test.label_sha,
            Build.date_created < test.build.date_created,
            Build.status == Status.finished
        ).order_by(Build.date_created.desc())[:25]

        first_run = Test.query.join(Build).outerjoin(Author).filter(
            Test.group_sha == test.group_sha,
            Test.label_sha == test.label_sha,
            Build.status == Status.finished
        ).order_by(Build.date_created.asc()).first()
        # if we end up just discovering ourself, then we must be the first run
        if first_run.id == test.id:
            first_run = None

        extended_serializers = {
            Test: TestWithBuildSerializer(),
        }

        context = {
            'build': test.build,
            'test': test,
            'previousRuns': self.serialize(previous_runs, extended_serializers),
            'firstRun': self.serialize(first_run, extended_serializers),
{'fraction_non_alphanumeric': 0.10484261501210654, 'fraction_numerical': 0.002421307506053269, 'mean_word_length': 2.2916334661354583, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3799502', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1099, 'n_words': 269}