def testSliceOnMetaFeature(self):
        # We want to make sure that slicing on the newly added feature works, so
        # pulling in slice here.
        with beam.Pipeline() as pipeline:
            fpls = create_fpls()
            metrics = (
                pipeline
                | 'CreateTestInput' >> beam.Create(fpls)
                | 'WrapFpls' >> beam.Map(wrap_fpl)
                | 'ExtractInterestsNum' >>
                meta_feature_extractor.ExtractMetaFeature(get_num_interests)
                | 'ExtractSlices' >> slice_key_extractor._ExtractSliceKeys([
                    slicer.SingleSliceSpec(),
                    slicer.SingleSliceSpec(columns=['num_interests'])
                ])
                | 'FanoutSlices' >> slicer.FanoutSlices())

            def check_result(got):
                try:
                    self.assertEqual(4, len(got), 'got: %s' % got)
                    expected_slice_keys = [
                        (),
                        (),
                        (('num_interests', 1), ),
                        (('num_interests', 2), ),
                    ]
                    self.assertEqual(sorted(slice_key for slice_key, _ in got),
                                     sorted(expected_slice_keys))
                except AssertionError as err:
                    raise util.BeamAssertException(err)

            util.assert_that(metrics, check_result)
    def testMetaFeatures(self):
        with beam.Pipeline() as pipeline:
            fpls = create_fpls()

            metrics = (
                pipeline
                | 'CreateTestInput' >> beam.Create(fpls)
                | 'WrapFpls' >> beam.Map(wrap_fpl)
                | 'ExtractInterestsNum' >>
                meta_feature_extractor.ExtractMetaFeature(get_num_interests))

            def check_result(got):
                try:
                    self.assertEqual(2, len(got), 'got: %s' % got)
                    for res in got:
                        self.assertIn(
                            'num_interests',
                            res[constants.
                                FEATURES_PREDICTIONS_LABELS_KEY].features)
                        self.assertEqual(
                            len(
                                meta_feature_extractor.get_feature_value(
                                    res[constants.
                                        FEATURES_PREDICTIONS_LABELS_KEY],
                                    'interest')),
                            meta_feature_extractor.get_feature_value(
                                res[constants.FEATURES_PREDICTIONS_LABELS_KEY],
                                'num_interests'))
                except AssertionError as err:
                    raise util.BeamAssertException(err)

            util.assert_that(metrics, check_result)
  def testNoModificationOfExistingKeys(self):

    def bad_meta_feature_fn(_):
      return {'interest': ['bad', 'key']}

    with self.assertRaises(ValueError):
      with beam.Pipeline() as pipeline:
        fpls = create_fpls()

        _ = (
            pipeline
            | 'CreateTestInput' >> beam.Create(fpls)
            | 'WrapFpls' >> beam.Map(wrap_fpl)
            | 'ExtractInterestsNum' >>
            meta_feature_extractor.ExtractMetaFeature(bad_meta_feature_fn))