Skip to content

Commit e180847

Browse files
Cleaned up tests, verified to pass on all supported Python versions with tox
1 parent 8298dcb commit e180847

6 files changed

Lines changed: 74 additions & 106 deletions

File tree

sendgrid/resources/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

test/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

test/base_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sendgrid
2+
from sendgrid.client import SendGridAPIClient
3+
try:
4+
import urllib.request as urllib_request
5+
from urllib.parse import urlencode
6+
from urllib.error import HTTPError
7+
except ImportError: # Python 2
8+
import urllib2 as urllib_request
9+
from urllib2 import HTTPError
10+
from urllib import urlencode
11+
12+
class BaseTest():
13+
def __init__(self):
14+
pass
15+
16+
class MockSendGridAPIClientRequest(SendGridAPIClient):
17+
def __init__(self, apikey, **opts):
18+
super(MockSendGridAPIClientRequest, self).__init__(apikey, **opts)
19+
self._req = None
20+
21+
def _build_request(self, url=None, json_header=False, method='GET', data=None):
22+
req = urllib_request.Request(url)
23+
req.get_method = lambda: method
24+
req.add_header('User-Agent', self.useragent)
25+
req.add_header('Authorization', 'Bearer ' + self.apikey)
26+
if json_header:
27+
req.add_header('Content-Type', 'application/json')
28+
body = data
29+
if method == 'POST':
30+
response = 201
31+
if method == 'PATCH':
32+
response = 200
33+
if method == 'DELETE':
34+
response = 204
35+
if method == 'GET':
36+
response = 200
37+
return response, body

test/test_api_client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from .base_test import BaseTest, MockSendGridAPIClientRequest
2+
import os
3+
try:
4+
import unittest2 as unittest
5+
except ImportError:
6+
import unittest
7+
try:
8+
from StringIO import StringIO
9+
except ImportError: # Python 3
10+
from io import StringIO
11+
12+
import sendgrid
13+
from sendgrid.client import SendGridAPIClient
14+
from sendgrid.version import __version__
15+
16+
SG_KEY = os.getenv('SG_KEY') or 'SENDGRID_APIKEY'
17+
18+
class TestSendGridAPIClient(unittest.TestCase):
19+
def setUp(self):
20+
self.client = MockSendGridAPIClientRequest
21+
self.client = SendGridAPIClient(SG_KEY)
22+
23+
def test_apikey_init(self):
24+
self.assertEqual(self.client.apikey, SG_KEY)
25+
26+
def test_useragent(self):
27+
useragent = 'sendgrid/' + __version__ + ';python_v3'
28+
self.assertEqual(self.client.useragent, useragent)
29+
30+
def test_host(self):
31+
host = 'https://api.sendgrid.com'
32+
self.assertEqual(self.client.host, host)
33+
34+
if __name__ == '__main__':
35+
unittest.main()

test/test_apikeys.py

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,20 @@
1+
from .base_test import BaseTest, MockSendGridAPIClientRequest
12
import os
23
try:
34
import unittest2 as unittest
45
except ImportError:
56
import unittest
6-
import json
7-
import sys
87
try:
98
from StringIO import StringIO
109
except ImportError: # Python 3
1110
from io import StringIO
12-
try:
13-
import urllib.request as urllib_request
14-
from urllib.parse import urlencode
15-
from urllib.error import HTTPError
16-
except ImportError: # Python 2
17-
import urllib2 as urllib_request
18-
from urllib2 import HTTPError
19-
from urllib import urlencode
2011

2112
import sendgrid
22-
from sendgrid import SendGridClient, Mail
23-
from sendgrid.exceptions import SendGridClientError, SendGridServerError
24-
from sendgrid.sendgrid import HTTPError
2513
from sendgrid.client import SendGridAPIClient
2614
from sendgrid.version import __version__
2715

2816
SG_KEY = os.getenv('SG_KEY') or 'SENDGRID_APIKEY'
2917

