Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
create simple test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hqy2000 committed Apr 23, 2024
1 parent 66b8a03 commit a64073c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
20 changes: 20 additions & 0 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Python unittest

on: [push]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Running tests
run: |
python -m unittest discover -p '*_test.py'
2 changes: 1 addition & 1 deletion cloud_function/replay_from_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def is_before_simulation(self, time: datetime):
return self.get_simulation_time(time) < self.simulation_start_time

def is_after_simulation(self, time: datetime):
return self.get_simulation_time(time) > self.simulation_end_time
return self.get_simulation_time(time) >= self.simulation_end_time

def get_simulation_time(self, time: datetime):
return self.simulation_start_time + (time - self.real_start_time) * self.speed
Expand Down
40 changes: 40 additions & 0 deletions cloud_function/replay_from_bigquery_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from datetime import datetime

from cloud_function.replay_from_bigquery import TimeConfig


class TestTimeConfig(unittest.TestCase):
config = TimeConfig(
real_start_time=datetime.fromtimestamp(4000).isoformat(),
simulation_start_time=datetime.fromtimestamp(10000).isoformat(),
simulation_end_time=datetime.fromtimestamp(20000).isoformat(),
speed=2,
scheduler_name=""
)

def test_is_before_simulation(self):
self.assertEqual(self.config.is_before_simulation(datetime.fromtimestamp(12000)), False)
self.assertEqual(self.config.is_before_simulation(datetime.fromtimestamp(10000)), False)
self.assertEqual(self.config.is_before_simulation(datetime.fromtimestamp(6000)), False)
self.assertEqual(self.config.is_before_simulation(datetime.fromtimestamp(4000)), False)
self.assertEqual(self.config.is_before_simulation(datetime.fromtimestamp(2000)), True)
self.assertEqual(self.config.is_before_simulation(datetime.fromtimestamp(3999)), True)

def test_is_after_simulation(self):
self.assertEqual(self.config.is_after_simulation(datetime.fromtimestamp(12000)), True)
self.assertEqual(self.config.is_after_simulation(datetime.fromtimestamp(10000)), True)
self.assertEqual(self.config.is_after_simulation(datetime.fromtimestamp(9000)), True)
self.assertEqual(self.config.is_after_simulation(datetime.fromtimestamp(8999)), False)
self.assertEqual(self.config.is_after_simulation(datetime.fromtimestamp(2000)), False)
self.assertEqual(self.config.is_after_simulation(datetime.fromtimestamp(3999)), False)

def test_get_simulation_time(self):
self.assertEqual(self.config.get_simulation_time(datetime.fromtimestamp(4000)), datetime.fromtimestamp(10000))
self.assertEqual(self.config.get_simulation_time(datetime.fromtimestamp(4001)), datetime.fromtimestamp(10002))
self.assertEqual(self.config.get_simulation_time(datetime.fromtimestamp(6000)), datetime.fromtimestamp(14000))
self.assertEqual(self.config.get_simulation_time(datetime.fromtimestamp(9000)), datetime.fromtimestamp(20000))


if __name__ == '__main__':
unittest.main()

0 comments on commit a64073c

Please sign in to comment.