<commit_before># -*- coding: utf-8 -*-

'''
JSON RPC Dispatcher Tests
-------------------------

'''

import json
import unittest
from datetime import datetime
from django.core.exceptions import ImproperlyConfigured

try:
    from rpc4django.jsonrpcdispatcher import JSONRPCDispatcher
except ImproperlyConfigured:
    # Configure Django if not already configured
    from django.conf import settings
    settings.configure(DEBUG=True)
    from rpc4django.jsonrpcdispatcher import JSONRPCDispatcher

try:
    # Python2
    basestring
except NameError:
    # Python3
    basestring = str

class TestJSONRPCDispatcher(unittest.TestCase):

    def setUp(self):
        def add(a, b):
            return a + b

        def factorial(num):
            if num > 1:
                return num * factorial(num - 1)
            else:
                return 1

        def unicode_ret():
            return u'はじめまして'

        def kwargstest(a, b, **kwargs):
            if kwargs.get('c', None) is not None:
                return True
            return False

        self.dispatcher = JSONRPCDispatcher()
        self.dispatcher.register_function(add, 'add')
        self.dispatcher.register_function(factorial, 'fact')
        self.dispatcher.register_function(unicode_ret, 'unicode_ret')
        self.dispatcher.register_function(kwargstest, 'kwargstest')
        self.dispatcher.register_function(lambda: datetime.now(), 'datetest')

    def test_dates(self):
        jsontxt = json.dumps({"method": "datetest", "id": 1, "params": []})
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue(jsondict['error'] is not None)

        class MyEncoder(json.JSONEncoder):
            def default(self, o):
                if isinstance(o, datetime):
                    return o.strftime("%Y-%m-%d %H:%M:%S")
                else:
                    return super(MyEncoder, self).default(o)
        dispatcher = JSONRPCDispatcher(MyEncoder)
        dispatcher.register_function(lambda: datetime.now(), 'datetest')
        resp = dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertTrue(isinstance(jsondict['result'], basestring))
        self.assertEqual(type(datetime.strptime(jsondict['result'], "%Y-%m-%d %H:%M:%S")), datetime)

    def test_jsonrpc_member(self):
        # Demonstrate that the jsondict contains "jsonrpc"
        d = dict()
        d['params'] = [1, 2]
        d['method'] = 'kwargstest'
        d['id'] = 1

        jsontxt = json.dumps(d)
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertEqual(jsondict['jsonrpc'], '2.0')

    def test_kwargs(self):
        d = dict()
        d['params'] = [1, 2]
        d['method'] = 'kwargstest'
        d['id'] = 1

        jsontxt = json.dumps(d)
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertFalse(jsondict['result'])

        resp = self.dispatcher.dispatch(jsontxt, c=1)
        jsondict = json.loads(resp)
        self.assertTrue(jsondict['result'])

    def test_unicode(self):
        jsontxt = '{"params":[],"method":"unicode_ret","id":1}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertEqual(jsondict['id'], 1)
        self.assertEqual(jsondict['result'], u'はじめまして')

        jsontxt = '{"params":["\u306f\u3058\u3081","\u307e\u3057\u3066"],"method":"add","id":2}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertEqual(jsondict['id'], 2)
        self.assertEqual(jsondict['result'], u'はじめまして')

    def test_dispatch_success(self):
        jsontxt = '{"params":[1,2],"method":"add","id":1}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertEqual(jsondict['id'], 1)
        self.assertEqual(jsondict['result'], 3)

        jsontxt = '{"params":[5],"method":"fact","id":"hello"}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertEqual(jsondict['id'], 'hello')
        self.assertEqual(jsondict['result'], 120)

    def test_method_error(self):
        jsontxt = '{"params":["a"],"method":"fact","id":"hello"}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertTrue(jsondict['error'] is not None)

    def test_dispatch_paramserror(self):
        jsontxt = '{"params":[1],"method":"add","id":"4"}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertTrue(jsondict['error'] is not None)

    def test_dispatch_nomethod(self):
        jsontxt = '{"params":[],"method":"add123","id":123}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], 123)
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 105)
        self.assertEqual(jsondict['error']['message'], 'method "add123" is not supported')

    def test_dispatch_badrequest(self):
        jsontxt = '"params":asdf[14]","method":"add","id":0}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], '')
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 101)
        self.assertEqual(jsondict['error']['message'], 'JSON decoding error')

        jsontxt = '["should", "be", "a", "Object"]'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], '')
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 102)
        self.assertEqual(jsondict['error']['message'], 'Cannot decode to a javascript Object')

        jsontxt = '{"params":"shouldbelist","method":"add","id":0}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], 0)
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 102)
        self.assertEqual(jsondict['error']['message'], 'params must be a javascript Array')

        jsontxt = '{"params":"[]","method":123,"id":42}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], 42)
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 102)
        self.assertEqual(jsondict['error']['message'], 'method must be a javascript String')

if __name__ == '__main__':
    unittest.main()
<commit_msg>Update test_jsonrpcdispatcher.py<commit_after># -*- coding: utf-8 -*-

'''
JSON RPC Dispatcher Tests
-------------------------

'''

import json
import unittest
from datetime import datetime
from django.core.exceptions import ImproperlyConfigured

try:
    from rpc4django.jsonrpcdispatcher import JSONRPCDispatcher
except ImproperlyConfigured:
    # Configure Django if not already configured
    from django.conf import settings
    settings.configure(DEBUG=True)
    from rpc4django.jsonrpcdispatcher import JSONRPCDispatcher

try:
    # Python2
    basestring
except NameError:
    # Python3
    basestring = str

class TestJSONRPCDispatcher(unittest.TestCase):

    def setUp(self):
        def add(a, b):
            return a + b

        def factorial(num):
            if num > 1:
                return num * factorial(num - 1)
            else:
                return 1

        def unicode_ret():
            return u'はじめまして'

        def kwargstest(a, b, **kwargs):
            if kwargs.get('c', None) is not None:
                return True
            return False

        def withoutargstest():
            return True

        def requestargtest(request,a):
            return request

        self.dispatcher = JSONRPCDispatcher()
        self.dispatcher.register_function(add, 'add')
        self.dispatcher.register_function(factorial, 'fact')
        self.dispatcher.register_function(unicode_ret, 'unicode_ret')
        self.dispatcher.register_function(kwargstest, 'kwargstest')
        self.dispatcher.register_function(lambda: datetime.now(), 'datetest')
        self.dispatcher.register_function(requestargtest, 'requestargtest')
        self.dispatcher.register_function(withoutargstest, 'withoutargstest')

    def test_dates(self):
        jsontxt = json.dumps({"method": "datetest", "id": 1, "params": []})
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue(jsondict['error'] is not None)

        class MyEncoder(json.JSONEncoder):
            def default(self, o):
                if isinstance(o, datetime):
                    return o.strftime("%Y-%m-%d %H:%M:%S")
                else:
                    return super(MyEncoder, self).default(o)
        dispatcher = JSONRPCDispatcher(MyEncoder)
        dispatcher.register_function(lambda: datetime.now(), 'datetest')
        resp = dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertTrue(isinstance(jsondict['result'], basestring))
        self.assertEqual(type(datetime.strptime(jsondict['result'], "%Y-%m-%d %H:%M:%S")), datetime)

    def test_jsonrpc_member(self):
        # Demonstrate that the jsondict contains "jsonrpc"
        d = dict()
        d['params'] = [1, 2]
        d['method'] = 'kwargstest'
        d['id'] = 1

        jsontxt = json.dumps(d)
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertEqual(jsondict['jsonrpc'], '2.0')

    def test_kwargs(self):
        d = dict()
        d['params'] = [1, 2]
        d['method'] = 'kwargstest'
        d['id'] = 1

        jsontxt = json.dumps(d)
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertFalse(jsondict['result'])

        resp = self.dispatcher.dispatch(jsontxt, c=1)
        jsondict = json.loads(resp)
        self.assertTrue(jsondict['result'])

        jsontxt = json.dumps({'params':(1,), 'method':'requestargtest'})
        resp = self.dispatcher.dispatch(jsontxt, request=True)
        jsondict = json.loads(resp)
        self.assertTrue(jsondict['result'])

        jsontxt = json.dumps({'params':[], 'method':'withoutargstest'})
        resp = self.dispatcher.dispatch(jsontxt, request='fakerequest')
        jsondict = json.loads(resp)
        self.assertTrue(jsondict['result'])

    def test_unicode(self):
        jsontxt = '{"params":[],"method":"unicode_ret","id":1}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertEqual(jsondict['id'], 1)
        self.assertEqual(jsondict['result'], u'はじめまして')

        jsontxt = '{"params":["\u306f\u3058\u3081","\u307e\u3057\u3066"],"method":"add","id":2}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertEqual(jsondict['id'], 2)
        self.assertEqual(jsondict['result'], u'はじめまして')

    def test_dispatch_success(self):
        jsontxt = '{"params":[1,2],"method":"add","id":1}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertEqual(jsondict['id'], 1)
        self.assertEqual(jsondict['result'], 3)

        jsontxt = '{"params":[5],"method":"fact","id":"hello"}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('error' not in jsondict)
        self.assertEqual(jsondict['id'], 'hello')
        self.assertEqual(jsondict['result'], 120)

    def test_method_error(self):
        jsontxt = '{"params":["a"],"method":"fact","id":"hello"}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertTrue(jsondict['error'] is not None)

    def test_dispatch_paramserror(self):
        jsontxt = '{"params":[1],"method":"add","id":"4"}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertTrue(jsondict['error'] is not None)

    def test_dispatch_nomethod(self):
        jsontxt = '{"params":[],"method":"add123","id":123}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], 123)
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 105)
        self.assertEqual(jsondict['error']['message'], 'method "add123" is not supported')

    def test_dispatch_badrequest(self):
        jsontxt = '"params":asdf[14]","method":"add","id":0}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], '')
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 101)
        self.assertEqual(jsondict['error']['message'], 'JSON decoding error')

        jsontxt = '["should", "be", "a", "Object"]'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], '')
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 102)
        self.assertEqual(jsondict['error']['message'], 'Cannot decode to a javascript Object')

        jsontxt = '{"params":"shouldbelist","method":"add","id":0}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], 0)
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 102)
        self.assertEqual(jsondict['error']['message'], 'params must be a javascript Array')

        jsontxt = '{"params":"[]","method":123,"id":42}'
        resp = self.dispatcher.dispatch(jsontxt)
        jsondict = json.loads(resp)
        self.assertTrue('result' not in jsondict)
        self.assertEqual(jsondict['id'], 42)
        self.assertTrue(isinstance(jsondict['error'], dict))
        self.assertEqual(jsondict['error']['code'], 102)
        self.assertEqual(jsondict['error']['message'], 'method must be a javascript String')

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>#Copyright ReportLab Europe Ltd. 2000-2004
#see license.txt for license details
"""Tests pageBreakBefore, frameBreakBefore, keepWithNext...
"""
__version__='''$Id$'''
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
setOutDir(__name__)
import sys, os, time
from string import split, strip, join, whitespace
from operator import truth
from types import StringType, ListType
import unittest
from reportlab.platypus.flowables import Flowable
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.frames import Frame
from reportlab.lib.randomtext import randomText, PYTHON
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate, Indenter, SimpleDocTemplate
from reportlab.platypus.paragraph import *

def myMainPageFrame(canvas, doc):
    "The page frame used for all PDF documents."

    canvas.saveState()
    canvas.setFont('Times-Roman', 12)
    pageNumber = canvas.getPageNumber()
    canvas.drawString(10*cm, cm, str(pageNumber))
    canvas.restoreState()

