Skip to content

Commit e29e885

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Fixing tests for backend (#448)
* Fixing tests * Update build-and-test.yml * Update docker-compose.yml * Update build-and-test.yml * Update build-and-test.yml --------- Co-authored-by: Leandro Ferreira <[email protected]>
1 parent cf45ba3 commit e29e885

File tree

8 files changed

+112
-45
lines changed

8 files changed

+112
-45
lines changed

.github/workflows/build-and-test.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,26 @@ jobs:
5757
general_build:
5858
name: General - Build debug
5959
runs-on: ubuntu-22.04
60+
services:
61+
postgres:
62+
image: postgres:latest
63+
env:
64+
POSTGRES_USER: postgres
65+
POSTGRES_PASSWORD: postgres
66+
POSTGRES_DB: writeopia
67+
ports:
68+
- 5432:5432
69+
options: >-
70+
--health-cmd="pg_isready"
71+
--health-interval=10s
72+
--health-timeout=5s
73+
--health-retries=5
6074
env:
6175
isCI: "true"
76+
IN_MEMORY_DATABASE: 'true'
77+
DB_USER: 'user'
78+
WRITEOPIA_FIREBASE_ID: 'id'
79+
WRITEOPIA_CLIENT_BASE_URL: "baseurl"
6280
steps:
6381
- name: Check out code
6482
uses: actions/checkout@v4
@@ -67,6 +85,18 @@ jobs:
6785
with:
6886
distribution: adopt
6987
java-version: '21'
88+
- name: Wait for Postgres to be healthy
89+
run: |
90+
for i in {1..10}; do
91+
pg_isready -h localhost -p 5432 && echo "Postgres is ready" && break
92+
echo "Waiting for postgres..." && sleep 5
93+
done
94+
- name: Run SQL init script
95+
run: |
96+
sudo apt-get install -y postgresql-client
97+
psql -h localhost -U postgres -d writeopia -f ./docker/postgres/init.sql
98+
env:
99+
PGPASSWORD: postgres
70100
- name: Build debug
71101
run: ./gradlew assembleDebug test --no-daemon
72102
ios_build:
@@ -87,6 +117,20 @@ jobs:
87117
backend_build_and_test:
88118
name: Backend - build
89119
runs-on: ubuntu-22.04
120+
services:
121+
postgres:
122+
image: postgres:latest
123+
env:
124+
POSTGRES_USER: postgres
125+
POSTGRES_PASSWORD: postgres
126+
POSTGRES_DB: writeopia
127+
ports:
128+
- 5432:5432
129+
options: >-
130+
--health-cmd="pg_isready"
131+
--health-interval=10s
132+
--health-timeout=5s
133+
--health-retries=5
90134
env:
91135
isCI: "true"
92136
IN_MEMORY_DATABASE: 'true'
@@ -101,6 +145,18 @@ jobs:
101145
with:
102146
distribution: adopt
103147
java-version: '21'
148+
- name: Wait for Postgres to be healthy
149+
run: |
150+
for i in {1..10}; do
151+
pg_isready -h localhost -p 5432 && echo "Postgres is ready" && break
152+
echo "Waiting for postgres..." && sleep 5
153+
done
154+
- name: Run SQL init script
155+
run: |
156+
sudo apt-get install -y postgresql-client
157+
psql -h localhost -U postgres -d writeopia -f ./docker/postgres/init.sql
158+
env:
159+
PGPASSWORD: postgres
104160
- name: Build backend
105161
run: ./gradlew :backend:gateway:test --no-daemon
106162
- name: upload results

backend/documents/documents/src/main/java/io/writeopia/api/documents/documents/DocumentsService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ object DocumentsService {
2222

2323
suspend fun receiveDocuments(
2424
documents: List<Document>,
25-
writeopiaDb: WriteopiaDbBackend
25+
writeopiaDb: WriteopiaDbBackend,
26+
useAi: Boolean
2627
): Boolean {
2728
documents.forEach { document ->
2829
writeopiaDb.saveDocument(document)
2930
}
3031

31-
return sendToAiHub(documents)
32+
return if(useAi) sendToAiHub(documents) else true
3233
}
3334

3435
suspend fun search(query: String, writeopiaDb: WriteopiaDbBackend): ResultData<List<Document>> =

backend/documents/documents/src/main/java/io/writeopia/api/documents/routing/DocumentsRouting.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import io.writeopia.sdk.serialization.extensions.toApi
1818
import io.writeopia.sdk.serialization.extensions.toModel
1919
import io.writeopia.sql.WriteopiaDbBackend
2020

21-
fun Routing.documentsRoute(writeopiaDb: WriteopiaDbBackend) {
21+
fun Routing.documentsRoute(writeopiaDb: WriteopiaDbBackend, useAi: Boolean) {
2222
route("api/document") {
2323
get("/{id}") {
2424
val id = call.pathParameters["id"]!!
@@ -74,7 +74,6 @@ fun Routing.documentsRoute(writeopiaDb: WriteopiaDbBackend) {
7474
}
7575

7676
get("/search") {
77-
println("received search request!")
7877
val query = call.queryParameters["q"]
7978

8079
if (query == null) {
@@ -93,13 +92,12 @@ fun Routing.documentsRoute(writeopiaDb: WriteopiaDbBackend) {
9392
}
9493

9594
post<List<DocumentApi>> { documentApiList ->
96-
println("Received documents!")
97-
9895
try {
9996
if (documentApiList.isNotEmpty()) {
10097
val addedToHub = DocumentsService.receiveDocuments(
10198
documentApiList.map { it.toModel() },
102-
writeopiaDb
99+
writeopiaDb,
100+
useAi
103101
)
104102

105103
if (addedToHub) {

backend/gateway/src/main/kotlin/io/writeopia/api/geteway/Application.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ fun main() {
1515
).start(wait = true)
1616
}
1717

18-
fun Application.module(writeopiaDb: WriteopiaDbBackend= configurePersistence()) {
19-
configureRouting(writeopiaDb)
18+
fun Application.module(
19+
writeopiaDb: WriteopiaDbBackend = configurePersistence(),
20+
useAi: Boolean = System.getenv("WRITEOPIA_USE_AI")?.toBoolean() ?: false
21+
) {
22+
configureRouting(writeopiaDb, useAi)
2023
configureSerialization()
2124
configureEditorSockets()
2225
configureHTTP()

backend/gateway/src/main/kotlin/io/writeopia/api/geteway/Routing.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import io.ktor.server.routing.routing
88
import io.writeopia.api.documents.routing.documentsRoute
99
import io.writeopia.sql.WriteopiaDbBackend
1010

11-
fun Application.configureRouting(writeopiaDb: WriteopiaDbBackend) {
11+
fun Application.configureRouting(writeopiaDb: WriteopiaDbBackend, useAi: Boolean) {
1212
routing {
13-
documentsRoute(writeopiaDb)
13+
documentsRoute(writeopiaDb, useAi)
1414

1515
get {
1616
call.respondText("Hi")

backend/gateway/src/test/kotlin/io/writeopia/api/gateway/DocumentsIntegrationTest.kt

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,78 +19,87 @@ import kotlin.test.assertEquals
1919

2020
class ApplicationTest {
2121

22-
val db = configurePersistence()
22+
private val db = configurePersistence()
2323

2424
@Test
25-
@Ignore
2625
fun `it should be possible to save and query document by id`() = testApplication {
2726
application {
2827
module(db)
2928
}
3029

3130
val client = defaultClient()
3231

33-
val documentApi = DocumentApi(
34-
id = "testiaskkakakaka",
35-
title = "Test Note",
36-
userId = "some user",
37-
parentId = "parentIdddd",
38-
isLocked = false,
39-
createdAt = 1000L,
40-
lastUpdatedAt = 2000L
32+
val documentApiList = listOf(
33+
DocumentApi(
34+
id = "testiaskkakakaka",
35+
title = "Test Note",
36+
userId = "some user",
37+
parentId = "parentIdddd",
38+
isLocked = false,
39+
createdAt = 1000L,
40+
lastUpdatedAt = 2000L,
41+
lastSyncedAt = 2000
42+
)
4143
)
4244

4345
val response = client.post("/api/document") {
4446
contentType(ContentType.Application.Json)
45-
setBody(documentApi)
47+
setBody(documentApiList)
4648
}
4749

4850
assertEquals(HttpStatusCode.OK, response.status)
4951

50-
val response1 = client.get("/api/document/${documentApi.id}")
52+
val response1 = client.get("/api/document/${documentApiList.first().id}")
5153

5254
assertEquals(HttpStatusCode.OK, response1.status)
53-
assertEquals(documentApi, response1.body())
55+
assertEquals(documentApiList.first(), response1.body())
5456

55-
db.deleteDocumentById(documentApi.id)
57+
documentApiList.forEach { documentApi ->
58+
db.deleteDocumentById(documentApi.id)
59+
}
5660
}
5761

5862
@Test
59-
@Ignore
6063
fun `it should be possible to save and query documents by parent id`() = testApplication {
6164
application {
6265
module(db)
6366
}
6467

6568
val client = defaultClient()
6669

67-
val documentApi = DocumentApi(
68-
id = "testias",
69-
title = "Test Note",
70-
userId = "some user",
71-
parentId = "parentId",
72-
isLocked = false,
73-
createdAt = 1000L,
74-
lastUpdatedAt = 2000L
70+
val documentApiList = listOf(
71+
DocumentApi(
72+
id = "testiaskkakakaka",
73+
title = "Test Note",
74+
userId = "some user",
75+
parentId = "parentIdddd",
76+
isLocked = false,
77+
createdAt = 1000L,
78+
lastUpdatedAt = 2000L,
79+
lastSyncedAt = 2000
80+
)
7581
)
7682

7783
val response = client.post("/api/document") {
7884
contentType(ContentType.Application.Json)
79-
setBody(documentApi)
85+
setBody(documentApiList)
8086
}
8187

8288
assertEquals(HttpStatusCode.OK, response.status)
8389

84-
val response1 = client.get("/api/document/parent/${documentApi.parentId}")
90+
val response1 = client.get(
91+
"/api/document/parent/${documentApiList.first().parentId}"
92+
)
8593

8694
assertEquals(HttpStatusCode.OK, response1.status)
87-
assertEquals(listOf(documentApi), response1.body())
95+
assertEquals(listOf(documentApiList.first()), response1.body())
8896

89-
db.deleteDocumentById(documentApi.id)
97+
documentApiList.forEach { documentApi ->
98+
db.deleteDocumentById(documentApi.id)
99+
}
90100
}
91101

92102
@Test
93-
@Ignore
94103
fun `it should be possible to save and query ids by parent id`() = testApplication {
95104
application {
96105
module(db)
@@ -125,7 +134,6 @@ class ApplicationTest {
125134
}
126135

127136
@Test
128-
@Ignore
129137
fun `it should be possible to get diff of folders`() = testApplication {
130138
application {
131139
module(db)
@@ -140,21 +148,22 @@ class ApplicationTest {
140148
parentId = "parentId",
141149
isLocked = false,
142150
createdAt = 1000L,
143-
lastUpdatedAt = 2000L
151+
lastUpdatedAt = 2000L,
152+
lastSyncedAt = 4000
144153
)
145154

146155
val documentApi2 = documentApi.copy(id = "testias2", lastUpdatedAt = 4000L)
147156

148157
val response = client.post("/api/document") {
149158
contentType(ContentType.Application.Json)
150-
setBody(documentApi)
159+
setBody(listOf(documentApi))
151160
}
152161

153162
assertEquals(HttpStatusCode.OK, response.status)
154163

155164
val response1 = client.post("/api/document") {
156165
contentType(ContentType.Application.Json)
157-
setBody(documentApi2)
166+
setBody(listOf(documentApi2))
158167
}
159168

160169
assertEquals(HttpStatusCode.OK, response1.status)

docker/postgres/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
- "5432:5432"
1111
volumes:
1212
- postgres_data:/var/lib/postgresql/data
13-
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
13+
- .docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
1414
restart: unless-stopped
1515

1616
volumes:

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ platformtools = "0.2.9"
1919
postgresSocketFactory = "1.15.1"
2020
r2dbcDriver = "2.0.2"
2121
room = "2.7.0-rc03"
22-
agp = "8.9.2"
22+
agp = "8.10.0"
2323
kotlin = "2.1.10"
2424
composeTest = "1.7.8"
2525
webWorkerDriver = "2.0.0"

0 commit comments

Comments
 (0)