Skip to content

Commit

Permalink
test: update tests and makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
skyme5 committed Oct 13, 2021
1 parent 8d2f873 commit fb75ab8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 30 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ clean-test: ## remove test and coverage artifacts
lint: ## check style with flake8
black snapchat_dl tests

test: ## run tests quickly with the default Python
test: clean-test ## run tests quickly with the default Python
pytest tests --cache-clear

test-all: ## run tests on every Python version with tox
test-all: clean-test ## run tests on every Python version with tox
pytest tests --cache-clear

coverage: ## check code coverage quickly with the default Python
coverage: clean-test ## check code coverage quickly with the default Python
coverage run -m pytest -v tests
coverage report -m --skip-covered
coverage html
Expand Down
37 changes: 37 additions & 0 deletions tests/mock_data/invalidusername-nostories.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/mock_data/invalidusername.html

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions tests/mock_data/invalidusername.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@
"id": "1PgHW1XZSgWteDhyTiIFbgAAgcnlmYnB0ZWpiAXxms2FCAXxms17GAAAAAA",
"media": {
"type": "VIDEO",
"mediaUrl": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerJoyrides.jpg",
"mediaStreamingUrl": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerJoyrides.jpg",
"mediaPreviewUrl": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerJoyrides.jpg"
"mediaUrl": "https://filesamples.com/samples/video/mp4/sample_640x360.mp4",
"mediaStreamingUrl": "https://filesamples.com/samples/video/mp4/sample_640x360.mp4",
"mediaPreviewUrl": "https://filesamples.com/samples/video/mp4/sample_640x360.mp4"
},
"overlayImage": {
"mediaUrl": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerJoyrides.jpg",
"mediaStreamingUrl": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerJoyrides.jpg"
"mediaUrl": "https://filesamples.com/samples/video/mp4/sample_640x360.mp4",
"mediaStreamingUrl": "https://filesamples.com/samples/video/mp4/sample_640x360.mp4"
},
"captureTimeSecs": "1633810603"
},
{
"id": "1PgHW1XZSgWteDhyTiIFbgAAgZWZqZ2t2ZGN4AXxpJwZoAXxpJwRfAAAAAA",
"media": {
"type": "IMAGE",
"mediaUrl": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerJoyrides.jpg"
"mediaUrl": "https://filesamples.com/samples/video/mp4/sample_640x360.mp4"
},
"captureTimeSecs": "1633851737"
},
{
"id": "1PgHW1XZSgWteDhyTiIFbgAAgdGlyeXpodGx4AXxpR4jsAXxpR4XzAAAAAA",
"media": {
"type": "IMAGE",
"mediaUrl": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerJoyrides.jpg"
"mediaUrl": "https://filesamples.com/samples/video/mp4/sample_640x360.mp4"
},
"captureTimeSecs": "1633853867"
},
{
"id": "1PgHW1XZSgWteDhyTiIFbgAAgdnJlc2hjeWd5AXxqhQjjAXxqhQaeAAAAAA",
"media": {
"type": "IMAGE",
"mediaUrl": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerJoyrides.jpg"
"mediaUrl": "https://filesamples.com/samples/video/mp4/sample_640x360.mp4"
},
"captureTimeSecs": "1633874675"
}
Expand Down
27 changes: 14 additions & 13 deletions tests/test_downlaoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@
from snapchat_dl.downloader import download_url


def teardown_module(module):
shutil.rmtree(".test-data")


class Test_downloader(unittest.TestCase):
"""Tests for `snapchat_dl.downloader.download_url` package."""

def setUp(self):
"""Set up test fixtures."""
self.test_url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4"
self.test_url = "https://filesamples.com/samples/video/mp4/sample_640x360.mp4"
self.test_url404 = "https://google.com/error.html"
self.username = "invalidusername"

@classmethod
def tearDownClass(cls):
"""Tear down test fixtures."""
for file in ["test_dl_23.mp4", "test_dl_23.html"]:
if os.path.isfile(file):
os.remove(file)

def test_download_url(self):
"""Test snapchat_dl download_url."""
with open("test_dl_23.mp4", "a") as f:
f.close()
download_url(self.test_url, "test_dl_23", sleep_interval=0)
download_url(self.test_url, ".test-data/test_dl_23.mp4", sleep_interval=0)

def test_empty_download(self):
"""Test snapchat_dl download_url."""
open(".test-data/test_dl_23.mp4", "w").close()
download_url(self.test_url, ".test-data/test_dl_23.mp4", sleep_interval=0)

def test_download_url_raise(self):
"""Test snapchat_dl download_url with invalid url."""
with self.assertRaises(HTTPError):
download_url(self.test_url404, "test_dl_23", sleep_interval=0)
download_url(
self.test_url404, ".test-data/test_dl_23.mp4", sleep_interval=0
)
18 changes: 14 additions & 4 deletions tests/test_snapchat_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def teardown_module(module):
os.system("rm -rf .test-data")
shutil.rmtree(".test-data")


class TestSnapchat_dl(unittest.TestCase):
Expand All @@ -23,24 +23,34 @@ def setUp(self):
self.snapchat_dl = SnapchatDL(
limit_story=10, quiet=True, directory_prefix=".test-data", dump_json=True,
)
self.test_url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4"
self.test_url = "https://filesamples.com/samples/video/mp4/sample_640x360.mp4"
self.test_url404 = "https://google.com/error.html"
self.username = "invalidusername"
self.html = open(
"tests/mock_data/invalidusername.html", "r", encoding="utf8"
).read()
self.html_nostories = open(
"tests/mock_data/invalidusername-nostories.html", "r", encoding="utf8"
).read()

def test_class_init(self):
"""Test snapchat_dl init."""
self.assertTrue(self.snapchat_dl)

def test_get_stories_no_stories(self):
def test_invalid_username(self):
"""Test snapchat_dl Stories are not available."""
with self.assertRaises(APIResponseError):
self.snapchat_dl.download("username")

@mock.patch("snapchat_dl.snapchat_dl.SnapchatDL._api_response")
def test_get_stories_web_ok(self, api_response):
def test_get_stories(self, api_response):
"""Test snapchat_dl Download."""
api_response.return_value = self.html
self.snapchat_dl.download(self.username)

@mock.patch("snapchat_dl.snapchat_dl.SnapchatDL._api_response")
def test_no_stories(self, api_response):
"""Test snapchat_dl Download."""
api_response.return_value = self.html_nostories
with self.assertRaises(NoStoriesAvailable):
self.snapchat_dl.download(self.username)

0 comments on commit fb75ab8

Please sign in to comment.