class MyDocTemplate(BaseDocTemplate):
    _invalidInitArgs = ('pageTemplates',)

    def __init__(self, filename, **kw):
        frame1 = Frame(2.5*cm, 15.5*cm, 6*cm, 10*cm, id='F1')
        frame2 = Frame(11.5*cm, 15.5*cm, 6*cm, 10*cm, id='F2')
        frame3 = Frame(2.5*cm, 2.5*cm, 6*cm, 10*cm, id='F3')
        frame4 = Frame(11.5*cm, 2.5*cm, 6*cm, 10*cm, id='F4')
        self.allowSplitting = 0
        self.showBoundary = 1
        BaseDocTemplate.__init__(self, filename, **kw)
        template = PageTemplate('normal', [frame1, frame2, frame3, frame4], myMainPageFrame)
        self.addPageTemplates(template)

_text1='''Furthermore, the fundamental error of regarding functional notions as
categorial delimits a general convention regarding the forms of the
grammar.  I suggested that these results would follow from the
assumption that the descriptive power of the base component may remedy
and, at the same time, eliminate a descriptive fact.  Thus a subset of
English sentences interesting on quite independent grounds raises
serious doubts about the ultimate standard that determines the accuracy
of any proposed grammar.  Of course, the natural general principle that
will subsume this case can be defined in such a way as to impose the
strong generative capacity of the theory.  By combining adjunctions and
certain deformations, the descriptive power of the base component is not
subject to the levels of acceptability from fairly high (e.g. (99a)) to
virtual gibberish (e.g. (98d)).
'''
def _test0(self):
    "This makes one long multi-page paragraph."

    # Build story.
    story = []
    a = story.append

    styleSheet = getSampleStyleSheet()
    h1 = styleSheet['Heading1']
    h1.pageBreakBefore = 1
    h1.keepWithNext = 1

    h2 = styleSheet['Heading2']
    h2.frameBreakBefore = 1
    h2.keepWithNext = 1

    h3 = styleSheet['Heading3']
    h3.backColor = colors.cyan
    h3.keepWithNext = 1

    bt = styleSheet['BodyText']
    btj = ParagraphStyle('bodyText1j',parent=bt,alignment=TA_JUSTIFY)
    btr = ParagraphStyle('bodyText1r',parent=bt,alignment=TA_RIGHT)
    btc = ParagraphStyle('bodyText1c',parent=bt,alignment=TA_CENTER)
    a(Paragraph("""
        Subsequent pages test pageBreakBefore, frameBreakBefore and
        keepTogether attributes.  Generated at %s.  The number in brackets
        at the end of each paragraph is its position in the story. (%d)""" % (
            time.ctime(time.time()), len(story)), bt))

    for i in xrange(10):
        a(Paragraph('Heading 1 always starts a new page (%d)' % len(story), h1))
        for j in xrange(3):
            a(Paragraph('Heading1 paragraphs should always'
                            'have a page break before.  Heading 2 on the other hand'
                            'should always have a FRAME break before (%d)' % len(story), bt))
            a(Paragraph('Heading 2 always starts a new frame (%d)' % len(story), h2))
            a(Paragraph('Heading1 paragraphs should always'
                            'have a page break before.  Heading 2 on the other hand'
                            'should always have a FRAME break before (%d)' % len(story), bt))
            for j in xrange(3):
                a(Paragraph(randomText(theme=PYTHON, sentences=2)+' (%d)' % len(story), bt))
                a(Paragraph('I should never be at the bottom of a frame (%d)' % len(story), h3))
                a(Paragraph(randomText(theme=PYTHON, sentences=1)+' (%d)' % len(story), bt))

    for align,bts in [('left',bt),('JUSTIFIED',btj),('RIGHT',btr),('CENTER',btc)]:
        a(Paragraph('Now we do <br/> tests(align=%s)' % align, h1))
        a(Paragraph('First off no br tags',h3))
        a(Paragraph(_text1,bts))
        a(Paragraph("<br/> after 'the' in line 4",h3))
        a(Paragraph(_text1.replace('forms of the','forms of the
',1),bts))
        a(Paragraph("2*<br/> after 'the' in line 4",h3))
        a(Paragraph(_text1.replace('forms of the','forms of the

',1),bts))
        a(Paragraph("<br/> after 'I suggested ' in line 5",h3))
        a(Paragraph(_text1.replace('I suggested ','I suggested
',1),bts))
        a(Paragraph("2*<br/> after 'I suggested ' in line 5",h3))
        a(Paragraph(_text1.replace('I suggested ','I suggested

',1),bts))
        a(Paragraph("<br/> at the end of the paragraph!",h3))
        a(Paragraph("""text one
text two
""",bts))
        a(Paragraph("Border with <br/> at the end of the paragraph!",h3))
        bt1 = ParagraphStyle('bodyText1',bts)
        bt1.borderWidth = 0.5
        bt1.borderColor = colors.toColor('red')
        bt1.backColor = colors.pink
        bt1.borderRadius = 2
        bt1.borderPadding = 3
        a(Paragraph("""text one
text two
""",bt1))
        a(Paragraph("Border no <br/> at the end of the paragraph!",h3))
        bt1 = ParagraphStyle('bodyText1',bts)
        bt1.borderWidth = 0.5
        bt1.borderColor = colors.toColor('red')
        bt1.backColor = colors.pink
        bt1.borderRadius = 2
        bt1.borderPadding = 3
        a(Paragraph("""text one
text two""",bt1))
        a(Paragraph("Different border style!",h3))
        bt2 = ParagraphStyle('bodyText1',bt1)
        bt2.borderWidth = 1.5
        bt2.borderColor = colors.toColor('blue')
        bt2.backColor = colors.gray
        bt2.borderRadius = 3
        bt2.borderPadding = 3
        a(Paragraph("""text one
text two
""",bt2))
    for i in 0, 1, 2:
        P = Paragraph("""This is a paragraph with with an incredibly
long and boring link in side of it that
contains lots and lots of stupidly boring and worthless information.
So that we can split the link and see if we get problems like Dinu's.
I hope we don't, but you never do Know.""",bt)
        a(P)

    doc = MyDocTemplate(outputfile('test_platypus_breaking.pdf'))
    doc.multiBuild(story)

class BreakingTestCase(unittest.TestCase):
    "Test multi-page splitting of paragraphs (eyeball-test)."
    def test0(self):
        _test0(self)

    def test1(self):
        '''Ilpo Nyyss\xf6nen posted this broken test'''
        normalStyle = ParagraphStyle(name = 'normal')
        keepStyle = ParagraphStyle(name = 'keep', keepWithNext = True)
        content = [
            Paragraph("line 1", keepStyle),
            Indenter(left = 1 * cm),
            Paragraph("line 2", normalStyle),
            ]
        doc = SimpleDocTemplate(outputfile('test_platypus_breaking1.pdf'))
        doc.build(content)

    def test2(self):
        sty = ParagraphStyle(name = 'normal')
        sty.fontName = 'Times-Roman'
        sty.fontSize = 10
        sty.leading = 12

        p = Paragraph('one two three',sty)
        p.wrap(20,36)
        self.assertEqual(len(p.split(20,24)),2) #widows allowed
        self.assertEqual(len(p.split(20,16)),0) #orphans disallowed
        p.allowWidows = 0
        self.assertEqual(len(p.split(20,24)),0) #widows disallowed
        p.allowOrphans = 1
        self.assertEqual(len(p.split(20,16)),2) #orphans allowed

    def test3(self):
        from reportlab.pdfgen.canvas import Canvas

        aW=307
        styleSheet = getSampleStyleSheet()
        bt = styleSheet['BodyText']
        btj = ParagraphStyle('bodyText1j',parent=bt,alignment=TA_JUSTIFY)
        p=Paragraph("""Subsequent pages test pageBreakBefore, frameBreakBefore and
                keepTogether attributes.  Generated at 1111. The number in brackets
                at the end of each paragraph is its position in the story. llllllllllllllllllllllllll
                bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc ddddddddddddddddddddd eeeeyyy""",btj)

        w,h=p.wrap(aW,1000)
        canv=Canvas('test_platypus_paragraph_just.pdf',pagesize=(aW,h))
        i=len(canv._code)
        p.drawOn(canv,0,0)
        ParaCode=canv._code[i:]
        canv.saveState()
        canv.setLineWidth(0)
        canv.setStrokeColorRGB(1,0,0)
        canv.rect(0,0,aW,h)
        canv.restoreState()
        canv.showPage()
        canv.save()
        from reportlab import rl_config
        x = rl_config.paraFontSizeHeightOffset and '50' or '53.17'
        good = ['q', '1 0 0 1 0 0 cm', 'q', 'BT 1 0 0 1 0 '+x+' Tm 10.082 Tw 12 TL /F2 10 Tf 0 0 0 rg (Subsequent pages test pageBreakBefore, frameBreakBefore and) Tj T* 0 Tw .985 Tw (keepTogether attributes. Generated at 1111. The number in brackets at the) Tj T* 0 Tw 3.78 Tw (end of each paragraph is its position in the story. llllllllllllllllllllllllll) Tj T* 0 Tw 92.38 Tw (bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc) Tj T* 0 Tw (ddddddddddddddddddddd eeeeyyy) Tj T* ET', 'Q', 'Q']
        ok= ParaCode==good
        assert ok, "\nParaCode=%r\nexpected=%r" % (ParaCode,good)

def makeSuite():
    return makeSuiteForClasses(BreakingTestCase)

#noruntests
if __name__ == "__main__": #NORUNTESTS
    if 'debug' in sys.argv:
        _test0(None)
    else:
        unittest.TextTestRunner().run(makeSuite())
        printLocation()
<commit_msg>reportlab: new base font mechanism more fully applied<commit_after>#Copyright ReportLab Europe Ltd. 2000-2004
#see license.txt for license details
"""Tests pageBreakBefore, frameBreakBefore, keepWithNext...
"""
__version__='''$Id$'''
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
setOutDir(__name__)
import sys, os, time
from string import split, strip, join, whitespace
from operator import truth
from types import StringType, ListType
import unittest
from reportlab.platypus.flowables import Flowable
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.frames import Frame
from reportlab.lib.randomtext import randomText, PYTHON
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate, Indenter, SimpleDocTemplate
from reportlab.platypus.paragraph import *

def myMainPageFrame(canvas, doc):
    "The page frame used for all PDF documents."

    canvas.saveState()
    canvas.setFont('Times-Roman', 12)
    pageNumber = canvas.getPageNumber()
    canvas.drawString(10*cm, cm, str(pageNumber))
    canvas.restoreState()

class MyDocTemplate(BaseDocTemplate):
    _invalidInitArgs = ('pageTemplates',)

    def __init__(self, filename, **kw):
        frame1 = Frame(2.5*cm, 15.5*cm, 6*cm, 10*cm, id='F1')
        frame2 = Frame(11.5*cm, 15.5*cm, 6*cm, 10*cm, id='F2')
        frame3 = Frame(2.5*cm, 2.5*cm, 6*cm, 10*cm, id='F3')
        frame4 = Frame(11.5*cm, 2.5*cm, 6*cm, 10*cm, id='F4')
        self.allowSplitting = 0
        self.showBoundary = 1
        BaseDocTemplate.__init__(self, filename, **kw)
        template = PageTemplate('normal', [frame1, frame2, frame3, frame4], myMainPageFrame)
        self.addPageTemplates(template)

_text1='''Furthermore, the fundamental error of regarding functional notions as
categorial delimits a general convention regarding the forms of the
grammar.  I suggested that these results would follow from the
assumption that the descriptive power of the base component may remedy
and, at the same time, eliminate a descriptive fact.  Thus a subset of
English sentences interesting on quite independent grounds raises
serious doubts about the ultimate standard that determines the accuracy
of any proposed grammar.  Of course, the natural general principle that
will subsume this case can be defined in such a way as to impose the
strong generative capacity of the theory.  By combining adjunctions and
certain deformations, the descriptive power of the base component is not
subject to the levels of acceptability from fairly high (e.g. (99a)) to
virtual gibberish (e.g. (98d)).
'''
def _test0(self):
    "This makes one long multi-page paragraph."

    # Build story.
    story = []
    a = story.append

    styleSheet = getSampleStyleSheet()
    h1 = styleSheet['Heading1']
    h1.pageBreakBefore = 1
    h1.keepWithNext = 1

    h2 = styleSheet['Heading2']
    h2.frameBreakBefore = 1
    h2.keepWithNext = 1

    h3 = styleSheet['Heading3']
    h3.backColor = colors.cyan
    h3.keepWithNext = 1

    bt = styleSheet['BodyText']
    btj = ParagraphStyle('bodyText1j',parent=bt,alignment=TA_JUSTIFY)
    btr = ParagraphStyle('bodyText1r',parent=bt,alignment=TA_RIGHT)
    btc = ParagraphStyle('bodyText1c',parent=bt,alignment=TA_CENTER)
    a(Paragraph("""
        Subsequent pages test pageBreakBefore, frameBreakBefore and
        keepTogether attributes.  Generated at %s.  The number in brackets
        at the end of each paragraph is its position in the story. (%d)""" % (
            time.ctime(time.time()), len(story)), bt))

    for i in xrange(10):
        a(Paragraph('Heading 1 always starts a new page (%d)' % len(story), h1))
        for j in xrange(3):
            a(Paragraph('Heading1 paragraphs should always'
                            'have a page break before.  Heading 2 on the other hand'
                            'should always have a FRAME break before (%d)' % len(story), bt))
            a(Paragraph('Heading 2 always starts a new frame (%d)' % len(story), h2))
            a(Paragraph('Heading1 paragraphs should always'
                            'have a page break before.  Heading 2 on the other hand'
                            'should always have a FRAME break before (%d)' % len(story), bt))
            for j in xrange(3):
                a(Paragraph(randomText(theme=PYTHON, sentences=2)+' (%d)' % len(story), bt))
                a(Paragraph('I should never be at the bottom of a frame (%d)' % len(story), h3))
                a(Paragraph(randomText(theme=PYTHON, sentences=1)+' (%d)' % len(story), bt))

    for align,bts in [('left',bt),('JUSTIFIED',btj),('RIGHT',btr),('CENTER',btc)]:
        a(Paragraph('Now we do <br/> tests(align=%s)' % align, h1))
        a(Paragraph('First off no br tags',h3))
        a(Paragraph(_text1,bts))
        a(Paragraph("<br/> after 'the' in line 4",h3))
        a(Paragraph(_text1.replace('forms of the','forms of the
',1),bts))
        a(Paragraph("2*<br/> after 'the' in line 4",h3))
        a(Paragraph(_text1.replace('forms of the','forms of the

',1),bts))
        a(Paragraph("<br/> after 'I suggested ' in line 5",h3))
        a(Paragraph(_text1.replace('I suggested ','I suggested
',1),bts))
        a(Paragraph("2*<br/> after 'I suggested ' in line 5",h3))
        a(Paragraph(_text1.replace('I suggested ','I suggested

',1),bts))
        a(Paragraph("<br/> at the end of the paragraph!",h3))
        a(Paragraph("""text one
text two
""",bts))
        a(Paragraph("Border with <br/> at the end of the paragraph!",h3))
        bt1 = ParagraphStyle('bodyText1',bts)
        bt1.borderWidth = 0.5
        bt1.borderColor = colors.toColor('red')
        bt1.backColor = colors.pink
        bt1.borderRadius = 2
        bt1.borderPadding = 3
        a(Paragraph("""text one
text two
""",bt1))
        a(Paragraph("Border no <br/> at the end of the paragraph!",h3))
        bt1 = ParagraphStyle('bodyText1',bts)
        bt1.borderWidth = 0.5
        bt1.borderColor = colors.toColor('red')
        bt1.backColor = colors.pink
        bt1.borderRadius = 2
        bt1.borderPadding = 3
        a(Paragraph("""text one
text two""",bt1))
        a(Paragraph("Different border style!",h3))
        bt2 = ParagraphStyle('bodyText1',bt1)
        bt2.borderWidth = 1.5
        bt2.borderColor = colors.toColor('blue')
        bt2.backColor = colors.gray
        bt2.borderRadius = 3
        bt2.borderPadding = 3
        a(Paragraph("""text one
text two
""",bt2))
    for i in 0, 1, 2:
        P = Paragraph("""This is a paragraph with with an incredibly
long and boring link in side of it that
contains lots and lots of stupidly boring and worthless information.
So that we can split the link and see if we get problems like Dinu's.
I hope we don't, but you never do Know.""",bt)
        a(P)

    doc = MyDocTemplate(outputfile('test_platypus_breaking.pdf'))
    doc.multiBuild(story)

class BreakingTestCase(unittest.TestCase):
    "Test multi-page splitting of paragraphs (eyeball-test)."
    def test0(self):
        _test0(self)

    def test1(self):
        '''Ilpo Nyyss\xf6nen posted this broken test'''
        normalStyle = ParagraphStyle(name = 'normal')
        keepStyle = ParagraphStyle(name = 'keep', keepWithNext = True)
        content = [
            Paragraph("line 1", keepStyle),
            Indenter(left = 1 * cm),
            Paragraph("line 2", normalStyle),
            ]
        doc = SimpleDocTemplate(outputfile('test_platypus_breaking1.pdf'))
        doc.build(content)

    def test2(self):
        sty = ParagraphStyle(name = 'normal')
        sty.fontName = 'Times-Roman'
        sty.fontSize = 10
        sty.leading = 12

        p = Paragraph('one two three',sty)
        p.wrap(20,36)
        self.assertEqual(len(p.split(20,24)),2) #widows allowed
        self.assertEqual(len(p.split(20,16)),0) #orphans disallowed
        p.allowWidows = 0
        self.assertEqual(len(p.split(20,24)),0) #widows disallowed
        p.allowOrphans = 1
        self.assertEqual(len(p.split(20,16)),2) #orphans allowed

    def test3(self):
        from reportlab.pdfgen.canvas import Canvas

        aW=307
        styleSheet = getSampleStyleSheet()
        bt = styleSheet['BodyText']
        btj = ParagraphStyle('bodyText1j',parent=bt,alignment=TA_JUSTIFY)
        p=Paragraph("""Subsequent pages test pageBreakBefore, frameBreakBefore and
                keepTogether attributes.  Generated at 1111. The number in brackets
                at the end of each paragraph is its position in the story. llllllllllllllllllllllllll
                bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc ddddddddddddddddddddd eeeeyyy""",btj)

        w,h=p.wrap(aW,1000)
        canv=Canvas('test_platypus_paragraph_just.pdf',pagesize=(aW,h))
        i=len(canv._code)
        p.drawOn(canv,0,0)
        ParaCode=canv._code[i:]
        canv.saveState()
        canv.setLineWidth(0)
        canv.setStrokeColorRGB(1,0,0)
        canv.rect(0,0,aW,h)
        canv.restoreState()
        canv.showPage()
        canv.save()
        from reportlab import rl_config
        x = rl_config.paraFontSizeHeightOffset and '50' or '53.17'
        good = ['q', '1 0 0 1 0 0 cm', 'q', 'BT 1 0 0 1 0 '+x+' Tm 3.59 Tw 12 TL /F1 10 Tf 0 0 0 rg (Subsequent pages test pageBreakBefore, frameBreakBefore and) Tj T* 0 Tw .23 Tw (keepTogether attributes. Generated at 1111. The number in brackets) Tj T* 0 Tw .299167 Tw (at the end of each paragraph is its position in the story. llllllllllllllllllllllllll) Tj T* 0 Tw 66.9 Tw (bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc) Tj T* 0 Tw (ddddddddddddddddddddd eeeeyyy) Tj T* ET', 'Q', 'Q']
        ok= ParaCode==good
        assert ok, "\nParaCode=%r\nexpected=%r" % (ParaCode,good)

def makeSuite():
    return makeSuiteForClasses(BreakingTestCase)

#noruntests
if __name__ == "__main__": #NORUNTESTS
    if 'debug' in sys.argv:
        _test0(None)
    else:
        unittest.TextTestRunner().run(makeSuite())
        printLocation()
<|endoftext|>


<commit_before>from typing import Union, Tuple, Any, List

import pytest

from runtime_typecheck.runtime_typecheck import check_type, check_args, DetailedTypeError

def test_any():
    assert check_type(3, Any)
    assert check_type([5, "hi"], Any)

def test_numbers():
    assert check_type(3, int)
    assert not check_type(3, float)

def test_unions():
    assert check_type(3, Union[int, str])
    assert check_type("hello", Union[int, str])
    assert not check_type(4.78, Union[int, str])

def test_tuples():
    assert check_type((1, 67), Tuple[int, int])
    assert not check_type((1, "new york"), Tuple[int, int])
    # NOTE not a tuple, currently a list is accepted instead of a tuple and vice-versa
    assert check_type([1, "new york"], Tuple[int, str])
    assert check_type((1, 67, "Amsterdam"), Tuple[int, int, str])
    assert not check_type(("Amsterdam", 1, 67), Tuple[int, int, str])

def test_lists():
    assert check_type([1, 27, 33, 1956], List[int])
    assert not check_type([1.11, 27, 33, 1956], List[int])
    assert not check_type([1, 27, 33, 1956, "h", 42], List[int])

def test_nested_types():
    assert check_type([1, 27, 33, 1956], List[Union[str, int]])
    assert check_type([(12, "Texas"), (-5, "Particle")], List[Tuple[int, str]])
    assert not check_type([(1.9, "Texas"), (-5, "Particle")],
                          List[Tuple[int, str]])
    assert not check_type([1.11, 27, 33, 1956], List[Tuple[int, str]])

@check_args
def dummy_fun(a: int = 0, b: str = '', c: Tuple[int, str] = (0, '')) -> int:
    return a + len(b) + c[0] + len(c[1]) + 7

@check_args
def dummy_fun_with_nonoptional(x: int, y: str = '', z: Tuple[int, str] = (0, '')) -> int:
    return x + len(y) + z[0] + len(z[1]) + 7

def test_args():
    assert dummy_fun() == 7
    assert dummy_fun(10, 'antani', (1, '0')) == 25
    assert dummy_fun(a=10, b='antani', c=(1, '0')) == 25

def test_dict():
    param_dic = {'y': 'antani', 'z': (1, '0')}
    assert dummy_fun_with_nonoptional(10, **param_dic) == 25

def test_raises():
    with pytest.raises(DetailedTypeError):
        dummy_fun('1')
    with pytest.raises(DetailedTypeError):
        dummy_fun(1, 1)
    with pytest.raises(DetailedTypeError):
        dummy_fun(1, '1', ('1', '0'))
    with pytest.raises(TypeError):
        dummy_fun(1, '1', (1, '0'), extra=42)
    with pytest.raises(DetailedTypeError):
        dummy_fun(a=1, b='1', c=('1', '0'))
<commit_msg>PEP 8<commit_after>from typing import Union, Tuple, Any, List

import pytest

from runtime_typecheck.runtime_typecheck import check_type, check_args, DetailedTypeError

def test_any():
    assert check_type(3, Any)
    assert check_type([5, "hi"], Any)

def test_numbers():
    assert check_type(3, int)
    assert not check_type(3, float)

def test_unions():
    assert check_type(3, Union[int, str])
    assert check_type("hello", Union[int, str])
    assert not check_type(4.78, Union[int, str])

def test_tuples():
    assert check_type((1, 67), Tuple[int, int])
    assert not check_type((1, "new york"), Tuple[int, int])
    # NOTE not a tuple, currently a list is accepted instead of a tuple and vice-versa
    assert check_type([1, "new york"], Tuple[int, str])
    assert check_type((1, 67, "Amsterdam"), Tuple[int, int, str])
    assert not check_type(("Amsterdam", 1, 67), Tuple[int, int, str])

def test_lists():
    assert check_type([1, 27, 33, 1956], List[int])
    assert not check_type([1.11, 27, 33, 1956], List[int])
    assert not check_type([1, 27, 33, 1956, "h", 42], List[int])

def test_nested_types():
    assert check_type([1, 27, 33, 1956], List[Union[str, int]])
    assert check_type([(12, "Texas"), (-5, "Particle")], List[Tuple[int, str]])
    assert not check_type([(1.9, "Texas"), (-5, "Particle")],
                          List[Tuple[int, str]])
    assert not check_type([1.11, 27, 33, 1956], List[Tuple[int, str]])

@check_args
def dummy_fun(a: int = 0, b: str = '', c: Tuple[int, str] = (0, '')) -> int:
    return a + len(b) + c[0] + len(c[1]) + 7

@check_args
def dummy_fun_with_nonoptional(x: int, y: str = '', z: Tuple[int, str] = (0, '')) -> int:
    return x + len(y) + z[0] + len(z[1]) + 7

def test_args():
    assert dummy_fun() == 7
    assert dummy_fun(10, 'antani', (1, '0')) == 25
    assert dummy_fun(a=10, b='antani', c=(1, '0')) == 25

def test_dict():
    param_dic = {'y': 'antani', 'z': (1, '0')}
    assert dummy_fun_with_nonoptional(10, **param_dic) == 25

def test_raises():
    with pytest.raises(DetailedTypeError):
        dummy_fun('1')
    with pytest.raises(DetailedTypeError):
        dummy_fun(1, 1)
    with pytest.raises(DetailedTypeError):
        dummy_fun(1, '1', ('1', '0'))
    with pytest.raises(TypeError):
        dummy_fun(1, '1', (1, '0'), extra=42)
    with pytest.raises(DetailedTypeError):
        dummy_fun(a=1, b='1', c=('1', '0'))
<|endoftext|>


<commit_before># coding=utf-8
# Copyright 2018 The Google AI Language Team Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os
import unittest

from transformers.file_utils import cached_property
from transformers.testing_utils import slow
from transformers.tokenization_fsmt import VOCAB_FILES_NAMES, FSMTTokenizer

from .test_tokenization_common import TokenizerTesterMixin

# using a different tiny model than the one used for default params defined in init to ensure proper testing
FSMT_TINY2 = "stas/tiny-wmt19-en-ru"

class FSMTTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
    tokenizer_class = FSMTTokenizer

    def setUp(self):
        super().setUp()

        # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
        vocab = [
            "l",
            "o",
            "w",
            "e",
            "r",
            "s",
            "t",
            "i",
            "d",
            "n",
            "w</w>",
            "r</w>",
            "t</w>",
            "lo",
            "low",
            "er</w>",
            "low</w>",
            "lowest</w>",
            "newer</w>",
            "wider</w>",
            "<unk>",
        ]
        vocab_tokens = dict(zip(vocab, range(len(vocab))))
        merges = ["l o 123", "lo w 1456", "e r</w> 1789", ""]

        self.langs = ["en", "ru"]
        config = {
            "langs": self.langs,
            "src_vocab_size": 10,
            "tgt_vocab_size": 20,
        }

        self.src_vocab_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["src_vocab_file"])
        self.tgt_vocab_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["tgt_vocab_file"])
        config_file = os.path.join(self.tmpdirname, "tokenizer_config.json")
        self.merges_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["merges_file"])
        with open(self.src_vocab_file, "w") as fp:
            fp.write(json.dumps(vocab_tokens))
        with open(self.tgt_vocab_file, "w") as fp:
            fp.write(json.dumps(vocab_tokens))
        with open(self.merges_file, "w") as fp:
            fp.write("\n".join(merges))
        with open(config_file, "w") as fp:
            fp.write(json.dumps(config))

    @cached_property
    def tokenizer_ru_en(self):
        return FSMTTokenizer.from_pretrained("facebook/wmt19-ru-en")

    @cached_property
    def tokenizer_en_ru(self):
        return FSMTTokenizer.from_pretrained("facebook/wmt19-en-ru")

    def test_online_tokenizer_config(self):
        """this just tests that the online tokenizer files get correctly fetched and
        loaded via its tokenizer_config.json and it's not slow so it's run by normal CI
        """
        tokenizer = FSMTTokenizer.from_pretrained(FSMT_TINY2)
        self.assertListEqual([tokenizer.src_lang, tokenizer.tgt_lang], ["en", "ru"])
        self.assertEqual(tokenizer.src_vocab_size, 21)
        self.assertEqual(tokenizer.tgt_vocab_size, 21)

    def test_full_tokenizer(self):
        """ Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt """
        tokenizer = FSMTTokenizer(self.langs, self.src_vocab_file, self.tgt_vocab_file, self.merges_file)

        text = "lower"
        bpe_tokens = ["low", "er</w>"]
        tokens = tokenizer.tokenize(text)
        self.assertListEqual(tokens, bpe_tokens)

        input_tokens = tokens + ["<unk>"]
        input_bpe_tokens = [14, 15, 20]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(input_tokens), input_bpe_tokens)

    @slow
    def test_sequence_builders(self):
        tokenizer = self.tokenizer_ru_en

        text = tokenizer.encode("sequence builders", add_special_tokens=False)
        text_2 = tokenizer.encode("multi-sequence build", add_special_tokens=False)

        encoded_sentence = tokenizer.build_inputs_with_special_tokens(text)
        encoded_pair = tokenizer.build_inputs_with_special_tokens(text, text_2)

        assert encoded_sentence == text + [2]
        assert encoded_pair == text + [2] + text_2 + [2]

    @slow
    def test_match_encode_decode(self):
        tokenizer_enc = self.tokenizer_en_ru
        tokenizer_dec = self.tokenizer_ru_en

        targets = [
            [
                "Here's a little song I wrote. Don't worry, be happy.",
                [2470, 39, 11, 2349, 7222, 70, 5979, 7, 8450, 1050, 13160, 5, 26, 6445, 7, 2],
            ],
            ["This is it. No more. I'm done!", [132, 21, 37, 7, 1434, 86, 7, 70, 6476, 1305, 427, 2]],
        ]

        # if data needs to be recreated or added, run:
        # import torch
        # model = torch.hub.load("pytorch/fairseq", "transformer.wmt19.en-ru", checkpoint_file="model4.pt", tokenizer="moses", bpe="fastbpe")
        # for src_text, _ in targets: print(f"""[\n"{src_text}",\n {model.encode(src_text).tolist()}\n],""")

        for src_text, tgt_input_ids in targets:
            input_ids = tokenizer_enc.encode(src_text, return_tensors="pt")[0].tolist()
            self.assertListEqual(input_ids, tgt_input_ids)

            # and decode backward, using the reversed languages model
            decoded_text = tokenizer_dec.decode(input_ids, skip_special_tokens=True)
            self.assertEqual(decoded_text, src_text)

    @unittest.skip("FSMTConfig.__init__  requires non-optional args")
    def test_torch_encode_plus_sent_to_model(self):
        pass

    @unittest.skip("FSMTConfig.__init__  requires non-optional args")
    def test_np_encode_plus_sent_to_model(self):
        pass
<commit_msg>fsmt slow test uses lists (#8031)<commit_after># coding=utf-8
# Copyright 2018 The Google AI Language Team Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os
import unittest

from transformers.file_utils import cached_property
from transformers.testing_utils import slow
from transformers.tokenization_fsmt import VOCAB_FILES_NAMES, FSMTTokenizer

from .test_tokenization_common import TokenizerTesterMixin

# using a different tiny model than the one used for default params defined in init to ensure proper testing
FSMT_TINY2 = "stas/tiny-wmt19-en-ru"

class FSMTTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
    tokenizer_class = FSMTTokenizer

    def setUp(self):
        super().setUp()

        # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
        vocab = [
            "l",
            "o",
            "w",
            "e",
            "r",
            "s",
            "t",
            "i",
            "d",
            "n",
            "w</w>",
            "r</w>",
            "t</w>",
            "lo",
            "low",
            "er</w>",
            "low</w>",
            "lowest</w>",
            "newer</w>",
            "wider</w>",
            "<unk>",
        ]
        vocab_tokens = dict(zip(vocab, range(len(vocab))))
        merges = ["l o 123", "lo w 1456", "e r</w> 1789", ""]

        self.langs = ["en", "ru"]
        config = {
            "langs": self.langs,
            "src_vocab_size": 10,
            "tgt_vocab_size": 20,
        }

        self.src_vocab_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["src_vocab_file"])
        self.tgt_vocab_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["tgt_vocab_file"])
        config_file = os.path.join(self.tmpdirname, "tokenizer_config.json")
        self.merges_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["merges_file"])
        with open(self.src_vocab_file, "w") as fp:
            fp.write(json.dumps(vocab_tokens))
        with open(self.tgt_vocab_file, "w") as fp:
            fp.write(json.dumps(vocab_tokens))
        with open(self.merges_file, "w") as fp:
            fp.write("\n".join(merges))
        with open(config_file, "w") as fp:
            fp.write(json.dumps(config))

    @cached_property
    def tokenizer_ru_en(self):
        return FSMTTokenizer.from_pretrained("facebook/wmt19-ru-en")

    @cached_property
    def tokenizer_en_ru(self):
        return FSMTTokenizer.from_pretrained("facebook/wmt19-en-ru")

    def test_online_tokenizer_config(self):
        """this just tests that the online tokenizer files get correctly fetched and
        loaded via its tokenizer_config.json and it's not slow so it's run by normal CI
        """
        tokenizer = FSMTTokenizer.from_pretrained(FSMT_TINY2)
        self.assertListEqual([tokenizer.src_lang, tokenizer.tgt_lang], ["en", "ru"])
        self.assertEqual(tokenizer.src_vocab_size, 21)
        self.assertEqual(tokenizer.tgt_vocab_size, 21)

    def test_full_tokenizer(self):
        """ Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt """
        tokenizer = FSMTTokenizer(self.langs, self.src_vocab_file, self.tgt_vocab_file, self.merges_file)

        text = "lower"
        bpe_tokens = ["low", "er</w>"]
        tokens = tokenizer.tokenize(text)
        self.assertListEqual(tokens, bpe_tokens)

        input_tokens = tokens + ["<unk>"]
        input_bpe_tokens = [14, 15, 20]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(input_tokens), input_bpe_tokens)

    @slow
    def test_sequence_builders(self):
        tokenizer = self.tokenizer_ru_en

        text = tokenizer.encode("sequence builders", add_special_tokens=False)
        text_2 = tokenizer.encode("multi-sequence build", add_special_tokens=False)

        encoded_sentence = tokenizer.build_inputs_with_special_tokens(text)
        encoded_pair = tokenizer.build_inputs_with_special_tokens(text, text_2)

        assert encoded_sentence == text + [2]
        assert encoded_pair == text + [2] + text_2 + [2]

    @slow
    def test_match_encode_decode(self):
        tokenizer_enc = self.tokenizer_en_ru
        tokenizer_dec = self.tokenizer_ru_en

        targets = [
            [
                "Here's a little song I wrote. Don't worry, be happy.",
                [2470, 39, 11, 2349, 7222, 70, 5979, 7, 8450, 1050, 13160, 5, 26, 6445, 7, 2],
            ],
            ["This is it. No more. I'm done!", [132, 21, 37, 7, 1434, 86, 7, 70, 6476, 1305, 427, 2]],
        ]

        # if data needs to be recreated or added, run:
        # import torch
        # model = torch.hub.load("pytorch/fairseq", "transformer.wmt19.en-ru", checkpoint_file="model4.pt", tokenizer="moses", bpe="fastbpe")
        # for src_text, _ in targets: print(f"""[\n"{src_text}",\n {model.encode(src_text).tolist()}\n],""")

        for src_text, tgt_input_ids in targets:
            encoded_ids = tokenizer_enc.encode(src_text, return_tensors=None)
            self.assertListEqual(encoded_ids, tgt_input_ids)

            # and decode backward, using the reversed languages model
            decoded_text = tokenizer_dec.decode(encoded_ids, skip_special_tokens=True)
            self.assertEqual(decoded_text, src_text)

    @unittest.skip("FSMTConfig.__init__  requires non-optional args")
    def test_torch_encode_plus_sent_to_model(self):
        pass

    @unittest.skip("FSMTConfig.__init__  requires non-optional args")
    def test_np_encode_plus_sent_to_model(self):
        pass
<|endoftext|>


<commit_before>'''This script crawls a specified reddit and posts comments as the world
   renowned magician Tony Wonder'''

import sys
import os
import json
import praw
import pytest
import inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir)

from wonderbot import WonderBot

## Tests ##

def test_submission_needs_reply(login_file_name, subreddit_name):
    reddit_bot = WonderBot(login_file=login_file_name, subreddit_name=subreddit_name)
    assert reddit_bot.submission_needs_reply("I wonder if this works")
    assert reddit_bot.submission_needs_reply("I WONDER if this works")
    assert reddit_bot.submission_needs_reply("I wonder wonder wonder wonder if this works")
    assert reddit_bot.submission_needs_reply("I *wonder* if this works")
    assert reddit_bot.submission_needs_reply("I **wonder** if this works")
    assert reddit_bot.submission_needs_reply("I ~~wonder~~ if this works")
    assert reddit_bot.submission_needs_reply("*wonder* if this works")
    assert reddit_bot.submission_needs_reply("I this works **wonder**")
    assert reddit_bot.submission_needs_reply("~~wonder~~ if this works")
    assert reddit_bot.submission_needs_reply("Mr.Wonder is my hero")
    assert reddit_bot.submission_needs_reply("I dig Mr.Wonder, he is my hero")
    assert reddit_bot.submission_needs_reply("I dig Mr.Wonder he is my hero")
    assert reddit_bot.submission_needs_reply("I dig Wonder's, he is my hero")
    assert reddit_bot.submission_needs_reply("I dig Wonder's he is my hero")
    assert reddit_bot.submission_needs_reply("I dig Mr.Wonder, he wonder is my hero")
    assert reddit_bot.submission_needs_reply("I dig Mr.Wonder he is my WONDER hero")
    assert reddit_bot.submission_needs_reply("I dig Wonder's, he is my wonder hero")
    assert reddit_bot.submission_needs_reply("I dig Wonder's he is my wonder hero")
    assert reddit_bot.submission_needs_reply("He is amazing, he can work wonders")
<commit_msg>Garbage cleanup, beep beep :truck:<commit_after>import sys
import os
import json
import praw
import pytest
import inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir)

from wonderbot import WonderBot

## Tests ##

def test_submission_needs_reply(login_file_name, subreddit_name):
    reddit_bot = WonderBot(login_file=login_file_name, subreddit_name=subreddit_name)
    assert reddit_bot.submission_needs_reply("I wonder if this works")
    assert reddit_bot.submission_needs_reply("I WONDER if this works")
    assert reddit_bot.submission_needs_reply("I wonder wonder wonder wonder if this works")
    assert reddit_bot.submission_needs_reply("I *wonder* if this works")
    assert reddit_bot.submission_needs_reply("I **wonder** if this works")
    assert reddit_bot.submission_needs_reply("I ~~wonder~~ if this works")
    assert reddit_bot.submission_needs_reply("*wonder* if this works")
    assert reddit_bot.submission_needs_reply("I this works **wonder**")
    assert reddit_bot.submission_needs_reply("~~wonder~~ if this works")
    assert reddit_bot.submission_needs_reply("Mr.Wonder is my hero")
    assert reddit_bot.submission_needs_reply("I dig Mr.Wonder, he is my hero")
    assert reddit_bot.submission_needs_reply("I dig Mr.Wonder he is my hero")
    assert reddit_bot.submission_needs_reply("I dig Wonder's, he is my hero")
    assert reddit_bot.submission_needs_reply("I dig Wonder's he is my hero")
    assert reddit_bot.submission_needs_reply("I dig Mr.Wonder, he wonder is my hero")
    assert reddit_bot.submission_needs_reply("I dig Mr.Wonder he is my WONDER hero")
    assert reddit_bot.submission_needs_reply("I dig Wonder's, he is my wonder hero")
    assert reddit_bot.submission_needs_reply("I dig Wonder's he is my wonder hero")
    assert reddit_bot.submission_needs_reply("He is amazing, he can work wonders")
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from copy import deepcopy

from django.conf import settings
from django.test import TestCase

from channels.backends.yo import YoChannel
from channels.exceptions import HttpError, ImproperlyConfigured

config = settings.CHANNELS["CHANNELS"]["channels.backends.yo.YoChannel"]

class YoChannelTestCase(TestCase):
    def setUp(self):
        self.channel = YoChannel(**config)

    def test_init(self):
        with self.assertRaises(TypeError):
            YoChannel(**{})

    def test_send(self):
        self.channel.send("Just Yo")

        self.channel.send("Yo Link", options={
            "yo": {"link": "http://docs.justyo.co/v1.0/docs/yo"}})

        self.channel.send("Yo Location", options={
            "yo": {"location": "35.0261581,135.7818476"}})

    def test_send_fail(self):
        conf = deepcopy(config)
        conf["api_token"] = "api_token"
        channel = YoChannel(**conf)

        with self.assertRaises(HttpError):
            channel.send("Yo", fail_silently=False)

        channel.send("Yo", fail_silently=True)
<commit_msg>Use mock in YoChannelTestCase<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from unittest import mock

from django.conf import settings
from django.test import TestCase
import requests

from channels.backends.yo import YoChannel
from channels.exceptions import HttpError

config = settings.CHANNELS["CHANNELS"]["channels.backends.yo.YoChannel"]

class YoChannelTestCase(TestCase):
    def setUp(self):
        self.channel = YoChannel(**config)

    def test_init(self):
        with self.assertRaises(TypeError):
            YoChannel(**{})

    @mock.patch("requests.post")
    def test_send(self, m):
        response = requests.Response()
        response.status_code = requests.codes.ok
        m.return_value = response

        self.channel.send("Just Yo")

        self.channel.send("Yo Link", options={
            "yo": {"link": "http://docs.justyo.co/v1.0/docs/yo"}})

        self.channel.send("Yo Location", options={
            "yo": {"location": "35.0261581,135.7818476"}})

    @mock.patch("requests.post")
    def test_send_fail(self, m):
        response = requests.Response()
        response.status_code = requests.codes.forbidden
        m.return_value = response

        with self.assertRaises(HttpError):
            self.channel.send("Yo", fail_silently=False)

        self.channel.send("Yo", fail_silently=True)
<|endoftext|>


<commit_before>
"""
Test text channel.
"""

import dbus

from twisted.words.xish import domish

from gabbletest import exec_test

def test(q, bus, conn, stream):
    conn.Connect()
    q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])

    # <message type="chat"><body>hello</body</message>
    m = domish.Element(('', 'message'))
    m['from'] = 'foo@bar.com/Pidgin'
    m['type'] = 'chat'
    m.addElement('body', content='hello')
    stream.send(m)

    event = q.expect('dbus-signal', signal='NewChannel')
    text_chan = bus.get_object(conn._named_service, event.args[0])
    assert event.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text'
    # check that handle type == contact handle
    assert event.args[2] == 1
    jid = conn.InspectHandles(1, [event.args[3]])[0]
    assert jid == 'foo@bar.com'

    # Exercise basic Channel Properties from spec 0.17.7
    channel_props = text_chan.GetAll(
            'org.freedesktop.Telepathy.Channel',
            dbus_interface='org.freedesktop.DBus.Properties')
    assert channel_props.get('TargetHandle') == event.args[3],\
            (channel_props.get('TargetHandle'), event.args[3])
    assert channel_props.get('TargetHandleType') == 1,\
            channel_props.get('TargetHandleType')
    assert channel_props.get('ChannelType') == \
            'org.freedesktop.Telepathy.Channel.Type.Text',\
            channel_props.get('ChannelType')
    assert 'org.freedesktop.Telepathy.Channel.Interface.ChatState' in \
            channel_props.get('Interfaces', ()), \
            channel_props.get('Interfaces')
    assert 'org.freedesktop.Telepathy.Channel.FUTURE' in \
            channel_props.get('Interfaces', ()), \
            channel_props.get('Interfaces')

    future_props = text_chan.GetAll(
            'org.freedesktop.Telepathy.Channel.FUTURE',
            dbus_interface='org.freedesktop.DBus.Properties')
    assert future_props['TargetID'] == jid,\
            (future_props['TargetID'], jid)

    event = q.expect('dbus-signal', signal='Received')

    # message type: normal
    assert event.args[3] == 0
    # flags: none
    assert event.args[4] == 0
    # body
    assert event.args[5] == 'hello'

    dbus.Interface(text_chan,
        u'org.freedesktop.Telepathy.Channel.Type.Text').Send(0, 'goodbye')

    event = q.expect('stream-message')

    elem = event.stanza
    assert elem.name == 'message'
    assert elem['type'] == 'chat'
    body = list(event.stanza.elements())[0]
    assert body.name == 'body'
    assert body.children[0] == u'goodbye'

    conn.Disconnect()
    q.expect('dbus-signal', signal='StatusChanged', args=[2, 1])

if __name__ == '__main__':
    exec_test(test)

<commit_msg>test-text.py: assert that suppress_handler is False on incoming channels<commit_after>
"""
Test text channel.
"""

import dbus

from twisted.words.xish import domish

from gabbletest import exec_test

def test(q, bus, conn, stream):
    conn.Connect()
    q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])

    # <message type="chat"><body>hello</body</message>
    m = domish.Element(('', 'message'))
    m['from'] = 'foo@bar.com/Pidgin'
    m['type'] = 'chat'
    m.addElement('body', content='hello')
    stream.send(m)

    event = q.expect('dbus-signal', signal='NewChannel')
    text_chan = bus.get_object(conn._named_service, event.args[0])
    assert event.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text'
    # check that handle type == contact handle
    assert event.args[2] == 1
    jid = conn.InspectHandles(1, [event.args[3]])[0]
    assert jid == 'foo@bar.com'
    assert event.args[4] == False   # suppress handler

    # Exercise basic Channel Properties from spec 0.17.7
    channel_props = text_chan.GetAll(
            'org.freedesktop.Telepathy.Channel',
            dbus_interface='org.freedesktop.DBus.Properties')
    assert channel_props.get('TargetHandle') == event.args[3],\
            (channel_props.get('TargetHandle'), event.args[3])
    assert channel_props.get('TargetHandleType') == 1,\
            channel_props.get('TargetHandleType')
    assert channel_props.get('ChannelType') == \
            'org.freedesktop.Telepathy.Channel.Type.Text',\
            channel_props.get('ChannelType')
    assert 'org.freedesktop.Telepathy.Channel.Interface.ChatState' in \
            channel_props.get('Interfaces', ()), \
            channel_props.get('Interfaces')
    assert 'org.freedesktop.Telepathy.Channel.FUTURE' in \
            channel_props.get('Interfaces', ()), \
            channel_props.get('Interfaces')

    future_props = text_chan.GetAll(
            'org.freedesktop.Telepathy.Channel.FUTURE',
            dbus_interface='org.freedesktop.DBus.Properties')
    assert future_props['TargetID'] == jid,\
            (future_props['TargetID'], jid)

    event = q.expect('dbus-signal', signal='Received')

    # message type: normal
    assert event.args[3] == 0
    # flags: none
    assert event.args[4] == 0
    # body
    assert event.args[5] == 'hello'

    dbus.Interface(text_chan,
        u'org.freedesktop.Telepathy.Channel.Type.Text').Send(0, 'goodbye')

    event = q.expect('stream-message')

    elem = event.stanza
    assert elem.name == 'message'
    assert elem['type'] == 'chat'
    body = list(event.stanza.elements())[0]
    assert body.name == 'body'
    assert body.children[0] == u'goodbye'

    conn.Disconnect()
    q.expect('dbus-signal', signal='StatusChanged', args=[2, 1])

if __name__ == '__main__':
    exec_test(test)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
    :codauthor: :email:`Mike Place <mp@saltstack.com>`
'''

# Import Salt Testing libs
from salttesting import TestCase, skipIf
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch, call

ensure_in_syspath('../../')

from salt.modules import cron

STUB_USER = 'root'
STUB_PATH = '/tmp'

STUB_CRON_TIMESTAMP = {'minute': '1',
                  'hour': '2',
                  'daymonth': '3',
                  'month': '4',
                  'dayweek': '5'}

STUB_SIMPLE_RAW_CRON = '5 0 * * * /tmp/no_script.sh'
STUB_SIMPLE_CRON_DICT = {'pre': ['5 0 * * * /tmp/no_script.sh'], 'crons': [], 'env': [], 'special': []}

__grains__ = {}

@skipIf(NO_MOCK, NO_MOCK_REASON)
class PsTestCase(TestCase):
    def test__needs_change(self):
        self.assertTrue(cron._needs_change(True, False))

    def test__needs_change_random(self):
        '''
        Assert that if the new var is 'random' and old is '* that we return True
        '''
        self.assertTrue(cron._needs_change('*', 'random'))

    ## Still trying to figure this one out.
    # def test__render_tab(self):
    #     pass

    def test__get_cron_cmdstr_solaris(self):
        cron.__grains__ = __grains__
        with patch.dict(cron.__grains__, {'os_family': 'Solaris'}):
            self.assertEqual('su - root -c "crontab /tmp"',
                             cron._get_cron_cmdstr(STUB_USER, STUB_PATH))

    def test__get_cron_cmdstr(self):
        cron.__grains__ = __grains__
        with patch.dict(cron.__grains__, {'os': None}):
            self.assertEqual('crontab -u root /tmp',
                             cron._get_cron_cmdstr(STUB_USER, STUB_PATH))

    def test__date_time_match(self):
        '''
        Passes if a match is found on all elements. Note the conversions to strings here!
        :return:
        '''
        self.assertTrue(cron._date_time_match(STUB_CRON_TIMESTAMP,
                                    minute=STUB_CRON_TIMESTAMP['minute'],
                                    hour=STUB_CRON_TIMESTAMP['hour'],
                                    daymonth=STUB_CRON_TIMESTAMP['daymonth'],
                                    dayweek=STUB_CRON_TIMESTAMP['dayweek']
                                    ))

    @patch('salt.modules.cron.raw_cron', new=MagicMock(return_value=STUB_SIMPLE_RAW_CRON))
    def test_list_tab(self):
        self.assertDictEqual(STUB_SIMPLE_CRON_DICT, cron.list_tab('DUMMY_USER'))

    @patch('salt.modules.cron._write_cron_lines')
    @patch('salt.modules.cron.list_tab', new=MagicMock(return_value=STUB_SIMPLE_CRON_DICT))
    def test_set_special(self, write_cron_lines_mock):
        expected_write_call = call('DUMMY_USER',
                                   ['5 0 * * * /tmp/no_script.sh\n',
                                    '# Lines below here are managed by Salt, do not edit\n',
                                    '@hourly echo Hi!\n'])
        ret = cron.set_special('DUMMY_USER', '@hourly', 'echo Hi!')
        write_cron_lines_mock.assert_has_calls(expected_write_call)

    def test__get_cron_date_time(self):
        ret = cron._get_cron_date_time(minute=STUB_CRON_TIMESTAMP['minute'],
                                    hour=STUB_CRON_TIMESTAMP['hour'],
                                    daymonth=STUB_CRON_TIMESTAMP['daymonth'],
                                    dayweek=STUB_CRON_TIMESTAMP['dayweek'],
                                    month=STUB_CRON_TIMESTAMP['month'])
        self.assertDictEqual(ret, STUB_CRON_TIMESTAMP)

    ## FIXME: More sophisticated _get_cron_date_time checks should be added here.

    @patch('salt.modules.cron._write_cron_lines', new=MagicMock(return_value={'retcode': False}))
    @patch('salt.modules.cron.raw_cron', new=MagicMock(return_value=STUB_SIMPLE_RAW_CRON))
    def test_set_job(self):
        cron.__grains__ = __grains__
        with patch.dict(cron.__grains__, {'os': None}):
            cron.set_job('DUMMY_USER', 1, 2, 3, 4, 5,
                         '/bin/echo NOT A DROID',
                         'WERE YOU LOOKING FOR ME?')
            expected_call = call('DUMMY_USER',
                                 ['5 0 * * * /tmp/no_script.sh\n',
                                  '# Lines below here are managed by Salt, do not edit\n',
                                  '# WERE YOU LOOKING FOR ME?\n',
                                  '1 2 3 4 5 /bin/echo NOT A DROID\n'])
            cron._write_cron_lines.call_args.assert_called_with(expected_call)

    @patch('salt.modules.cron._write_cron_lines', new=MagicMock(return_value={'retcode': False}))
    @patch('salt.modules.cron.raw_cron', new=MagicMock(return_value=STUB_SIMPLE_RAW_CRON))
    def test_rm_job_is_absent(self):
        with patch.dict(cron.__grains__, {'os': None}):
            ret = cron.rm_job('DUMMY_USER', '/bin/echo NOT A DROID', 1, 2, 3, 4, 5)
            self.assertEqual('absent', ret)
<commit_msg>Correctly grain for test "os" => "os_family": issue-10215<commit_after># -*- coding: utf-8 -*-
'''
    :codauthor: :email:`Mike Place <mp@saltstack.com>`
'''

# Import Salt Testing libs
from salttesting import TestCase, skipIf
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch, call

ensure_in_syspath('../../')

from salt.modules import cron

STUB_USER = 'root'
STUB_PATH = '/tmp'

STUB_CRON_TIMESTAMP = {'minute': '1',
                  'hour': '2',
                  'daymonth': '3',
                  'month': '4',
                  'dayweek': '5'}

STUB_SIMPLE_RAW_CRON = '5 0 * * * /tmp/no_script.sh'
STUB_SIMPLE_CRON_DICT = {'pre': ['5 0 * * * /tmp/no_script.sh'], 'crons': [], 'env': [], 'special': []}

__grains__ = {}

@skipIf(NO_MOCK, NO_MOCK_REASON)
class PsTestCase(TestCase):
    def test__needs_change(self):
        self.assertTrue(cron._needs_change(True, False))

    def test__needs_change_random(self):
        '''
        Assert that if the new var is 'random' and old is '* that we return True
        '''
        self.assertTrue(cron._needs_change('*', 'random'))

    ## Still trying to figure this one out.
    # def test__render_tab(self):
    #     pass

    def test__get_cron_cmdstr_solaris(self):
        cron.__grains__ = __grains__
        with patch.dict(cron.__grains__, {'os_family': 'Solaris'}):
            self.assertEqual('su - root -c "crontab /tmp"',
                             cron._get_cron_cmdstr(STUB_USER, STUB_PATH))

    def test__get_cron_cmdstr(self):
        cron.__grains__ = __grains__
        with patch.dict(cron.__grains__, {'os_family': None}):
            self.assertEqual('crontab -u root /tmp',
                             cron._get_cron_cmdstr(STUB_USER, STUB_PATH))

    def test__date_time_match(self):
        '''
        Passes if a match is found on all elements. Note the conversions to strings here!
        :return:
        '''
        self.assertTrue(cron._date_time_match(STUB_CRON_TIMESTAMP,
                                    minute=STUB_CRON_TIMESTAMP['minute'],
                                    hour=STUB_CRON_TIMESTAMP['hour'],
                                    daymonth=STUB_CRON_TIMESTAMP['daymonth'],
                                    dayweek=STUB_CRON_TIMESTAMP['dayweek']
                                    ))

    @patch('salt.modules.cron.raw_cron', new=MagicMock(return_value=STUB_SIMPLE_RAW_CRON))
    def test_list_tab(self):
        self.assertDictEqual(STUB_SIMPLE_CRON_DICT, cron.list_tab('DUMMY_USER'))

    @patch('salt.modules.cron._write_cron_lines')
    @patch('salt.modules.cron.list_tab', new=MagicMock(return_value=STUB_SIMPLE_CRON_DICT))
    def test_set_special(self, write_cron_lines_mock):
        expected_write_call = call('DUMMY_USER',
                                   ['5 0 * * * /tmp/no_script.sh\n',
                                    '# Lines below here are managed by Salt, do not edit\n',
                                    '@hourly echo Hi!\n'])
        ret = cron.set_special('DUMMY_USER', '@hourly', 'echo Hi!')
        write_cron_lines_mock.assert_has_calls(expected_write_call)

    def test__get_cron_date_time(self):
        ret = cron._get_cron_date_time(minute=STUB_CRON_TIMESTAMP['minute'],
                                    hour=STUB_CRON_TIMESTAMP['hour'],
                                    daymonth=STUB_CRON_TIMESTAMP['daymonth'],
                                    dayweek=STUB_CRON_TIMESTAMP['dayweek'],
                                    month=STUB_CRON_TIMESTAMP['month'])
        self.assertDictEqual(ret, STUB_CRON_TIMESTAMP)

    ## FIXME: More sophisticated _get_cron_date_time checks should be added here.

    @patch('salt.modules.cron._write_cron_lines', new=MagicMock(return_value={'retcode': False}))
    @patch('salt.modules.cron.raw_cron', new=MagicMock(return_value=STUB_SIMPLE_RAW_CRON))
    def test_set_job(self):
        cron.__grains__ = __grains__
        with patch.dict(cron.__grains__, {'os': None}):
            cron.set_job('DUMMY_USER', 1, 2, 3, 4, 5,
                         '/bin/echo NOT A DROID',
                         'WERE YOU LOOKING FOR ME?')
            expected_call = call('DUMMY_USER',
                                 ['5 0 * * * /tmp/no_script.sh\n',
                                  '# Lines below here are managed by Salt, do not edit\n',
                                  '# WERE YOU LOOKING FOR ME?\n',
                                  '1 2 3 4 5 /bin/echo NOT A DROID\n'])
            cron._write_cron_lines.call_args.assert_called_with(expected_call)

    @patch('salt.modules.cron._write_cron_lines', new=MagicMock(return_value={'retcode': False}))
    @patch('salt.modules.cron.raw_cron', new=MagicMock(return_value=STUB_SIMPLE_RAW_CRON))
    def test_rm_job_is_absent(self):
        with patch.dict(cron.__grains__, {'os': None}):
            ret = cron.rm_job('DUMMY_USER', '/bin/echo NOT A DROID', 1, 2, 3, 4, 5)
            self.assertEqual('absent', ret)
<|endoftext|>


<commit_before>from mock import MagicMock

from gitfs.utils.commits import CommitsList

class TestCommitList(object):
    def test_contains(self):
        mocked_commit = MagicMock()
        mocked_commit.hex = "hexish"

        commit_list = CommitsList()
        assert mocked_commit not in commit_list

    def test_index(self):
        mocked_commit = MagicMock()
        mocked_commit.hex = "hexish"

        commit_list = CommitsList()
        commit_list.append(mocked_commit)
        assert commit_list.index(mocked_commit) == 0

    def test_getitem(self):
        mocked_commit = MagicMock()
        mocked_commit.hex = "hexish"

        commit_list = CommitsList()
        commit_list.append(mocked_commit)
        assert commit_list[:1].hashes == ["hexish"]
        assert commit_list[:1].commits == [mocked_commit]

    def test_iter(self):
        mocked_commit = MagicMock()
        mocked_commit.hex = "hexish"

        commit_list = CommitsList()
        commit_list.append(mocked_commit)
        commit_list.append(mocked_commit)

        for commit in commit_list:
            assert commit == mocked_commit
<commit_msg>test: repr method from commit list<commit_after>from mock import MagicMock

from gitfs.utils.commits import CommitsList

class TestCommitList(object):
    def test_contains(self):
        mocked_commit = MagicMock()
        mocked_commit.hex = "hexish"

        commit_list = CommitsList()
        assert mocked_commit not in commit_list

    def test_index(self):
        mocked_commit = MagicMock()
        mocked_commit.hex = "hexish"

        commit_list = CommitsList()
        commit_list.append(mocked_commit)
        assert commit_list.index(mocked_commit) == 0

    def test_getitem(self):
        mocked_commit = MagicMock()
        mocked_commit.hex = "hexish"

        commit_list = CommitsList()
        commit_list.append(mocked_commit)
        assert commit_list[:1].hashes == ["hexish"]
        assert commit_list[:1].commits == [mocked_commit]

    def test_iter(self):
        mocked_commit = MagicMock()
        mocked_commit.hex = "hexish"

        commit_list = CommitsList()
        commit_list.append(mocked_commit)
        commit_list.append(mocked_commit)

        for commit in commit_list:
            assert commit == mocked_commit

    def test_repr(self):
        mocked_commit = MagicMock()
        mocked_commit.hex = "hexish"

        commit_list = CommitsList()
        commit_list.append(mocked_commit)

        assert repr(commit_list) == repr([mocked_commit])
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals, print_function, division, absolute_import
import os
import json
from http.client import BAD_REQUEST, OK, NOT_FOUND
from copy import deepcopy

from nose.tools import assert_equal, assert_in
import dpath

from openfisca_core.commons import to_unicode
from openfisca_country_template.situation_examples import couple

from . import subject

def post_json(data = None, file = None):
    if file:
        file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets', file)
        with open(file_path, 'r') as file:
            data = file.read()
    return subject.post('/calculate', data = data, content_type = 'application/json')

def check_response(data, expected_error_code, path_to_check, content_to_check):
    response = post_json(data)
    assert_equal(response.status_code, expected_error_code)
    json_response = json.loads(response.data.decode('utf-8'))
    if path_to_check:
        content = dpath.util.get(json_response, path_to_check)
        assert_in(content_to_check, content)

def test_responses():
    tests = [
        ('{"a" : "x", "b"}', BAD_REQUEST, 'error', 'Invalid JSON'),
        ('["An", "array"]', BAD_REQUEST, 'error', 'Invalid type'),
        ('{"persons": {}}', BAD_REQUEST, 'persons', 'At least one person'),
        ('{"persons": {"bob": {}}, "unknown_entity": {}}', BAD_REQUEST, 'unknown_entity', 'entities are not found',),
        ('{"persons": {"bob": {}}, "households": {"dupont": {"parents": {}}}}', BAD_REQUEST, 'households/dupont/parents', 'type',),
        ('{"persons": {"bob": {"unknown_variable": {}}}}', NOT_FOUND, 'persons/bob/unknown_variable', 'You tried to calculate or to set',),
        ('{"persons": {"bob": {"housing_allowance": {}}}}', BAD_REQUEST, 'persons/bob/housing_allowance', "You tried to compute the variable 'housing_allowance' for the entity 'persons'",),
        ('{"persons": {"bob": {"salary": 4000 }}}', BAD_REQUEST, 'persons/bob/salary', 'period',),
        ('{"persons": {"bob": {"salary": {"2017-01": "toto"} }}}', BAD_REQUEST, 'persons/bob/salary/2017-01', 'must be of type number',),
        ('{"persons": {"bob": {"salary": {"2017-01": {}} }}}', BAD_REQUEST, 'persons/bob/salary/2017-01', 'must be of type number',),
        ('{"persons": {"bob": {"age": {"2017-01": "toto"} }}}', BAD_REQUEST, 'persons/bob/age/2017-01', 'must be of type integer',),
        ('{"persons": {"bob": {"birth": {"2017-01": "toto"} }}}', BAD_REQUEST, 'persons/bob/birth/2017-01', 'Invalid date',),
        ('{"persons": {"bob": {}}, "households": {"household": {"parents": ["unexpected_person_id"]}}}', BAD_REQUEST, 'households/household/parents', 'has not been declared in persons',),
        ('{"persons": {"bob": {}}, "households": {"household": {"parents": ["bob", "bob"]}}}', BAD_REQUEST, 'households/household/parents', 'has been declared more than once',),
        ('{"persons": {"bob": {}}, "households": {"household": {"parents": ["bob", {}]}}}', BAD_REQUEST, 'households/household/parents/1', 'Invalid type',),
        ('{"persons": {"bob": {"salary": {"invalid period": 2000 }}}}', BAD_REQUEST, 'persons/bob/salary/invalid period', 'Expected a period',),
        ('{"persons": {"bob": {"salary": {"invalid period": null }}}}', BAD_REQUEST, 'persons/bob/salary/invalid period', 'Expected a period',),
        ('{"persons": {"bob": {"basic_income": {"2017": 2000 }}}}', BAD_REQUEST, 'persons/bob/basic_income/2017', 'basic_income is only defined for months',),
        ('{"persons": {"bob": {"birth": {"ETERNITY": "1980-01-01"} }}}', BAD_REQUEST, 'households', 'is not a member of any household',),
        ('{"persons": {"bob": {"salary": {"ETERNITY": 2000} }}}', BAD_REQUEST, 'persons/bob/salary/ETERNITY', 'salary is only defined for months',),
        ]

    for test in tests:
        yield (check_response,) + test

def test_basic_calculation():
    simulation_json = json.dumps({
        "persons": {
            "bill": {
                "birth": {
                    "2017-12": "1980-01-01"
                    },
                "age": {
                    "2017-12": None
                    },
                "salary": {
                    "2017-12": 2000
                    },
                "basic_income": {
                    "2017-12": None
                    },
                "income_tax": {
                    "2017-12": None
                    }
                },
            "bob": {
                "salary": {
                    "2017-12": 15000
                    },
                "basic_income": {
                    "2017-12": None
                    },
                "social_security_contribution": {
                    "2017-12": None
                    }
                },
            },
        "households": {
            "first_household": {
                "parents": ['bill', 'bob'],
                "housing_tax": {
                    "2017": None
                    },
                "accommodation_size": {
                    "2017-01": 300
                    }
                },
            }
        })

    response = post_json(simulation_json)
    assert_equal(response.status_code, OK)
    response_json = json.loads(response.data.decode('utf-8'))
    assert_equal(dpath.get(response_json, 'persons/bill/basic_income/2017-12'), 600)  # Universal basic income
    assert_equal(dpath.get(response_json, 'persons/bill/income_tax/2017-12'), 300)  # 15% of the salary
    assert_equal(dpath.get(response_json, 'persons/bill/age/2017-12'), 37)  # 15% of the salary
    assert_equal(dpath.get(response_json, 'persons/bob/basic_income/2017-12'), 600)
    assert_equal(dpath.get(response_json, 'persons/bob/social_security_contribution/2017-12'), 816)  # From social_security_contribution.yaml test
    assert_equal(dpath.get(response_json, 'households/first_household/housing_tax/2017'), 3000)

def test_enums_sending_identifier():
    simulation_json = json.dumps({
        "persons": {
            "bill": {}
            },
        "households": {
            "_": {
                "parents": ["bill"],
                "housing_tax": {
                    "2017": None
                    },
                "accommodation_size": {
                    "2017-01": 300
                    },
                "housing_occupancy_status": {
                    "2017-01": "free_lodger"
                    }
                }
            }
        })

    response = post_json(simulation_json)
    assert_equal(response.status_code, OK)
    response_json = json.loads(response.data.decode('utf-8'))
    assert_equal(dpath.get(response_json, 'households/_/housing_tax/2017'), 0)

def test_enum_output():
    simulation_json = json.dumps({
        "persons": {
            "bill": {},
            },
        "households": {
            "_": {
                "parents": ["bill"],
                "housing_occupancy_status": {
                    "2017-01": None
                    }
                },
            }
        })

    response = post_json(simulation_json)
    assert_equal(response.status_code, OK)
    response_json = json.loads(response.data.decode('utf-8'))
    assert_equal(dpath.get(response_json, "households/_/housing_occupancy_status/2017-01"), "tenant")

def test_enum_wrong_value():
    simulation_json = json.dumps({
        "persons": {
            "bill": {},
            },
        "households": {
            "_": {
                "parents": ["bill"],
                "housing_occupancy_status": {
                    "2017-01": "Unknown value lodger"
                    }
                },
            }
        })

    response = post_json(simulation_json)
    assert_equal(response.status_code, BAD_REQUEST)
    response_json = json.loads(response.data.decode('utf-8'))
    assert_in(
        "Possible values are ['owner', 'tenant', 'free_lodger', 'homeless']",
        dpath.get(response_json, "households/_/housing_occupancy_status/2017-01")
        )

def test_encoding_variable_value():
    simulation_json = json.dumps({
        "persons": {
            "toto": {}
            },
        "households": {
            "_": {
                "housing_occupancy_status": {
                    "2017-07": "Locataire ou sous-locataire d‘un logement loué vide non-HLM"

                    },
                "parent": [
                    "toto",
                    ]
                }
            }
        })

    # No UnicodeDecodeError
    response = post_json(simulation_json)
    assert_equal(response.status_code, BAD_REQUEST, response.data.decode('utf-8'))
    response_json = json.loads(response.data.decode('utf-8'))
    assert_in(
        "'Locataire ou sous-locataire d‘un logement loué vide non-HLM' is not a valid value for 'housing_occupancy_status'. Possible values are ",
        dpath.get(response_json, 'households/_/housing_occupancy_status/2017-07')
        )

def test_encoding_entity_name():
    simulation_json = json.dumps({
        "persons": {
            "O‘Ryan": {},
            "Renée": {}
            },
        "households": {
            "_": {
                "parents": [
                    "O‘Ryan",
                    "Renée"
                    ]
                }
            }
        })

    # No UnicodeDecodeError
    response = post_json(simulation_json)
    response_json = json.loads(response.data.decode('utf-8'))

    # In Python 3, there is no encoding issue.
    if response.status_code != OK:
        assert_in(
            "'O‘Ryan' is not a valid ASCII value.",
            response_json['error']
            )

def test_encoding_period_id():
    simulation_json = json.dumps({
        "persons": {
            "bill": {
                "salary": {
                    "2017": 60000
                    }
                },
            "bell": {
                "salary": {
                    "2017": 60000
                    }
                }
            },
        "households": {
            "_": {
                "parents": ["bill", "bell"],
                "housing_tax": {
                    "à": 400
                    },
                "accommodation_size": {
                    "2017-01": 300
                    },
                "housing_occupancy_status": {
                    "2017-01": "tenant"
                    }
                }
            }
        })

    # No UnicodeDecodeError
    response = post_json(simulation_json)
    assert_equal(response.status_code, BAD_REQUEST)
    response_json = json.loads(response.data.decode('utf-8'))

    # In Python 3, there is no encoding issue.
    if "Expected a period" not in to_unicode(response.data):
        assert_in(
            "'à' is not a valid ASCII value.",
            response_json['error']
            )

def test_str_variable():
    new_couple = deepcopy(couple)
    new_couple['households']['_']['postal_code'] = {'2017-01': None}
    simulation_json = json.dumps(new_couple)

    response = subject.post('/calculate', data = simulation_json, content_type = 'application/json')

    assert_equal(response.status_code, OK)
<commit_msg>Fix test checking wording of error messages<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals, print_function, division, absolute_import
import os
import json
from http.client import BAD_REQUEST, OK, NOT_FOUND
from copy import deepcopy

from nose.tools import assert_equal, assert_in
import dpath

from openfisca_core.commons import to_unicode
from openfisca_country_template.situation_examples import couple

from . import subject

def post_json(data = None, file = None):
    if file:
        file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets', file)
        with open(file_path, 'r') as file:
            data = file.read()
    return subject.post('/calculate', data = data, content_type = 'application/json')

def check_response(data, expected_error_code, path_to_check, content_to_check):
    response = post_json(data)
    assert_equal(response.status_code, expected_error_code)
    json_response = json.loads(response.data.decode('utf-8'))
    if path_to_check:
        content = dpath.util.get(json_response, path_to_check)
        assert_in(content_to_check, content)

def test_responses():
    tests = [
        ('{"a" : "x", "b"}', BAD_REQUEST, 'error', 'Invalid JSON'),
        ('["An", "array"]', BAD_REQUEST, 'error', 'Invalid type'),
        ('{"persons": {}}', BAD_REQUEST, 'persons', 'At least one person'),
        ('{"persons": {"bob": {}}, "unknown_entity": {}}', BAD_REQUEST, 'unknown_entity', 'entities are not found',),
        ('{"persons": {"bob": {}}, "households": {"dupont": {"parents": {}}}}', BAD_REQUEST, 'households/dupont/parents', 'type',),
        ('{"persons": {"bob": {"unknown_variable": {}}}}', NOT_FOUND, 'persons/bob/unknown_variable', 'You tried to calculate or to set',),
        ('{"persons": {"bob": {"housing_allowance": {}}}}', BAD_REQUEST, 'persons/bob/housing_allowance', "You tried to compute the variable 'housing_allowance' for the entity 'persons'",),
        ('{"persons": {"bob": {"salary": 4000 }}}', BAD_REQUEST, 'persons/bob/salary', 'period',),
        ('{"persons": {"bob": {"salary": {"2017-01": "toto"} }}}', BAD_REQUEST, 'persons/bob/salary/2017-01', 'expected type number',),
        ('{"persons": {"bob": {"salary": {"2017-01": {}} }}}', BAD_REQUEST, 'persons/bob/salary/2017-01', 'expected type number',),
        ('{"persons": {"bob": {"age": {"2017-01": "toto"} }}}', BAD_REQUEST, 'persons/bob/age/2017-01', 'expected type integer',),
        ('{"persons": {"bob": {"birth": {"2017-01": "toto"} }}}', BAD_REQUEST, 'persons/bob/birth/2017-01', 'Can\'t deal with date',),
        ('{"persons": {"bob": {}}, "households": {"household": {"parents": ["unexpected_person_id"]}}}', BAD_REQUEST, 'households/household/parents', 'has not been declared in persons',),
        ('{"persons": {"bob": {}}, "households": {"household": {"parents": ["bob", "bob"]}}}', BAD_REQUEST, 'households/household/parents', 'has been declared more than once',),
        ('{"persons": {"bob": {}}, "households": {"household": {"parents": ["bob", {}]}}}', BAD_REQUEST, 'households/household/parents/1', 'Invalid type',),
        ('{"persons": {"bob": {"salary": {"invalid period": 2000 }}}}', BAD_REQUEST, 'persons/bob/salary/invalid period', 'Expected a period',),
        ('{"persons": {"bob": {"salary": {"invalid period": null }}}}', BAD_REQUEST, 'persons/bob/salary/invalid period', 'Expected a period',),
        ('{"persons": {"bob": {"basic_income": {"2017": 2000 }}}}', BAD_REQUEST, 'persons/bob/basic_income/2017', 'basic_income is only defined for months',),
        ('{"persons": {"bob": {"birth": {"ETERNITY": "1980-01-01"} }}}', BAD_REQUEST, 'households', 'is not a member of any household',),
        ('{"persons": {"bob": {"salary": {"ETERNITY": 2000} }}}', BAD_REQUEST, 'persons/bob/salary/ETERNITY', 'salary is only defined for months',),
        ]

    for test in tests:
        yield (check_response,) + test

def test_basic_calculation():
    simulation_json = json.dumps({
        "persons": {
            "bill": {
                "birth": {
                    "2017-12": "1980-01-01"
                    },
                "age": {
                    "2017-12": None
                    },
                "salary": {
                    "2017-12": 2000
                    },
                "basic_income": {
                    "2017-12": None
                    },
                "income_tax": {
                    "2017-12": None
                    }
                },
            "bob": {
                "salary": {
                    "2017-12": 15000
                    },
                "basic_income": {
                    "2017-12": None
                    },
                "social_security_contribution": {
                    "2017-12": None
                    }
                },
            },
        "households": {
            "first_household": {
                "parents": ['bill', 'bob'],
                "housing_tax": {
                    "2017": None
                    },
                "accommodation_size": {
                    "2017-01": 300
                    }
                },
            }
        })

    response = post_json(simulation_json)
    assert_equal(response.status_code, OK)
    response_json = json.loads(response.data.decode('utf-8'))
    assert_equal(dpath.get(response_json, 'persons/bill/basic_income/2017-12'), 600)  # Universal basic income
    assert_equal(dpath.get(response_json, 'persons/bill/income_tax/2017-12'), 300)  # 15% of the salary
    assert_equal(dpath.get(response_json, 'persons/bill/age/2017-12'), 37)  # 15% of the salary
    assert_equal(dpath.get(response_json, 'persons/bob/basic_income/2017-12'), 600)
    assert_equal(dpath.get(response_json, 'persons/bob/social_security_contribution/2017-12'), 816)  # From social_security_contribution.yaml test
    assert_equal(dpath.get(response_json, 'households/first_household/housing_tax/2017'), 3000)

def test_enums_sending_identifier():
    simulation_json = json.dumps({
        "persons": {
            "bill": {}
            },
        "households": {
            "_": {
                "parents": ["bill"],
                "housing_tax": {
                    "2017": None
                    },
                "accommodation_size": {
                    "2017-01": 300
                    },
                "housing_occupancy_status": {
                    "2017-01": "free_lodger"
                    }
                }
            }
        })

    response = post_json(simulation_json)
    assert_equal(response.status_code, OK)
    response_json = json.loads(response.data.decode('utf-8'))
    assert_equal(dpath.get(response_json, 'households/_/housing_tax/2017'), 0)

def test_enum_output():
    simulation_json = json.dumps({
        "persons": {
            "bill": {},
            },
        "households": {
            "_": {
                "parents": ["bill"],
                "housing_occupancy_status": {
                    "2017-01": None
                    }
                },
            }
        })

    response = post_json(simulation_json)
    assert_equal(response.status_code, OK)
    response_json = json.loads(response.data.decode('utf-8'))
    assert_equal(dpath.get(response_json, "households/_/housing_occupancy_status/2017-01"), "tenant")

def test_enum_wrong_value():
    simulation_json = json.dumps({
        "persons": {
            "bill": {},
            },
        "households": {
            "_": {
                "parents": ["bill"],
                "housing_occupancy_status": {
                    "2017-01": "Unknown value lodger"
                    }
                },
            }
        })

    response = post_json(simulation_json)
    assert_equal(response.status_code, BAD_REQUEST)
    response_json = json.loads(response.data.decode('utf-8'))
    assert_in(
        "Possible values are ['owner', 'tenant', 'free_lodger', 'homeless']",
        dpath.get(response_json, "households/_/housing_occupancy_status/2017-01")
        )

def test_encoding_variable_value():
    simulation_json = json.dumps({
        "persons": {
            "toto": {}
            },
        "households": {
            "_": {
                "housing_occupancy_status": {
                    "2017-07": "Locataire ou sous-locataire d‘un logement loué vide non-HLM"

                    },
                "parent": [
                    "toto",
                    ]
                }
            }
        })

    # No UnicodeDecodeError
    response = post_json(simulation_json)
    assert_equal(response.status_code, BAD_REQUEST, response.data.decode('utf-8'))
    response_json = json.loads(response.data.decode('utf-8'))
    assert_in(
        "'Locataire ou sous-locataire d‘un logement loué vide non-HLM' is not a known value for 'housing_occupancy_status'. Possible values are ",
        dpath.get(response_json, 'households/_/housing_occupancy_status/2017-07')
        )

def test_encoding_entity_name():
    simulation_json = json.dumps({
        "persons": {
            "O‘Ryan": {},
            "Renée": {}
            },
        "households": {
            "_": {
                "parents": [
                    "O‘Ryan",
                    "Renée"
                    ]
                }
            }
        })

    # No UnicodeDecodeError
    response = post_json(simulation_json)
    response_json = json.loads(response.data.decode('utf-8'))

    # In Python 3, there is no encoding issue.
    if response.status_code != OK:
        assert_in(
            "'O‘Ryan' is not a valid ASCII value.",
            response_json['error']
            )

def test_encoding_period_id():
    simulation_json = json.dumps({
        "persons": {
            "bill": {
                "salary": {
                    "2017": 60000
                    }
                },
            "bell": {
                "salary": {
                    "2017": 60000
                    }
                }
            },
        "households": {
            "_": {
                "parents": ["bill", "bell"],
                "housing_tax": {
                    "à": 400
                    },
                "accommodation_size": {
                    "2017-01": 300
                    },
                "housing_occupancy_status": {
                    "2017-01": "tenant"
                    }
                }
            }
        })

    # No UnicodeDecodeError
    response = post_json(simulation_json)
    assert_equal(response.status_code, BAD_REQUEST)
    response_json = json.loads(response.data.decode('utf-8'))

    # In Python 3, there is no encoding issue.
    if "Expected a period" not in to_unicode(response.data):
        assert_in(
            "'à' is not a valid ASCII value.",
            response_json['error']
            )

def test_str_variable():
    new_couple = deepcopy(couple)
    new_couple['households']['_']['postal_code'] = {'2017-01': None}
    simulation_json = json.dumps(new_couple)

    response = subject.post('/calculate', data = simulation_json, content_type = 'application/json')

    assert_equal(response.status_code, OK)
<|endoftext|>