Question

I've a piece of code

   assert (len(max_test_scores) != 1), \
          "Internal error - migration 0011 - module Programs." \
          " Please contact with developers - " + str(len(max_test_scores))

And during execution this code, I've got an assertion error:

AssertionError: Internal error - migration 0011 - module Programs. Please contact with developers - 1

So 1 != 1 ?

I was searching, googling and thinking how this can happend, but have no idea. Here is code with it's context

def forwards(self, orm):
    problems_and_groups = orm.Test.objects \
            .values('problem', 'group').distinct()

    problems_instances_and_groups = []
    for pi in orm['contests.ProblemInstance'].objects.all():
        for pg in problems_and_groups:
            if pi.problem.pk == pg['problem']:
                problems_instances_and_groups \
                        .append({'problem_instance': pi.pk,
                                 'group': pg['group']})

    count = len(problems_instances_and_groups)
    num = 0
    update_every = max(1, count / 20)
    pb = ProgressBar(count)
    print "Migrating %d groups" % count
    for pig in problems_instances_and_groups:
        if num % update_every == 0:
            pb.update(num)
        num += 1

        submissions = orm['contests.Submission'].objects \
                .filter(problem_instance = pig['problem_instance']) \
                .values('pk')

        print submissions

        submission_reports = orm['contests.SubmissionReport'].objects \
                .filter(submission__in = submissions).values('pk')

        print submission_reports

        test_reports = orm.TestReport.objects \
                .filter(test_group = pig['group'],
                        submission_report__in = submission_reports)

        max_score = None

        max_test_scores = frozenset(test_report.test.max_score
                for test_report in test_reports)

        assert (len(max_test_scores) != 1), \
                "Internal error - migration 0011 - module Programs." \
                " Please contact with developers - " + str(len(max_test_scores))

        max_score = ScoreValue(list(max_test_scores)[0])

        GroupReport.filter(test_group=pig['group'],
                submission_report__in = submission_reports) \
                .update(max_score=max_score)
Was it helpful?

Solution

An assertion fails when the expression is not true:

>>> val = 1
>>> assert val != 1, 'Oops, val is 1'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: Oops, val is 1

The assertion expression must evaluate to true for the assertion not to fail.

If you want to assert that the set length is 1, then test for equality.

You don't need quite as many backslashes either:

assert len(max_test_scores) == 1, (
    "Internal error - migration 0011 - module Programs. "
    "Please contact with developers - {}".format(len(max_test_scores)))

OTHER TIPS

The assert statement throws an AssertionError if the assertion is False.

In this case, it obviously is, because in your example, len(max_test_scores) returns 1 (as seen in the assert error message, and 1 != 1 is False.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top