def get_as_map(self):
        """
        Converts object value into AnyMap or returns empty AnyMap if conversion is not possible.

        :return: AnyMap value or empty AnyMap if conversion is not supported.
        """
        return AnyValueMap.from_value(self.value)
    def update_partially(self, correlation_id: Optional[str], id: Any,
                         data: AnyValueMap) -> T:
        """
        Updates only few selected fields in a data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param id: an id of data item to be updated.

        :param data: a map with fields to be updated.

        :return: an updated item.
        """
        if data is None or id is None:
            return

        new_item = data.get_as_object()
        new_item = self._convert_from_public_partial(new_item)

        item = self._collection.find_one_and_update(
            {'_id': id}, {'$set': new_item},
            return_document=pymongo.ReturnDocument.AFTER)

        self._logger.trace(correlation_id,
                           "Updated partially in %s with id = %s",
                           self._collection_name, id)

        item = self._convert_to_public(item)
        return item
    def update_partially(self, correlation_id: Optional[str], id: Any,
                         data: AnyValueMap) -> T:
        """
        Updates only few selected fields in a data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param id: an id of data item to be updated.

        :param data: a map with fields to be updated.

        :return: an updated item.
        """
        new_item = None

        with self._lock:
            old_item = self._find_one(id)
            if old_item is None:
                return None

            for k, v in data.items():
                setattr(old_item, k, v)

            new_item = old_item

        self._logger.trace(correlation_id,
                           "Partially updated " + str(old_item))

        # Avoid reentry
        self.save(correlation_id)
        return new_item
    def test_set_properties(self):
        obj = JsonConverter.to_map(
            "{ \"value1\": 123, \"value2\": { \"value21\": 111, \"value22\": 222 }, \"value3\": [ 444, "
            "{ \"value311\": 555 } ] } "
        )

        values = AnyValueMap.from_tuples(
            "value1", "AAA",
            "value2", "BBB",
            "value3.1.value312", "CCC",
            "value3.3", "DDD",
            "value4.1", "EEE"
        )
        RecursiveObjectWriter.set_properties(obj, values)

        values = RecursiveObjectReader.get_properties(obj)
        assert 8 == len(values)
        assert "AAA" == values["value1"]
        assert "BBB", values["value2"]
        assert 444 == values["value3.0"]
        assert 555 == values["value3.1.value311"]
        assert "CCC", values["value3.1.value312"]
        assert None is values["value3.2"]
        assert "DDD" == values["value3.3"]
        assert "EEE" == values["value4.1"]
