def lengthen_url(url, conn): """Takes in one of our shortened URLs and returns the correct long url.""" #isolate code from shortened url if not is_valid_short(url): #url was not constructed properly return "%s404" % OURDOMAIN code = url[14:] #just the code, ie. h7K9g0 id = resolve_to_id(code) #convert shortened code to id long = db.search_id(id, db.MYTABLE, conn) if not long: #id was not found in database return "%s404" % OURDOMAIN #issue 404 return long #url to perform 301 re-direct on
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)
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)
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)