예제 #1
0
 def test_pass(self):
     resp = self.get_response()
     expect(resp).to.not_.be.ok
     expect(resp).json.path('status').to.not_.be.like('SUCCESS')
     expect(resp).json.path('status').to.be.equal('ERROR')
     expect(resp).to.have.header('Content-Type').like('json')
     expect(resp).to.not_.have.headers(['Content-Encoding', 'Date'])
예제 #2
0
 def test_add_bar_property(self):
     expect.add_assertion('bar', self.get_assertion(), is_property=True)
     obj = expect('bar').bar
     assert obj.predicate == ['assert_bar']
     try:
         expect('foo').bar
     except AssertionError:
         pass
     del expect.bar
예제 #3
0
 def setup_method(self, method):
     self.headers = {'foo': 'bar', 'baz': 'foobar'}
     with requests_mock.mock() as m:
         m.get('http://example.com',
               headers=self.headers,
               json={'foo': 'bar'})
         self.obj = expect(requests.get('http://example.com'))
예제 #4
0
 def setup_method(self, test_method):
     with requests_mock.mock() as m:
         m.get('http://example.com',
               headers={'foo': 'bar'},
               json={'bar': 'baz'})
         self.obj = expect(requests.get('http://example.com'))
     self.compared_obj = 'Not the same as obj'
예제 #5
0
 def test_add_bar_property_with_predicate_str(self):
     expect.add_assertion('bar',
                          self.get_assertion(),
                          is_property=True,
                          predicate_str='foobaz')
     obj = expect('bar').bar
     assert obj.predicate == ['foobaz']
     del expect.bar
예제 #6
0
    def test_pass(self):
        self.text = '{"foo": "bar"}'
        self.headers = {'Content-Type': 'application/json'}
        with requests_mock.mock() as m:
            m.get('http://example.com',
                  headers=self.headers,
                  text=self.text,
                  status_code=200)
            self.obj = expect(requests.get('http://example.com'))

        self.obj.json.valid
예제 #7
0
    def test_add_bar_not_prop(self):
        def is_value(self, value):
            return self.response_under_test == value

        expect.add_assertion('is_value', is_value, is_property=False)
        obj = expect('bar')
        obj.is_value('bar')
        try:
            obj.is_value('foo')
        except AssertionError:
            pass
예제 #8
0
    def test_fail(self):
        self.text = '{"foo": "bar",}'  # Invalid JSON
        self.headers = {'Content-Type': 'application/json'}
        with requests_mock.mock() as m:
            m.get('http://example.com',
                  headers=self.headers,
                  text=self.text,
                  status_code=200)
            self.obj = expect(requests.get('http://example.com'))

        with pytest.raises(AssertionError):
            self.obj.json.valid
예제 #9
0
    def test_fail(self):
        self.json = {'foo': 'bar'}
        self.headers = {'foo': 'bar'}
        with requests_mock.mock() as m:
            m.get('http://example.com',
                  headers=self.headers,
                  json=self.json,
                  status_code=500)
            self.obj = expect(requests.get('http://example.com'))

        with pytest.raises(AssertionError):
            self.obj.ok
예제 #10
0
 def test_schema(self):
     resp = self.get_response()
     expect(resp).to.be.ok
     expect(resp).to.have.encoding('utf-8')
     expect(resp).json.to.have.the.schema({
         'type': 'object',
         'required': ['foo', 'baz'],
         'properties': {
             'foo': {
                 'type': 'string'
             },
             'baz': {
                 'type':
                 'array',
                 'items': [{
                     'type': 'object',
                     'required': ['1', '2'],
                     'properties': {
                         '1': {
                             'type': 'string'
                         },
                         '2': {
                             'type': 'number'
                         },
                     }
                 }]
             }
         }
     })
예제 #11
0
 def test_pass(self):
     resp = self.get_response()
     expect(resp).to.have.status_code(200)
     expect(resp).json.to.be.valid
     expect(resp).json.path('baz[0]."1"').to.be.equal('one')
예제 #12
0
 def test_add_foo_with_predicate_str(self):
     expect.add_join_word('foo', 'bar')
     obj = expect('baz')
     assert not obj.predicate
     assert obj.foo is obj
     assert obj.predicate == ['bar']
예제 #13
0
파일: example.py 프로젝트: huntcsg/expectly
import requests

from expectly import expect

resp = requests.get('http://ip.jsontest.com/')
expect(resp).to.have.header('Content-Type').equal(
    'application/json; charset=ISO-8859-1')
expect(resp).json.to.have.schema({
    "type": "object",
    "properties": {
        "ip": {
            "type": "string"
        }
    },
})
expect(resp).to.have.header('Content-Type').like('json')
expect(resp).to.have.header('Content-Type').that.contains('charset')
expect(resp).to.not_.have.encoding('utf-8')

expect(resp).json.path('ip').to.be.like(r'[\w\:\d]*')

expect(resp).json.value.to.be.equal({
    'ip':
    '2601:184:4a80:1140:7159:f448:4d45:ced4',
})

# The following four assertions are equivalent
expect(resp).json.value.contains('ip')
expect(resp).json('to have key "ip"', lambda self: 'ip' in self.get())
expect(resp).json.to('have key "ip"', lambda self: 'ip' in self.get())
expect(resp).json.to.have('key "ip"', lambda self: 'ip' in self.get())