Exemplo n.º 1
0
def test_build_tree():
    test_names = [
        'foo.bar.bar',
        'foo.bar.biz',
        'foo.biz',
        'blah.brah',
        'blah.blah.blah',
    ]

    result = build_tree(test_names, min_children=2)

    assert sorted(result) == ['blah', 'foo']

    result = build_tree(test_names, min_children=2, parent='foo')

    assert sorted(result) == ['foo.bar', 'foo.biz']

    result = build_tree(test_names, min_children=2, parent='foo.biz')

    assert result == set()
Exemplo n.º 2
0
    def get(self, project_id):
        project = Project.get(project_id)
        if not project:
            return '', 404

        args = self.parser.parse_args()

        latest_job = Job.query.join(
            Source, Source.id == Job.source_id,
        ).filter(
            Source.patch_id == None,  # NOQA
            Job.project_id == project.id,
            Job.result == Result.passed,
            Job.status == Status.finished,
        ).order_by(
            Job.date_created.desc(),
        ).limit(1).first()

        if not latest_job:
            return self.respond([])

        # use the most recent test
        test_list = db.session.query(
            TestCase.name, TestCase.duration
        ).filter(
            TestCase.project_id == project_id,
            TestCase.job_id == latest_job.id,
        )
        if args.parent:
            test_list = test_list.filter(
                TestCase.name.startswith(args.parent),
            )
        test_list = list(test_list)

        if test_list:
            sep = TestCase(name=test_list[0][0]).sep

            groups = build_tree(
                [t[0] for t in test_list],
                sep=sep,
                min_children=2,
                parent=args.parent,
            )

            results = []
            for group in groups:
                num_tests = 0
                total_duration = 0
                for name, duration in test_list:
                    if name == group or name.startswith(group + sep):
                        num_tests += 1
                        total_duration += duration

                if args.parent:
                    name = group[len(args.parent) + len(sep):]
                else:
                    name = group
                data = {
                    'name': name,
                    'path': group,
                    'totalDuration': total_duration,
                    'numTests': num_tests,
                }
                results.append(data)
            results.sort(key=lambda x: x['totalDuration'], reverse=True)

            trail = []
            context = []
            if args.parent:
                for chunk in args.parent.split(sep):
                    context.append(chunk)
                    trail.append({
                        'path': sep.join(context),
                        'name': chunk,
                    })
        else:
            results = []
            trail = []

        data = {
            'groups': results,
            'trail': trail,
        }

        return self.respond(data, serialize=False)
Exemplo n.º 3
0
    def get(self, project_id):
        project = Project.get(project_id)
        if not project:
            return '', 404

        args = self.parser.parse_args()

        latest_build = Build.query.join(
            Source, Source.id == Build.source_id,
        ).filter(
            Source.patch_id == None,  # NOQA
            Build.project_id == project.id,
            Build.result == Result.passed,
            Build.status == Status.finished,
        ).order_by(
            Build.date_created.desc(),
        ).limit(1).first()

        if not latest_build:
            return self.respond([])

        # use the most recent coverage
        cover_list = FileCoverage.query.filter(
            FileCoverage.job_id.in_(
                db.session.query(Job.id).filter(
                    Job.build_id == latest_build.id,
                )
            )
        )

        if args.parent:
            cover_list = cover_list.filter(
                FileCoverage.filename.startswith(args.parent),
            )

        cover_list = list(cover_list)

        groups = build_tree(
            [c.filename for c in cover_list],
            sep='/',
            min_children=2,
            parent=args.parent,
        )

        results = []
        for group in groups:
            num_files = 0
            total_lines_covered = 0
            total_lines_uncovered = 0
            for file_coverage in cover_list:
                filename = file_coverage.filename
                if filename == group or filename.startswith(group + '/'):
                    num_files += 1
                    total_lines_covered += file_coverage.lines_covered
                    total_lines_uncovered += file_coverage.lines_uncovered

            if args.parent:
                filename = group[len(args.parent) + len('/'):]
            else:
                filename = group

            data = {
                'filename': filename,
                'path': group,
                'totalLinesCovered': total_lines_covered,
                'totalLinesUncovered': total_lines_uncovered,
                'numFiles': num_files,
            }
            results.append(data)
        results.sort(key=lambda x: x['totalLinesUncovered'], reverse=True)

        trail = []
        context = []
        if args.parent:
            for chunk in args.parent.split('/'):
                context.append(chunk)
                trail.append({
                    'path': '/'.join(context),
                    'name': chunk,
                })

        data = {
            'groups': results,
            'trail': trail,
        }

        return self.respond(data, serialize=False)
