Skip to content

Commit

Permalink
fix: db write
Browse files Browse the repository at this point in the history
  • Loading branch information
Morriz committed Feb 17, 2024
1 parent cee64b6 commit 89ecd4b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
26 changes: 19 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@
},
"black-formatter.importStrategy": "fromEnvironment",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"isort.check": true,
"isort.importStrategy": "fromEnvironment",
"mypy-type-checker.importStrategy": "fromEnvironment",
"mypy-type-checker.reportingScope": "workspace",
"pylint.importStrategy": "fromEnvironment",
"pylint.severity": {
"convention": "Information",
"error": "Error",
"fatal": "Error",
"info": "Information",
"refactor": "Information",
"warning": "Warning",
"info": "Information"
"warning": "Warning"
},
"python.analysis.addImport.exactMatchOnly": true,
"python.analysis.autoImportCompletions": true,
"python.analysis.enableSyncServer": true,
"python.defaultInterpreterPath": ".venv/bin/python",
"python.testing.unittestArgs": [ "-v", "-s", ".", "-p", "*_test.py" ],
"python.testing.unittestEnabled": true,
"isort.check": true,
"isort.importStrategy": "fromEnvironment"
}
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"*_test.py"
],
"files.associations": {
"*.yml": "yaml",
},
"files.exclude": {
"**/.history": true
},
"python.testing.unittestEnabled": true
}
2 changes: 0 additions & 2 deletions db.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ projects:
services:
- image: otomi/nodejs-helloworld:v1.2.13
name: master
port: 8080
env:
TARGET: cost concerned people
INFORMANT: http://test-informant:8080
Expand All @@ -28,6 +27,5 @@ projects:
- /etc/dida
- image: otomi/nodejs-helloworld:v1.2.13
name: informant
port: 8080
env:
TARGET: boss
3 changes: 2 additions & 1 deletion lib/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def get_projects(filter: Callable[[Project, Service], bool] = None) -> List[Proj

def write_projects(projects: List[Project]) -> None:
"""Write the projects to the db"""
projects_dump = [p.model_dump(exclude_defaults=True) for p in projects]
with open("db.yml", "w", encoding="utf-8") as f:
yaml.dump({"projects": projects}, f)
yaml.dump({"projects": projects_dump}, f)


def get_project(name: str, throw: bool = True) -> Project:
Expand Down
43 changes: 33 additions & 10 deletions lib/data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from lib.data import get_project, get_projects, get_service, upsert_env, upsert_project
from lib.data import (
get_project,
get_projects,
get_service,
upsert_env,
upsert_project,
write_projects,
)
from lib.models import Env, Project, Service

with open("db.yml.sample", encoding="utf-8") as f:
Expand Down Expand Up @@ -56,21 +63,21 @@

class TestCodeUnderTest(unittest.TestCase):

# Get all projects with no filter
# write all projects
@mock.patch(
"lib.data.get_db",
return_value=_ret_db.copy(),
"lib.data.yaml",
return_value={"dump": mock.Mock()},
)
def test_get_projects_no_filter(self, mock_get_db: Mock) -> None:
@mock.patch("builtins.open", new_callable=mock.mock_open)
def test_write_projects(self, mock_open: Mock, mock_yaml: Mock) -> None:

# Call the function under test
result = get_projects()
write_projects(_ret_get_projects)

# Assert that the mock functions were called correctly
mock_get_db.assert_called_once()
mock_open.assert_called_once_with("db.yml", "w", encoding="utf-8")

# Assert the result
self.assertEqual(result, _ret_get_projects)
# Assert that the mock functions were called correctly
mock_yaml.dump.assert_called_once_with(_ret_db, mock_open())

# Get projects with filter
@mock.patch(
Expand All @@ -96,6 +103,22 @@ def test_get_projects_with_filter(self, _: Mock) -> None:
]
self.assertEqual(result, expected_result)

# Get all projects with no filter
@mock.patch(
"lib.data.get_db",
return_value=_ret_db.copy(),
)
def test_get_projects_no_filter(self, mock_get_db: Mock) -> None:

# Call the function under test
result = get_projects()

# Assert that the mock functions were called correctly
mock_get_db.assert_called_once()

# Assert the result
self.assertEqual(result, _ret_get_projects)

# Get a project by name that does not exist
@mock.patch(
"lib.data.get_projects",
Expand Down

0 comments on commit 89ecd4b

Please sign in to comment.