Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #4

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/test_market_time.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Run Tests on PR with changes to market_time.py

on:
pull_request:
paths:
- 'MarketTime/market_time.py'
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Copy MarketTime/ folder to tests/ directory
run: |
cp -r MarketTime/ tests/

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
python -m unittest tests/test_market_time.py

- name: Add test results to PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const results = `Test results:\n${process.env.unittests}`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: results
})
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ __pycache__/
MarketTime.egg-info/
build/
dist/
*test*
*token*
test.py
1 change: 1 addition & 0 deletions MarketTime/market_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class MarketTime:
close_date_set = set()

# just test
@staticmethod
def init_close_date_set(close_date_list: Sequence[str] = None):
"""Init the market close date set.
Expand Down
107 changes: 107 additions & 0 deletions tests/test_market_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import unittest
from datetime import datetime, date
from MarketTime.market_time import MarketTime as mt


class TestMarketTime(unittest.TestCase):
def setUp(self):
close_dates = ["2024-04-04", "2024-04-05"]
mt.init_close_date_set(close_dates)

def test_init_close_date_set(self):
self.assertTrue(
datetime(2024, 4, 4).date() in mt.close_date_set
) # in close_dates
self.assertTrue(
datetime(2024, 4, 5).date() in mt.close_date_set
) # in close_dates
self.assertFalse(
datetime(2024, 4, 6).date() in mt.close_date_set
) # not in close_dates
self.assertTrue(len(mt.close_date_set) == 2)

def test__get_date_obj(self):
time = datetime.now()
self.assertTrue(time.date(), mt._get_date_obj(time)) # datetime
self.assertTrue(time.date(), mt._get_date_obj(time.date())) # date
self.assertTrue(isinstance(mt._get_date_obj(), date)) # None

def test_in_open_day(self):
open_day = datetime(2024, 4, 1) # Monday
self.assertTrue(mt.in_open_day(open_day))

open_day = datetime(2024, 4, 2) # Tuesday
self.assertTrue(mt.in_open_day(open_day))

open_day = datetime(2024, 4, 3) # Wednesday
self.assertTrue(mt.in_open_day(open_day))

close_day = datetime(2024, 4, 4) # Thursday but in close_dates
self.assertFalse(mt.in_open_day(close_day))

close_day = datetime(2024, 4, 5) # Friday but in close_dates
self.assertFalse(mt.in_open_day(close_day))

close_day = datetime(2024, 4, 6) # Saturday
self.assertFalse(mt.in_open_day(close_day))

close_day = datetime(2024, 4, 7) # Sunday
self.assertFalse(mt.in_open_day(close_day))

def test_next_open_day(self):
today = datetime(2024, 4, 1) # Monday
next_open_day = mt.next_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 2).date())

today = datetime(2024, 4, 2) # Tuesday
next_open_day = mt.next_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 3).date())

today = datetime(2024, 4, 3) # Wednesday
next_open_day = mt.next_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 8).date())

today = datetime(2024, 4, 4) # Thursday
next_open_day = mt.next_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 8).date())

today = datetime(2024, 4, 5) # Friday
next_open_day = mt.next_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 8).date())

today = datetime(2024, 4, 6) # Saturday
next_open_day = mt.next_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 8).date())

today = datetime(2024, 4, 7) # Sunday
next_open_day = mt.next_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 8).date())

def test_last_open_day(self):
today = datetime(2024, 4, 1) # Monday
next_open_day = mt.last_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 3, 29).date())

today = datetime(2024, 4, 2) # Tuesday
next_open_day = mt.last_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 1).date())

today = datetime(2024, 4, 3) # Wednesday
next_open_day = mt.last_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 2).date())

today = datetime(2024, 4, 4) # Thursday
next_open_day = mt.last_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 3).date())

today = datetime(2024, 4, 5) # Friday
next_open_day = mt.last_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 3).date())

today = datetime(2024, 4, 6) # Saturday
next_open_day = mt.last_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 3).date())

today = datetime(2024, 4, 7) # Sunday
next_open_day = mt.last_open_day(today)
self.assertEqual(next_open_day, datetime(2024, 4, 3).date())
Loading