Skip to content

Commit 7ab607c

Browse files
author
Abhilash Joseph C
committed
Added few test cases.
1 parent be40276 commit 7ab607c

File tree

6 files changed

+92
-76
lines changed

6 files changed

+92
-76
lines changed

py3resttest/binding.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class Context(object):
1212
""" Manages binding of variables & generators, with both variable name and generator name being strings """
13-
13+
# variables = {}
1414
def __init__(self):
1515
self.variables = {} # Maps variable name to current value
1616
self.generators = {} # Maps generator name to generator function
@@ -21,6 +21,7 @@ def bind_variable(self, variable_name, variable_value):
2121
This allows for passing in variables in testing """
2222
str_name = str(variable_name)
2323
prev = self.variables.get(str_name)
24+
2425
if prev != variable_value:
2526
self.variables[str(variable_name)] = variable_value
2627
self.mod_count = self.mod_count + 1

py3resttest/testcase.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __init__(self):
8686
def parse(self, base_url: str, testcase_list: List, test_file=None, working_directory=None, variable_dict=None):
8787

8888
if working_directory is None:
89-
working_directory = os.path.abspath(os.getcwd())
89+
working_directory = Path(os.path.abspath(os.getcwd()))
9090
else:
9191
working_directory = Path(working_directory)
9292
if variable_dict is None:
@@ -116,11 +116,12 @@ def parse(self, base_url: str, testcase_list: List, test_file=None, working_dire
116116
self.parse(base_url, import_testcase_list, variable_dict=variable_dict)
117117
elif key == YamlKeyWords.IMPORT:
118118
if sub_testcase_node not in self.__testcase_file:
119-
testcase_file_path = os.path.dirname(os.path.realpath(sub_testcase_node))
119+
testcase_file_path = sub_testcase_node
120120
logger.debug("Importing testcase from %s", testcase_file_path)
121+
testcase_file_path = working_directory.joinpath("%s" % testcase_file_path).resolve()
121122
self.__testcase_file.add(sub_testcase_node)
122123
import_testcase_list = read_testcase_file(testcase_file_path)
123-
with ChangeDir(testcase_file_path):
124+
with ChangeDir(working_directory):
124125
self.parse(base_url, import_testcase_list, variable_dict=variable_dict)
125126
elif key == YamlKeyWords.URL:
126127
__group_name = TestCaseGroup.DEFAULT_GROUP
@@ -368,6 +369,8 @@ def url(self):
368369
val = self.__url
369370
if not self.__abs_url:
370371
val = urljoin(self.__base_url, val)
372+
if isinstance(val, dict):
373+
logger.warning("URL is not applied template values.")
371374
return val
372375

373376
@url.setter
@@ -490,8 +493,10 @@ def body(self, value):
490493
if value:
491494
if isinstance(value, bytes):
492495
self.__body = ContentHandler.parse_content(value.decode())
493-
else:
496+
elif isinstance(value, str):
494497
self.__body = ContentHandler.parse_content(value)
498+
else:
499+
self.__body = value
495500
else:
496501
self.__body = value
497502

@@ -500,7 +505,7 @@ def failures(self):
500505
return self.__failure_list
501506

502507
def realize_template(self, variable_name, context):
503-
if context is None or self.templates is None or variable_name not in self.templates:
508+
if (context or self.templates) is None or (variable_name not in self.templates):
504509
return None
505510
if not context.get_values():
506511
return None

tests/content-test-include.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- include:
2+
- tests.content-test

tests/test_binding.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def test_variables(self):
1919
""" Test bind/return of variables """
2020

2121
context = Context()
22+
context.variables = {}
2223
self.assertTrue(context.get_value('foo') is None)
2324
self.assertEqual(0, context.mod_count)
2425

tests/test_testcase.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,31 @@ def test_config(self):
115115
}
116116
]
117117
self.assertRaises(TypeError, config_object.parse, config_list)
118+
test_case = TestCase('https://api.github.com', None, None)
119+
testcase_list = [
120+
{'name': 'Get github user abihijo89-to'},
121+
{'url': '/search/users?q=abhijo89-to'},
122+
{'method': 'GET'},
123+
{'headers': {'Content-Type': 'application/json'}},
124+
125+
]
126+
127+
test_case.parse(testcase_list)
128+
test_case.run()
129+
self.assertTrue(test_case.is_passed)
130+
131+
def test_include(self):
132+
with open("%s/content-test-include.yaml" % current_module_path.parent, 'r') as f:
133+
test_dict_list = yaml.safe_load(f.read())
134+
135+
ts = TestSet()
136+
ts.parse('', test_dict_list)
137+
self.assertEqual(1, len(ts.test_group_list_dict))
138+
ts.parse('', [{'url': 'http://google.com'}])
139+
self.assertEqual(1, len(ts.test_group_list_dict))
140+
ts.parse('', [{'import': 'tests/content-test.yaml'}])
141+
self.assertEqual(1, len(ts.test_group_list_dict))
142+
118143

119-
if __name__ == '__main__':
120-
unittest.main()
144+
if __name__ == '__main__':
145+
unittest.main()

tests/test_tests.py

Lines changed: 50 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# -*- coding: utf-8 -*-
2-
2+
import string
33
import unittest
44

5-
from py3resttest.exception import BindError, HttpMethodError, ValidatorError
6-
from py3resttest.validators import ComparatorValidator, ExtractTestValidator
75
from pytest import fail
86

97
from py3resttest import generators
108
from py3resttest.binding import Context
9+
from py3resttest.contenthandling import ContentHandler
10+
from py3resttest.exception import BindError, HttpMethodError, ValidatorError
1111
from py3resttest.testcase import TestCase
12+
from py3resttest.validators import ComparatorValidator, ExtractTestValidator
1213

1314

1415
class TestsTest(unittest.TestCase):
@@ -210,18 +211,18 @@ def test_parse_extractor_bind(self):
210211
test = TestCase('', None, None, context)
211212
test.parse(test_config)
212213
test.pre_update(context)
213-
# self.assertTrue(test.extract_binds)
214-
# self.assertEqual(2, len(test.extract_binds))
215-
# self.assertTrue('id' in test.extract_binds)
216-
# self.assertTrue('name' in test.extract_binds)
214+
self.assertTrue(test.extract_binds)
215+
self.assertEqual(2, len(test.extract_binds))
216+
self.assertTrue('id' in test.extract_binds)
217+
self.assertTrue('name' in test.extract_binds)
217218
#
218-
# # Test extractors config'd correctly for extraction
219-
# myjson = '{"idfield": 3, "firstname": "bob"}'
220-
# extracted = test.extract_binds['id'].extract(myjson)
221-
# self.assertEqual(3, extracted)
219+
# Test extractors config'd correctly for extraction
220+
myjson = '{"idfield": 3, "firstname": "bob"}'
221+
extracted = test.extract_binds['id'].extract(myjson)
222+
self.assertEqual(3, extracted)
222223
#
223-
# extracted = test.extract_binds['name'].extract(myjson)
224-
# self.assertEqual('bob', extracted)
224+
extracted = test.extract_binds['name'].extract(myjson)
225+
self.assertEqual('bob', extracted)
225226

226227
def test_parse_extractor_errors(self):
227228
""" Test that expected errors are thrown on parsing """
@@ -292,9 +293,11 @@ def test_parse_validator_extract_test(self):
292293
def test_variable_binding(self):
293294
""" Test that tests successfully bind variables """
294295
element = 3
295-
test_config = [{"url": "/ping"}, {"name": "cheese"},
296-
{"expected_status": ["200", 204, "202"]}]
297-
test_config.append({"variable_binds": {'var': 'value'}})
296+
test_config = [
297+
{"url": "/ping"}, {"name": "cheese"},
298+
{"expected_status": ["200", 204, "202"]},
299+
{"variable_binds": {'var': 'value'}}
300+
]
298301
context = Context()
299302
test = TestCase('', None, context)
300303
test.parse(test_config)
@@ -307,59 +310,38 @@ def test_variable_binding(self):
307310
test.pre_update(context)
308311
self.assertEqual('value', context.get_value('var'))
309312

313+
def test_url_templating(self):
314+
context = Context()
315+
test = TestCase('', None, None, context)
316+
test.url = {'template': "$cheese"}
317+
self.assertTrue(test.is_dynamic())
318+
self.assertEqual({'template': '$cheese'}, test.url)
319+
self.assertTrue(test.templates['url'])
320+
context.bind_variable('cheese', 'liquid_cheese')
321+
self.assertEqual('liquid_cheese', test.url)
322+
323+
def test_test_content_templating(self):
324+
context = Context()
325+
test = TestCase('', None, None, context)
326+
handler = ContentHandler()
327+
handler.is_template_content = True
328+
handler.content = '{"first_name": "Gaius","id": "$id","last_name": "Baltar","login": "$login"}'
329+
context.bind_variables({'id': 9, 'login': 'username'})
330+
test.body = handler
331+
test.pre_update(context=context)
332+
self.assertEqual(string.Template(handler.content).safe_substitute(context.get_values()),
333+
test.body)
334+
335+
def test_header_templating(self):
336+
context = Context()
337+
test = TestCase('', None, None, context)
338+
head_templated = {'x': {'template': "$val"}}
310339

311-
# def test_test_url_templating(self):
312-
# test = Test()
313-
# test.set_url('$cheese', isTemplate=True)
314-
# self.assertTrue(test.is_dynamic())
315-
# self.assertEqual('$cheese', test.get_url())
316-
# self.assertTrue(test.templates['url'])
317-
#
318-
# context = Context()
319-
# context.bind_variable('cheese', 'stilton')
320-
# self.assertEqual('stilton', test.get_url(context=context))
321-
#
322-
# realized = test.realize(context)
323-
# self.assertEqual('stilton', realized.url)
324-
325-
# def test_test_content_templating(self):
326-
# test = Test()
327-
# handler = ContentHandler()
328-
# handler.is_template_content = True
329-
# handler.content = '{"first_name": "Gaius","id": "$id","last_name": "Baltar","login": "$login"}'
330-
# context = Context()
331-
# context.bind_variables({'id': 9, 'login': 'kvothe'})
332-
# test.set_body(handler)
333-
#
334-
# templated = test.realize(context=context)
335-
# self.assertEqual(string.Template(handler.content).safe_substitute(context.get_values()),
336-
# templated.body)
337-
338-
# def test_header_templating(self):
339-
# test = Test()
340-
# head_templated = {'$key': "$val"}
341-
# context = Context()
342-
# context.bind_variables({'key': 'cheese', 'val': 'gouda'})
343-
#
344-
# # No templating applied
345-
# test.headers = head_templated
346-
# head = test.get_headers()
347-
# self.assertEqual(1, len(head))
348-
# self.assertEqual('$val', head['$key'])
349-
#
350-
# test.set_headers(head_templated, is_template=True)
351-
# self.assertTrue(test.templates)
352-
# self.assertTrue(test.NAME_HEADERS in test.templates)
353-
#
354-
# # No context, no templating
355-
# head = test.headers
356-
# self.assertEqual(1, len(head))
357-
# self.assertEqual('$val', head['$key'])
358-
#
359-
# # Templated with context
360-
# head = test.get_headers(context=context)
361-
# self.assertEqual(1, len(head))
362-
# self.assertEqual('gouda', head['cheese'])
340+
context.bind_variables({'val': 'gouda'})
341+
# No templating applied
342+
test.headers = head_templated
343+
self.assertEqual(1, len(test.headers))
344+
self.assertEqual('gouda', test.headers['x'])
363345

364346
def test_update_context_variables(self):
365347
variable_binds = {'foo': 'correct', 'test': 'value'}

0 commit comments

Comments
 (0)