leading u problem with repr of looped Collections in modeltests.lookup

Dave Weber avatarDave Weber created an issue
======================================================================
File "/home/dave/workspace/django-3k/build/tests/modeltests/lookup/models.py", line ?, in modeltests.lookup.models.__test__.API_TESTS
Failed example:
    for d in Article.objects.values('id', 'headline'):
        i = sorted(d.items())
        i
Expected:
    [('headline', u'Article 5'), ('id', 5)]
    [('headline', u'Article 6'), ('id', 6)]
    [('headline', u'Article 4'), ('id', 4)]
    [('headline', u'Article 2'), ('id', 2)]
    [('headline', u'Article 3'), ('id', 3)]
    [('headline', u'Article 7'), ('id', 7)]
    [('headline', u'Article 1'), ('id', 1)]
Got:
    [('headline', 'Article 5'), ('id', 5)]
    [('headline', 'Article 6'), ('id', 6)]
    [('headline', 'Article 4'), ('id', 4)]
    [('headline', 'Article 2'), ('id', 2)]
    [('headline', 'Article 3'), ('id', 3)]
    [('headline', 'Article 7'), ('id', 7)]
    [('headline', 'Article 1'), ('id', 1)]
----------------------------------------------------------------------
File "/home/dave/workspace/django-3k/build/tests/modeltests/lookup/models.py", line ?, in modeltests.lookup.models.__test__.API_TESTS
Failed example:
    for d in Article.objects.values('id', 'headline').iterator():
        i = sorted(d.items())
        i
Expected:
    [('headline', u'Article 5'), ('id', 5)]
    [('headline', u'Article 6'), ('id', 6)]
    [('headline', u'Article 4'), ('id', 4)]
    [('headline', u'Article 2'), ('id', 2)]
    [('headline', u'Article 3'), ('id', 3)]
    [('headline', u'Article 7'), ('id', 7)]
    [('headline', u'Article 1'), ('id', 1)]
Got:
    [('headline', 'Article 5'), ('id', 5)]
    [('headline', 'Article 6'), ('id', 6)]
    [('headline', 'Article 4'), ('id', 4)]
    [('headline', 'Article 2'), ('id', 2)]
    [('headline', 'Article 3'), ('id', 3)]
    [('headline', 'Article 7'), ('id', 7)]
    [('headline', 'Article 1'), ('id', 1)]

Comments (5)

  1. Johan Harjono

    The problem is that in our custom displayhook, we get each of the value of i independently

    so at one iteration we would get a string in the form of "[('headline', 'Article 5'), ('id', 5)]"

    but then when we look at the expected value, we get the whole expected output.

    So we would get something like this:

    def py3_displayhook(value):
        if not value:
            # None should not be considered at all
            return original_displayhook(value)
        
        # Collect the repr output in one variable
        s = repr(value)
        # Strip b"" and u"" prefixes from the repr and expected output
        # TODO: better way of stripping the prefixes?
        expected = example.want
        
        ################################
        # s == "[('headline', 'Article 5'), ('id', 5)]"
        #
        # expected == "[('headline', u'Article 5'), ('id', 5)]
        #[('headline', u'Article 6'), ('id', 6)] 
        #[('headline', u'Article 4'), ('id', 4)]
        #[('headline', u'Article 2'), ('id', 2)]
        #[('headline', u'Article 3'), ('id', 3)]
        #[('headline', u'Article 7'), ('id', 7)]
        #[('headline', u'Article 1'), ('id', 1)]"
        ###############################
        
        expected = expected.strip() # be wary of newlines
        s = s.replace("u", "")
        s = s.replace("b", "")
        expected = expected.replace("u", "")
        expected = expected.replace("b", "")
    
        ##############################
        # s != expected, even when the u characters are all stripped
        ##############################
        
        if s == expected: # be wary of false positives here
            # they should be the same, print expected value
            print(example.want.strip(), file=sys.stdout)
        else:
            print(repr(value), file=sys.stdout)
    
  2. Johan Harjono

    Resolved with changeset 0c315cff556a

    followed Martin's suggestion of:

    1. have sys.displayhook collect all lines in a variable.
    2. take the expected output, and discount the same number of lines
    3. fix expected output, line-by-line
    4. compare
    
  3. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.