Example #1
0
def test_concept_post_get_patch(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    random_string = uuid.uuid4().hex
    random_concept_id = "some-concept-id-狗-" + random_string
    random_concept_name = "some-concept-name-的な-" + random_string

    post_concepts_response = stub.PostConcepts(
        service_pb2.PostConceptsRequest(concepts=[
            resources_pb2.Concept(id=random_concept_id,
                                  name=random_concept_name)
        ]),
        metadata=metadata(),
    )
    raise_on_failure(post_concepts_response)

    get_concepts_response = stub.GetConcept(
        service_pb2.GetConceptRequest(concept_id=random_concept_id),
        metadata=metadata())
    raise_on_failure(get_concepts_response)
    assert get_concepts_response.concept.id == random_concept_id
    assert get_concepts_response.concept.name == random_concept_name

    duplicated_post_concepts_response = stub.PostConcepts(
        service_pb2.PostConceptsRequest(
            concepts=[resources_pb2.Concept(id=random_concept_id, )]),
        metadata=metadata(),
    )
    assert (duplicated_post_concepts_response.status.code ==
            status_code_pb2.StatusCode.CONCEPTS_INVALID_REQUEST)
    assert duplicated_post_concepts_response.status.description == "Invalid request"
    assert "duplicate" in duplicated_post_concepts_response.status.details.lower(
    )

    post_concepts_searches_response = stub.PostConceptsSearches(
        service_pb2.PostConceptsSearchesRequest(
            concept_query=resources_pb2.ConceptQuery(
                name=random_concept_name)),
        metadata=metadata(),
    )
    raise_on_failure(post_concepts_searches_response)
    assert random_concept_name in post_concepts_searches_response.concepts[
        0].name

    patch_concepts_response = stub.PatchConcepts(
        service_pb2.PatchConceptsRequest(
            action="overwrite",
            concepts=[
                resources_pb2.Concept(id=random_concept_id,
                                      name="some new concept name")
            ],
        ),
        metadata=metadata(),
    )
    raise_on_failure(patch_concepts_response)
def test_search_public_concepts_in_english(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    post_concepts_searches_response = stub.PostConceptsSearches(
        service_pb2.PostConceptsSearchesRequest(
            concept_query=resources_pb2.ConceptQuery(name="dog*")
        ),
        metadata=metadata(),
    )
    raise_on_failure(post_concepts_searches_response)
    assert len(post_concepts_searches_response.concepts) > 0
def test_post_annotations_searches(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    palm_search_response = stub.PostConceptsSearches(
        service_pb2.PostConceptsSearchesRequest(
            concept_query=resources_pb2.ConceptQuery(name="palm")),
        metadata=metadata(),
    )
    raise_on_failure(palm_search_response)
    palm_concept_id = palm_search_response.concepts[0].id

    water_search_response = stub.PostConceptsSearches(
        service_pb2.PostConceptsSearchesRequest(
            concept_query=resources_pb2.ConceptQuery(name="water")),
        metadata=metadata(),
    )
    raise_on_failure(water_search_response)
    water_concept_id = water_search_response.concepts[0].id

    with SetupImage(stub) as input_:
        post_palm_annotations_response = stub.PostAnnotations(
            service_pb2.PostAnnotationsRequest(annotations=[
                resources_pb2.Annotation(
                    input_id=input_.id,
                    data=resources_pb2.Data(regions=[
                        resources_pb2.Region(
                            region_info=resources_pb2.RegionInfo(
                                bounding_box=resources_pb2.BoundingBox(
                                    top_row=0,
                                    left_col=0,
                                    bottom_row=0.45,
                                    right_col=1)),
                            data=resources_pb2.Data(concepts=[
                                resources_pb2.Concept(id=palm_concept_id,
                                                      value=1)
                            ]),
                        ),
                    ]),
                ),
            ]),
            metadata=metadata(),
        )
        raise_on_failure(post_palm_annotations_response)

        post_water_annotations_response = stub.PostAnnotations(
            service_pb2.PostAnnotationsRequest(annotations=[
                resources_pb2.Annotation(
                    input_id=input_.id,
                    data=resources_pb2.Data(regions=[
                        resources_pb2.Region(
                            region_info=resources_pb2.RegionInfo(
                                bounding_box=resources_pb2.BoundingBox(
                                    top_row=0.6,
                                    left_col=0,
                                    bottom_row=1,
                                    right_col=0.98)),
                            data=resources_pb2.Data(concepts=[
                                resources_pb2.Concept(id=water_concept_id,
                                                      value=1)
                            ]),
                        ),
                    ]),
                ),
            ]),
            metadata=metadata(),
        )
        raise_on_failure(post_water_annotations_response)

        post_palm_annotations_searches_response = stub.PostAnnotationsSearches(
            service_pb2.PostAnnotationsSearchesRequest(
                searches=[
                    Search(query=resources_pb2.Query(filters=[
                        resources_pb2.Filter(
                            annotation=resources_pb2.Annotation(
                                data=resources_pb2.Data(concepts=[
                                    resources_pb2.Concept(id=palm_concept_id,
                                                          value=1)
                                ])))
                    ]))
                ],
                pagination=service_pb2.Pagination(page=1, per_page=1000),
            ),
            metadata=metadata(),
        )
        raise_on_failure(post_palm_annotations_searches_response)
        assert input_.id in [
            hit.input.id
            for hit in post_palm_annotations_searches_response.hits
        ]

        post_water_annotations_searches_response = stub.PostAnnotationsSearches(
            service_pb2.PostAnnotationsSearchesRequest(
                searches=[
                    Search(query=resources_pb2.Query(filters=[
                        resources_pb2.Filter(
                            annotation=resources_pb2.Annotation(
                                data=resources_pb2.Data(concepts=[
                                    resources_pb2.Concept(id=water_concept_id,
                                                          value=1)
                                ])))
                    ]))
                ],
                pagination=service_pb2.Pagination(page=1, per_page=1000),
            ),
            metadata=metadata(),
        )
        raise_on_failure(post_water_annotations_searches_response)
        assert input_.id in [
            hit.input.id
            for hit in post_water_annotations_searches_response.hits
        ]

        post_palm_and_water_annotations_searches_response = stub.PostAnnotationsSearches(
            service_pb2.PostAnnotationsSearchesRequest(
                searches=[
                    Search(query=resources_pb2.Query(filters=[
                        resources_pb2.Filter(
                            annotation=resources_pb2.Annotation(
                                data=resources_pb2.Data(concepts=[
                                    resources_pb2.Concept(id=palm_concept_id,
                                                          value=1)
                                ])), ),
                        resources_pb2.Filter(
                            annotation=resources_pb2.Annotation(
                                data=resources_pb2.Data(concepts=[
                                    resources_pb2.Concept(id=water_concept_id,
                                                          value=1)
                                ]))),
                    ]))
                ],
                pagination=service_pb2.Pagination(page=1, per_page=1000),
            ),
            metadata=metadata(),
        )
        raise_on_failure(post_palm_and_water_annotations_searches_response)
        # No single annotation can have two concepts, so this will return false.
        assert len(post_palm_and_water_annotations_searches_response.hits) == 0