Skip to content

Commit d3479c5

Browse files
committed
fix: db write
1 parent b29062f commit d3479c5

File tree

4 files changed

+78
-18
lines changed

4 files changed

+78
-18
lines changed

lib/data.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ def get_db() -> Dict[str, List[Dict[str, Any]] | Dict[str, Any]]:
1313
return yaml.safe_load(f)
1414

1515

16+
def write_db(partial: Dict[str, List[Dict[str, Any]] | Dict[str, Any]]) -> None:
17+
"""Write the db"""
18+
# get the db first
19+
db = get_db()
20+
# merge wwith partial
21+
db = {**db, **partial}
22+
with open("db.yml", "w", encoding="utf-8") as f:
23+
yaml.dump(db, f)
24+
25+
1626
def validate_db() -> None:
1727
"""Validate db.yml contents"""
1828
debug("Validating db.yml")
@@ -67,8 +77,7 @@ def write_projects(projects: List[Project]) -> None:
6777
"""Write the projects to the db"""
6878
debug(f"Writing {len(projects)} projects to the db")
6979
projects_dump = [p.model_dump(exclude_defaults=True, exclude_none=True, exclude_unset=True) for p in projects]
70-
with open("db.yml", "w", encoding="utf-8") as f:
71-
yaml.dump({"projects": projects_dump}, f)
80+
write_db({"projects": projects_dump})
7281

7382

7483
def get_project(name: str, throw: bool = True) -> Project:

lib/data_test.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
get_service,
1414
upsert_env,
1515
upsert_project,
16+
write_db,
1617
write_projects,
1718
)
1819
from lib.models import Env, Project, Service
@@ -21,21 +22,32 @@
2122

2223
class TestCodeUnderTest(unittest.TestCase):
2324

24-
# write all projects
25+
@mock.patch("lib.data.get_db", return_value=test_db.copy())
2526
@mock.patch(
2627
"lib.data.yaml",
2728
return_value={"dump": mock.Mock()},
2829
)
2930
@mock.patch("builtins.open", new_callable=mock.mock_open)
30-
def test_write_projects(self, mock_open: Mock, mock_yaml: Mock) -> None:
31+
def test_write_db(self, mock_open: Mock, mock_yaml: Mock, _: Mock) -> None:
3132

3233
# Call the function under test
33-
write_projects(test_projects)
34+
write_db({"projects": test_db["projects"]})
3435

3536
mock_open.assert_called_once_with("db.yml", "w", encoding="utf-8")
3637

3738
# Assert that the mock functions were called correctly
38-
mock_yaml.dump.assert_called_once_with({"projects": test_db["projects"]}, mock_open())
39+
mock_yaml.dump.assert_called_once_with(
40+
{"plugins": test_db["plugins"], "projects": test_db["projects"]}, mock_open()
41+
)
42+
43+
@mock.patch("lib.data.write_db")
44+
def test_write_projects(self, mock_write_db: Mock) -> None:
45+
46+
# Call the function under test
47+
write_projects(test_projects)
48+
49+
# Assert that the mock functions were called correctly
50+
mock_write_db.assert_called_once_with({"projects": test_db["projects"]})
3951

4052
# Get projects with filter
4153
@mock.patch(

lib/test_stubs.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
import yaml
22

3-
from lib.models import Project, Service
3+
from lib.models import Plugin, Project, Service
44

55
with open("db.yml.sample", encoding="utf-8") as f:
66
test_db = yaml.safe_load(f)
77

8+
test_plugins = {
9+
"crowdsec": Plugin(
10+
**{
11+
"enabled": False,
12+
"version": "v1.2.0",
13+
"options": {
14+
"logLevel": "INFO",
15+
"updateIntervalSeconds": 60,
16+
"defaultDecisionSeconds": 60,
17+
"httpTimeoutSeconds": 10,
18+
"crowdsecCapiMachineId": "login",
19+
"crowdsecCapiPassword": "password",
20+
"crowdsecCapiScenarios": [
21+
"crowdsecurity/http-path-traversal-probing",
22+
"crowdsecurity/http-xss-probing",
23+
"crowdsecurity/http-generic-bf",
24+
],
25+
},
26+
}
27+
)
28+
}
29+
830
test_projects = [
931
Project(
1032
name="home-assistant",

openapi.yaml

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -444,32 +444,49 @@ components:
444444
description: Project model
445445
Service:
446446
properties:
447-
name:
447+
command:
448448
type: string
449-
title: Name
450-
port:
451-
type: integer
452-
title: Port
453-
default: 8080
449+
title: Command
450+
env:
451+
allOf:
452+
- $ref: '#/components/schemas/Env'
454453
image:
455454
type: string
456455
title: Image
457-
command:
456+
labels:
457+
items:
458+
type: string
459+
type: array
460+
title: Labels
461+
default: []
462+
name:
458463
type: string
459-
title: Command
464+
title: Name
460465
passthrough:
461466
type: boolean
462467
title: Passthrough
463468
default: false
469+
path_prefix:
470+
type: string
471+
title: Path Prefix
472+
path_remove:
473+
type: boolean
474+
title: Path Remove
475+
default: false
476+
port:
477+
type: integer
478+
title: Port
479+
default: 8080
480+
proxyprotocol:
481+
type: boolean
482+
title: Proxyprotocol
483+
default: true
464484
volumes:
465485
items:
466486
type: string
467487
type: array
468488
title: Volumes
469489
default: []
470-
env:
471-
allOf:
472-
- $ref: '#/components/schemas/Env'
473490
type: object
474491
required:
475492
- name

0 commit comments

Comments
 (0)