Skip to content

Commit 4d47532

Browse files
committed
Add phpypam.api as fixture for most tests
Move api connection to function definition in `conftest.py` to provide it as a fixture for most tests. The fixture is module scoped.
1 parent 88cb2be commit 4d47532

File tree

8 files changed

+43
-78
lines changed

8 files changed

+43
-78
lines changed

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
"""Provide some base configurations for tests."""
22
import inspect
3+
import phpypam
34
import pytest
45
import py.path
56
import re
7+
import yaml
8+
69
from urllib.parse import urlparse, urlunparse
710

811
TEST_CASES_PATH = py.path.local(__file__).realpath() / '..' / 'test_cases'
912

13+
with open('tests/vars/server.yml') as c:
14+
server = yaml.safe_load(c)
15+
16+
17+
@pytest.fixture(scope='module')
18+
def pi(*arg, **kwargs):
19+
"""Create a phpypam.api object and return it.
20+
21+
:return: object phpypam
22+
:rtype: phpypam.api
23+
"""
24+
url = kwargs.pop('url', server['url'])
25+
app_id = kwargs.pop('app_id', server['app_id'])
26+
username = kwargs.pop('username', server['username'])
27+
password = kwargs.pop('password', server['password'])
28+
ssl_verify = kwargs.pop('ssl_verify', True)
29+
30+
return phpypam.api(
31+
url=url,
32+
app_id=app_id,
33+
username=username,
34+
password=password,
35+
ssl_verify=ssl_verify,
36+
**kwargs
37+
)
38+
1039

1140
def find_all_test_cases():
1241
"""Generate list of test cases.

tests/test_cases/controllers.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
"""Test controller method."""
2-
import phpypam
32
import pytest
43
import vcr
5-
import yaml
64

75
from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS, FILTER_RESPONSE_HEADERS
86

97

10-
with open('tests/vars/server.yml') as c:
11-
server = yaml.safe_load(c)
12-
13-
148
@vcr.use_cassette(cassette_name('test_controllers'),
159
filter_headers=FILTER_REQUEST_HEADERS,
1610
before_record_request=filter_request_uri,
1711
before_recorde_response=filter_response
1812
)
19-
def test_controllers():
13+
def test_controllers(pi):
2014
"""Test if controllers method returns correct datatype."""
21-
pi = phpypam.api(
22-
url=server['url'],
23-
app_id=server['app_id'],
24-
username=server['username'],
25-
password=server['password'],
26-
ssl_verify=True
27-
)
28-
2915
controllers = pi.controllers()
3016
assert isinstance(controllers, set)

tests/test_cases/ensure_nameserver.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111
from phpypam import PHPyPAMEntityNotFoundException
1212

1313

14-
pi = phpypam.api(
15-
url=server['url'],
16-
app_id=server['app_id'],
17-
username=server['username'],
18-
password=server['password'],
19-
ssl_verify=True
20-
)
21-
2214
my_nameserver = dict(
2315
name='my dns',
2416
namesrv1='127.0.01',
@@ -31,7 +23,7 @@
3123
before_record_request=filter_request_uri,
3224
before_recorde_response=filter_response
3325
)
34-
def test_create_nameserver():
26+
def test_create_nameserver(pi):
3527
"""Test to create a new nameserver.
3628
3729
Create a nameserver if it doesn't exists
@@ -50,7 +42,7 @@ def test_create_nameserver():
5042
before_record_request=filter_request_uri,
5143
before_recorde_response=filter_response
5244
)
53-
def test_update_nameserver():
45+
def test_update_nameserver(pi):
5446
"""Test to update an existing nameserver.
5547
5648
Update one field of an existing nameserver
@@ -70,7 +62,7 @@ def test_update_nameserver():
7062
before_record_request=filter_request_uri,
7163
before_recorde_response=filter_response
7264
)
73-
def test_delete_nameserver():
65+
def test_delete_nameserver(pi):
7466
"""Test to delete an existing nameserver.
7567
7668
Delete the nameserver which we created before

tests/test_cases/ensure_section.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111
with open('tests/vars/server.yml') as c:
1212
server = yaml.safe_load(c)
1313