def _generate_testgroup_data(build, project_id, parent=None):
    job_list = db.session.query(Job.id).filter(
        Job.build_id == build.id,
    )

    if job_list:
        # use the most recent test
        test_list = db.session.query(
            TestCase.name, TestCase.duration, TestCase.id
        ).filter(
            TestCase.job_id.in_(job_list),
        )
        if parent:
            test_list = test_list.filter(
                TestCase.name.startswith(parent),
            )
        test_list = list(test_list)
    else:
        test_list = []

    if test_list:
        sep = TestCase(name=test_list[0][0]).sep

        groups = build_tree(
            [t[0] for t in test_list],
            sep=sep,
            min_children=1,
            parent=parent,
        )

        results = []
        for group in groups:
            num_tests = 0
            total_duration = 0
            test_id = None
            for name, duration, tid in test_list:
                if name == group or name.startswith(group + sep):
                    num_tests += 1
                    total_duration += duration
                    test_id = tid

            if parent:
                name = group[len(parent) + len(sep):]
            else:
                name = group
            data = {
                'name': name,
                'path': group,
                'totalDuration': total_duration,
                'numTests': num_tests,
            }
            if num_tests == 1:
                data['id'] = test_id
            results.append(data)
        results.sort(key=lambda x: x['totalDuration'], reverse=True)

        trail = []
        context = []
        if parent:
            for chunk in parent.split(sep):
                context.append(chunk)
                trail.append({
                    'path': sep.join(context),
                    'name': chunk,
                })
    else:
        results = []
        trail = []

    options = dict(
        (o.name, o.value) for o in ProjectOption.query.filter(
            ProjectOption.project_id == project_id,
            ProjectOption.name == 'build.test-duration-warning',
        )
    )

    over_threshold_duration = options.get('build.test-duration-warning')
    if over_threshold_duration:
        over_threshold_count = TestCase.query.filter(
            TestCase.project_id == project_id,
            TestCase.job_id.in_(job_list),
            TestCase.duration >= over_threshold_duration,
        ).count()
    else:
        over_threshold_count = 0

    return {
        'groups': results,
        'trail': trail,
        'overThreshold': {
            'count': over_threshold_count,
            'duration': over_threshold_duration,
        }
    }
Exemplo n.º 5
0
    def get(self, project_id):
        project = Project.get(project_id)
        if not project:
            return '', 404

        args = self.parser.parse_args()

        latest_build = Build.query.join(
            Source, Source.id == Build.source_id,
        ).filter(
            Source.patch_id == None,  # NOQA
            Build.project_id == project.id,
            Build.result == Result.passed,
            Build.status == Status.finished,
        ).order_by(
            Build.date_created.desc(),
        ).limit(1).first()

        if not latest_build:
            return self.respond()

        # use the most recent coverage
        cover_list = FileCoverage.query.filter(
            FileCoverage.job_id.in_(
                db.session.query(Job.id).filter(
                    Job.build_id == latest_build.id,
                )
            )
        )

        if args.parent:
            cover_list = cover_list.filter(
                FileCoverage.filename.startswith(args.parent),
            )

        cover_list = list(cover_list)

        groups = build_tree(
            [c.filename for c in cover_list],
            sep='/',
            min_children=2,
            parent=args.parent,
        )

        results = []
        for group in groups:
            num_files = 0
            total_lines_covered = 0
            total_lines_uncovered = 0
            for file_coverage in cover_list:
                filename = file_coverage.filename
                if filename == group or filename.startswith(group + '/'):
                    num_files += 1
                    total_lines_covered += file_coverage.lines_covered
                    total_lines_uncovered += file_coverage.lines_uncovered

            if args.parent:
                filename = group[len(args.parent) + len('/'):]
            else:
                filename = group

            data = {
                'filename': filename,
                'path': group,
                'totalLinesCovered': total_lines_covered,
                'totalLinesUncovered': total_lines_uncovered,
                'numFiles': num_files,
            }
            results.append(data)
        results.sort(key=lambda x: x['totalLinesUncovered'], reverse=True)

        trail = []
        context = []
        if args.parent:
            for chunk in args.parent.split('/'):
                context.append(chunk)
                trail.append({
                    'path': '/'.join(context),
                    'name': chunk,
                })

        data = {
            'groups': results,
            'trail': trail,
        }

        return self.respond(data, serialize=False)
