def test_create_url(self): seq_generator = PGSeqGenerator() seq = seq_generator.get_next() origin = 'https://www.google.com' url = Url(origin, seq) assert len(url.shorten) <= 10
def test_shortify_raise_duplicated(self): origin_url = 'https://www.google.com' service = UrlService(SAUrlRepository(), PGSeqGenerator()) service.shortify(origin_url) with pytest.raises(AlreadyExistOriginUrl): service.shortify(origin_url)
def test_shortify(self): origin_url = 'https://www.google.com' service = UrlService(SAUrlRepository(), PGSeqGenerator()) shorten_hash = service.shortify(origin_url) assert len(shorten_hash) <= 10
def test_get_origin_by_shorten(self): origin_url_in = 'https://www.naver.com' service = UrlService(SAUrlRepository(), PGSeqGenerator()) shorten_hash = service.shortify(origin_url_in) origin_url_out = service.get_origin_by_shorten(shorten_hash) assert origin_url_in == origin_url_out
def test_get_shorten_by_origin(self): origin_url = 'https://www.naver.com' service = UrlService(SAUrlRepository(), PGSeqGenerator()) shorten_hash_before = service.shortify(origin_url) shorten_hash_after = service.get_shorten_by_origin(origin_url) assert shorten_hash_before == shorten_hash_after
def go(hash_): service = UrlService(SAUrlRepository(), PGSeqGenerator()) try: origin = service.get_origin_by_shorten(hash_) except NotExistShortUrlError as e: abort(404, str(e)) return redirect(origin)
def test_shortify_save(self): origin_url = 'https://www.google.com' service = UrlService(SAUrlRepository(), PGSeqGenerator()) service.shortify(origin_url) with Session.begin() as session: url = session.query(UrlDAO).filter( UrlDAO.origin == origin_url, ).first() assert url is not None
def generate(): origin = request.form.get('origin') if not origin: abort(400, 'origin URL is required') service = UrlService(SAUrlRepository(), PGSeqGenerator()) try: shorten_hash = service.shortify(origin) except AlreadyExistOriginUrl: shorten_hash = service.get_shorten_by_origin(origin) url_root = request.url_root.replace('http://', 'https://') shorten_url = f'{url_root}{shorten_hash}' return render_template('generated.html', shorten_url=shorten_url)