1
1
# -*- coding: utf-8 -*-
2
-
2
+ import string
3
3
import unittest
4
4
5
- from py3resttest .exception import BindError , HttpMethodError , ValidatorError
6
- from py3resttest .validators import ComparatorValidator , ExtractTestValidator
7
5
from pytest import fail
8
6
9
7
from py3resttest import generators
10
8
from py3resttest .binding import Context
9
+ from py3resttest .contenthandling import ContentHandler
10
+ from py3resttest .exception import BindError , HttpMethodError , ValidatorError
11
11
from py3resttest .testcase import TestCase
12
+ from py3resttest .validators import ComparatorValidator , ExtractTestValidator
12
13
13
14
14
15
class TestsTest (unittest .TestCase ):
@@ -210,18 +211,18 @@ def test_parse_extractor_bind(self):
210
211
test = TestCase ('' , None , None , context )
211
212
test .parse (test_config )
212
213
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 )
217
218
#
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 )
222
223
#
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 )
225
226
226
227
def test_parse_extractor_errors (self ):
227
228
""" Test that expected errors are thrown on parsing """
@@ -292,9 +293,11 @@ def test_parse_validator_extract_test(self):
292
293
def test_variable_binding (self ):
293
294
""" Test that tests successfully bind variables """
294
295
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
+ ]
298
301
context = Context ()
299
302
test = TestCase ('' , None , context )
300
303
test .parse (test_config )
@@ -307,59 +310,38 @@ def test_variable_binding(self):
307
310
test .pre_update (context )
308
311
self .assertEqual ('value' , context .get_value ('var' ))
309
312
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" }}
310
339
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' ])
363
345
364
346
def test_update_context_variables (self ):
365
347
variable_binds = {'foo' : 'correct' , 'test' : 'value' }
0 commit comments