-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
83 lines (69 loc) · 2.91 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#! coding=utf-8
import sys
sys.dont_write_bytecode = True
from os import environ
import unittest
from doclient import DOClient, Droplet
from doclient.errors import InvalidArgumentError, APIAuthError, APIError
from doclient.meta import Domain, Snapshot
NoneType = type(None)
class DOClientTest(unittest.TestCase):
"""Tests for DigitalOcean client class"""
token = environ.get("DO_KEY")
invalid_token = ""
client = None
def setUp(self):
try:
self.client = DOClient(self.token)
except BaseException:
self.assertRaises(APIAuthError)
def test_valid_client(self):
"""Test valid DOClient instance initalization"""
if self.client:
self.assertTrue(hasattr(self.client, "filter_droplets") == True)
self.assertNotIsInstance(self.client.droplets, NoneType)
self.assertIsInstance(self.client.droplets, list)
for droplet in self.client.droplets:
self.assertIsInstance(droplet, Droplet)
def test_invalid_client(self):
"""Test invalid DOClient instance initalization"""
try:
client = DOClient(self.invalid_token)
except BaseException:
self.assertRaises(APIAuthError)
def test_domain_methods(self):
"""Tests for domain create, list, get, and delete methods"""
if self.client:
name = environ.get("test_domain_name")
ip_address = environ.get("test_domain_ip")
try:
create_response = self.client.create_domain(
name, ip_address)
self.assertIsInstance(create_response, dict)
domain = self.client.get_domain(name)
self.assertIsInstance(domain, Domain)
domains = self.client.get_domains()
self.assertIsInstance(domains, list)
all_domains = all((isinstance(domain, Domain)
for domain in domains))
self.assertTrue(all_domains)
del_response = self.client.delete_domain(name)
self.assertIsInstance(del_response, dict)
except BaseException as error:
self.assertIsInstance(error,
(APIAuthError, APIError, InvalidArgumentError))
def test_droplet_methods(self):
"""Test droplet functions"""
if self.client:
droplet = self.client.filter_droplets("f")[0]
self.assertIsInstance(droplet, Droplet)
snapshots = droplet.get_snapshots()
self.assertIsInstance(snapshots, list)
for snapshot in snapshots:
self.assertIsInstance(snapshot, Snapshot)
neighbours = droplet.get_neighbours()
self.assertIsInstance(neighbours, list)
for neighbour in neighbours:
self.assertIsInstance(neighbour, Droplet)
if __name__ == "__main__":
unittest.main()