예제 #5
0
    def get_as_map(self, index):
        """
        Converts array element into an AnyValueMap or returns empty AnyValueMap if conversion is not possible.

        :param index: an index of element to get.

        :return: AnyValueMap value of the element or empty AnyValueMap if conversion is not supported.
        """
        value = self[index]
        return AnyValueMap.from_value(value)
    def test_crud_operations(self):
        # Create one dummy
        dummy1 = self._persistence.create(None, DUMMY1)

        assert dummy1 != None
        assert dummy1['id'] != None
        assert DUMMY1['key'] == dummy1['key']
        assert DUMMY1['content'] == dummy1['content']

        # Create another dummy
        dummy2 = self._persistence.create(None, DUMMY2)

        assert dummy2 != None
        assert dummy2['id'] != None
        assert DUMMY2['key'] == dummy2['key']
        assert DUMMY2['content'] == dummy2['content']

        # Get all dummies
        dummies = self._persistence.get_page_by_filter(None, None, None)
        assert dummies != None
        assert 2 == len(dummies.data)

        # Update the dummy
        dummy1['content'] = "Updated Content 1";
        dummy = self._persistence.update(
            None,
            dummy1
        )

        # Partially update the dummy
        dummy = self._persistence.update_partially(
            None,
            dummy1['id'],
            AnyValueMap.from_tuples(
                'content', 'Partually Updated Content 1'
            )
        )

        assert dummy != None
        assert dummy1['id'] == dummy['id']
        assert dummy1['key'] == dummy['key']
        assert "Partually Updated Content 1" == dummy['content']

        # Get the dummy by Id
        result = self._persistence.get_one_by_id(None, dummy1['id'])
        assert result is not None
        assert result['id'] == dummy1['id']
        assert result['key'] == result['key']

        # Delete the dummy
        self._persistence.delete_by_id(None, dummy1['id'])

        # Try to get deleted dummy
        dummy = self._persistence.get_one_by_id(None, dummy1['id'])
        assert dummy == None
    def test_crud_operations(self):
        # Create one dummy
        dummy1 = self._persistence.create(None, DUMMY1)

        assert dummy1 is not None
        assert dummy1.id is not None
        assert DUMMY1.key == dummy1.key
        assert DUMMY1.content == dummy1.content

        # Create another dummy
        dummy2 = self._persistence.create(None, DUMMY2)

        assert dummy2 is not None
        assert dummy2.id is not None
        assert DUMMY2.key == dummy2.key
        assert DUMMY2.content == dummy2.content

        # Get all dummies
        dummies = self._persistence.get_page_by_filter(None, None, None)
        assert dummies is not None
        assert 2 == len(dummies.data)

        # Update the dummy
        dummy1.content = "Updated Content 1"
        dummy = self._persistence.update(
            None,
            dummy1
        )

        # Partially update the dummy
        dummy = self._persistence.update_partially(
            None,
            dummy1.id,
            AnyValueMap.from_tuples(
                'content', 'Partially Updated Content 1'
            )
        )

        assert dummy is not None
        assert dummy1.id == dummy.id
        assert dummy1.key == dummy.key
        assert "Partially Updated Content 1" == dummy.content

        # Get the dummy by Id
        result = self._persistence.get_one_by_id(None, dummy1.id)
        assert result is not None
        assert result.id == dummy1.id
        assert result.key == dummy1.key

        # Delete the dummy
        self._persistence.delete_by_id(None, dummy1.id)

        # Try to get deleted dummy
        dummy = self._persistence.get_one_by_id(None, dummy1.id)
        assert dummy is None
    def test_set_map_property(self):
        map = AnyValueMap.from_tuples("key1", 123, "key2", "ABC")

        ObjectWriter.set_property(map, "key3", "AAAA")
        assert "AAAA" == map.get("key3")

        ObjectWriter.set_property(map, "Key1", 5555)
        assert 5555 == map.get("key1")

        ObjectWriter.set_property(map, "Key2", "BBBB")
        assert "BBBB" == map.get("key2")
예제 #9
0
    def test_get_map_property(self):
        map = AnyValueMap.from_tuples("key1", 123, "key2", "ABC")

        value = ObjectReader.get_property(map, "key3")
        assert None == value

        value = ObjectReader.get_property(map, "Key1")
        assert 123 == value

        value = ObjectReader.get_property(map, "KEY2")
        assert "ABC" == value
예제 #10
0
    def test_get_map_properties(self):
        map = AnyValueMap.from_tuples("key1", 123, "key2", "ABC")

        names = ObjectReader.get_property_names(map)
        assert 2 == len(names)
        assert "key1" in names
        assert "key2" in names

        values = ObjectReader.get_properties(map)
        assert 2 == len(values)
        assert 123 == values["key1"]
        assert "ABC" == values["key2"]
    def test_config_from_object(self):
        value = AnyValueMap.from_tuples(
            "field1", ConfigParams.from_string("field11=123;field12=ABC"),
            "field2",
            AnyValueArray.from_values(
                123, "ABC",
                ConfigParams.from_string("field21=543;field22=XYZ")), "field3",
            True)

        config = ConfigParams.from_value(value)
        assert 7 == len(config)
        assert 123 == config.get_as_integer("field1.field11")
        assert "ABC" == config.get_as_string("field1.field12")
        assert 123 == config.get_as_integer("field2.0")
        assert "ABC" == config.get_as_string("field2.1")
        assert 543 == config.get_as_integer("field2.2.field21")
        assert "XYZ" == config.get_as_string("field2.2.field22")
        assert True == config.get_as_boolean("field3")
    def test_get_as_string(self):
        message = AnyValueMap.from_tuples("key1", 123, "key2", "ABC")

        assert len(message) == 2
        assert message.get_as_integer("key1") == 123
        assert message.get_as_string("key2") == "ABC"