30-
class MockSendGridAPIClientRequest(SendGridAPIClient):
31-
def __init__(self, apikey, **opts):
32-
super(MockSendGridAPIClientRequest, self).__init__(apikey, **opts)
33-
self._req = None
34-
35-
def _build_request(self, url=None, json_header=False, method='GET', data=None):
36-
req = urllib_request.Request(url)
37-
req.get_method = lambda: method
38-
req.add_header('User-Agent', self.useragent)
39-
req.add_header('Authorization', 'Bearer ' + self.apikey)
40-
if json_header:
41-
req.add_header('Content-Type', 'application/json')
42-
body = data
43-
if method == 'POST':
44-
response = 201
45-
if method == 'PATCH':
46-
response = 200
47-
if method == 'DELETE':
48-
response = 204
49-
if method == 'GET':
50-
response = 200
51-
return response, body
52-
53-
class TestSendGridAPIClient(unittest.TestCase):
54-
def setUp(self):
55-
self.client = MockSendGridAPIClientRequest
56-
self.client = SendGridAPIClient(SG_KEY)
57-
58-
def test_apikey_init(self):
59-
self.assertEqual(self.client.apikey, SG_KEY)
60-
61-
def test_useragent(self):
62-
useragent = 'sendgrid/' + __version__ + ';python_v3'
63-
self.assertEqual(self.client.useragent, useragent)
64-
65-
def test_host(self):
66-
host = 'https://api.sendgrid.com'
67-
self.assertEqual(self.client.host, host)
68-
6918
class TestAPIKeys(unittest.TestCase):
7019
def setUp(self):
7120
SendGridAPIClient = MockSendGridAPIClientRequest

test/test_asm_groups.py

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,20 @@
1+
from .base_test import BaseTest, MockSendGridAPIClientRequest
12
import os
23
try:
34
import unittest2 as unittest
45
except ImportError:
56
import unittest
6-
import json
7-
import sys
87
try:
98
from StringIO import StringIO
109
except ImportError: # Python 3
1110
from io import StringIO
12-
try:
13-
import urllib.request as urllib_request
14-
from urllib.parse import urlencode
15-
from urllib.error import HTTPError
16-
except ImportError: # Python 2
17-
import urllib2 as urllib_request
18-
from urllib2 import HTTPError
19-
from urllib import urlencode
2011

2112
import sendgrid
22-
from sendgrid import SendGridClient, Mail
23-
from sendgrid.exceptions import SendGridClientError, SendGridServerError
24-
from sendgrid.sendgrid import HTTPError
2513
from sendgrid.client import SendGridAPIClient
2614
from sendgrid.version import __version__
2715

2816
SG_KEY = os.getenv('SG_KEY') or 'SENDGRID_APIKEY'
2917

30-
class MockSendGridAPIClientRequest(SendGridAPIClient):
31-
def __init__(self, apikey, **opts):
32-
super(MockSendGridAPIClientRequest, self).__init__(apikey, **opts)
33-
self._req = None
34-
35-
def _build_request(self, url=None, json_header=False, method='GET', data=None):
36-
req = urllib_request.Request(url)
37-
req.get_method = lambda: method
38-
req.add_header('User-Agent', self.useragent)
39-
req.add_header('Authorization', 'Bearer ' + self.apikey)
40-
if json_header:
41-
req.add_header('Content-Type', 'application/json')
42-
body = data
43-
if method == 'POST':
44-
response = 201
45-
if method == 'PATCH':
46-
response = 200
47-
if method == 'DELETE':
48-
response = 204
49-
if method == 'GET':
50-
response = 200
51-
return response, body
52-
53-
class TestSendGridAPIClient(unittest.TestCase):
54-
def setUp(self):
55-
self.client = MockSendGridAPIClientRequest
56-
self.client = SendGridAPIClient(SG_KEY)
57-
58-
def test_apikey_init(self):
59-
self.assertEqual(self.client.apikey, SG_KEY)
60-
61-
def test_useragent(self):
62-
useragent = 'sendgrid/' + __version__ + ';python_v3'
63-
self.assertEqual(self.client.useragent, useragent)
64-
65-
def test_host(self):
66-
host = 'https://api.sendgrid.com'
67-
self.assertEqual(self.client.host, host)
68-
6918
class TestASMGroups(unittest.TestCase):
7019
def setUp(self):
7120
SendGridAPIClient = MockSendGridAPIClientRequest

0 commit comments

Comments
 (0)