Skip to content

Commit

Permalink
Document the test for the Snap.get method
Browse files Browse the repository at this point in the history
  • Loading branch information
james-garner-canonical committed Sep 17, 2024
1 parent 65ebb75 commit 2a41ea3
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions tests/unit/test_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import unittest
from subprocess import CalledProcessError
from typing import Iterable, Optional
from typing import Any, Dict, Iterable, Optional
from unittest.mock import MagicMock, mock_open, patch

import fake_snapd as fake_snapd
Expand Down Expand Up @@ -842,7 +842,26 @@ def __getitem__(self, name: str) -> snap.Snap:
self.assertIn("Failed to install or refresh snap(s): nothere", ctx.exception.message)

def test_snap_get(self):
"""Test the multiple different ways of calling the Snap.get function.
Valid ways:
("key", typed=False) -> returns a string
("key", typed=True) -> returns value parsed from json
(None, typed=True) -> returns parsed json for all keys
("", typed=True) -> returns parsed json for all keys
An invalid key will raise an error if typed=False, but return None if typed=True.
"""
def fake_snap(command: str, optargs: Optional[Iterable[str]] = None) -> str:
"""Snap._snap would normally call subprocess.check_output(["snap", ...], ...)
Here we only handle the "get" commands generated by Snap.get:
["snap", "get", "-d"] -- equivalent to (None, typed=True)
["snap", "get", "key"] -- equivalent to ("key", typed=False)
["snap", "get", "-d" "key"] -- equivalent to ("key", typed=True)
Values are returned from the local keys_and_values dict instead of calling out to snap.
"""
assert command == "get"
assert optargs is not None
optargs = list(optargs)
Expand All @@ -862,7 +881,7 @@ def fake_snap(command: str, optargs: Optional[Iterable[str]] = None) -> str:

foo = snap.Snap("foo", snap.SnapState.Latest, "stable", "1", "classic")
foo._snap = MagicMock(side_effect=fake_snap)
keys_and_values = {
keys_and_values: Dict[str, Any] = {
"key_w_string_value": "string",
"key_w_float_value": 4.2,
"key_w_int_value": 13,
Expand Down

0 comments on commit 2a41ea3

Please sign in to comment.