Files
firebase-ios-sdk/Example/Database/Tests/Unit/FRangeMergeTest.m
T

337 lines
12 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>
#import "FEmptyNode.h"
2019-08-05 13:27:47 -07:00
#import "FNode.h"
#import "FRangeMerge.h"
#import "FTestHelpers.h"
2017-05-15 12:27:07 -07:00
@interface FRangeMergeTest : XCTestCase
@end
@implementation FRangeMergeTest
- (void)testSmokeTest {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{
@"bar" : @"bar-value",
@"foo" : @{@"a" : @{@"deep-a-1" : @1, @"deep-a-2" : @2}, @"b" : @"b", @"c" : @"c", @"d" : @"d"},
@"quu" : @"quu-value"
}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{
@"foo" :
@{@"a" : @{@"deep-a-2" : @"new-a-2", @"deep-a-3" : @3}, @"b-2" : @"new-b", @"c" : @"new-c"}
}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"foo/a/deep-a-1")
end:PATH(@"foo/c")
updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{
@"bar" : @"bar-value",
@"foo" : @{
@"a" : @{@"deep-a-1" : @1, @"deep-a-2" : @"new-a-2", @"deep-a-3" : @3},
@"b-2" : @"new-b",
@"c" : @"new-c",
@"d" : @"d"
},
@"quu" : @"quu-value"
}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testStartIsExclusive {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"bar" : @"bar-value", @"foo" : @"foo-value", @"quu" : @"quu-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"foo" : @"new-foo-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"bar")
end:PATH(@"foo")
updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected =
NODE((@{@"bar" : @"bar-value", @"foo" : @"new-foo-value", @"quu" : @"quu-value"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testStartIsExclusiveButIncludesChildren {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"bar" : @"bar-value", @"foo" : @"foo-value", @"quu" : @"quu-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates =
NODE((@{@"bar" : @{@"bar-child" : @"bar-child-value"}, @"foo" : @"new-foo-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"bar")
end:PATH(@"foo")
updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{
@"bar" : @{@"bar-child" : @"bar-child-value"},
@"foo" : @"new-foo-value",
@"quu" : @"quu-value"
}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testEndIsInclusive {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"bar" : @"bar-value", @"foo" : @"foo-value", @"quu" : @"quu-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"baz" : @"baz-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"bar")
end:PATH(@"foo")
updates:updates]; // foo should be deleted
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected =
NODE((@{@"bar" : @"bar-value", @"baz" : @"baz-value", @"quu" : @"quu-value"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testEndIsInclusiveButExcludesChildren {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{
@"bar" : @"bar-value",
@"foo" : @{@"foo-child" : @"foo-child-value"},
@"quu" : @"quu-value"
}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"baz" : @"baz-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"bar")
end:PATH(@"foo")
updates:updates]; // foo should be deleted
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{
@"bar" : @"bar-value",
@"baz" : @"baz-value",
@"foo" : @{@"foo-child" : @"foo-child-value"},
@"quu" : @"quu-value"
}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testCanUpdateLeafNode {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE(@"leaf-value");
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"bar" : @"bar-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:nil end:PATH(@"foo") updates:updates];
id<FNode> expected = NODE((@{@"bar" : @"bar-value"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)testCanReplaceLeafNodeWithLeafNode {
id<FNode> node = NODE(@"leaf-value");
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE(@"new-leaf-value");
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:nil end:PATH(@"") updates:updates];
id<FNode> expected = NODE(@"new-leaf-value");
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testLeafsAreUpdatedWhenRangesIncludeDeeperPath {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"foo" : @{@"bar" : @"bar-value"}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"foo" : @{@"bar" : @"new-bar-value"}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"foo")
end:PATH(@"foo/bar/deep")
updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{@"foo" : @{@"bar" : @"new-bar-value"}}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testLeafsAreNotUpdatedWhenRangesIncludeDeeperPaths {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"foo" : @{@"bar" : @"bar-value"}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"foo" : @{@"bar" : @"new-bar-value"}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"foo/bar")
end:PATH(@"foo/bar/deep")
updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{@"foo" : @{@"bar" : @"bar-value"}}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testUpdatingEntireRangeUpdatesEverything {
2019-08-05 13:27:47 -07:00
id<FNode> node = [FEmptyNode emptyNode];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"foo" : @"foo-value", @"bar" : @{@"child" : @"bar-child-value"}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:nil end:nil updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{@"foo" : @"foo-value", @"bar" : @{@"child" : @"bar-child-value"}}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testUpdatingRangeWithUnboundedLeftPostWorks {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"bar" : @"bar-value", @"foo" : @"foo-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"bar" : @"new-bar"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:nil end:PATH(@"bar") updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{@"bar" : @"new-bar", @"foo" : @"foo-value"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testUpdatingRangeWithRightPostChildOfLeftPostWorks {
2019-08-05 13:27:47 -07:00
id<FNode> node =
NODE((@{@"foo" : @{@"a" : @"a", @"b" : @{@"1" : @"1", @"2" : @"2"}, @"c" : @"c"}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"foo" : @{@"a" : @"new-a", @"b" : @{@"1" : @"new-1"}}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"foo")
end:PATH(@"foo/b/1")
updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected =
NODE((@{@"foo" : @{@"a" : @"new-a", @"b" : @{@"1" : @"new-1", @"2" : @"2"}, @"c" : @"c"}}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testUpdatingRangeWithRightPostChildOfLeftPostWorksWithIntegerKeys {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE(
(@{@"foo" : @{@"a" : @"a", @"b" : @{@"1" : @"1", @"2" : @"2", @"10" : @"10"}, @"c" : @"c"}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"foo" : @{@"a" : @"new-a", @"b" : @{@"1" : @"new-1"}}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"foo")
end:PATH(@"foo/b/2")
updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected =
NODE((@{@"foo" : @{@"a" : @"new-a", @"b" : @{@"1" : @"new-1", @"10" : @"10"}, @"c" : @"c"}}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testUpdatingLeafIncludesPriority {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"bar" : @"bar-value", @"foo" : @"foo-value", @"quu" : @"quu-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"foo" : @{@".value" : @"new-foo", @".priority" : @"prio"}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"bar")
end:PATH(@"foo")
updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{
@"bar" : @"bar-value",
@"foo" : @{@".value" : @"new-foo", @".priority" : @"prio"},
@"quu" : @"quu-value"
}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testUpdatingPriorityInChildrenNodeWorks {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"bar" : @"bar-value", @"foo" : @"foo-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"bar" : @"new-bar", @".priority" : @"prio"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:nil end:PATH(@"bar") updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected =
NODE((@{@"bar" : @"new-bar", @"foo" : @"foo-value", @".priority" : @"prio"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
// TODO: this test should actuall;y work, but priorities on empty nodes are ignored :(
- (void)updatingPriorityInChildrenNodeWorksAlone {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"bar" : @"bar-value", @"foo" : @"foo-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@".priority" : @"prio"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:nil
end:PATH(@".priority")
updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected =
NODE((@{@"bar" : @"bar-value", @"foo" : @"foo-value", @".priority" : @"prio"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testUpdatingPriorityOnInitiallyEmptyNodeDoesNotBreak {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@".priority" : @"prio", @"foo" : @"foo-value"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:nil end:PATH(@"foo") updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{@"foo" : @"foo-value", @".priority" : @"prio"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testPriorityIsDeletedWhenIncludedInChildrenRange {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"bar" : @"bar-value", @"foo" : @"foo-value", @".priority" : @"prio"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@"bar" : @"new-bar"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:nil
end:PATH(@"bar")
updates:updates]; // deletes priority
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{@"bar" : @"new-bar", @"foo" : @"foo-value"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testPriorityIsIncludedInOpenStart {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE((@{@"foo" : @{@"bar" : @"bar-value"}}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@".priority" : @"prio", @"baz" : @"baz"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:nil end:PATH(@"foo/bar") updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{@"baz" : @"baz", @".priority" : @"prio"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
- (void)testPriorityIsIncludedInOpenEnd {
2019-08-05 13:27:47 -07:00
id<FNode> node = NODE(@"leaf-node");
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> updates = NODE((@{@".priority" : @"prio", @"foo" : @"bar"}));
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FRangeMerge *merge = [[FRangeMerge alloc] initWithStart:PATH(@"/") end:nil updates:updates];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
id<FNode> expected = NODE((@{@"foo" : @"bar", @".priority" : @"prio"}));
id<FNode> actual = [merge applyToNode:node];
XCTAssertEqualObjects(actual, expected);
2017-05-15 12:27:07 -07:00
}
@end