Skip to content

Commit

Permalink
Send snapshot on updates which render an item in list to not qualify …
Browse files Browse the repository at this point in the history
…a predicate.
  • Loading branch information
poojamat committed Nov 10, 2021
1 parent a80aa9c commit 4665a7b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,22 +311,20 @@ public void querySavedDataWithNumericalPredicates() throws DataStoreException, I
// 1, 4, 5, 6
QueryPredicate predicate = Post.RATING.ge(4)
.and(Post.RATING.lt(7))
.or(
Post.RATING.eq(1)
.and(Post.RATING.ne(7))
);
.or(Post.RATING.eq(1)
.and(Post.RATING.ne(7)));

Consumer<Cancelable> observationStarted = value -> {
};
Consumer<Cancelable> observationStarted = value -> { };
AtomicInteger count = new AtomicInteger(0);
Consumer<DataStoreQuerySnapshot<Post>> onQuerySnapshot = value -> {
if (count.get() == 0) {
assertEquals(4, value.getItems().size());
assertTrue(value.getItems().contains(savedModels.get(1)));
assertTrue(value.getItems().contains(savedModels.get(4)));
assertTrue(value.getItems().contains(savedModels.get(5)));
assertTrue(value.getItems().contains(savedModels.get(6)));
latch.countDown();
} else if (count.get() == 1) {
}else if (count.get() == 2) {
assertEquals(5, value.getItems().size());
assertTrue(value.getItems().contains(savedModels.get(1)));
assertTrue(value.getItems().contains(savedModels.get(4)));
Expand All @@ -337,10 +335,8 @@ public void querySavedDataWithNumericalPredicates() throws DataStoreException, I
}
count.incrementAndGet();
};
Consumer<DataStoreException> onObservationError = value -> {
};
Action onObservationComplete = () -> {
};
Consumer<DataStoreException> onObservationError = value -> { };
Action onObservationComplete = () -> { };

adapter.observeQuery(
Post.class,
Expand All @@ -351,8 +347,7 @@ public void querySavedDataWithNumericalPredicates() throws DataStoreException, I
onObservationComplete);

assertTrue(latch.await(5, TimeUnit.SECONDS));

for (int counter = 0; counter < 2; counter++) {
for (int counter = 3; counter < 5; counter++) {
final Post post = Post.builder()
.title("titlePrefix:" + counter + "change")
.status(PostStatus.INACTIVE)
Expand Down Expand Up @@ -428,23 +423,18 @@ public void querySavedDataWithPredicatesOnForeignKey() throws DataStoreException
.name("Jane Doe")
.build();
adapter.save(blogOwner);

final Blog blog = Blog.builder()
.name("Jane's Commercial Real Estate Blog")
.owner(blogOwner)
.build();
adapter.save(blog);
CountDownLatch latch = new CountDownLatch(1);
Consumer<Cancelable> observationStarted = value -> {
};
Consumer<Cancelable> observationStarted = value -> { };
Consumer<DataStoreQuerySnapshot<Blog>> onQuerySnapshot = value -> {
assertTrue(value.getItems().contains(blog));
latch.countDown();
};
Consumer<DataStoreException> onObservationError = value -> {
};
Action onObservationComplete = () -> {
};
latch.countDown(); };
Consumer<DataStoreException> onObservationError = value -> { };
Action onObservationComplete = () -> { };

adapter.observeQuery(
Blog.class,
Expand Down Expand Up @@ -609,7 +599,6 @@ public void queryWithOrderByRelatedModel() throws DataStoreException, Interrupte
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
@Ignore("Failing in build")
@Test
public void querySavedDataWithMultipleItemsThenItemSaves() throws DataStoreException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
Expand Down Expand Up @@ -658,4 +647,68 @@ public void querySavedDataWithMultipleItemsThenItemSaves() throws DataStoreExcep
}
assertTrue(changeLatch.await(9, TimeUnit.SECONDS));
}

/**
* Test querying the saved item in the SQLite database with observeQuery.
*
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void querySavedDataWithMultipleItemsThenUpdateOnItemSoItNoLongerSatisfiesPredicate() throws DataStoreException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch changeLatch = new CountDownLatch(1);
Consumer<Cancelable> observationStarted = value -> { };
Consumer<DataStoreException> onObservationError = value -> { };
Action onObservationComplete = () -> { };
final List<BlogOwner> savedModels = new ArrayList<>();
final int numModels = 5;
//Blogowner to be updated
final BlogOwner blogOwnerToBeUpdated = BlogOwner.builder()
.name("namePrefix:")
.build();
adapter.save(blogOwnerToBeUpdated);
savedModels.add(blogOwnerToBeUpdated);
AtomicInteger count = new AtomicInteger(0);
for (int counter = 1; counter < numModels; counter++) {
final BlogOwner blogOwner = BlogOwner.builder()
.name("namePrefix:" + counter)
.build();
adapter.save(blogOwner);
savedModels.add(blogOwner);
}

Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = value -> {
if (count.get() == 0) {
for (BlogOwner blogOwner : savedModels) {
assertTrue(value.getItems().contains(blogOwner));
}
assertEquals(5, value.getItems().size());
latch.countDown();
} else {
assertEquals(4, value.getItems().size());
changeLatch.countDown();
}
count.incrementAndGet();
};
adapter.observeQuery(
BlogOwner.class,
new ObserveQueryOptions(BlogOwner.NAME.beginsWith("namePrefix"), null),
observationStarted,
onQuerySnapshot,
onObservationError,
onObservationComplete);

assertTrue(latch.await(30, TimeUnit.SECONDS));
final BlogOwner blogOwnerUpdated = blogOwnerToBeUpdated.copyOfBuilder()
.name("updated").build();
try {
adapter.save(blogOwnerUpdated);
} catch (DataStoreException exception) {
exception.printStackTrace();
}

savedModels.add(blogOwnerUpdated);
assertTrue(changeLatch.await(900000, TimeUnit.SECONDS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,25 @@ public void observeQuery(
onObservationStarted.accept(this);

Consumer<Object> onItemChanged = value -> {

@SuppressWarnings("unchecked") StorageItemChange<T> itemChanged = (StorageItemChange<T>) value;
updateCompleteItemMap(itemChanged);
collect(itemChanged, onQuerySnapshot, itemClass, options, onObservationError);
try{
if(sqlQueryProcessor.modelExists(itemChanged.item(), options.getQueryPredicate())){
updateCompleteItemMap(itemChanged);
} else if(itemChanged.type() == StorageItemChange.Type.UPDATE){
completeItemMap.remove(itemChanged.item().getId());
}
collect(itemChanged, onQuerySnapshot, itemClass, options, onObservationError);
} catch (DataStoreException exception) {
onObservationError.accept(exception);
}


};
threadPool.submit(() -> queryLocalData(itemClass, options, onQuerySnapshot, onObservationError));

disposable = itemChangeSubject
.filter(x -> x.item().getClass().isAssignableFrom(itemClass) &&
sqlQueryProcessor.modelExists(x.item(), options.getQueryPredicate()))
.filter(x -> x.item().getClass().isAssignableFrom(itemClass))
.subscribe(
onItemChanged::accept,
failure -> {
Expand Down

0 comments on commit 4665a7b

Please sign in to comment.