示例#1
0
def test_serialisation():
    store = ProgressStore()

    store.add_completed_location(Location(section_id="s1", block_id="one"))
    store.add_completed_location(Location(section_id="s1", block_id="two"))
    store.update_section_status(
        section_status=CompletionStatus.COMPLETED, section_id="s1"
    )

    store.add_completed_location(
        Location(
            section_id="s2",
            block_id="another-one",
            list_name="people",
            list_item_id="abc123",
        )
    )
    store.update_section_status(
        section_status=CompletionStatus.IN_PROGRESS,
        section_id="s2",
        list_item_id="abc123",
    )

    serialised = store.serialise()

    assert serialised == [
        Progress.from_dict(
            {
                "section_id": "s1",
                "list_item_id": None,
                "status": CompletionStatus.COMPLETED,
                "block_ids": ["one", "two"],
            }
        ),
        Progress.from_dict(
            {
                "section_id": "s2",
                "list_item_id": "abc123",
                "status": CompletionStatus.IN_PROGRESS,
                "block_ids": ["another-one"],
            }
        ),
    ]
示例#2
0
def test_clear():
    in_progress_sections = [
        {
            "section_id": "s1",
            "list_item_id": None,
            "status": CompletionStatus.COMPLETED,
            "block_ids": ["one", "two"],
        },
        {
            "section_id": "s2",
            "list_item_id": "abc123",
            "status": CompletionStatus.COMPLETED,
            "block_ids": ["three", "four"],
        },
    ]
    store = ProgressStore(in_progress_sections)

    store.clear()

    assert store.serialise() == []
    assert store.is_dirty
class QuestionnaireStore:
    LATEST_VERSION = 1

    def __init__(self, storage, version=None):
        self._storage = storage
        if version is None:
            version = self.get_latest_version_number()
        self.version = version
        self._metadata = {}
        # self.metadata is a read-only view over self._metadata
        self.metadata = MappingProxyType(self._metadata)
        self.collection_metadata = {}
        self.list_store = ListStore()
        self.answer_store = AnswerStore()
        self.progress_store = ProgressStore()

        raw_data, version = self._storage.get_user_data()
        if raw_data:
            self._deserialise(raw_data)
        if version is not None:
            self.version = version

    def get_latest_version_number(self):
        return self.LATEST_VERSION

    def set_metadata(self, to_set):
        """
        Set metadata. This should only be used where absolutely necessary.
        Metadata should normally be read only.
        """
        self._metadata = to_set
        self.metadata = MappingProxyType(self._metadata)

        return self

    def _deserialise(self, data):
        json_data = json.loads(data, use_decimal=True)
        self.progress_store = ProgressStore(json_data.get("PROGRESS"))
        self.set_metadata(json_data.get("METADATA", {}))
        self.answer_store = AnswerStore(json_data.get("ANSWERS"))
        self.list_store = ListStore.deserialise(json_data.get("LISTS"))
        self.collection_metadata = json_data.get("COLLECTION_METADATA", {})

    def serialise(self):
        data = {
            "METADATA": self._metadata,
            "ANSWERS": list(self.answer_store),
            "LISTS": self.list_store.serialise(),
            "PROGRESS": self.progress_store.serialise(),
            "COLLECTION_METADATA": self.collection_metadata,
        }
        return json.dumps(data, for_json=True)

    def delete(self):
        self._storage.delete()
        self._metadata.clear()
        self.collection_metadata = {}
        self.answer_store.clear()
        self.progress_store.clear()

    def save(self):
        data = self.serialise()
        self._storage.save(data=data)