Exemplo n.º 1
0
    def test_create(self):
        # Post call ###########################################################
        def request_callback(request):
            payload = json.loads(request.body)

            assert 'OData-Version' in request.headers, 'OData-Version header not in request'

            assert 'ProductID' not in payload, 'Payload contains primary key'
            assert '@odata.type' in payload, 'Payload did not contain @odata.type'

            payload['ProductID'] = 1

            resp_body = payload
            headers = {}
            return requests.codes.created, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.POST, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        new_product = Product()
        new_product.name = u'New Test Product'
        new_product.category = u'Category #1'
        new_product.price = 34.5

        Service.save(new_product)

        assert new_product.id is not None, 'Product.id is not set'
Exemplo n.º 2
0
 def test_call_function_empty_result(self):
     with responses.RequestsMock() as rsps:
         rsps.add(rsps.GET,
                  Product.__odata_url__() + '/ODataTest.DemoFunction()',
                  content_type='application/json',
                  status=requests.codes.no_content)
         result = Product.DemoFunction()
     self.assertIsNone(result)
Exemplo n.º 3
0
 def test_call_bound_action_empty_result(self):
     with responses.RequestsMock() as rsps:
         rsps.add(
             rsps.POST,
             Product.__odata_url__() + '/ODataTest.DemoCollectionAction',
         )
         result = Product.DemoCollectionAction()
     self.assertIsNone(result)
Exemplo n.º 4
0
 def test_call_bound_action_with_result(self):
     with responses.RequestsMock() as rsps:
         rsps.add(
             rsps.POST,
             Product.__odata_url__() + '/ODataTest.DemoCollectionAction',
             content_type='application/json',
             json=dict(value='test'),
         )
         result = Product.DemoCollectionAction()
     self.assertEqual(result, 'test')
Exemplo n.º 5
0
    def test_new_entity(self):
        uuid = '3d46cd74-a3af-4afd-af94-512b5cee1ef0'

        product = Product()
        product.id = uuid
        product.name = u'Defender'
        product.category = u'Cars'
        product.price = 40000.00

        state = EntityState(product)

        data = dict(state.data_for_insert())

        assert data['ProductID'] == uuid
        assert data['ProductName'] == 'Defender'
        assert data['Category'] == 'Cars'
        assert data['Price'] == 40000.00

        assert state.dirty == []

        product.name = 'Toyota Carola'
        product.price = 32500.00

        data = dict(state.data_for_update())

        assert data['ProductName'] == 'Toyota Carola'
        assert data['Category'] == 'Cars'
        assert data['Price'] == 32500.00
Exemplo n.º 6
0
    def test_read(self):
        expected_id = 1024
        expected_name = 'Existing entity'
        expected_category = 'Existing category'
        expected_price = Decimal('85.2')

        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': expected_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()
        assert product.id == expected_id
        assert product.name == expected_name
        assert product.category == expected_category
        assert product.price == expected_price
Exemplo n.º 7
0
    def test_read(self):
        expected_id = 1024
        expected_name = 'Existing entity'
        expected_category = 'Existing category'
        expected_price = Decimal('85.2')

        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': expected_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()
        assert product.id == expected_id
        assert product.name == expected_name
        assert product.category == expected_category
        assert product.price == expected_price
Exemplo n.º 8
0
    def test_collection_bound_function_on_instance(self):
        test_product = Product()

        def _call():
            test_product.DemoFunction()

        self.assertRaises(AttributeError, _call)
Exemplo n.º 9
0
    def test_context_call_bound_action(self):
        with responses.RequestsMock() as rsps:
            rsps.add(rsps.POST, Product.__odata_url__() + '/ODataTest.DemoActionParameters')

            context = Service.create_context()
            context.call(Product.DemoActionWithParameters,
                         Name='TestName',
                         Price=decimal.Decimal('25.0'))
Exemplo n.º 10
0
    def test_create_with_primary_key(self):
        # Post call ###########################################################
        def request_callback(request):
            payload = json.loads(request.body)
            self.assertEqual(payload.get('ProductID'),
                             55,
                             msg='Did not receive ProductID')
            resp_body = payload
            headers = {}
            return requests.codes.created, headers, json.dumps(resp_body)

        new_product = Product()
        new_product.id = 55
        new_product.name = u'New Test Product'
        new_product.category = u'Category #1'
        new_product.price = 34.5

        with responses.RequestsMock() as rsps:
            rsps.add_callback(
                rsps.POST,
                Product.__odata_url__(),
                callback=request_callback,
                content_type='application/json',
            )

            Service.save(new_product)

        assert new_product.id is not None, 'Product.id is not set'
