def test_create_single_multicol(self, mock_seq_response): res = SEQ_API.create( Sequence( external_id="1", name="blabla", columns=[{ "valueType": "string", "last_updated_time": 123 }, { "externalId": "c2" }], )) assert isinstance(res, Sequence) assert mock_seq_response.calls[0].response.json( )["items"][0] == res.dump(camel_case=True) assert { "items": [{ "name": "blabla", "externalId": "1", "columns": [{ "externalId": "column0", "valueType": "STRING" }, { "externalId": "c2" }], }] } == jsgz_load(mock_seq_response.calls[0].request.body)
def test_create_single(self, mock_seq_response): res = SEQ_API.create(Sequence(external_id="1", name="blabla", columns=[{}])) assert isinstance(res, Sequence) assert mock_seq_response.calls[0].response.json()["items"][0] == res.dump(camel_case=True) assert {"items": [{"name": "blabla", "externalId": "1", "columns": [{"externalId": "column0"}]}]} == jsgz_load( mock_seq_response.calls[0].request.body )
def _create_or_update(self, either_id: EitherId) -> None: """ Create or update sequence, based on provided metadata and column definitions Args: either_id: Id/External Id of sequence to be updated """ column_def = self.column_definitions.get(either_id) try: seq = self.cdf_client.sequences.create( Sequence( id=either_id.internal_id, external_id=either_id.external_id, metadata=self.sequence_metadata.get(either_id, None), asset_id=self.sequence_asset_ids.get(either_id, None), data_set_id=self.sequence_asset_ids.get(either_id, None), columns=column_def, ) ) except CogniteDuplicatedError: self.logger.info("Sequnce already exist: {}".format(either_id)) seq = self.cdf_client.sequences.retrieve(id=either_id.internal_id, external_id=either_id.external_id) # Update definition of cached sequence cseq = self.upload_queue[either_id] cseq.columns = seq.columns
def test_create_columnid_passed(self, mock_seq_response): res = SEQ_API.create( Sequence(external_id="1", name="blabla", columns=[{"id": 1, "externalId": "a", "valueType": "STRING"}]) ) assert isinstance(res, Sequence) assert { "items": [{"name": "blabla", "externalId": "1", "columns": [{"valueType": "STRING", "externalId": "a"}]}] } == jsgz_load(mock_seq_response.calls[0].request.body)
def new_seq(): seq = COGNITE_CLIENT.sequences.create( Sequence(columns=[{ "valueType": "STRING" }])) yield seq COGNITE_CLIENT.sequences.delete(id=seq.id) assert COGNITE_CLIENT.sequences.retrieve(seq.id) is None
def test_create_sequence(): src_sequences = [ Sequence(metadata={}, id=1007, asset_id=3), Sequence(metadata={ "arbitrary_1": 12, "arbitrary_2": "some text" }, id=2007, asset_id=7), Sequence( id=3007, columns=[ { "valueType": "STRING", "externalId": "user", "description": "some description" }, { "valueType": "DOUBLE", "externalId": "amount" }, ], metadata={}, asset_id=5, ), ] runtime = 10000000 id_mapping = {3: 333, 7: 777, 5: 555} for i, src_sequence in enumerate(src_sequences): created_sequence = create_sequence(src_sequence, id_mapping, f"source project name {i}", runtime) assert created_sequence.metadata[ "_replicatedInternalId"] == src_sequences[i].id assert created_sequence.metadata[ "_replicatedSource"] == f"source project name {i}" if src_sequences[i].metadata: assert len(created_sequence.metadata.keys()) == len( src_sequences[i].metadata.keys()) + 3 for key in src_sequences[i].metadata.keys(): assert key in created_sequence.metadata.keys() assert src_sequences[i].metadata[ key] == created_sequence.metadata[key] assert created_sequence.columns == src_sequences[i].columns assert created_sequence.asset_id == id_mapping[ src_sequences[i].asset_id]
def new_seq_long(): seq = COGNITE_CLIENT.sequences.create( Sequence(columns=[{ "valueType": "LONG", "externalId": "a" }])) yield seq COGNITE_CLIENT.sequences.delete(id=seq.id) assert COGNITE_CLIENT.sequences.retrieve(seq.id) is None
def test_create_multiple(self, mock_seq_response): res = SEQ_API.create([ Sequence(external_id="1", name="blabla", columns=[{ "externalId": "cid" }]) ]) assert isinstance(res, SequenceList) assert mock_seq_response.calls[0].response.json()["items"] == res.dump( camel_case=True)
def test_create_wrong_type(self, mock_rel_response): with pytest.raises(ValueError): REL_API.create( Relationship( external_id="1", confidence=0.5, relationship_type="belongsTo", source=Sequence(external_id="a'"), target={"resourceId": "bbb", "resource": "Asset"}, ) )
def test_update_sequence(): src_sequences = [ Sequence(metadata={}, id=1007, asset_id=3), Sequence(metadata={}, id=2007, asset_id=7), Sequence(asset_id=5), ] dst_sequences = [ Sequence(asset_id=333, metadata={}), Sequence(asset_id=777, metadata={"_replicatedInternalId": 7}), Sequence(asset_id=555, metadata={"source": "None"}), ] runtime = 10000000 id_mapping = {3: 333, 7: 777, 5: 555} for i in range(len(src_sequences)): updated_sequence = update_sequence(src_sequences[i], dst_sequences[i], id_mapping, "source project name", runtime) assert updated_sequence.metadata[ "_replicatedInternalId"] == src_sequences[i].id assert updated_sequence.metadata[ "_replicatedSource"] == "source project name" if src_sequences[i].metadata: assert len(updated_sequence.metadata.keys()) == len( src_sequences[i].metadata.keys()) + 3 for key in src_sequences[i].metadata.keys(): assert key in updated_sequence.metadata.keys() assert src_sequences[i].metadata[ key] == updated_sequence.metadata[key] assert updated_sequence.asset_id == id_mapping[ src_sequences[i].asset_id]
def create_sequence(src_seq: Sequence, src_dst_ids_assets: Dict[int, int], project_src: str, runtime: int) -> Sequence: """ Make a new copy of the sequence to be replicated based on a source sequence. Args: src_seq: The sequence from the source to be replicated to the destination. src_dst_ids_assets: A dictionary of all the mappings of source asset id to destination asset id. project_src: The name of the project the object is being replicated from. runtime: The timestamp to be used in the new replicated metadata. Returns: The replicated sequence to be created in the destination. """ logging.debug(f"Creating a new sequence based on source sequence id {src_seq.id}") # TODO: Bug happens at line 29 get_asset_ids returns empty list return Sequence( name=src_seq.name, description=src_seq.description, asset_id=replication.get_asset_ids([src_seq.asset_id], src_dst_ids_assets)[0] if src_seq.asset_id else None, external_id=src_seq.external_id, metadata=replication.new_metadata(src_seq, project_src, runtime), columns=src_seq.columns, )
def test_update_with_resource_class(self, mock_seq_response): res = SEQ_API.update(Sequence(id=1)) assert isinstance(res, Sequence) assert mock_seq_response.calls[0].response.json()["items"][0] == res.dump(camel_case=True)
def new_small_seq(small_sequence): seq = COGNITE_CLIENT.sequences.create( Sequence(columns=small_sequence.columns)) yield seq COGNITE_CLIENT.sequences.delete(id=seq.id) assert COGNITE_CLIENT.sequences.retrieve(seq.id) is None
def new_seq(): seq = COGNITE_CLIENT.sequences.create( Sequence(name="test_temp", columns=[{}])) yield seq COGNITE_CLIENT.sequences.delete(id=seq.id) assert COGNITE_CLIENT.sequences.retrieve(seq.id) is None
def test_create_single(self, mock_seq_response): res = SEQ_API.create( Sequence(external_id="1", name="blabla", columns=[{}])) assert isinstance(res, Sequence) assert mock_seq_response.calls[0].response.json( )["items"][0] == res.dump(camel_case=True)