Exemplo n.º 1
0
def shorten_url(url, conn):
    """Takes in a standard url and returns a shortened version."""
    url = standardize_url(url)
    if url is None: #tried to shorten invalid url
        return None
    
    #get the id for this url (whether new or otherwise)
    id = db.search_url(url, db.MYTABLE, conn)
    if not id: #url not yet inserted into database
        id = db.insert_url(url, db.MYTABLE, conn) #insert and get its id
    
    code = convert_to_code(id)
    return "%s%s" % (OURDOMAIN, code)
Exemplo n.º 2
0
    id = 78950039
    code = convert_to_code(id)
    a = resolve_to_id(code)
    assert a == id
    print "%d -> %s -> %d" % (id, code, a)

    id = -1  #should resolve to 0 so should not equal resolved id
    code = convert_to_code(id)
    assert resolve_to_id(code) != id
    print "%d -> %s -> %d" % (id, code, id)

    id = 17180131327  #large prime number
    code = convert_to_code(id)
    assert resolve_to_id(code) == id
    print "%d -> %s -> %d" % (id, code, id)

    #unit tests for shortening/lengthening
    shortened = shorten_url("example.com", conn)
    lengthened = lengthen_url(shortened, conn)
    assert lengthened == standard
    print "%s -> %s -> %s" % ("example.com", shortened, lengthened)

    id = db.search_url(standard, db.MYTABLE, conn)
    assert db.search_id(id, db.MYTABLE, conn) == standard

    wiki = "http://en.wikipedia.org/wiki/List_of_prime_numbers"
    shortened = shorten_url(wiki, conn)
    lengthened = lengthen_url(shortened, conn)
    assert lengthened == wiki
    print "%s -> %s -> %s" % (wiki, shortened, lengthened)
Exemplo n.º 3
0
    id = 78950039
    code = convert_to_code(id)
    a = resolve_to_id(code)
    assert a == id
    print "%d -> %s -> %d" % (id, code, a)

    id = -1  # should resolve to 0 so should not equal resolved id
    code = convert_to_code(id)
    assert resolve_to_id(code) != id
    print "%d -> %s -> %d" % (id, code, id)

    id = 17180131327  # large prime number
    code = convert_to_code(id)
    assert resolve_to_id(code) == id
    print "%d -> %s -> %d" % (id, code, id)

    # unit tests for shortening/lengthening
    shortened = shorten_url("example.com", conn)
    lengthened = lengthen_url(shortened, conn)
    assert lengthened == standard
    print "%s -> %s -> %s" % ("example.com", shortened, lengthened)

    id = db.search_url(standard, db.MYTABLE, conn)
    assert db.search_id(id, db.MYTABLE, conn) == standard

    wiki = "http://en.wikipedia.org/wiki/List_of_prime_numbers"
    shortened = shorten_url(wiki, conn)
    lengthened = lengthen_url(shortened, conn)
    assert lengthened == wiki
    print "%s -> %s -> %s" % (wiki, shortened, lengthened)
Exemplo n.º 4
0
 def test_search_id(self):
     id = db.search_url(standard, db.MYTABLE, conn)
     result = db.search_id(id, db.MYTABLE, conn)
     self.assertTrue(result == standard)