From 96b5be34a66b78c5f523578f99bec352ec356a78 Mon Sep 17 00:00:00 2001 From: Mike Taylor Date: Fri, 22 May 2020 13:30:20 -0500 Subject: [PATCH] No issue - Add first tests for test_api_helpers.py --- requirements.txt | 1 + tests/__init__.py | 0 .../fixtures/test_file_with_thirty_lines.csv | 30 ++++++++++++ tests/test_api_helpers.py | 48 +++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/fixtures/test_file_with_thirty_lines.csv create mode 100644 tests/test_api_helpers.py diff --git a/requirements.txt b/requirements.txt index 97a3ecc..72fe770 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ Flask==1.1.2 markdown==3.2.1 requests==2.23.0 tqdm==4.46.0 +pytest diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/test_file_with_thirty_lines.csv b/tests/fixtures/test_file_with_thirty_lines.csv new file mode 100644 index 0000000..693c9a7 --- /dev/null +++ b/tests/fixtures/test_file_with_thirty_lines.csv @@ -0,0 +1,30 @@ +1,google.com +2,youtube.com +3,facebook.com +4,tmall.com +5,qq.com +6,netflix.com +7,baidu.com +8,microsoft.com +9,sohu.com +10,twitter.com +11,login.tmall.com +12,instagram.com +13,taobao.com +14,360.cn +15,linkedin.com +16,jd.com +17,wikipedia.org +18,yahoo.com +19,windowsupdate.com +20,amazon.com +21,apple.com +22,zoom.us +23,sina.com.cn +24,live.com +25,pages.tmall.com +26,weibo.com +27,reddit.com +28,googletagmanager.com +29,adobe.com +30,doubleclick.net diff --git a/tests/test_api_helpers.py b/tests/test_api_helpers.py new file mode 100644 index 0000000..dcdb096 --- /dev/null +++ b/tests/test_api_helpers.py @@ -0,0 +1,48 @@ +import os +from trexa.api.endpoints import trim_csv + +"""Tests for the API helper methods.""" + +test_dir = os.path.dirname(os.path.abspath(__file__)) +test_csv = os.path.join(test_dir, 'fixtures', + 'test_file_with_thirty_lines.csv') + + +def get_csv_length(desired_length): + """Helper method for trimming.""" + f = trim_csv(test_csv, desired_length) + new_file = [] + while True: + try: + new_file.append(next(f)) + except StopIteration: + break + return len(new_file) + + +def test_trimming_smaller(): + """Test that we end up with a smaller file.""" + assert get_csv_length(10) == 10 + assert get_csv_length(0) == 0 + + +def test_trimming_none(): + """Test that passing in None returns the entire file.""" + assert get_csv_length(None) == 30 + # Test not passing in a count + f = trim_csv(test_csv) + new_file = [] + while True: + try: + new_file.append(next(f)) + except StopIteration: + break + assert len(new_file) == 30 + + +def test_trimming_with_garbage(): + """Test that nothing weird happens.""" + assert get_csv_length(35) == 30 + assert get_csv_length('WHAT') == 30 + assert get_csv_length('🤬') == 30 + assert get_csv_length(hex(10)) == 30