-
Notifications
You must be signed in to change notification settings - Fork 6
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
Test Coverage #8
base: main
Are you sure you want to change the base?
Changes from all commits
baead1d
61067cb
753c256
853a9ed
a6b0a5d
dde365f
81939ef
0dcfc0f
8cc9e25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this enum required? |
||
public enum resources { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
debug=false | ||
configserver.port=8082 | ||
spring.application.name=studioservice | ||
spring.config.import=configserver:${CONFIG_SERVER} | ||
config-server.port=8082 | ||
spring.application.name=studioService | ||
spring.config.import=optional:config-server:${CONFIG_SERVER} | ||
spring.jpa.open-in-view=false | ||
spring.flyway.baseline-on-migrate = true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.gamedoora.backend.studioservices; | ||
|
||
import java.util.*; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.*; | ||
import org.springframework.boot.autoconfigure.domain.EntityScan; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
|
||
import com.gamedoora.backend.studioservices.repository.StudioRepository; | ||
import com.gamedoora.model.dao.*; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
|
||
@DataJpaTest(properties="spring.cloud.config.enabled=false") | ||
@TestPropertySource("classpath:test.properties") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use application.properties in the test/resources. The idea is to follow similar pattern across the projects. |
||
@EntityScan("com.gamedoora.model.*") // Scanning the model(s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this to the Application class. |
||
class repositoryTest { | ||
|
||
@Autowired | ||
private StudioRepository repo; | ||
|
||
|
||
@Autowired | ||
private TestEntityManager entityManager; | ||
|
||
// Can Use @BeforeEach annotation | ||
|
||
|
||
@Test | ||
public void test() { | ||
// Checking object creation and its storing | ||
|
||
Studios studio = Studios.builder().id(123).name("Yoshita").build(); | ||
|
||
Studios savedStudio = repo.save(studio); | ||
|
||
Assertions.assertNotNull(savedStudio); | ||
|
||
//fail("Not yet implemented"); | ||
} | ||
|
||
@Test | ||
public void findByIsCommunity(){ | ||
// single checking for IsCommunity | ||
|
||
// To use for IsCommunity - 0 or 1 as True - False | ||
|
||
Studios studio1 = Studios.builder().id(1).name("A").isCommunity(1).description("DA").build(); | ||
Studios studio2 = Studios.builder().id(2).name("b").isCommunity(0).description("DA").visibility(false).build(); | ||
Studios studio3 = Studios.builder().id(3).name("b").isCommunity(1).description("DA").visibility(false).build(); | ||
|
||
// check 1 | ||
repo.save(studio1); | ||
List<Studios> commu = repo.findByIsCommunity(1); | ||
Assertions.assertEquals(1,commu.size()); | ||
|
||
Assertions.assertEquals("A",commu.get(0).getName()); | ||
|
||
// check 1 Extended | ||
repo.save(studio2); | ||
repo.save(studio3); | ||
//repo.save(studio2); // sequence changing affects below assertions | ||
|
||
commu = repo.findByIsCommunity(1); | ||
Assertions.assertEquals(2,commu.size()); | ||
Assertions.assertEquals("A",commu.get(0).getName()); | ||
Assertions.assertEquals("b",commu.get(1).getName()); | ||
} | ||
|
||
@Test | ||
public void testFindByIsCommunity() { | ||
// Create some test studios with different is_community values | ||
|
||
// To use for IsCommunity - 0 or 1 as True - False | ||
|
||
Studios studio1 = Studios.builder().id(1).name("A").isCommunity(1).description("DA").build(); | ||
Studios studio2 = Studios.builder().id(2).name("A").isCommunity(0).description("DA").visibility(false).build(); | ||
|
||
List<Studios> list = List.of(studio1,studio2); | ||
repo.saveAll(list); | ||
// Perform the repository method call | ||
List<Studios> communityStudios = repo.findByIsCommunity(1); | ||
|
||
assertEquals(1,communityStudios.size()); | ||
for(int i=0; i<communityStudios.size();i++) | ||
assertEquals("A",(communityStudios.get(i)).getName()); // works well with for loops | ||
|
||
//assertThat(communityStudios).hasSize(1).contains(studio1); // Uses ArrayList | ||
|
||
} | ||
|
||
// To use TestEntityManager instead of EntityManager for testing environment | ||
|
||
// Below Shows detached state error for TestEntityManager.persist(_object_) | ||
|
||
// @Test | ||
// public void findByUsersSet_Name() | ||
// { | ||
// Studios studio1 = Studios.builder().id(1).name("A").isCommunity(1).description("DA").build(); | ||
// Studios studio2 = Studios.builder().id(1).name("b").isCommunity(2).description("DA").visibility(false).build(); | ||
// | ||
// List<Studios> list = List.of(studio1,studio2); | ||
// List<Studios> lis = repo.saveAll(list); | ||
// | ||
// Assertions.assertNotNull(lis); | ||
// | ||
// Users user1 = Users.builder().id(1).providerToken("").email("yo").build(); | ||
// entityManager.persist(user1); | ||
// | ||
// List<Studios> com = repo.findByUsersSet_Name("A"); | ||
// | ||
// Assertions.assertEquals(1,com.size()); | ||
// | ||
// } | ||
|
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# H2 Database | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add these properties to application properties in test/resources |
||
|
||
db.primary.url=jdbc:h2:mem:testdb;MODE=PostgreSQL | ||
db.primary.password=pass | ||
db.primary.driver=org.h2.Driver | ||
db.primary.hbm2ddl.auto=create-drop | ||
db.primary.user=dh | ||
db.primary.hibernate.dialect=org.hibernate.dialect.H2Dialect | ||
|
||
spring.h2.console.enabled=true | ||
|
||
spring.flyway.enabled=false | ||
keycloak.server.url=http://127.0.0.1:8080/u/auth | ||
keycloak.server.username=admin | ||
keycloak.server.clientid= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Observational: Returning the list within the if block can remove the need for else block.