Exemplo n.º 11
0
    def test_insert_value(self):

        def request_callback(request):
            content = json.loads(request.body)
            content['ProductID'] = 1
            self.assertIn('ColorSelection', content)
            self.assertEqual(content.get('ColorSelection'), 'Black')
            headers = {}
            return requests.codes.ok, headers, json.dumps(content)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(rsps.POST,
                              Product.__odata_url__(),
                              callback=request_callback,
                              content_type='application/json')

            new_product = Product()
            new_product.name = 'Test Product'
            new_product.color_selection = ColorSelection.Black
            Service.save(new_product)
Exemplo n.º 12
0
    def test_call_action_with_result(self):
        def request_callback(request):
            payload = json.loads(request.body)
            expected = dict(Name='TestName', Price=decimal.Decimal('10.0'))

            self.assertDictEqual(payload, expected)

            headers = {}
            body = dict(value='ok')
            return requests.codes.ok, headers, json.dumps(body)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(
                rsps.POST,
                Product.__odata_url__() + '/ODataTest.DemoActionParameters',
                callback=request_callback,
                content_type='application/json',
            )
            result = Product.DemoActionWithParameters(
                Name='TestName', Price=decimal.Decimal('10.0'))
        self.assertEqual(result, 'ok')
Exemplo n.º 13
0
    def test_create(self):
        # Post call ###########################################################
        def request_callback(request):
            payload = json.loads(request.body)

            assert 'OData-Version' in request.headers, 'OData-Version header not in request'

            assert 'ProductID' not in payload, 'Payload contains primary key'
            assert '@odata.type' in payload, 'Payload did not contain @odata.type'

            payload['ProductID'] = 1

            resp_body = payload
            headers = {}
            return requests.codes.created, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.POST,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        new_product = Product()
        new_product.name = u'New Test Product'
        new_product.category = u'Category #1'
        new_product.price = 34.5

        Service.save(new_product)

        assert new_product.id is not None, 'Product.id is not set'
Exemplo n.º 14
0
    def test_context_query_without_auth(self):
        def request_callback(request):
            self.assertIsNone(request.headers.get('Authorization'))
            headers = {}
            body = dict(value=[])
            return requests.codes.ok, headers, json.dumps(body)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(rsps.GET, Product.__odata_url__(),
                              callback=request_callback,
                              content_type='application/json')

            context = Service.create_context()
            context.query(Product).first()
Exemplo n.º 15
0
    def test_read_value(self):
        test_product_values = dict(
            ProductID=1,
            ProductName='Test Product',
            Category='',
            ColorSelection='Red',
            Price=0.0,
        )
        with responses.RequestsMock() as rsps:
            rsps.add(rsps.GET, Product.__odata_url__(),
                     content_type='application/json',
                     json=dict(value=[test_product_values]))

            product = Service.query(Product).get(1)

        self.assertIsInstance(product.color_selection, ColorSelection)
        self.assertEqual(product.color_selection, ColorSelection.Red)
Exemplo n.º 16
0
    def test_call_function_with_result_query(self):
        def request_callback(request):
            self.assertTrue(
                'filter=ProductName+eq+%27testtest%27' in request.url)

            headers = {}
            body = dict(value='ok')
            return requests.codes.ok, headers, json.dumps(body)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(rsps.GET,
                              Product.__odata_url__() +
                              '/ODataTest.DemoFunction()',
                              request_callback,
                              content_type='application/json')

            query = Service.query(Product)
            query = query.filter(Product.name == 'testtest')
            Product.DemoFunction.with_query(query)()
Exemplo n.º 17
0
    def test_parse_error_json(self):
        expected_code = '0451'
        expected_message = 'Testing error message handling'
        expected_innererror_message = 'Detailed messages here'

        # Initial data ########################################################
        def request_callback(request):
            resp_body = {
                'error': {
                    'code': expected_code,
                    'message': expected_message,
                    'innererror': {
                        'message': expected_innererror_message
                    }
                }
            }
            headers = {
                'Content-Type': 'application/json;odata.metadata=minimal'
            }
            return requests.codes.bad_request, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )

        #######################################################################

        def action():
            try:
                Service.query(Product).first()
            except ODataError as e:
                errmsg = str(e)
                assert expected_code in errmsg, 'Code not in text'
                assert expected_message in errmsg, 'Upper level message not in text'
                assert expected_innererror_message in errmsg, 'Detailed message not in text'
                raise

        self.assertRaises(ODataError, action)
Exemplo n.º 18
0
    def test_call_bound_action_on_instance(self):
        test_product = Product()
        test_product.name = 'TestProduct'
        test_product.id = 1234
        test_product.price = decimal.Decimal('20.0')

        # shortcut for saving the entity
        test_product.__odata__.persisted = True

        with responses.RequestsMock() as rsps:
            rsps.add(
                rsps.POST,
                test_product.__odata__.instance_url + '/ODataTest.DemoAction',
            )
            result = test_product.DemoAction()
        self.assertIsNone(result)
