def test_should_add_relationship_to_relationships(): Vx = Variable() Vy = Variable() Rxy = Relationship(Vx, Vy) Am = AssociationMatrix() Am.add_relationship(Rxy) assert Am.relationships == {frozenset((Vx.uuid, Vy.uuid)): Rxy}
def test_should_push_to_subscribers(): rel = Relationship(Variable(), Variable()) sub = Mock() sub.on_data = MagicMock() rel.subscribe(sub) rel._push_to_subscribers(10) sub.on_data.assert_called_with(10)
def test_should_create_db(): if os.path.exists("spearframe.db"): os.remove("spearframe.db") var1 = Variable() var2 = Variable() SpearframeRelationship(var1, var2) assert os.path.exists("spearframe.db")
def test_should_return_whole_dict(): Rxy = SpearframeRelationship(Variable(), Variable()) Rwz = SpearframeRelationship(Variable(), Variable()) Am = AssociationMatrix() Am.add_relationship(Rxy) Am.add_relationship(Rwz) key1 = frozenset((Rxy.sensor_x.get_uuid(), Rxy.sensor_y.get_uuid())) key2 = frozenset((Rwz.sensor_x.get_uuid(), Rwz.sensor_y.get_uuid())) a = Am.get_value_matrix() assert a == {key1: 0.0, key2: 0.0}
def test_should_remove_specified_relationship(): Vx = Variable() Vy = Variable() Rxy = Relationship(Vx, Vy) Rwz = Relationship(Variable(), Variable()) Am = AssociationMatrix() Am.add_relationship(Rxy) Am.add_relationship(Rwz) Am.remove_relationship(Rwz) assert Am.relationships == {frozenset((Vx.uuid, Vy.uuid)): Rxy}
def test_should_delete_db(): if os.path.exists("spearframe.db"): os.remove("spearframe.db") var1 = Variable() var2 = Variable() rel = SpearframeRelationship(var1, var2) if os.path.exists("spearframe.db"): rel.clean_up() assert not os.path.exists("spearframe.db")
def test_should_return_requested_relationship(): Rxy = Relationship(Variable(), Variable()) Rwz = Relationship(Variable(), Variable()) Am = AssociationMatrix() Am.add_relationship(Rxy) Am.add_relationship(Rwz) a = Am.get_relationship_from_sensors(Rxy.sensor_x, Rxy.sensor_y) assert a == Rxy b = Am.get_relationship_from_sensors(Rwz.sensor_x, Rwz.sensor_y) assert b == Rwz
def test_should_return_float(): if os.path.exists("spearframe.db"): os.remove("spearframe.db") var1 = Variable() var2 = Variable() rel = SpearframeRelationship(var1, var2) for degree in range(50): var1.on_data(math.sin(math.radians(degree * 10)), degree, degree + 1) var2.on_data(math.cos(math.radians(degree * 10)), degree, degree + 1) assert isinstance(rel.get_correlation_coefficient(), float)
def test_should_show_perfect_association(): if os.path.exists("spearframe.db"): os.remove("spearframe.db") var1 = Variable() var2 = Variable() rel = SpearframeRelationship(var1, var2) for degree in range(180): var1.on_data(math.sin(math.radians(degree * 10)), degree, degree + 1) var2.on_data(math.sin(math.radians(degree * 10)), degree, degree + 1) assert rel.get_correlation_coefficient() == 1
def test_should_not_return_nan(): if os.path.exists("spearframe.db"): os.remove("spearframe.db") var1 = Variable() var2 = Variable() rel = SpearframeRelationship(var1, var2) for degree in range(180): var1.on_data(math.sin(math.radians(degree * 10)), degree, degree + 1) var2.on_data(math.sin(math.radians(degree * 100)), degree, degree + 1) assert not math.isnan(rel.get_correlation_coefficient())
def test_should_return_float_from_frame_db(): if os.path.exists("spearframe.db"): os.remove("spearframe.db") var1 = Variable() var2 = Variable() rel = SpearframeRelationship(var1, var2) for degree in range(180): var1.on_data(math.sin(math.radians(degree * 10)), degree, degree + 1) var2.on_data(math.cos(math.radians(degree * 10)), degree, degree + 1) assert isinstance(rel.get_value_between_times(0, 63), float)
def test_variable_can_add_and_remove_subscribers(): var = Variable() rel = Mock() rel.get_uuid = MagicMock(return_value="abcd") var.add_subscriber(rel) assert var.subscribers["abcd"] is rel var.remove_subscriber("abcd") assert not var.subscribers
def add_sensor(self, sensor): """ This function adds a new sensor to the snapper module and generates a corresponding variable object with mappings. :return: """ self.sensors.append(sensor) self.snapper.add_sensor(sensor) newVariable = Variable() for var in self.variables: relationship = SpearframeRelationship(newVariable, var) self.matrix.add_relationship(relationship) self.variables.append(newVariable) self.route_map[sensor.uuid] = newVariable self.reverse_route_map[str(newVariable.uuid)] = sensor.uuid
def test_should_cause_frame_skip(): if os.path.exists("spearframe.db"): os.remove("spearframe.db") var1 = Variable() var2 = Variable() rel = SpearframeRelationship(var1, var2) for degree in range(20): if degree > 15 and rel.current_iteration[rel.sensor_x.get_uuid()]: var1.on_data(None, degree, degree + 1) var2.on_data(math.cos(math.radians(degree * 10)), degree, degree + 1) break else: var1.on_data(math.sin(math.radians(degree * 10)), degree, degree + 1) var2.on_data(math.cos(math.radians(degree * 10)), degree, degree + 1) assert not rel.current_iteration[rel.sensor_x.get_uuid()]
def test_should_have_last_pushed_value_as_none_on_initialization(): relationship = Relationship(Variable(), Variable()) assert relationship.get_last_pushed_value() is None
def test_should_allow_objects_to_subscribe(): rel = Relationship(Variable(), Variable()) assert len(rel.subscribers) == 0 rel.subscribe(2) assert len(rel.subscribers) == 1 and rel.subscribers[0] == 2
def test_should_update_last_pushed_value_on_push(): relationship = Relationship(Variable(), Variable()) relationship._push_to_subscribers(1) assert relationship.get_last_pushed_value() is 1
def test_should_automatically_subscribe_to_variables(): var1 = Variable() var2 = Variable() relationship = Relationship(var1, var2) assert var1.subscribers[relationship.get_uuid()] is not None assert var2.subscribers[relationship.get_uuid()] is not None
def test_should_have_personal_uuid(): relationship = Relationship(Variable(), Variable()) assert relationship.get_uuid() is not None
def test_should_should_throw_error_trying_clean_up(): with pytest.raises(NotImplementedError) as excinfo: Relationship(Variable(), Variable()).clean_up() assert "Underlying algorithm should implement this" in str(excinfo.value)
def test_should_should_throw_error_trying_to_get_value_between_times(): with pytest.raises(NotImplementedError) as excinfo: Relationship(Variable(), Variable()).get_value_between_times(1, 2) assert "Underlying algorithm should implement this" in str(excinfo.value)
def test_should_should_throw_error_trying_get_new_value_from_variables(): with pytest.raises(NotImplementedError) as excinfo: Relationship(Variable(), Variable()).on_new_value(1, 1, 0, 1) assert "Underlying algorithm should implement this" in str(excinfo.value)
def test_should_should_throw_error_trying_to_grab_coefficient(): with pytest.raises(NotImplementedError) as excinfo: Relationship(Variable(), Variable()).get_correlation_coefficient() assert "Underlying algorithm should implement this" in str(excinfo.value)
def test_should_raise_error_when_unknown_publisher_emits(): var1 = Variable() var2 = Variable() rel = SpearframeRelationship(var1, var2) with pytest.raises(ValueError): rel.on_new_value(1, 4, 0, 1)
def rel_75(): rel = Relationship(Variable(), Variable()) rel.get_correlation_coefficient = MagicMock(return_value=0.75) return rel