Skip to content

Commit

Permalink
test: add tests (#16)
Browse files Browse the repository at this point in the history
* test: add tests

* ci: travis

* ci: ignore coverage of files in tests folder

* docs: add badge
  • Loading branch information
wenboyu2 authored Dec 2, 2018
1 parent 2a6859b commit 39bd9e5
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: python

python:
- 3.6

install:
- pip install pipenv
- pipenv sync -d

script:
- pipenv run mamba --enable-coverage tests; coverage html --omit=*/site-packages/*,*_spec.py,*__init__.py

after_success:
- pipenv run codecov
17 changes: 17 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"

[dev-packages]
mamba = "*"
expects = "*"
mockito = "*"
coverage = "*"
codecov = "*"

[requires]
python_version = "3.7"
174 changes: 174 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Yahoo! Earnings Calendar Scraper
[![codecov](https://codecov.io/gh/wenboyu2/yahoo-earnings-calendar/branch/master/graph/badge.svg)](https://codecov.io/gh/wenboyu2/yahoo-earnings-calendar)
[![Build Status](https://travis-ci.com/wenboyu2/yahoo-earnings-calendar.svg?branch=master)](https://travis-ci.com/wenboyu2/yahoo-earnings-calendar)

Scrapes Yahoo! Finance earnings calendar to get data for a specific date or a date range.

## Installation
Expand Down
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- "tests"
122 changes: 122 additions & 0 deletions tests/scraper_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
from mamba import description, context, it
from expects import expect, equal
from mockito import when, mock, unstub, ANY, verify
import datetime

from yahoo_earnings_calendar import YahooEarningsCalendar

BASE_URL = 'https://finance.yahoo.com/calendar/earnings'
BASE_STOCK_URL = 'https://finance.yahoo.com/quote'

with description('YahooEarningsCalendar') as self:
with before.each:
self.yec = YahooEarningsCalendar()
self.symbol = 'yo'

with description('get_next_earnings_date') as self:
with it('should get data with correct URL'):
data = {
'context': {
'dispatcher': {
'stores' : {
'QuoteSummaryStore': {
'calendarEvents': {
'earnings': {
'earningsDate': [
{
'raw': 0
}
]
}
}
}
}
}
}
}

expected_url = '{0}/{1}'.format(BASE_STOCK_URL, self.symbol)
when(self.yec)._get_data_dict(expected_url).thenReturn(data)
self.yec.get_next_earnings_date(self.symbol)

verify(self.yec)._get_data_dict(expected_url)

with it('should return the next earnings date'):
expected_date = 321
data = {
'context': {
'dispatcher': {
'stores' : {
'QuoteSummaryStore': {
'calendarEvents': {
'earnings': {
'earningsDate': [
{
'raw': expected_date
}
]
}
}
}
}
}
}
}

when(self.yec)._get_data_dict(ANY(str)).thenReturn(data)
expect(self.yec.get_next_earnings_date(self.symbol)).to(equal(expected_date))

with description('earnings_on') as self:
with it('should get data with correct URL'):
date = datetime.datetime.strptime('May 5 2017 10:00AM', '%b %d %Y %I:%M%p')
data = {
'context': {
'dispatcher': {
'stores' : {
'ScreenerResultsStore': {
'results': {
'rows': 'blob'
}
}
}
}
}
}

expected_url = '{0}?day={1}'.format(BASE_URL, '2017-05-05')
when(self.yec)._get_data_dict(expected_url).thenReturn(data)
self.yec.earnings_on(date)

verify(self.yec)._get_data_dict(expected_url)

with it('should return earnings on date'):
date = datetime.datetime.strptime('May 5 2017 10:00AM', '%b %d %Y %I:%M%p')
expected_data = 'hi'
data = {
'context': {
'dispatcher': {
'stores' : {
'ScreenerResultsStore': {
'results': {
'rows': expected_data
}
}
}
}
}
}

when(self.yec)._get_data_dict(ANY(str)).thenReturn(data)
self.yec.earnings_on(date)

expect(self.yec.earnings_on(date)).to(equal(expected_data))

with description('earnings_between') as self:
with it('should return all earnings between start and end date'):
start_date = datetime.datetime.strptime('May 5 2017 10:00AM', '%b %d %Y %I:%M%p')
end_date = datetime.datetime.strptime('May 7 2017 10:00AM', '%b %d %Y %I:%M%p')
expected_data = [1, 2, 3]

when(self.yec).earnings_on(ANY).thenReturn([1]).thenReturn([2]).thenReturn([3])

expect(self.yec.earnings_between(start_date, end_date)).to(equal(expected_data))
2 changes: 1 addition & 1 deletion yahoo_earnings_calendar/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def earnings_between(self, from_date, to_date):
return earnings_data


if __name__ == '__main__':
if __name__ == '__main__': # pragma: no cover
date_from = datetime.datetime.strptime(
'May 5 2017 10:00AM', '%b %d %Y %I:%M%p')
date_to = datetime.datetime.strptime(
Expand Down

0 comments on commit 39bd9e5

Please sign in to comment.