def test__constructed__has_first_page_of_data(self):
        data = [
            TagData("tag1", DataType.BOOLEAN),
            TagData("tag2", DataType.DATE_TIME),
            TagData("tag3", DataType.DOUBLE),
        ]

        uut = self.MockAsyncTagQueryResultCollection(data, len(data), 0)

        assert data == list(uut.current_page)
        assert len(data) == uut.total_count
    def test__constructed__has_first_page_of_data(self):
        data = [
            TagData("tag1", DataType.BOOLEAN),
            TagData("tag2", DataType.DATE_TIME),
            TagData("tag3", DataType.DOUBLE),
        ]

        uut = self.MockTagQueryResultCollection(data, len(data), 0)

        itr = iter(uut)
        assert data == next(itr)
        assert len(data) == uut.total_count
    async def test__move_next_page__current_page_queried_and_updated(self):
        first_page = [
            TagData("tag1", DataType.BOOLEAN),
            TagData("tag2", DataType.DATE_TIME),
        ]

        second_page = [TagData("tag3", DataType.DOUBLE)]

        uut = self.MockAsyncTagQueryResultCollection(
            first_page,
            len(first_page) + len(second_page), 0)
        uut.setup(next_page=second_page)

        await uut.move_next_page_async()
        assert len(first_page) + len(second_page) == uut.total_count
        assert second_page == uut.current_page
        uut.verify([len(first_page)])
    async def test__reset__page_is_queried_with_initial_skip(self):
        initial_skip = 1
        tag2 = TagData("tag2", DataType.DATE_TIME)
        uut = self.MockAsyncTagQueryResultCollection([tag2], 3, initial_skip)

        tag2b = TagData("tag2b", DataType.DATE_TIME)
        uut.setup(next_page=[tag2b], new_total_count=2)
        await uut.reset_async()

        assert 2 == uut.total_count
        assert 1 == len(uut.current_page)
        assert tag2b is uut.current_page[0]

        await uut.move_next_page_async()
        assert uut.current_page is None

        uut.verify([initial_skip])
    async def test__server_error__reset__client_sees_error_but_can_retry(self):
        exception_to_throw = ApiException()
        initial_tags = [TagData("tag", DataType.INT32)]
        first_page = [TagData("tag2", DataType.STRING)]
        uut = self.MockAsyncTagQueryResultCollection(initial_tags, 1, 0)
        uut.setup(exception_to_throw=exception_to_throw)

        # After the failure, the current_page should remain unchanged
        with pytest.raises(ApiException):
            await uut.reset_async()
        assert initial_tags == uut.current_page

        # Try again after error -- let it work this time
        uut.setup(next_page=first_page)
        await uut.reset_async()
        assert first_page == uut.current_page

        uut.verify([0, 0])
    def test__iterating__second_page_queried(self):
        first_page = [
            TagData("tag1", DataType.BOOLEAN),
            TagData("tag2", DataType.DATE_TIME),
        ]

        second_page = [TagData("tag3", DataType.DOUBLE)]

        uut = self.MockTagQueryResultCollection(
            first_page,
            len(first_page) + len(second_page), 0)
        uut.setup(second_page)

        itr = iter(uut)
        next(itr)
        page = next(itr)
        assert len(first_page) + len(second_page) == uut.total_count
        assert second_page == page
        uut.verify([len(first_page)])
    async def test__total_count_changes__move_next_page__total_count_updated(
            self):
        tag1 = TagData("tag1", DataType.BOOLEAN)
        tag2 = TagData("tag2", DataType.DATE_TIME)
        uut = self.MockAsyncTagQueryResultCollection([tag1], 3, 0)
        uut.setup(next_page=[tag2], new_total_count=2)

        assert 3 == uut.total_count
        await uut.move_next_page_async()
        assert 2 == uut.total_count
        assert 1 == len(uut.current_page)
        assert tag2 is uut.current_page[0]
        uut.verify([1])

        await uut.move_next_page_async()
        assert 2 == uut.total_count
        assert uut.current_page is None
        uut.verify(
            [1])  # still only one query has happened, due to new total_count
