From 71efe02178a71f1c27066cad9da0c676c2a608d3 Mon Sep 17 00:00:00 2001 From: chris-greening Date: Sun, 13 Dec 2020 22:10:18 -0500 Subject: [PATCH] add: added IGTV test --- tests/scrapers/test_igtv.py | 78 +++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/scrapers/test_igtv.py diff --git a/tests/scrapers/test_igtv.py b/tests/scrapers/test_igtv.py new file mode 100644 index 0000000..a6927ab --- /dev/null +++ b/tests/scrapers/test_igtv.py @@ -0,0 +1,78 @@ +import csv +import datetime +import json +import re +import os + +import pytest +from bs4 import BeautifulSoup +import requests + +from instascrape import IGTV + + +class TestIGTV: + @pytest.fixture + def url(self): + return "https://www.instagram.com/tv/CIrIIMYl8VQ/" + + @pytest.fixture + def get_request(self, url): + return requests.get(url, headers={"User-Agent": "user-agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36 Edg/87.0.664.57"}) + + @pytest.fixture + def page_instance(self, url): + random_google_igtv = IGTV(url) + random_google_igtv.scrape() + return random_google_igtv + + def test_from_html(self, get_request, page_instance): + igtv_html = get_request.text + igtv_obj = IGTV(igtv_html) + igtv_obj.scrape() + assert igtv_obj.likes == page_instance.likes + + def test_from_soup(self, get_request, page_instance): + igtv_html = get_request.text + igtv_soup = BeautifulSoup(igtv_html, features='lxml') + igtv_obj = IGTV(igtv_soup) + igtv_obj.scrape() + assert igtv_obj.likes == page_instance.likes + + def test_to_dict(self, page_instance): + assert isinstance(page_instance.to_dict(), dict) + + def test_embed(self, page_instance): + html_embed = page_instance.embed() + embed_copied_from_instagram = '
View this post on Instagram

A post shared by Google (@google)

' + assert html_embed == embed_copied_from_instagram + + @pytest.mark.file_io + def test_to_json(self, page_instance, tmpdir): + file = tmpdir.join("data.json") + page_instance.to_json(fp=str(file)) + with open(str(file), "r") as injson: + json_dict = json.load(injson) + assert page_instance['shortcode'] == json_dict['shortcode'] + + @pytest.mark.file_io + def test_to_csv(self, page_instance, tmpdir): + + # write to CSV + file = tmpdir.join("data.csv") + page_instance.to_csv(fp=str(file)) + + # reread the csv + with open(str(file), mode="r") as infile: + reader = csv.reader(infile) + csv_dict = {row[0]: row[1] for row in reader} + + assert page_instance['shortcode'] == csv_dict['shortcode'] + + @pytest.mark.file_io + def test_download_photo(self, page_instance, tmpdir): + + # donwload photo + file = tmpdir.join("image.jpg") + page_instance.download(fp=str(file)) + assert os.path.exists(file)