Exemplo n.º 6
0
    def get(self, project_id):
        project = Project.get(project_id)
        if not project:
            return '', 404

        args = self.parser.parse_args()

        latest_build = Build.query.join(
            Source,
            Source.id == Build.source_id,
        ).filter(
            Source.patch_id == None,  # NOQA
            Build.project_id == project.id,
            Build.result == Result.passed,
            Build.status == Status.finished,
        ).order_by(Build.date_created.desc(), ).limit(1).first()

        if not latest_build:
            return self.respond({})

        job_list = db.session.query(Job.id).filter(
            Job.build_id == latest_build.id, )

        # use the most recent test
        test_list = db.session.query(TestCase.name, TestCase.duration).filter(
            TestCase.job_id.in_(job_list), )
        if args.parent:
            test_list = test_list.filter(TestCase.name.startswith(
                args.parent), )
        test_list = list(test_list)

        if test_list:
            sep = TestCase(name=test_list[0][0]).sep

            groups = build_tree(
                [t[0] for t in test_list],
                sep=sep,
                min_children=2,
                parent=args.parent,
            )

            results = []
            for group in groups:
                num_tests = 0
                total_duration = 0
                for name, duration in test_list:
                    if name == group or name.startswith(group + sep):
                        num_tests += 1
                        total_duration += duration

                if args.parent:
                    name = group[len(args.parent) + len(sep):]
                else:
                    name = group
                data = {
                    'name': name,
                    'path': group,
                    'totalDuration': total_duration,
                    'numTests': num_tests,
                }
                results.append(data)
            results.sort(key=lambda x: x['totalDuration'], reverse=True)

            trail = []
            context = []
            if args.parent:
                for chunk in args.parent.split(sep):
                    context.append(chunk)
                    trail.append({
                        'path': sep.join(context),
                        'name': chunk,
                    })
        else:
            results = []
            trail = []

        options = dict((o.name, o.value) for o in ProjectOption.query.filter(
            ProjectOption.project_id == project.id,
            ProjectOption.name == 'build.test-duration-warning',
        ))

        over_threshold_duration = options.get('build.test-duration-warning')
        if over_threshold_duration:
            over_threshold_count = TestCase.query.filter(
                TestCase.project_id == project_id,
                TestCase.job_id.in_(job_list),
                TestCase.duration >= over_threshold_duration,
            ).count()
        else:
            over_threshold_count = 0

        data = {
            'groups': results,
            'trail': trail,
            'overThreshold': {
                'count': over_threshold_count,
                'duration': over_threshold_duration,
            }
        }

        return self.respond(data, serialize=False)
