Exemple #1
0
def step_impl(context):
    context.rest = REST()
    assert context.rest is not None
 def setUp(self):
     self.rest = REST()
Exemple #3
0
 def setUp(self):
     self.rest = REST()
class restingTests(unittest.TestCase):
    def setUp(self):
        self.rest = REST()

    def assert_valid_dict(self, dict):
        for key in ['id', 'body', 'title', 'userId']:
            if key not in dict.keys():
                raise self.failureException("%s not in dict"%key)

            if dict[key] is None:
                raise self.failureException("no value for %s"%key)

        #validate_json(str(dict))

    def assert_valid_list(self, list):
        for dict in list:
            self.assert_valid_dict(dict)


    def test_url_show_resource(self):
        url = self.rest.show_resource(number='1').url
        self.assertEqual(BASE_URL+'/posts/1', url)

    def test_show_resource(self):
        j = self.rest.show_resource(number='1').json
        pp = pprint.PrettyPrinter(indent=4)
        #pp.pprint(j)
        # Verify valid json
        self.assertTrue(isinstance(j,dict))
        self.assert_valid_dict(j)


    def test_url_list_resources(self):
        url = self.rest.list_resources().url
        self.assertEqual(BASE_URL+'/posts', url)

    def test_list_resources(self):
        j = self.rest.list_resources().json
        pp = pprint.PrettyPrinter(indent=4)
        #pp.pprint(j)
        self.assertTrue(isinstance(j,list))
        self.assert_valid_list(j)

    def test_url_create_resource(self):
        data1 = {"title":"foo", "body":"bar", "userId":"1"}
        data = json.dumps(data1)
        j, text = self.rest.create_resource().create(data)
        expect = {"id":101, "title":"foo", "body":"bar", "userId":1}
        text = json.loads(text)

        self.assertTrue(j.status_code == requests.codes.ok)

        self.assertEqual(expect, text)

    def test_url_update_resource(self):

        data1 = {'title':'foo', 'body':'bar', 'userId':'1'}
        data = json.dumps(data1)     
        j, text = self.rest.update_resource(number='1').update(data)
        expect = {"id":1, "title":'foo', "body":'bar', "userId":1}
        text = json.loads(text)

        self.assertTrue(j.status_code == requests.codes.ok)
        self.assertEqual(expect, text)


    def test_delete_resource(self):
        r = self.rest.delete_resource(number='1').delete()

        #Delete should return 204 instead of 200, weird??

        self.assertTrue(r.status_code == requests.codes.ok)
Exemple #5
0
class restingTests(unittest.TestCase):
    def setUp(self):
        self.rest = REST()

    def assert_valid_dict(self, dict):
        for key in ['id', 'body', 'title', 'userId']:
            if key not in dict.keys():
                raise self.failureException("%s not in dict" % key)

            if dict[key] is None:
                raise self.failureException("no value for %s" % key)

        validate_json(str(dict))

    def assert_valid_list(self, list):
        for dict in list:
            self.assert_valid_dict(dict)

    def test_list_primitives(self):
        prim = self.rest.show_primitives().options()
        ll = set(prim.split(','))
        at_least = set(['PUT', 'GET'])
        self.assertTrue(at_least.issubset(ll))

    def test_url_show_resource(self):
        url = self.rest.show_resource(number='1').url
        self.assertEqual(BASE_URL + '/posts/1', url)

    def test_show_resource(self):
        j = self.rest.show_resource(number='1').json
        pp = pprint.PrettyPrinter(indent=4)
        #pp.pprint(j)
        k = json.dumps(j)
        #pp.pprint(k)
        # Verify valid json
        self.assertTrue(isinstance(j, dict))
        self.assert_valid_dict(j)

    def test_url_list_resources(self):
        url = self.rest.list_resources().url
        self.assertEqual(BASE_URL + '/posts', url)

    def test_list_resources(self):
        j = self.rest.list_resources().json
        pp = pprint.PrettyPrinter(indent=4)
        #pp.pprint(j)
        self.assertTrue(isinstance(j, list))
        self.assert_valid_list(j)

    def test_url_create_resource(self):
        data1 = {"title": "foo", "body": "bar", "userId": "1"}
        data = json.dumps(data1)
        j, text = self.rest.create_resource().create(data)
        expect = {"id": 101, "title": "foo", "body": "bar", "userId": 1}
        text = json.loads(text)

        self.assertTrue(j.status_code == requests.codes.ok)

        self.assertEqual(expect, text)

    def test_url_update_resource(self):

        data1 = {'title': 'foo', 'body': 'bar', 'userId': '1'}
        data = json.dumps(data1)
        j, text = self.rest.update_resource(number='1').update(data)
        expect = {"id": 1, "title": 'foo', "body": 'bar', "userId": 1}
        text = json.loads(text)

        self.assertTrue(j.status_code == requests.codes.ok)
        self.assertEqual(expect, text)

    def test_delete_resource(self):
        r = self.rest.delete_resource(number='1').delete()

        #Delete should return 204 instead of 200, weird??

        self.assertTrue(r.status_code == requests.codes.ok)