Пример #1
0
def test_streaming_transformer_schema_get_time_type_error(schema_loader: SchemaLoader,
                                                          schema_spec: Dict[str, Any]) -> None:
    schema_spec['Time'] = '1'
    streaming_bts = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_bts, schema_loader)
    with pytest.raises(TimeError, match='Could not determine time using 1'):
        assert transformer_schema.get_time(Context())
Пример #2
0
def test_streaming_transformer_schema_get_identity_error(schema_loader: SchemaLoader,
                                                         schema_spec: Dict[str, Any]) -> None:
    schema_spec['Identity'] = 'source.user'
    streaming_bts = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_bts, schema_loader)
    with pytest.raises(IdentityError, match='Could not determine identity using source.user'):
        assert transformer_schema.get_identity(Record())
Пример #3
0
def test_streaming_transformer_schema_get_time_datetime_not_defined(
        schema_loader: SchemaLoader, schema_spec: Dict[str, Any]) -> None:
    del schema_spec['Import']
    streaming_bts = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_bts, schema_loader)
    with pytest.raises(NameError, match='name \'datetime\' is not defined'):
        assert transformer_schema.get_time(Context())
def test_streaming_transformer_schema_get_identity_from_record(
        schema_loader: SchemaLoader, schema_spec: Dict[str, Any]) -> None:
    schema_spec['Identity'] = 'source.user'
    streaming_dtc = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_dtc,
                                                    schema_loader)
    assert transformer_schema.get_identity(Record({'user':
                                                   '******'})) == 'user1'
def test_streaming_transformer_schema_schema_init(
        schema_loader: SchemaLoader, schema_spec: Dict[str, Any]) -> None:
    streaming_dtc = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_dtc,
                                                    schema_loader)
    assert transformer_schema.identity.code_string == '\'user1\''
    assert transformer_schema.time.code_string == 'datetime(2016,10,10)'
Пример #6
0
def test_streaming_transformer_evaluate(schema_loader: SchemaLoader,
                                        schema_spec: Dict[str, Any]) -> None:
    streaming_bts = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_bts, schema_loader)
    transformer = StreamingTransformer(transformer_schema, 'user1')
    transformer.run_evaluate(Record())

    assert transformer._snapshot == {'test_group': {'_identity': 'user1', 'events': 1}}
Пример #7
0
def test_streaming_transformer_evaluate_time_error(schema_loader: SchemaLoader,
                                                   schema_spec: Dict[str, Any]) -> None:
    del schema_spec['Import']
    streaming_bts = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_bts, schema_loader)
    transformer = StreamingTransformer(transformer_schema, 'user1')
    with pytest.raises(NameError, match='name \'datetime\' is not defined'):
        assert transformer.run_evaluate(Record())
Пример #8
0
def test_streaming_transformer_evaluate_user_mismatch(schema_loader: SchemaLoader,
                                                      schema_spec: Dict[str, Any]) -> None:
    streaming_bts = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_bts, schema_loader)
    transformer = StreamingTransformer(transformer_schema, 'user2')
    with pytest.raises(
            IdentityError,
            match='Identity in transformer \(user2\) and new record \(user1\) do not match'):
        assert transformer.run_evaluate(Record())
Пример #9
0
def test_streaming_transformer_finalize(schema_loader: SchemaLoader,
                                        schema_spec: Dict[str, Any]) -> None:
    streaming_bts = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_bts, schema_loader)
    transformer = StreamingTransformer(transformer_schema, 'user1')
    store = schema_loader.get_store('test.memstore')

    transformer.run_finalize()
    assert store.get(Key(KeyType.DIMENSION, 'user1', 'test_group')) is None

    transformer.run_evaluate(Record())
    transformer.run_finalize()
    assert store.get(Key(KeyType.DIMENSION, 'user1', 'test_group')) == {
        '_identity': 'user1',
        'events': 1
    }
Пример #10
0
def test_streaming_transformer_schema_missing_attributes_adds_error(schema_loader: SchemaLoader,
                                                                    schema_spec: Dict[str, Any]):
    del schema_spec[StreamingTransformerSchema.ATTRIBUTE_IDENTITY]
    del schema_spec[StreamingTransformerSchema.ATTRIBUTE_TIME]
    del schema_spec[StreamingTransformerSchema.ATTRIBUTE_STORES]
    del schema_spec[StreamingTransformerSchema.ATTRIBUTE_AGGREGATES][0][
        AggregateSchema.ATTRIBUTE_STORE]
    schema_spec[StreamingTransformerSchema.ATTRIBUTE_AGGREGATES][0][
        AggregateSchema.ATTRIBUTE_TYPE] = Type.BLURR_AGGREGATE_VARIABLE

    streaming_bts = schema_loader.add_schema_spec(schema_spec)
    schema = StreamingTransformerSchema(streaming_bts, schema_loader)

    assert len(schema.errors) == 3
    assert RequiredAttributeError(streaming_bts, schema_spec,
                                  StreamingTransformerSchema.ATTRIBUTE_IDENTITY) in schema.errors
    assert RequiredAttributeError(streaming_bts, schema_spec,
                                  StreamingTransformerSchema.ATTRIBUTE_TIME) in schema.errors
    assert RequiredAttributeError(streaming_bts, schema_spec,
                                  StreamingTransformerSchema.ATTRIBUTE_STORES) in schema.errors
def test_streaming_transformer_schema_missing_attributes_adds_error(
        schema_loader: SchemaLoader, schema_spec: Dict[str, Any]):
    del schema_spec[StreamingTransformerSchema.ATTRIBUTE_IDENTITY]
    del schema_spec[StreamingTransformerSchema.ATTRIBUTE_TIME]
    del schema_spec[StreamingTransformerSchema.ATTRIBUTE_STORES]
    del schema_spec[StreamingTransformerSchema.ATTRIBUTE_AGGREGATES][0][
        AggregateSchema.ATTRIBUTE_STORE]
    schema_spec[StreamingTransformerSchema.ATTRIBUTE_AGGREGATES][0][
        AggregateSchema.ATTRIBUTE_TYPE] = Type.BLURR_AGGREGATE_VARIABLE

    streaming_dtc = schema_loader.add_schema_spec(schema_spec)
    schema = StreamingTransformerSchema(streaming_dtc, schema_loader)

    assert len(schema.errors) == 3
    assert isinstance(schema.errors[0], RequiredAttributeError)
    assert schema.errors[
        0].attribute == StreamingTransformerSchema.ATTRIBUTE_IDENTITY
    assert isinstance(schema.errors[1], RequiredAttributeError)
    assert schema.errors[
        1].attribute == StreamingTransformerSchema.ATTRIBUTE_TIME
    assert isinstance(schema.errors[2], RequiredAttributeError)
    assert schema.errors[
        2].attribute == StreamingTransformerSchema.ATTRIBUTE_STORES
def test_streaming_transformer_schema_get_time_constant(
        schema_loader: SchemaLoader, schema_spec: Dict[str, Any]) -> None:
    streaming_dtc = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_dtc,
                                                    schema_loader)
    assert transformer_schema.get_time(Record()) == datetime(2016, 10, 10)
def test_streaming_transformer_schema_get_identity_constant(
        schema_loader: SchemaLoader, schema_spec: Dict[str, Any]) -> None:
    streaming_dtc = schema_loader.add_schema_spec(schema_spec)
    transformer_schema = StreamingTransformerSchema(streaming_dtc,
                                                    schema_loader)
    assert transformer_schema.get_identity(Context()) == 'user1'