1/* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package androidx.room.migration.bundle; 18 19import static org.hamcrest.CoreMatchers.is; 20import static org.hamcrest.MatcherAssert.assertThat; 21 22import static java.util.Arrays.asList; 23 24import org.junit.Test; 25import org.junit.runner.RunWith; 26import org.junit.runners.JUnit4; 27 28import java.util.Collections; 29 30@SuppressWarnings("ArraysAsListWithZeroOrOneArgument") 31@RunWith(JUnit4.class) 32public class EntityBundleTest { 33 @Test 34 public void schemaEquality_same_equal() { 35 EntityBundle bundle = new EntityBundle("foo", "sq", 36 asList(createFieldBundle("foo"), createFieldBundle("bar")), 37 new PrimaryKeyBundle(false, asList("foo")), 38 asList(createIndexBundle("foo")), 39 asList(createForeignKeyBundle("bar", "foo"))); 40 41 EntityBundle other = new EntityBundle("foo", "sq", 42 asList(createFieldBundle("foo"), createFieldBundle("bar")), 43 new PrimaryKeyBundle(false, asList("foo")), 44 asList(createIndexBundle("foo")), 45 asList(createForeignKeyBundle("bar", "foo"))); 46 47 assertThat(bundle.isSchemaEqual(other), is(true)); 48 } 49 50 @Test 51 public void schemaEquality_reorderedFields_equal() { 52 EntityBundle bundle = new EntityBundle("foo", "sq", 53 asList(createFieldBundle("foo"), createFieldBundle("bar")), 54 new PrimaryKeyBundle(false, asList("foo")), 55 Collections.<IndexBundle>emptyList(), 56 Collections.<ForeignKeyBundle>emptyList()); 57 58 EntityBundle other = new EntityBundle("foo", "sq", 59 asList(createFieldBundle("bar"), createFieldBundle("foo")), 60 new PrimaryKeyBundle(false, asList("foo")), 61 Collections.<IndexBundle>emptyList(), 62 Collections.<ForeignKeyBundle>emptyList()); 63 64 assertThat(bundle.isSchemaEqual(other), is(true)); 65 } 66 67 @Test 68 public void schemaEquality_diffFields_notEqual() { 69 EntityBundle bundle = new EntityBundle("foo", "sq", 70 asList(createFieldBundle("foo"), createFieldBundle("bar")), 71 new PrimaryKeyBundle(false, asList("foo")), 72 Collections.<IndexBundle>emptyList(), 73 Collections.<ForeignKeyBundle>emptyList()); 74 75 EntityBundle other = new EntityBundle("foo", "sq", 76 asList(createFieldBundle("foo2"), createFieldBundle("bar")), 77 new PrimaryKeyBundle(false, asList("foo")), 78 Collections.<IndexBundle>emptyList(), 79 Collections.<ForeignKeyBundle>emptyList()); 80 81 assertThat(bundle.isSchemaEqual(other), is(false)); 82 } 83 84 @Test 85 public void schemaEquality_reorderedForeignKeys_equal() { 86 EntityBundle bundle = new EntityBundle("foo", "sq", 87 Collections.<FieldBundle>emptyList(), 88 new PrimaryKeyBundle(false, asList("foo")), 89 Collections.<IndexBundle>emptyList(), 90 asList(createForeignKeyBundle("x", "y"), 91 createForeignKeyBundle("bar", "foo"))); 92 93 EntityBundle other = new EntityBundle("foo", "sq", 94 Collections.<FieldBundle>emptyList(), 95 new PrimaryKeyBundle(false, asList("foo")), 96 Collections.<IndexBundle>emptyList(), 97 asList(createForeignKeyBundle("bar", "foo"), 98 createForeignKeyBundle("x", "y"))); 99 100 101 assertThat(bundle.isSchemaEqual(other), is(true)); 102 } 103 104 @Test 105 public void schemaEquality_diffForeignKeys_notEqual() { 106 EntityBundle bundle = new EntityBundle("foo", "sq", 107 Collections.<FieldBundle>emptyList(), 108 new PrimaryKeyBundle(false, asList("foo")), 109 Collections.<IndexBundle>emptyList(), 110 asList(createForeignKeyBundle("bar", "foo"))); 111 112 EntityBundle other = new EntityBundle("foo", "sq", 113 Collections.<FieldBundle>emptyList(), 114 new PrimaryKeyBundle(false, asList("foo")), 115 Collections.<IndexBundle>emptyList(), 116 asList(createForeignKeyBundle("bar2", "foo"))); 117 118 assertThat(bundle.isSchemaEqual(other), is(false)); 119 } 120 121 @Test 122 public void schemaEquality_reorderedIndices_equal() { 123 EntityBundle bundle = new EntityBundle("foo", "sq", 124 Collections.<FieldBundle>emptyList(), 125 new PrimaryKeyBundle(false, asList("foo")), 126 asList(createIndexBundle("foo"), createIndexBundle("baz")), 127 Collections.<ForeignKeyBundle>emptyList()); 128 129 EntityBundle other = new EntityBundle("foo", "sq", 130 Collections.<FieldBundle>emptyList(), 131 new PrimaryKeyBundle(false, asList("foo")), 132 asList(createIndexBundle("baz"), createIndexBundle("foo")), 133 Collections.<ForeignKeyBundle>emptyList()); 134 135 assertThat(bundle.isSchemaEqual(other), is(true)); 136 } 137 138 @Test 139 public void schemaEquality_diffIndices_notEqual() { 140 EntityBundle bundle = new EntityBundle("foo", "sq", 141 Collections.<FieldBundle>emptyList(), 142 new PrimaryKeyBundle(false, asList("foo")), 143 asList(createIndexBundle("foo")), 144 Collections.<ForeignKeyBundle>emptyList()); 145 146 EntityBundle other = new EntityBundle("foo", "sq", 147 Collections.<FieldBundle>emptyList(), 148 new PrimaryKeyBundle(false, asList("foo")), 149 asList(createIndexBundle("foo2")), 150 Collections.<ForeignKeyBundle>emptyList()); 151 152 assertThat(bundle.isSchemaEqual(other), is(false)); 153 } 154 155 private FieldBundle createFieldBundle(String name) { 156 return new FieldBundle("foo", name, "text", false); 157 } 158 159 private IndexBundle createIndexBundle(String colName) { 160 return new IndexBundle("ind_" + colName, false, 161 asList(colName), "create"); 162 } 163 164 private ForeignKeyBundle createForeignKeyBundle(String targetTable, String column) { 165 return new ForeignKeyBundle(targetTable, "CASCADE", "CASCADE", 166 asList(column), asList(column)); 167 } 168} 169