<commit_before>from __future__ import with_statement

try:
    import json
except ImportError:
    import simplejson as json

import logging
import random
import sys
import unittest

from remoteobjects import tests
import typepad

class TestRemoteObjects(unittest.TestCase):

    def http(self, *args, credentials=None, **kwargs):
        return tests.MockedHttp(*args, **kwargs)

    def testUser(self):
        content = """{"displayName": "<NAME>", "email": "<EMAIL>"}"""
        headers = { 'accept': 'application/json' }
        with self.mockHttp('http://127.0.0.1:8000/users/1.json', content, headers=headers) as h:
            user = typepad.User.get('http://127.0.0.1:8000/users/1.json', http=h)
            self.assertEquals(user.display_name, '<NAME>')
            self.assertEquals(user.email, '<EMAIL>')

    def testGroup(self):
        content = """{"displayName": "<NAME>"}"""
        headers = { 'accept': 'application/json' }
        with self.mockHttp('http://127.0.0.1:8000/groups/1.json', content, headers=headers) as h:
            group = typepad.Group.get('http://127.0.0.1:8000/groups/1.json', http=h)
            self.assertEquals(group.display_name, '<NAME>or')

    def testGroupMembers(self):
        g = typepad.Group()
        g._id = 'http://127.0.0.1:8000/groups/1.json'

        adminstatus  = dict(types=["tag:api.typepad.com,2009:%s" % x for x in ('Admin', 'Member')])
        memberstatus = dict(types=["tag:api.typepad.com,2009:Member"])
        users = [
            {"displayName": "Mike", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
        ]
        group = {
            "mumble": "butter",
        }
        content = json.dumps({
            "total-results": 5,
            "start-index":   0,
            "entries": [dict(status=adminstatus, target=group, source=users[0])] +
                [dict(status=memberstatus, target=group, source=u) for u in users[1:]],
        })

        headers = { 'accept': 'application/json' }
        with self.mockHttp('http://127.0.0.1:8000/groups/1/memberships.json', content, headers=headers) as h:
            m = g.memberships
            m._http = h
            self.assertEquals(len(m.entries), 7)
            self.assertEquals([u.source.display_name for u in m.entries],
                ['Mike', '<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>'])

        # Test sequence behavior of the API list.
        self.assertEquals(m.entries.__len__(), 7)
        self.assertEquals(m.__len__(), 7)
        self.assertEquals(len(m), 7)
        self.assert_(isinstance(m[0], typepad.RemoteObject))
        self.assertEquals(m[0].source.display_name, 'Mike')
        self.assertEquals([u.source.display_name for u in m[1:3]],
            ['<NAME>', '<NAME>'])

        # Test limit/offset parameters.
        with self.mockHttp('http://127.0.0.1:8000/groups/1/memberships.json?start-index=0', content, headers=headers) as h:
            m = g.memberships.filter(start_index=0)
            m._http = h
            m.deliver()

    def testCreateDeletePost(self):
        g = typepad.Group.get('http://127.0.0.1:8000/groups/1.json')

        somenum = random.randint(1, 10000)
        p = typepad.Post(
            title="New post #%d" % (somenum,),
            content="Hi this post has some content is it not nifty"
        )

        headers = { 'accept': 'application/json' }
        content = """{"content": "Hi this post has some content is it not nifty", "objectTypes": ["tag:api.typepad.com,2009:Post"], "title": "New post #%d"}""" % (somenum,)
        request = dict(url='http://127.0.0.1:8000/groups/1/assets.json', headers=headers, body=content, method='POST')
        resp_content = """{
            "title": "New post #%d",
            "content": "Hi this post has some content is it not nifty",
            "objectTypes": ["tag:api.typepad.com,2009:Post"],
            "published": "2009-03-23T00:00:00Z",
            "updated": "2009-03-23T00:00:00Z"
        }""" % (somenum,)
        response = dict(status=201, content=resp_content)
        response['location'] = 'http://127.0.0.1:8000/assets/307.json'
        with self.mockHttp(request, response, credentials=('<EMAIL>', 'password')) as h:
            g.assets.post(p, http=h)

        self.assert_(p._id is not None)
        self.assertEquals(p.title, "New post #%d" % (somenum,))
        self.assert_(hasattr(p, 'published'))
        self.assert_(p.published is not None)

        request = dict(url=p._id, headers=headers)
        with self.mockHttp(request, resp_content) as h:
            post_got = typepad.Post.get(p._id, http=h)
            self.assertEquals(post_got.title, 'New post #%d' % (somenum,))
            self.assertEquals(post_got._id, p._id)

        del_headers = {'if-match': '7', 'accept': 'application/json'}
        request = dict(url=p._id, method='DELETE', headers=del_headers)
        response = dict(status=204)
        with self.mockHttp(request, response) as h:
            p.delete(http=h)

        self.assert_(p._id is None)

        request = dict(url=post_got._id, headers=headers)
        response = dict(status=404)
        with self.mockHttp(request, response) as h:
            not_there = typepad.Post.get(post_got._id, http=h)
            self.assertRaises(typepad.Post.NotFound, lambda: not_there.title)

    def testChangePost(self):
        # Get post #1 directly from group #1?
        g = typepad.Group.get('http://127.0.0.1:8000/groups/1.json')

        headers = { 'accept': 'application/json' }
        content = """{
            "title": "Fames Vivamus Placerat at Condimentum at Primis Consectetuer Nonummy Inceptos Porta dis",
            "content": "Posuere felis vestibulum nibh justo vitae elementum.",
            "objectTypes": ["tag:api.typepad.com,2009:Post"]
        }"""
        request = dict(url='http://127.0.0.1:8000/assets/1.json', headers=headers)
        with self.mockHttp(request, content) as h:
            e = typepad.Post.get('http://127.0.0.1:8000/assets/1.json', http=h)
            self.assertEquals(e.title, 'Fames Vivamus Placerat at Condimentum at Primis Consectetuer Nonummy Inceptos Porta dis')
            self.assertEquals(e.content, 'Posuere felis vestibulum nibh justo vitae elementum.')
        old_etag = e._etag

        # Modify the existing post.
        e.title = 'Omg hai'
        e.content = 'Yay this is my post'

        # Save the modified post.
        headers = {
            'accept': 'application/json',
            'if-match': old_etag,  # default etag
        }
        content = """{"content": "Yay this is my post", "objectTypes": ["tag:api.typepad.com,2009:Post"], "title": "Omg hai"}"""
        request = dict(url='http://127.0.0.1:8000/assets/1.json', method='PUT', headers=headers, body=content)
        response = dict(content=content, etag='xyz')
        with self.mockHttp(request, response, credentials=('<EMAIL>', 'password')) as h:
            e.put(http=h)
        self.assertEquals(e.title, 'Omg hai')
        self.assertNotEqual(e._etag, old_etag)

        # Make a new post in group #1.

if __name__ == '__main__':
    tests.log()
    unittest.main()<commit_msg>Use requests with 'uri' and built-in headers here too<commit_after>from __future__ import with_statement

try:
    import json
except ImportError:
    import simplejson as json

import logging
import random
import sys
import unittest

from remoteobjects import tests
import typepad

class TestRemoteObjects(unittest.TestCase):

    def http(self, request, response, credentials=None):
        return tests.MockedHttp(request, response)

    def testUser(self):
        content = """{"displayName": "<NAME>", "email": "<EMAIL>"}"""
        request = {
            'uri': 'http://127.0.0.1:8000/users/1.json',
            'headers': {'accept': 'application/json'},
        }
        with self.http(request, content) as h:
            user = typepad.User.get('http://127.0.0.1:8000/users/1.json', http=h)
            self.assertEquals(user.display_name, '<NAME>')
            self.assertEquals(user.email, '<EMAIL>')

    def testGroup(self):
        content = """{"displayName": "Augue Tempor"}"""
        request = {
            'uri': 'http://127.0.0.1:8000/groups/1.json',
            'headers': {'accept': 'application/json'},
        }
        with self.http(request, content) as h:
            group = typepad.Group.get('http://127.0.0.1:8000/groups/1.json', http=h)
            self.assertEquals(group.display_name, 'Augue Tempor')

    def testGroupMembers(self):
        g = typepad.Group()
        g._id = 'http://127.0.0.1:8000/groups/1.json'

        adminstatus  = dict(types=["tag:api.typepad.com,2009:%s" % x for x in ('Admin', 'Member')])
        memberstatus = dict(types=["tag:api.typepad.com,2009:Member"])
        users = [
            {"displayName": "Mike", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
            {"displayName": "<NAME>", "email": "<EMAIL>"},
        ]
        group = {
            "mumble": "butter",
        }
        content = json.dumps({
            "total-results": 5,
            "start-index":   0,
            "entries": [dict(status=adminstatus, target=group, source=users[0])] +
                [dict(status=memberstatus, target=group, source=u) for u in users[1:]],
        })

        request = {
            'uri': 'http://127.0.0.1:8000/groups/1/memberships.json',
            'headers': {'accept': 'application/json'},
        }
        with self.http(request, content) as h:
            m = g.memberships
            m._http = h
            self.assertEquals(len(m.entries), 7)
            self.assertEquals([u.source.display_name for u in m.entries],
                ['Mike', '<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>'])

        # Test sequence behavior of the API list.
        self.assertEquals(m.entries.__len__(), 7)
        self.assertEquals(m.__len__(), 7)
        self.assertEquals(len(m), 7)
        self.assert_(isinstance(m[0], typepad.RemoteObject))
        self.assertEquals(m[0].source.display_name, 'Mike')
        self.assertEquals([u.source.display_name for u in m[1:3]],
            ['<NAME>', '<NAME>'])

        # Test limit/offset parameters.
        request = {
            'uri': 'http://127.0.0.1:8000/groups/1/memberships.json?start-index=0',
            'headers': {'accept': 'application/json'},
        }
        with self.http(request, content) as h:
            m = g.memberships.filter(start_index=0)
            m._http = h
            m.deliver()

    def testCreateDeletePost(self):
        g = typepad.Group.get('http://127.0.0.1:8000/groups/1.json')

        somenum = random.randint(1, 10000)
        p = typepad.Post(
            title="New post #%d" % (somenum,),
            content="Hi this post has some content is it not nifty"
        )

        headers = { 'accept': 'application/json' }
        content = """{"content": "Hi this post has some content is it not nifty", "objectTypes": ["tag:api.typepad.com,2009:Post"], "title": "New post #%d"}""" % (somenum,)
        request = dict(uri='http://1192.168.127.12:8000/groups/1/assets.json', headers=headers, body=content, method='POST')
        resp_content = """{
            "title": "New post #%d",
            "content": "Hi this post has some content is it not nifty",
            "objectTypes": ["tag:api.typepad.com,2009:Post"],
            "published": "2009-03-23T00:00:00Z",
            "updated": "2009-03-23T00:00:00Z"
        }""" % (somenum,)
        response = dict(status=201, content=resp_content)
        response['location'] = 'http://127.0.0.1:8000/assets/307.json'
        with self.http(request, response, credentials=('<EMAIL>', 'password')) as h:
            g.assets.post(p, http=h)

        self.assert_(p._id is not None)
        self.assertEquals(p.title, "New post #%d" % (somenum,))
        self.assert_(hasattr(p, 'published'))
        self.assert_(p.published is not None)

        request = dict(uri=p._id, headers=headers)
        with self.http(request, resp_content) as h:
            post_got = typepad.Post.get(p._id, http=h)
            self.assertEquals(post_got.title, 'New post #%d' % (somenum,))
            self.assertEquals(post_got._id, p._id)

        del_headers = {'if-match': '7', 'accept': 'application/json'}
        request = dict(uri=p._id, method='DELETE', headers=del_headers)
        response = dict(status=204)
        with self.http(request, response) as h:
            p.delete(http=h)

        self.assert_(p._id is None)

        request = dict(uri=post_got._id, headers=headers)
        response = dict(status=404)
        with self.http(request, response) as h:
            not_there = typepad.Post.get(post_got._id, http=h)
            self.assertRaises(typepad.Post.NotFound, lambda: not_there.title)

    def testChangePost(self):
        # Get post #1 directly from group #1?
        g = typepad.Group.get('http://127.0.0.1:8000/groups/1.json')

        headers = { 'accept': 'application/json' }
        content = """{
            "title": "Fames Vivamus Placerat at Condimentum at Primis Consectetuer Nonummy Inceptos Porta dis",
            "content": "Posuere felis vestibulum nibh justo vitae elementum.",
            "objectTypes": ["tag:api.typepad.com,2009:Post"]
        }"""
        request = dict(uri='http://127.0.0.1:8000/assets/1.json', headers=headers)
        with self.http(request, content) as h:
            e = typepad.Post.get('http://127.0.0.1:8000/assets/1.json', http=h)
            self.assertEquals(e.title, 'Fames Vivamus Placerat at Condimentum at Primis Consectetuer Nonummy Inceptos Porta dis')
            self.assertEquals(e.content, 'Posuere felis vestibulum nibh justo vitae elementum.')
        old_etag = e._etag

        # Modify the existing post.
        e.title = 'Omg hai'
        e.content = 'Yay this is my post'

        # Save the modified post.
        headers = {
            'accept': 'application/json',
            'if-match': old_etag,  # default etag
        }
        content = """{"content": "Yay this is my post", "objectTypes": ["tag:api.typepad.com,2009:Post"], "title": "Omg hai"}"""
        request = dict(uri='http://127.0.0.1:8000/assets/1.json', method='PUT', headers=headers, body=content)
        response = dict(content=content, etag='xyz')
        with self.http(request, response, credentials=('<EMAIL>', 'password')) as h:
            e.put(http=h)
        self.assertEquals(e.title, 'Omg hai')
        self.assertNotEqual(e._etag, old_etag)

        # Make a new post in group #1.

if __name__ == '__main__':
    tests.log()
    unittest.main()
{'fraction_non_alphanumeric': 0.16564619132266772, 'fraction_numerical': 0.0321285140562249, 'mean_word_length': 2.467244921250856, 'pattern_counts': {'":': 70, '<': 59, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 28, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7347755', 'n_tokens_mistral': 5085, 'n_tokens_neox': 4779, 'n_words': 1032}