Skip to content

Commit

Permalink
[#56] WIP: Added test file for copa_scrape, called test_copa_scrape
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-parsons committed Sep 23, 2019
1 parent 3f36a26 commit f0a7f85
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 57 deletions.
2 changes: 1 addition & 1 deletion invisible_flow/api/copa_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class CopaScrape:

def scrape_data_json(self):
url = SCRAPE_URL + ".json"
resp = requests.get(url=url)
Expand Down Expand Up @@ -36,5 +37,4 @@ def scrape_copa_not_in_entity(self):
def scrape_data_csv(self):
url = SCRAPE_URL + ".csv"
resp = requests.get(url=url)
print("*******************", type(resp.content))
return resp.content
1 change: 0 additions & 1 deletion invisible_flow/transformers/copa_scrape_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def __init__(self):
def save_scraped_data(self):
scraper = CopaScrape()
csv = scraper.scrape_data_csv()
print("^^^^^^^^^^^^^^^^^^^^", type(csv), csv)
self.storage.store_string('initial_data.csv', csv, f'Scrape-{self.current_date}/initial_data')
self.create_and_save_metadata('initial_data')

Expand Down
63 changes: 63 additions & 0 deletions tests/api/test_copa_scrape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import unittest

from unittest import mock

from invisible_flow.api import CopaScrape
from invisible_flow.constants import SCRAPE_URL


def mocked_requests_get(**kwargs):
class MockResponse:
def __init__(self, json_data, status_code, content):
self.json_data = json_data
self.status_code = status_code
self.content = content

def json(self):
return self.json_data
if kwargs == {"url": SCRAPE_URL + ".csv"}:
return MockResponse({"key1": "value1"}, 200, "bubbles")
elif kwargs == {"url": SCRAPE_URL + ".json"}:
return MockResponse({"key1": "value1"}, 200, "bubbles")
elif kwargs == {"url": SCRAPE_URL + ".csv?$where=assignment=\"COPA\""}:
return MockResponse({"key1": "value1"}, 200, "bubbles")
elif kwargs == {"url": SCRAPE_URL + ".csv?$where=assignment!=\"COPA\""}:
return MockResponse({"key1": "value1"}, 200, "bubbles")
elif kwargs == {"url": SCRAPE_URL + ".csv?$select=log_no,complaint_date,beat&$where=assignment!=\"COPA\""}:
return MockResponse({"key1": "value1"}, 200, "bubbles")
elif kwargs == {"url": SCRAPE_URL + ".csv?$select=log_no,assignment,case_type,current_status,current_category,"
"finding_code,police_shooting,race_of_complainants,sex_of_complainants,age_of_complainants,"
"race_of_involved_officers,sex_of_involved_officers,age_of_involved_officers,"
"years_on_force_of_officers,complaint_hour,complaint_day,complaint_month&"
"$where=assignment!=\"COPA\""}:
return MockResponse({"key1": "value1"}, 200, "bubbles")

return MockResponse(None, 404, None)


@mock.patch('requests.get', side_effect=mocked_requests_get)
class TestCopaScrape(unittest.TestCase):

def test_scrape_data_csv(self, get_mock):
should_be_bubbles = CopaScrape().scrape_data_csv()
assert should_be_bubbles == "bubbles"

def test_scrape_data_json(self, get_mock):
should_be_json_data = CopaScrape().scrape_data_json()
assert should_be_json_data == {"key1": "value1"}

def test_scrape_copa_csv(self, get_mock):
should_be_bubbles = CopaScrape().scrape_copa_csv()
assert should_be_bubbles == "bubbles"

def test_scrape_not_copa_csv(self, get_mock):
should_be_bubbles = CopaScrape().scrape_not_copa_csv()
assert should_be_bubbles == "bubbles"

def test_scrape_copa_ready_for_entity(self, get_mock):
should_be_bubbles = CopaScrape().scrape_copa_ready_for_entity()
assert should_be_bubbles == "bubbles"

def test_scrape_copa_not_in_entity(self, get_mock):
should_be_bubbles = CopaScrape().scrape_copa_not_in_entity()
assert should_be_bubbles == "bubbles"
55 changes: 0 additions & 55 deletions tests/error_handling_experiment.py

This file was deleted.

0 comments on commit f0a7f85

Please sign in to comment.