Exemplo n.º 7
0
    def get(self, project_id):
        project = Project.get(project_id)
        if not project:
            return '', 404

        args = self.parser.parse_args()

        latest_job = Job.query.join(
            Source,
            Source.id == Job.source_id,
        ).filter(
            Source.patch_id == None,  # NOQA
            Job.project_id == project.id,
            Job.result == Result.passed,
            Job.status == Status.finished,
        ).order_by(Job.date_created.desc(), ).limit(1).first()

        if not latest_job:
            return self.respond([])

        # use the most recent test
        test_list = db.session.query(TestCase.name, TestCase.duration).filter(
            TestCase.project_id == project_id,
            TestCase.job_id == latest_job.id,
        )
        if args.parent:
            test_list = test_list.filter(TestCase.name.startswith(
                args.parent), )
        test_list = list(test_list)

        if test_list:
            sep = TestCase(name=test_list[0][0]).sep

            groups = build_tree(
                [t[0] for t in test_list],
                sep=sep,
                min_children=2,
                parent=args.parent,
            )

            results = []
            for group in groups:
                num_tests = 0
                total_duration = 0
                for name, duration in test_list:
                    if name == group or name.startswith(group + sep):
                        num_tests += 1
                        total_duration += duration

                if args.parent:
                    name = group[len(args.parent) + len(sep):]
                else:
                    name = group
                data = {
                    'name': name,
                    'path': group,
                    'totalDuration': total_duration,
                    'numTests': num_tests,
                }
                results.append(data)
            results.sort(key=lambda x: x['totalDuration'], reverse=True)

            trail = []
            context = []
            if args.parent:
                for chunk in args.parent.split(sep):
                    context.append(chunk)
                    trail.append({
                        'path': sep.join(context),
                        'name': chunk,
                    })
        else:
            results = []
            trail = []

        data = {
            'groups': results,
            'trail': trail,
        }

        return self.respond(data, serialize=False)
Exemplo n.º 8
0
    def get(self, project_id):
        project = Project.get(project_id)
        if not project:
            return '', 404

        args = self.parser.parse_args()

        latest_build = Build.query.join(
            Source, Source.id == Build.source_id,
        ).filter(
            Source.patch_id == None,  # NOQA
            Build.project_id == project.id,
            Build.result == Result.passed,
            Build.status == Status.finished,
        ).order_by(
            Build.date_created.desc(),
        ).limit(1).first()

        if not latest_build:
            return self.respond({})

        job_list = db.session.query(Job.id).filter(
            Job.build_id == latest_build.id,
        )

        # use the most recent test
        test_list = db.session.query(
            TestCase.name, TestCase.duration
        ).filter(
            TestCase.job_id.in_(job_list),
        )
        if args.parent:
            test_list = test_list.filter(
                TestCase.name.startswith(args.parent),
            )
        test_list = list(test_list)

        if test_list:
            sep = TestCase(name=test_list[0][0]).sep

            groups = build_tree(
                [t[0] for t in test_list],
                sep=sep,
                min_children=2,
                parent=args.parent,
            )

            results = []
            for group in groups:
                num_tests = 0
                total_duration = 0
                for name, duration in test_list:
                    if name == group or name.startswith(group + sep):
                        num_tests += 1
                        total_duration += duration

                if args.parent:
                    name = group[len(args.parent) + len(sep):]
                else:
                    name = group
                data = {
                    'name': name,
                    'path': group,
                    'totalDuration': total_duration,
                    'numTests': num_tests,
                }
                results.append(data)
            results.sort(key=lambda x: x['totalDuration'], reverse=True)

            trail = []
            context = []
            if args.parent:
                for chunk in args.parent.split(sep):
                    context.append(chunk)
                    trail.append({
                        'path': sep.join(context),
                        'name': chunk,
                    })
        else:
            results = []
            trail = []

        options = dict(
            (o.name, o.value) for o in ProjectOption.query.filter(
                ProjectOption.project_id == project.id,
                ProjectOption.name == 'build.test-duration-warning',
            )
        )

        over_threshold_duration = options.get('build.test-duration-warning')
        if over_threshold_duration:
            over_threshold_count = TestCase.query.filter(
                TestCase.project_id == project_id,
                TestCase.job_id.in_(job_list),
                TestCase.duration >= over_threshold_duration,
            ).count()
        else:
            over_threshold_count = 0

        data = {
            'groups': results,
            'trail': trail,
            'overThreshold': {
                'count': over_threshold_count,
                'duration': over_threshold_duration,
            }
        }

        return self.respond(data, serialize=False)