@Test
public void testExpandView() throws IOException {

    OperatorNode<SequenceOperator> query = OperatorNode.create(SequenceOperator.FILTER,
            OperatorNode.create(SequenceOperator.JOIN,
                    OperatorNode.create(SequenceOperator.SCAN, Lists.newArrayList("left"), ImmutableList.of()).putAnnotation("alias", "left"),
                    OperatorNode.create(SequenceOperator.SCAN, Lists.newArrayList("right"), ImmutableList.of()).putAnnotation("alias", "right"),
                    OperatorNode.create(ExpressionOperator.EQ,
                            OperatorNode.create(ExpressionOperator.PROPREF, OperatorNode.create(ExpressionOperator.READ_RECORD, "left"), "id"),
                            OperatorNode.create(ExpressionOperator.PROPREF, OperatorNode.create(ExpressionOperator.READ_RECORD, "right"), "id"))),
            OperatorNode.create(ExpressionOperator.EQ,
                    OperatorNode.create(ExpressionOperator.PROPREF, OperatorNode.create(ExpressionOperator.READ_RECORD, "left"), "id"),
                    OperatorNode.create(ExpressionOperator.LITERAL, "1")));
    OperatorNode<SequenceOperator> transformed = new JoinFilterPushDown().visitSequenceOperator(query);
    Assert.assertEquals(transformed.getOperator(), SequenceOperator.JOIN);
    Assert.assertEquals(((OperatorNode) transformed.getArgument(0)).getOperator(), SequenceOperator.FILTER);
    Assert.assertEquals(((OperatorNode) transformed.getArgument(1)).getOperator(), SequenceOperator.SCAN);
    // now expand the view
    Registry scope = new Registry();
    scope.defineView("left", OperatorNode.create(SequenceOperator.FILTER,
            OperatorNode.create(SequenceOperator.SCAN, Lists.newArrayList("fancy"), ImmutableList.of()).putAnnotation("alias", "fancy"),
            OperatorNode.create(ExpressionOperator.EQ,
                    OperatorNode.create(ExpressionOperator.PROPREF, OperatorNode.create(ExpressionOperator.READ_RECORD, "fancy"), "id"),
                    OperatorNode.create(ExpressionOperator.LITERAL, "1"))));
    transformed = new ExpandViews(scope).visitSequenceOperator(transformed);
    Assert.assertEquals(transformed.getOperator(), SequenceOperator.JOIN);
    Assert.assertEquals(((OperatorNode) transformed.getArgument(0)).getOperator(), SequenceOperator.FILTER);
    Assert.assertEquals(((OperatorNode) transformed.getArgument(1)).getOperator(), SequenceOperator.SCAN);
    OperatorNode<SequenceOperator> left = (OperatorNode<SequenceOperator>) transformed.getArgument(0);
    // FILTER FILTER SCAN (already checked that left is FILTER, so the next should be too
    Assert.assertEquals(((OperatorNode) left.getArgument(0)).getOperator(), SequenceOperator.FILTER);
    // now look for the scan
    OperatorNode<SequenceOperator> scan = (OperatorNode) ((OperatorNode) left.getArgument(0)).getArgument(0);
    Assert.assertEquals(scan.getOperator(), SequenceOperator.SCAN);
    Assert.assertEquals(scan.getArgument(0), ImmutableList.of("fancy"));
}