Skip to content

Commit

Permalink
feat: declare optional schema on streamsync state
Browse files Browse the repository at this point in the history
* refact: add unit test on apply_mutation_marker method
  • Loading branch information
FabienArcellier committed Mar 5, 2024
1 parent f1f4138 commit e15f581
Showing 1 changed file with 59 additions and 13 deletions.
72 changes: 59 additions & 13 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import math
import unittest
from typing import Dict

import numpy as np
Expand Down Expand Up @@ -53,10 +54,11 @@
session.session_component_tree.ingest(sc)


class TestStateProxy:
class TestStateProxy(unittest.TestCase):

sp = State(raw_state_dict)._state_proxy
sp_simple_dict = State(simple_dict)._state_proxy
def setUp(self):
self.sp = State(raw_state_dict)._state_proxy
self.sp_simple_dict = State(simple_dict)._state_proxy

@classmethod
def count_initial_mutations(cls, d, count=0):
Expand Down Expand Up @@ -107,15 +109,59 @@ def test_mutations(self) -> None:
assert d.get("features").get("height") == "short"
assert d.get("state.with.dots").get("photo.jpeg") == "Corrupted"

self.sp.apply_mutation_marker("age")
m = self.sp.get_mutations_as_dict()

assert m.get("+age") == 2

del self.sp["best_feature"]
m = self.sp.get_mutations_as_dict()
assert "-best_feature" in m

def test_apply_mutation_marker(self) -> None:
self.sp.get_mutations_as_dict()
self.sp_simple_dict.get_mutations_as_dict()

# Apply the mutation to a specific key
self.sp.apply_mutation_marker("age")
m = self.sp.get_mutations_as_dict()
assert m == {
'+age': 1
}

# Apply the mutation to the state as a whole
self.sp.apply_mutation_marker()
m = self.sp.get_mutations_as_dict()
assert m == {
'+age': 1,
'+best_feature': 'eyes',
'+counter': 4,
'+features': None,
'+interests': ['lamps', 'cars'],
'+name': 'Robert',
'+state\\.with\\.dots': None,
'+utfࠀ': 23
}

self.sp_simple_dict.apply_mutation_marker()
m = self.sp_simple_dict.get_mutations_as_dict()
assert m == {
'+items': None
}

# Apply the mutation to the state as a whole and on all its children
self.sp_simple_dict.apply_mutation_marker(recursive=True)
m = self.sp_simple_dict.get_mutations_as_dict()
assert m == {
'+items': None,
'+items.Apple': None,
'+items.Apple.name': 'Apple',
'+items.Apple.type': 'fruit',
'+items.Cucumber': None,
'+items.Cucumber.name': 'Cucumber',
'+items.Cucumber.type': 'vegetable',
'+items.Lettuce': None,
'+items.Lettuce.name': 'Lettuce',
'+items.Lettuce.type': 'vegetable'
}


def test_dictionary_removal(self) -> None:
# Explicit removal test
del self.sp_simple_dict["items"]["Lettuce"]
Expand All @@ -137,8 +183,8 @@ def test_set_dictionary_in_a_state_should_transform_it_in_state_proxy_and_trigge
Tests that writing a dictionary in a State without schema is transformed into a StateProxy and
triggers mutations to update the interface
>>> _state = streamsync.init_state({'app': {}})
>>> _state["app"] = {"hello": "world"}
#>>> _state = streamsync.init_state({'app': {}})
#>>> _state["app"] = {"hello": "world"}
"""
_state = State()

Expand Down Expand Up @@ -171,8 +217,8 @@ def test_replace_dictionary_content_in_a_state_with_schema_should_transform_it_i
This processing must work after initialization and after recovering the mutations the first time.
>>> _state = State({'items': {}})
>>> _state["items"] = {k: v for k, v in _state["items"].items() if k != "Apple"}
#>>> _state = State({'items': {}})
#>>> _state["items"] = {k: v for k, v in _state["items"].items() if k != "Apple"}
"""
_state = State({"items": {
"Apple": {"name": "Apple", "type": "fruit"},
Expand Down Expand Up @@ -216,8 +262,8 @@ def test_changing_a_value_in_substate_is_accessible_and_mutations_are_present(se
Tests that the change of values in a child state is readable whatever the access mode and
that mutations are triggered
>>> _state = ComplexSchema({'app': {'title': ''}})
>>> _state.app.title = 'world'
#>>> _state = ComplexSchema({'app': {'title': ''}})
#>>> _state.app.title = 'world'
"""
class AppState(State):
title: str
Expand Down

0 comments on commit e15f581

Please sign in to comment.