Exemplo n.º 19
0
    def test_delete(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 2048,
                'ProductName': 'This product will be deleted',
                'Category': 'Something',
                'Price': 1234.5,
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()

        # Delete call #########################################################
        def request_callback_delete(request):
            headers = {}
            return requests.codes.ok, headers, ''

        responses.add_callback(
            responses.DELETE,
            product.__odata__.instance_url,
            callback=request_callback_delete,
            content_type='application/json',
        )
        #######################################################################

        Service.delete(product)
Exemplo n.º 20
0
    def test_parse_error_json(self):
        expected_code = '0451'
        expected_message = 'Testing error message handling'
        expected_innererror_message = 'Detailed messages here'

        # Initial data ########################################################
        def request_callback(request):
            resp_body = {'error': {
                'code': expected_code,
                'message': expected_message,
                'innererror': {
                    'message': expected_innererror_message
                }
            }}
            headers = {
                'Content-Type': 'application/json;odata.metadata=minimal'
            }
            return requests.codes.bad_request, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        def action():
            try:
                Service.query(Product).first()
            except ODataError as e:
                errmsg = str(e)
                assert expected_code in errmsg, 'Code not in text'
                assert expected_message in errmsg, 'Upper level message not in text'
                assert expected_innererror_message in errmsg, 'Detailed message not in text'
                raise

        self.assertRaises(ODataError, action)
Exemplo n.º 21
0
    def test_delete(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 2048,
                'ProductName': 'This product will be deleted',
                'Category': 'Something',
                'Price': 1234.5,
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()

        # Delete call #########################################################
        def request_callback_delete(request):
            headers = {}
            return requests.codes.ok, headers, ''

        responses.add_callback(
            responses.DELETE, product.__odata__.instance_url,
            callback=request_callback_delete,
            content_type='application/json',
        )
        #######################################################################

        Service.delete(product)
Exemplo n.º 22
0
    def test_context_query_with_basic_auth(self):
        test_username = '******'
        test_password = '******'
        test_auth = (test_username, test_password)

        def request_callback(request):
            auth_text = request.headers.get('Authorization')
            _, auth_b64 = auth_text.split(' ', 1)
            decoded = base64.urlsafe_b64decode(auth_b64.encode()).decode()
            username, password = decoded.split(':', 1)

            self.assertEqual(test_username, username)
            self.assertEqual(test_password, password)

            headers = {}
            body = dict(value=[])
            return requests.codes.ok, headers, json.dumps(body)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(rsps.GET, Product.__odata_url__(), request_callback,
                              content_type='application/json')

            context = Service.create_context(auth=test_auth)
            context.query(Product).first()
Exemplo n.º 23
0
    def test_update(self):
        expected_id = 1024
        expected_name = 'Existing entity'
        expected_category = 'Existing category'
        expected_price = Decimal('85.2')

        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': expected_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()
        new_name = 'Changed value'

        # Patch call ##########################################################
        def request_callback_patch(request):
            payload = json.loads(request.body)
            assert 'ProductName' in payload
            assert payload['ProductName'] == new_name
            headers = {}
            return requests.codes.no_content, headers, ''

        responses.add_callback(
            responses.PATCH, product.__odata__.instance_url,
            callback=request_callback_patch,
            content_type='application/json',
        )
        #######################################################################

        # Reload call #########################################################
        def request_callback_reload(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': new_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, product.__odata__.instance_url,
            callback=request_callback_reload,
            content_type='application/json',
        )
        #######################################################################

        product.name = new_name
        Service.save(product)

        assert product.name == new_name
Exemplo n.º 24
0
    def test_update(self):
        expected_id = 1024
        expected_name = 'Existing entity'
        expected_category = 'Existing category'
        expected_price = Decimal('85.2')

        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': expected_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()
        new_name = 'Changed value'

        # Patch call ##########################################################
        def request_callback_patch(request):
            payload = json.loads(request.body)
            assert 'ProductName' in payload
            assert payload['ProductName'] == new_name
            headers = {}
            return requests.codes.no_content, headers, ''

        responses.add_callback(
            responses.PATCH,
            product.__odata__.instance_url,
            callback=request_callback_patch,
            content_type='application/json',
        )

        #######################################################################

        # Reload call #########################################################
        def request_callback_reload(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': new_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            product.__odata__.instance_url,
            callback=request_callback_reload,
            content_type='application/json',
        )
        #######################################################################

        product.name = new_name
        Service.save(product)

        assert product.name == new_name
Exemplo n.º 25
0
 def _call():
     Product.DemoAction()