-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from uber/tests
Improvements around empty data handling
- Loading branch information
Showing
19 changed files
with
291 additions
and
72 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
...osimplestore/src/androidTest/java/com/uber/simplestore/proto/ExampleInstrumentedTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
protosimplestore/src/main/java/com/uber/simplestore/proto/impl/SimpleProtoStoreFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.uber.simplestore.proto.impl; | ||
|
||
import android.content.Context; | ||
import com.uber.simplestore.ScopeConfig; | ||
import com.uber.simplestore.impl.SimpleStoreFactory; | ||
import com.uber.simplestore.proto.SimpleProtoStore; | ||
|
||
/** | ||
* Obtain an instance of a storage scope with proto support. Only one instance per scope may exist | ||
* at any time. | ||
*/ | ||
public final class SimpleProtoStoreFactory { | ||
|
||
public static SimpleProtoStore create(Context context, String scope) { | ||
return create(context, scope, ScopeConfig.DEFAULT); | ||
} | ||
|
||
public static SimpleProtoStore create(Context context, String scope, ScopeConfig config) { | ||
return new SimpleProtoStoreImpl(SimpleStoreFactory.create(context, scope, config), config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
protosimplestore/src/test/java/com/uber/simplestore/proto/ExampleUnitTest.java
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
protosimplestore/src/test/java/com/uber/simplestore/proto/SimpleProtoStoreImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.uber.simplestore.proto; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import android.content.Context; | ||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.uber.simplestore.ScopeConfig; | ||
import com.uber.simplestore.SimpleStore; | ||
import com.uber.simplestore.proto.impl.SimpleProtoStoreFactory; | ||
import com.uber.simplestore.proto.test.TestProto; | ||
import java.nio.charset.Charset; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.annotation.Config; | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
@RunWith(RobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE) | ||
public class SimpleProtoStoreImplTest { | ||
private static final String TEST_KEY = "test"; | ||
private static final String FOO = "foo"; | ||
private Context context = RuntimeEnvironment.systemContext; | ||
|
||
@Test | ||
public void defaultInstanceWhenEmpty() throws Exception { | ||
try (SimpleProtoStore store = SimpleProtoStoreFactory.create(context, "")) { | ||
ListenableFuture<TestProto.Basic> future = store.get(TEST_KEY, TestProto.Basic.parser()); | ||
assertThat(future.get()).isNotNull(); | ||
assertThat(future.get()).isEqualTo(TestProto.Basic.getDefaultInstance()); | ||
} | ||
} | ||
|
||
@Test | ||
public void defaultInstanceWhenEmpty_withRequiredField() throws Exception { | ||
try (SimpleProtoStore store = SimpleProtoStoreFactory.create(context, "")) { | ||
ListenableFuture<TestProto.Required> future = | ||
store.get(TEST_KEY, TestProto.Required.parser()); | ||
try { | ||
Futures.getChecked(future, InvalidProtocolBufferException.class); | ||
fail(); | ||
} catch (InvalidProtocolBufferException e) { | ||
// expected | ||
assertThat(e).hasMessageThat().contains("Message was missing required fields."); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void savesDefaultInstance() throws Exception { | ||
TestProto.Basic basic = TestProto.Basic.getDefaultInstance(); | ||
try (SimpleProtoStore store = SimpleProtoStoreFactory.create(context, "")) { | ||
store.put(TEST_KEY, basic).get(); | ||
} | ||
try (SimpleProtoStore store = SimpleProtoStoreFactory.create(context, "")) { | ||
TestProto.Basic out = store.get(TEST_KEY, TestProto.Basic.parser()).get(); | ||
assertThat(out).isEqualTo(basic); | ||
} | ||
} | ||
|
||
@Test | ||
public void savesValue() throws Exception { | ||
TestProto.Basic basic = TestProto.Basic.newBuilder().setName(FOO).build(); | ||
try (SimpleProtoStore store = SimpleProtoStoreFactory.create(context, "")) { | ||
store.put(TEST_KEY, basic).get(); | ||
} | ||
try (SimpleProtoStore store = SimpleProtoStoreFactory.create(context, "")) { | ||
TestProto.Basic out = store.get(TEST_KEY, TestProto.Basic.parser()).get(); | ||
assertThat(out).isEqualTo(basic); | ||
} | ||
} | ||
|
||
@Test | ||
public void failsGracefullyOnParsingFail() throws Exception { | ||
try (SimpleStore simpleStore = SimpleProtoStoreFactory.create(context, "")) { | ||
simpleStore.put(TEST_KEY, "garbage".getBytes(Charset.defaultCharset())).get(); | ||
} | ||
|
||
try (SimpleProtoStore store = SimpleProtoStoreFactory.create(context, "")) { | ||
ListenableFuture<TestProto.Basic> out = store.get(TEST_KEY, TestProto.Basic.parser()); | ||
try { | ||
Futures.getChecked(out, InvalidProtocolBufferException.class); | ||
fail(); | ||
} catch (InvalidProtocolBufferException e) { | ||
// expected | ||
assertThat(e).hasMessageThat().contains("invalid wire type"); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void whenCache_returnsDefaultOnParseFailure() throws Exception { | ||
try (SimpleStore simpleStore = SimpleProtoStoreFactory.create(context, "")) { | ||
simpleStore.put(TEST_KEY, "garbage".getBytes(Charset.defaultCharset())).get(); | ||
} | ||
|
||
try (SimpleProtoStore store = SimpleProtoStoreFactory.create(context, "", ScopeConfig.CACHE)) { | ||
TestProto.Basic failed = store.get(TEST_KEY, TestProto.Basic.parser()).get(); | ||
assertThat(failed).isEqualTo(TestProto.Basic.getDefaultInstance()); | ||
} | ||
} | ||
} |
Oops, something went wrong.