Repository: yorkie-team/yorkie
Pull request #563 (2023-06-28 03:18:51)

Filter out removed documents from ListDocuments API
<!--  Thanks for sending a pull request! -->

**What this PR does / why we need it**:

Filter out removed documents from ListDocuments API

Previously, removed documents were exposed in the document list of CLI or Dashboard.
This commit ensures that removed documents are removed from the list, making it appears as if they are properly removed.

**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Related to #464

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note

```

**Additional documentation**:

<!--
This section can be blank if this pull request does not require a release note.

Please use the following format for linking documentation:
- [Usage]: <link>
- [Other doc]: <link>
-->
```docs

```

**Checklist**:
- [x] Added relevant tests or not required
- [x] Didn't break anything


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/yorkie-team/yorkie/pull/563?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) Report
> Merging [#563](https://app.codecov.io/gh/yorkie-team/yorkie/pull/563?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) (99ce519) into [main](https://app.codecov.io/gh/yorkie-team/yorkie/commit/525cacf2747f53187e41fd495e41f1b26e0a5d4e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) (525cacf) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head 99ce519 differs from pull request most recent head a2cc85d. Consider uploading reports for the commit a2cc85d to get more accurate results

```diff
@@            Coverage Diff             @@
##             main     #563      +/-   ##
==========================================
+ Coverage   52.25%   52.27%   +0.02%     
==========================================
  Files          67       67              
  Lines        6794     6797       +3     
==========================================
+ Hits         3550     3553       +3     
  Misses       2806     2806              
  Partials      438      438              
```


| [Impacted Files](https://app.codecov.io/gh/yorkie-team/yorkie/pull/563?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) | Coverage Δ | |
|---|---|---|
| [server/backend/database/memory/database.go](https://app.codecov.io/gh/yorkie-team/yorkie/pull/563?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team#diff-c2VydmVyL2JhY2tlbmQvZGF0YWJhc2UvbWVtb3J5L2RhdGFiYXNlLmdv) | `53.26% <100.00%> (ø)` | |
| [server/backend/database/mongo/client.go](https://app.codecov.io/gh/yorkie-team/yorkie/pull/563?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team#diff-c2VydmVyL2JhY2tlbmQvZGF0YWJhc2UvbW9uZ28vY2xpZW50Lmdv) | `38.01% <100.00%> (+0.23%)` | :arrow_up: |



Patch:
diff --git a/server/backend/database/memory/database.go b/server/backend/database/memory/database.go
index 1bf6ce48..88aa6641 100644
--- a/server/backend/database/memory/database.go
+++ b/server/backend/database/memory/database.go
@@ -1181,7 +1181,7 @@ func (d *DB) FindDocInfosByPaging(
 			break
 		}
 
-		if info.ID != paging.Offset {
+		if info.ID != paging.Offset && info.RemovedAt.IsZero() {
 			docInfos = append(docInfos, info)
 		}
 	}
diff --git a/server/backend/database/mongo/client.go b/server/backend/database/mongo/client.go
index 1e98f21d..25877945 100644
--- a/server/backend/database/mongo/client.go
+++ b/server/backend/database/mongo/client.go
@@ -1200,6 +1200,9 @@ func (c *Client) FindDocInfosByPaging(
 		"project_id": bson.M{
 			"$eq": encodedProjectID,
 		},
+		"removed_at": bson.M{
+			"$exists": false,
+		},
 	}
 	if paging.Offset != "" {
 		encodedOffset, err := encodeID(paging.Offset)


Test patch:
diff --git a/server/backend/database/testcases/testcase.go b/server/backend/database/testcases/testcases.go
similarity index 96%
rename from server/backend/database/testcases/testcase.go
rename to server/backend/database/testcases/testcases.go
index 443ecf1b..21d16ac1 100644
--- a/server/backend/database/testcases/testcase.go
+++ b/server/backend/database/testcases/testcases.go
@@ -584,6 +584,43 @@ func RunFindDocInfosByPagingTest(t *testing.T, db database.Database, projectID t
 			})
 		}
 	})
+
+	t.Run("FindDocInfosByPaging with docInfoRemovedAt test", func(t *testing.T) {
+		const testDocCnt = 3
+		ctx := context.Background()
+
+		// 01. Initialize a project and create documents.
+		projectInfo, err := db.CreateProjectInfo(ctx, t.Name(), dummyOwnerID, clientDeactivateThreshold)
+		assert.NoError(t, err)
+
+		var docInfos []*database.DocInfo
+		for i := 0; i < testDocCnt; i++ {
+			testDocKey := key.Key("key" + strconv.Itoa(i))
+			docInfo, err := db.FindDocInfoByKeyAndOwner(ctx, projectInfo.ID, dummyClientID, testDocKey, true)
+			assert.NoError(t, err)
+			docInfos = append(docInfos, docInfo)
+		}
+
+		// 02. List the documents.
+		result, err := db.FindDocInfosByPaging(ctx, projectInfo.ID, types.Paging[types.ID]{
+			PageSize:  10,
+			IsForward: false,
+		})
+		assert.NoError(t, err)
+		assert.Len(t, result, len(docInfos))
+
+		// 03. Remove a document.
+		err = db.CreateChangeInfos(ctx, projectInfo.ID, docInfos[0], 0, []*change.Change{}, true)
+		assert.NoError(t, err)
+
+		// 04. List the documents again and check the filtered result.
+		result, err = db.FindDocInfosByPaging(ctx, projectInfo.ID, types.Paging[types.ID]{
+			PageSize:  10,
+			IsForward: false,
+		})
+		assert.NoError(t, err)
+		assert.Len(t, result, len(docInfos)-1)
+	})
 }
 
 // RunCreateChangeInfosTest runs the CreateChangeInfos tests for the given db.