Example #8
0
    def from_tagdata(cls, data: tbase.TagData,
                     fields: tbase.TagUpdateFields) -> "TagDataUpdate":
        """Create an update by taking one or more fields from a :class:`TagData`.

        Args:
            data: The metadata to send to the server in the update.
            fields: One or more fields to include in the update.

        Raises:
            ValueError: if ``data`` is None.
            ValueError: if ``fields`` has no fields or invalid fields.
        """
        if data is None:
            raise ValueError("data cannot be None")

        if fields == 0:
            raise ValueError("Must specify at least one field to update")

        if (tbase.TagUpdateFields.ALL & fields) != fields:
            raise ValueError("Invalid field specified")

        obj = cls.__new__(cls)
        obj._path = data.path
        obj._data_type = data.data_type
        obj._keywords = None
        obj._properties = None
        obj._collect_aggregates = None

        if fields & tbase.TagUpdateFields.KEYWORDS and data.keywords:
            obj._keywords = list(data.keywords)

        if fields & tbase.TagUpdateFields.PROPERTIES and data.properties:
            obj._properties = dict(data.properties)
        if fields & tbase.TagUpdateFields.RETENTION:
            if obj._properties is None:
                obj._properties = {}
            data._copy_retention_properties(obj._properties)

        if fields & tbase.TagUpdateFields.COLLECT_AGGREGATES:
            obj._collect_aggregates = data.collect_aggregates

        return obj
    def test__server_error__iterating__client_sees_error_but_can_restart(self):
        exception_to_throw = ApiException()
        initial_tags = [TagData("tag", DataType.INT32)]
        next_page = [TagData("tag2", DataType.STRING)]
        uut = self.MockTagQueryResultCollection(initial_tags, 2, 0)

        itr = iter(uut)
        next(itr)
        uut.setup(exception_to_throw)

        with pytest.raises(ApiException):
            next(itr)

        # Try again after error -- let it work this time
        uut.setup(next_page)
        itr = iter(uut)
        page = next(itr)
        assert next_page == page

        uut.verify([1, 0])
    async def test__move_next_page__skip_is_respected(self):
        page1 = 2
        page2 = 5
        page3 = 1
        initial_skip = 1
        total_count = initial_skip + page1 + page2 + page3

        tag1 = TagData("tag1", DataType.BOOLEAN)
        tag2 = TagData("tag2", DataType.DATE_TIME)
        tag3 = TagData("tag3", DataType.DOUBLE)

        uut = self.MockAsyncTagQueryResultCollection([tag1] * page1,
                                                     total_count, initial_skip)
        uut.setup(next_page=[tag2] * page2)

        await uut.move_next_page_async()
        uut.setup(next_page=[tag3] * page3)
        await uut.move_next_page_async()

        uut.verify([initial_skip + page1, initial_skip + page1 + page2])
    def test__total_count_changes__iterating__total_count_updated(self):
        tag1 = TagData("tag1", DataType.BOOLEAN)
        tag2 = TagData("tag2", DataType.DATE_TIME)
        uut = self.MockTagQueryResultCollection([tag1], 3, 0)

        itr = iter(uut)

        page = next(itr)
        assert 3 == uut.total_count
        uut.setup([tag2], new_total_count=2)

        page = next(itr)
        assert 2 == uut.total_count
        assert 1 == len(page)
        assert tag2 is page[0]
        uut.verify([1])

        with pytest.raises(StopIteration):
            next(itr)
        assert 2 == uut.total_count
        uut.verify(
            [1])  # still only one query has happened, due to new total_count
    def test__restart_iter__page_is_queried_with_initial_skip(self):
        initial_skip = 1
        tag2 = TagData("tag2", DataType.DATE_TIME)
        uut = self.MockTagQueryResultCollection([tag2], 3, initial_skip)

        itr = iter(uut)
        next(itr)

        tag2b = TagData("tag2b", DataType.DATE_TIME)
        uut.setup([tag2b], new_total_count=2)

        itr = iter(uut)

        page = next(itr)
        assert 2 == uut.total_count
        assert 1 == len(page)
        assert tag2b is page[0]

        with pytest.raises(StopIteration):
            next(itr)

        uut.verify([initial_skip])
    async def test__server_data_added_after_empty_query__reset__query_has_results(
            self):
        tags = [TagData("tag", DataType.INT32)]
        uut = self.MockAsyncTagQueryResultCollection(None, 0, 0)
        uut.setup(next_page=tags, new_total_count=1)

        await uut.reset_async()
        assert 1 == uut.total_count
        assert tags == uut.current_page

        await uut.move_next_page_async()
        assert uut.current_page is None

        uut.verify([0])
    def test__page_count_changes__iterating__stops_early(self):
        tag = TagData("tag1", DataType.BOOLEAN)
        initial_total_count = 2
        uut = self.MockTagQueryResultCollection([tag], initial_total_count, 0)
        uut.setup(None, new_total_count=1)

        itr = iter(uut)
        next(itr)
        # according to initial_total_count, there should be more data, but there's not
        # - and that should be okay, but iterating should not produce any more results
        with pytest.raises(StopIteration):
            next(itr)

        uut.verify([1])
    def test__iterating__stops_when_done(self):
        tag = TagData("tag1", DataType.BOOLEAN)
        uut = self.MockTagQueryResultCollection([tag], 1, 0)

        assert 1 == uut.total_count

        itr = iter(uut)

        page = next(itr)
        assert len(page) == 1
        assert tag is page[0]

        with pytest.raises(StopIteration):
            next(itr)
    def test__iterating__skip_is_respected(self):
        tag1 = TagData("tag1", DataType.BOOLEAN)
        tag2 = TagData("tag2", DataType.DATE_TIME)
        tag3 = TagData("tag3", DataType.DOUBLE)
        page1 = [tag1] * 2
        page2 = [tag2] * 5
        page3 = [tag3] * 1
        initial_skip = 1
        total_count = initial_skip + len(page1) + len(page2) + len(page3)

        uut = self.MockTagQueryResultCollection(page1, total_count,
                                                initial_skip)
        itr = iter(uut)

        next(itr)
        uut.setup(page2)
        next(itr)
        uut.setup(page3)
        next(itr)

        uut.verify([
            initial_skip + len(page1), initial_skip + len(page1) + len(page2)
        ])
    async def test__server_data_removed_after_query__reset__query_has_no_results(
            self):
        tag = TagData("tag", DataType.INT32)
        uut = self.MockAsyncTagQueryResultCollection([tag], 1, 0)
        assert 1 == uut.total_count
        assert uut.current_page is not None

        uut.setup(new_total_count=0)
        await uut.reset_async()
        assert 0 == uut.total_count
        assert uut.current_page is None

        await uut.move_next_page_async()
        assert uut.current_page is None

        uut.verify([0])
    def test__server_data_removed_after_query__restart_iter__query_has_no_results(
            self):
        tag = TagData("tag", DataType.INT32)
        uut = self.MockTagQueryResultCollection([tag], 1, 0)

        itr = iter(uut)
        next(itr)
        assert 1 == uut.total_count

        uut.setup(None, new_total_count=0)

        itr = iter(uut)
        with pytest.raises(StopIteration):
            next(itr)
        assert 0 == uut.total_count
        uut.verify([0])
    async def test__on_last_page__move_next_page__current_page_set_to_null(
            self):
        tag = TagData("tag1", DataType.BOOLEAN)
        uut = self.MockAsyncTagQueryResultCollection([tag], 1, 0)

        assert 1 == uut.total_count
        assert 1 == len(uut.current_page)
        assert tag is uut.current_page[0]

        await uut.move_next_page_async()
        assert 1 == uut.total_count
        assert uut.current_page is None

        await uut.move_next_page_async()
        assert 1 == uut.total_count
        assert uut.current_page is None
    def __init__(self, writer: tbase.ITagWriter, tag: tbase.TagData) -> None:
        """Initialize an instance.

        Args:
            writer: The :class:`ITagWriter` to use when writing values.
            tag: The tag whose values will be written.
            path: The path of the tag whose values will be written.

        Raises:
            ValueError: if ``tag`` is not a tag of a valid type and with a valid path.
        """
        if tag.data_type == tbase.DataType.UNKNOWN:
            raise ValueError("tag.type cannot be UNKNOWN")

        self._path = tag.validate_path()
        self._data_type = tag.data_type
        self.__writer = writer
    async def test__page_count_changes__move_next_page__current_page_set_to_null(
            self):
        tag = TagData("tag1", DataType.BOOLEAN)
        initial_total_count = 2
        uut = self.MockAsyncTagQueryResultCollection([tag],
                                                     initial_total_count, 0)
        uut.setup(new_total_count=1)

        # according to initial_total_count, there should be more data, but there's not
        # - and that should be okay, and clear the current_page
        await uut.move_next_page_async()
        assert uut.current_page is None

        await uut.move_next_page_async()
        assert uut.current_page is None

        uut.verify([1])
    def test__server_data_added_after_empty_query__restart_iter__query_has_results(
        self, ):
        tags = [TagData("tag", DataType.INT32)]
        uut = self.MockTagQueryResultCollection(None, 0, 0)

        itr = iter(uut)
        with pytest.raises(StopIteration):
            next(itr)

        uut.setup(tags, new_total_count=1)

        itr = iter(uut)

        page = next(itr)
        assert 1 == uut.total_count
        assert tags == page

        with pytest.raises(StopIteration):
            next(itr)

        uut.verify([0])