14-
pi = phpypam.api(
15-
url=server['url'],
16-
app_id=server['app_id'],
17-
username=server['username'],
18-
password=server['password'],
19-
ssl_verify=True
20-
)
21-
2214
my_section = dict(
2315
name='foobar',
2416
description='new section',
@@ -31,7 +23,7 @@
3123
before_record_request=filter_request_uri,
3224
before_recorde_response=filter_response
3325
)
34-
def test_create_section():
26+
def test_create_section(pi):
3527
"""Test to create a new section.
3628
3729
Create a section if it doesn't exists
@@ -51,7 +43,7 @@ def test_create_section():
5143
before_record_request=filter_request_uri,
5244
before_recorde_response=filter_response
5345
)
54-
def test_update_section():
46+
def test_update_section(pi):
5547
"""Test to update an existing section.
5648
5749
Update one field of an existing section.
@@ -70,7 +62,7 @@ def test_update_section():
7062
before_record_request=filter_request_uri,
7163
before_recorde_response=filter_response
7264
)
73-
def test_delete_section():
65+
def test_delete_section(pi):
7466
"""Test to delete an existing section.
7567
7668
Delete one field of an existing section.

tests/test_cases/ensure_vlan.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111
with open('tests/vars/server.yml') as c:
1212
server = yaml.safe_load(c)
1313

14-
pi = phpypam.api(
15-
url=server['url'],
16-
app_id=server['app_id'],
17-
username=server['username'],
18-
password=server['password'],
19-
ssl_verify=True
20-
)
21-
2214
my_vlan = dict(
2315
name='my vlan',
2416
number='1337',
@@ -30,7 +22,7 @@
3022
before_record_request=filter_request_uri,
3123
before_recorde_response=filter_response
3224
)
33-
def test_create_vlan():
25+
def test_create_vlan(pi):
3426
"""Test to create a new vlan.
3527
3628
Create a vlan if it doesn't exists
@@ -49,7 +41,7 @@ def test_create_vlan():
4941
before_record_request=filter_request_uri,
5042
before_recorde_response=filter_response
5143
)
52-
def test_update_vlan():
44+
def test_update_vlan(pi):
5345
"""Test to update an existing vlan.
5446
5547
Update one field of an existing vlan
@@ -72,7 +64,7 @@ def test_update_vlan():
7264
before_record_request=filter_request_uri,
7365
before_recorde_response=filter_response
7466
)
75-
def test_delete_vlan():
67+
def test_delete_vlan(pi):
7668
"""Test to delete an existing vlan.
7769
7870
Delete vlan which we created before

tests/test_cases/search_address.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,9 @@
1717
before_record_request=filter_request_uri,
1818
before_recorde_response=filter_response
1919
)
20-
def test_address_not_found():
20+
def test_address_not_found(pi):
2121
"""Test address not found execption."""
22-
pi = phpypam.api(
23-
url=server['url'],
24-
app_id=server['app_id'],
25-
username=server['username'],
26-
password=server['password'],
27-
ssl_verify=True
28-
)
29-
3022
addr = '10.10.0.4'
31-
3223
search_kwargs = dict(controller='addresses', controller_path='search/' + addr)
3324

3425
pytest.raises(PHPyPAMEntityNotFoundException, pi.get_entity, **search_kwargs)

tests/test_cases/search_section_by_name.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,13 @@
1111
with open('tests/vars/server.yml') as c:
1212
server = yaml.safe_load(c)
1313

14-
pi = phpypam.api(
15-
url=server['url'],
16-
app_id=server['app_id'],
17-
username=server['username'],
18-
password=server['password'],
19-
ssl_verify=True
20-
)
21-
2214

2315
@vcr.use_cassette(cassette_name('test_search_not_existing_section'),
2416
filter_headers=FILTER_REQUEST_HEADERS,
2517
before_record_request=filter_request_uri,
2618
before_recorde_response=filter_response
2719
)
28-
def test_search_not_existing_section():
20+
def test_search_not_existing_section(pi):
2921
"""Search for non existing section.
3022
3123
Search for a non existign section and get NotFound exception
@@ -40,7 +32,7 @@ def test_search_not_existing_section():
4032
before_record_request=filter_request_uri,
4133
before_recorde_response=filter_response
4234
)
43-
def test_search_existing_section():
35+
def test_search_existing_section(pi):
4436
"""Search for existing section.
4537
4638
Search for an existing section

tests/test_cases/search_subnet.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,9 @@
1717
before_record_request=filter_request_uri,
1818
before_recorde_response=filter_response
1919
)
20-
def test_subnet_not_found():
20+
def test_subnet_not_found(pi):
2121
"""Test subnet not found exeption."""
22-
pi = phpypam.api(
23-
url=server['url'],
24-
app_id=server['app_id'],
25-
username=server['username'],
26-
password=server['password'],
27-
ssl_verify=True
28-
)
29-
3022
cidr = '10.0.0.0/24'
31-
3223
search_kwargs = dict(controller='subnets', controller_path='cidr/' + cidr)
3324

3425
pytest.raises(PHPyPAMEntityNotFoundException, pi.get_entity, **search_kwargs)

0 commit comments

Comments
 (0)