From fbda8d6f964cbdbcea43c005f57739090ab8836b Mon Sep 17 00:00:00 2001 From: Tim Powell Date: Mon, 21 Jan 2019 20:51:23 -0500 Subject: [PATCH 1/2] Move setup so tests can complete when offline --- odata/tests/test_nw_reflect_model.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/odata/tests/test_nw_reflect_model.py b/odata/tests/test_nw_reflect_model.py index b17b942..68c84c4 100644 --- a/odata/tests/test_nw_reflect_model.py +++ b/odata/tests/test_nw_reflect_model.py @@ -3,17 +3,28 @@ import unittest from odata.service import ODataService +from odata.exceptions import ODataConnectionError -url = 'http://services.odata.org/V4/Northwind/Northwind.svc/' -Service = ODataService(url, reflect_entities=True) -Customer = Service.entities.get('Customers') -Product = Service.entities.get('Products') +Service = None +Customer = None +Product = None @unittest.skip('unavailable') class NorthwindReflectModelReadTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + global Service, Customer, Product + url = 'http://services.odata.org/V4/Northwind/Northwind.svc/' + try: + Service = ODataService(url, reflect_entities=True) + except ODataConnectionError: + raise unittest.SkipTest('Unable to connect to Northwind service') + Customer = Service.entities.get('Customers') + Product = Service.entities.get('Products') + def test_query_one(self): q = Service.query(Customer) q = q.filter(Customer.ContactTitle.startswith('Sales')) From 54014966f0490dad8b5433d03a6f1b2cf7ec667d Mon Sep 17 00:00:00 2001 From: Tim Powell Date: Wed, 11 Dec 2019 17:15:34 -0500 Subject: [PATCH 2/2] Add env var to run remote tests, update README --- README.md | 18 ++++++++++++++++++ odata/tests/test_nw_manual_model.py | 13 ++++++++++++- odata/tests/test_nw_reflect_model.py | 5 +++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e6365b2..fec1221 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,21 @@ for order in query: order.Employee = empl Service.save(order) ``` + +## Running tests + +To run the tests, you can use pytest or unittest: + +- python -m pytest +- python -m unittest discover + +To include tests that call the Northwind service, set the envionment variable: + +``` +export ODATA_DO_REMOTE_TESTS=1 +``` + +The Northwind tests are automatically skipped if it cannot connect to the service. + +Test dependency: +- responses diff --git a/odata/tests/test_nw_manual_model.py b/odata/tests/test_nw_manual_model.py index 0aa982b..f615f96 100644 --- a/odata/tests/test_nw_manual_model.py +++ b/odata/tests/test_nw_manual_model.py @@ -1,8 +1,10 @@ # -*- coding: utf-8 -*- import unittest +import os from odata.service import ODataService +from odata.exceptions import ODataConnectionError from odata.entity import declarative_base from odata.property import StringProperty, IntegerProperty @@ -41,9 +43,18 @@ class Product(Base): quantity_per_unit = StringProperty('QuantityPerUnit') -@unittest.skip('unavailable') +@unittest.skipUnless(os.environ.get('ODATA_DO_REMOTE_TESTS', False), + 'Avoid Northwind service unless requested') class NorthwindManualModelReadTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + try: + # Test query to make sure we have an OData connection + q = service.query(Customer).first() + except ODataConnectionError: + raise unittest.SkipTest('Unable to connect to Northwind service') + def test_query_one(self): q = service.query(Customer) q = q.filter(Customer.contact_title.startswith('Sales')) diff --git a/odata/tests/test_nw_reflect_model.py b/odata/tests/test_nw_reflect_model.py index 68c84c4..5c6164e 100644 --- a/odata/tests/test_nw_reflect_model.py +++ b/odata/tests/test_nw_reflect_model.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import unittest +import os from odata.service import ODataService from odata.exceptions import ODataConnectionError @@ -10,8 +11,8 @@ Customer = None Product = None - -@unittest.skip('unavailable') +@unittest.skipUnless(os.environ.get('ODATA_DO_REMOTE_TESTS', False), + 'Avoid Northwind service unless requested') class NorthwindReflectModelReadTest(unittest.TestCase): @classmethod