Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core tests #1162

Merged
merged 18 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
relationship terminal state test
  • Loading branch information
wcekan committed Jan 25, 2020
commit 4274628268e9a6c01cb084924cd3e2aed58f3e9d
35 changes: 35 additions & 0 deletions elide-core/src/test/java/com/yahoo/elide/core/LifeCycleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.yahoo.elide.security.checks.Check;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

import example.Author;
Expand Down Expand Up @@ -252,6 +253,40 @@ public void testElideGet() throws Exception {
verify(tx).close();
}

@Test
public void testElideGetRelationship() throws Exception {
DataStore store = mock(DataStore.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
Book book = mock(Book.class);
Author author = mock(Author.class);
when(book.getId()).thenReturn(1L);
when(author.getId()).thenReturn(2L);
when(book.getAuthors()).thenReturn(ImmutableSet.of(author));

Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);

when(store.beginReadTransaction()).thenCallRealMethod();
when(store.beginTransaction()).thenReturn(tx);
when(tx.loadObject(eq(Book.class), any(), any(), isA(RequestScope.class))).thenReturn(book);

MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
ElideResponse response = elide.get("/book/1/relationships/authors", headers, null);
assertEquals(HttpStatus.SC_OK, response.getResponseCode());

/*
* This gets called for :
* - read pre-security for the book
* - read pre-commit for the book
* - read post-commit for the book
*/
verify(callback, times(3)).execute(eq(book), isA(RequestScope.class), any());
verify(tx).accessUser(any());
verify(tx).preCommit();
verify(tx).flush(any());
verify(tx).commit(any());
verify(tx).close();
}

@Test
public void testElidePatch() throws Exception {
DataStore store = mock(DataStore.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import com.yahoo.elide.security.User;
import com.yahoo.elide.security.checks.OperationCheck;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import example.Author;
Expand Down Expand Up @@ -115,6 +117,7 @@ public PersistentResourceTest() {
badUserScope = new RequestScope(null, null, mock(DataStoreTransaction.class),
new User(-1), null, elideSettings);
reset(goodUserScope.getTransaction());
assertFalse(goodUserScope.isUseFilterExpressions());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a test. Why use assert here?

Copy link
Contributor Author

@wcekan wcekan Jan 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not a test? Will move.

}

@Test
Expand Down Expand Up @@ -1665,6 +1668,7 @@ public void testCreateObjectSuccess() {
assertEquals(parent, created.getObject(),
"The create function should return the requested parent object"
);
assertTrue(goodScope.isNewResource(parent));
}

@Test()
Expand Down Expand Up @@ -2199,6 +2203,7 @@ public void testFilterExpressionByType() {

RequestScope scope = new RequestScope("/", null, mock(DataStoreTransaction.class),
new User(1), queryParams, elideSettings);

Optional<FilterExpression> filter = scope.getLoadFilterExpression(Author.class);
FilterPredicate predicate = (FilterPredicate) filter.get();
assertEquals("name", predicate.getField());
Expand All @@ -2208,6 +2213,20 @@ public void testFilterExpressionByType() {
assertEquals("[Author].name", predicate.getPath().toString());
}

@Test
public void testSparseFields() {
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();

queryParams.add("fields[author]", "name");

RequestScope scope = new RequestScope("/", null, mock(DataStoreTransaction.class),
new User(1), queryParams, elideSettings);
Map<String, Set<String>> expected = ImmutableMap.of("author", ImmutableSet.of("name"));
System.err.println(scope.getPagination());
assertEquals(10, scope.getPagination().getLimit());
assertEquals(0, scope.getPagination().getPageTotals());
}

@Test
public void testEqualsAndHashcode() {
Child childWithId = newChild(1);
Expand Down