Esempio n. 1
0
def test_serialize_deserialize_pickle():
    """
    Test that serialization and the deserialization of a
    MemoryDetectionElement instance with populated detection components
    results in a different but equivalent instance.
    """
    expected_uuid = 'some-uuid'
    expected_bbox = AxisAlignedBoundingBox([0, 0], [1, 2])
    expected_ce_map = {'a': .24, 'b': 0.5, 0: .26}
    expected_ce = MemoryClassificationElement('ce-type', expected_uuid)
    expected_ce.set_classification(expected_ce_map)

    e1 = MemoryDetectionElement(expected_uuid)
    e1._bbox = expected_bbox
    e1._classification = expected_ce

    buff = pickle.dumps(e1)

    #: :type: MemoryDetectionElement
    e2 = pickle.loads(buff)
    assert e2 is not e1
    assert e2.uuid == expected_uuid

    e2_bbox, e2_ce = e2.get_detection()
    # Intentionally checking e2_bbox is not the same instance
    assert e2_bbox is not expected_bbox  # lgtm[py/comparison-using-is]
    assert e2_bbox == expected_bbox
    assert e2_ce is not expected_ce
    assert e2_ce == expected_ce
    assert e2_ce.get_classification() == expected_ce_map
Esempio n. 2
0
def test_has_detection_one_none_member():
    """
    Test that has_detections is false if at least one of the members is None.
    """
    # Possible "valid" values.
    bbox = mock.MagicMock(spec_set=AxisAlignedBoundingBox)
    celem = mock.MagicMock(spec_set=ClassificationElement)
    celem.has_classifications.return_value = True

    inst = MemoryDetectionElement(0)
    inst._bbox = None
    inst._classification = celem
    assert inst.has_detection() is False

    inst._bbox = bbox
    inst._classification = None
    assert inst.has_detection() is False
Esempio n. 3
0
def test_get_classification():
    """ Test successfully getting the detection classification element. """
    c_elem = mock.MagicMock(spec_set=ClassificationElement)
    # Simulate a populated ClassificationElement
    c_elem.has_classifications.return_value = True
    inst = MemoryDetectionElement(0)
    inst._classification = c_elem
    assert inst.get_classification() == c_elem
Esempio n. 4
0
def test_has_detection():
    """
    Test that has_detection is true for True-evaluating attributes
    """
    inst = MemoryDetectionElement(0)
    inst._bbox = mock.MagicMock(spec=AxisAlignedBoundingBox)
    # Simulate having a non-empty element.
    inst._classification = mock.MagicMock(spec_set=ClassificationElement)
    inst._classification.has_classifications.return_value = True

    assert inst.has_detection() is True
Esempio n. 5
0
def test_get_detection():
    """ Test successfully getting the detection components. """
    bbox = mock.MagicMock(spec_set=AxisAlignedBoundingBox)
    c_elem = mock.MagicMock(spec_set=ClassificationElement)
    # Simulate a populated ClassificationElement
    c_elem.has_classifications.return_value = True

    inst = MemoryDetectionElement(0)
    inst._bbox = bbox
    inst._classification = c_elem
    assert inst.get_detection() == (bbox, c_elem)
Esempio n. 6
0
def test_get_classification_empty_classification():
    """ Test attempting to get the classification element when it is empty. """
    c_elem = mock.MagicMock(spec_set=ClassificationElement)
    # Simulate an empty ClassificationElement
    c_elem.has_classifications.return_value = False

    inst = MemoryDetectionElement(0)
    inst._classification = c_elem
    with pytest.raises(NoDetectionError,
                       match="Missing or empty classification for in-memory "
                       "detection with UUID 0"):
        inst.get_classification()
Esempio n. 7
0
def test_has_detection_empty_classification_element():
    """
    Test that when one or both attributes are false-evaluating but not None,
    has_detection returns false.
    """
    bbox = mock.MagicMock(spec_set=AxisAlignedBoundingBox)
    celem = mock.MagicMock(spec_set=ClassificationElement)
    celem.has_classifications.return_value = False

    inst = MemoryDetectionElement(0)
    inst._bbox = bbox
    inst._classification = celem

    assert inst.has_detection() is False
Esempio n. 8
0
def test_get_detection_error_empty_classification():
    """
    Test that NoDetectionError is raised when the classification element is
    false-evaluating.
    """
    bbox = mock.MagicMock(spec_set=AxisAlignedBoundingBox)
    celem = mock.MagicMock(spec_set=ClassificationElement)
    celem.has_classifications.return_value = False

    inst = MemoryDetectionElement(0)
    inst._bbox = bbox
    inst._classification = celem

    with pytest.raises(NoDetectionError,
                       match="Missing detection bounding box or "
                             "missing/invalid classification for in-memory "
                             "detection with UUID 0"):
        inst.get_detection()