Skip to content
This repository was archived by the owner on Feb 20, 2026. It is now read-only.

refactor: Add a separate function for building the entity proto#1350

Merged
danieljbruce merged 35 commits intoexclude-from-indexes-cleanupfrom
376498249-separate-function-for-building-the-entity-proto
Nov 5, 2024
Merged

refactor: Add a separate function for building the entity proto#1350
danieljbruce merged 35 commits intoexclude-from-indexes-cleanupfrom
376498249-separate-function-for-building-the-entity-proto

Conversation

@danieljbruce
Copy link
Copy Markdown
Contributor

@danieljbruce danieljbruce commented Oct 31, 2024

Summary:

Currently in the save method, the entity objects that the user passed in will be converted to entity protos. The save method has code that does this for arrays and code that does this for non-arrays. This PR refactors all of this code into a separate function called buildEntityProto because it allows us to unit test this code to make sure it is working correctly. We cannot unit test this code when it is buried inside the save function like it is right now and it allows us to complete the next steps listed below.

This is a follow-up to this PR.

Changes

  • In src/index.ts, code inside the save function is moved out into buildEntityProto.ts so that it can be unit tested.
  • In src/utils/entity/buildEntityProto.ts the buildEntityProto function is created that now contains the code that used to be in src/index.ts.
  • In test/entity.ts, some constants in a test are moved out into a fixtures file so that they can be reused by another test.
  • In test/entity/buildEntityProto.ts, some tests have been created for the buildEntityProto function and some of those tests use the constants that have been moved to the fixtures file.
  • In test/fixtures/entityObjectAndProto.ts, constants are added that used to be in test/entity.ts.
  • In test/index.ts, new mocks need to be added to the proxyquire object due to the fact that the location of the code that builds the entity proto is now in src/utils/entity/buildEntityProto.ts. So mocks that used to apply for src/index.ts will now apply for ../src/utils/entity/buildEntityProto.js.

Next steps:

Next we need to write tests to ensure that for any input, whenever extendExcludeFromIndexes is used and then buildEntityProto is used, the resulting entity proto always has excludeFromIndexes: true in all the places of the entity proto where the large properties are.

Against the current source code, this test fails because the code throws an error, but it should pass.
Pull out the code that extends the excludeFromIndexes list
The structure change requires another mock.
@product-auto-label product-auto-label bot added size: xl Pull request size is extra large. api: datastore Issues related to the googleapis/nodejs-datastore API. labels Oct 31, 2024
@danieljbruce danieljbruce changed the base branch from main to exclude-from-indexes-cleanup October 31, 2024 19:53
# Conflicts:
#	src/index.ts
#	src/utils/entity/extendExcludeFromIndexes.ts
} else {
// This code builds the right entityProto from the entityObject
entityProto = entity.entityToEntityProto(entityObject);
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This entire code block has been pulled into the buildEntityProto function.

* designed to be used for non-array entities.
*
*/
export function buildEntityProto(entityObject: Entity) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the new function that all the code from src/index.ts is being pulled into

@@ -1107,426 +1111,6 @@ describe('entity', () => {
});

it('should respect excludeFromIndexes', () => {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The fixed constants defined below are now going to get reused for the new buildEntityProto function so we pull them out into a separate file so that they can be used here and for buildEntityProto tests as well.

import {
entityObject,
expectedEntityProto,
} from '../fixtures/entityObjectAndProto';
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Import the fixed constants that were moved out of test/entity.ts.

{
'../../entity.js': {entity: fakeEntity},
}
);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These tests replace entity with fakeEntity using './entity.js': {entity: fakeEntity}, in the src/index.ts file. However, since the code that builds the entity proto used in these tests is no longer in src/index.ts, but is in buildEntityProto.js instead then the entity in buildEntityProto.js needs to be mocked out as well.

@danieljbruce danieljbruce marked this pull request as ready for review November 5, 2024 15:30
@danieljbruce danieljbruce requested a review from a team November 5, 2024 15:30
@danieljbruce danieljbruce requested a review from a team as a code owner November 5, 2024 15:30
Copy link
Copy Markdown

@daniel-sanche daniel-sanche left a comment

Choose a reason for hiding this comment

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

LGTM (although I mostly looked at the code, and not the tests)

@danieljbruce
Copy link
Copy Markdown
Contributor Author

LGTM (although I mostly looked at the code, and not the tests)

Sounds good. You'll have the opportunity to re-review the tests when all the PRs are put together for a merge onto main.

@danieljbruce danieljbruce merged commit 6e83861 into exclude-from-indexes-cleanup Nov 5, 2024
@danieljbruce danieljbruce deleted the 376498249-separate-function-for-building-the-entity-proto branch November 5, 2024 20:29
danieljbruce added a commit that referenced this pull request Nov 6, 2024
#1356)

* fix: The save function should not throw an error for name/value properties in arrays (#1336)

* Add some test cases for findLargeProperties_

* Add a test for arrays and skip it.

* Add a test for the complex case wrapped in an arra

* Add a test case for an array without name/value.

* Add another test for name/value pair

* Change the test case involving a name/value pair

* Add two simple test cases for excludeFromIndexes

* Add another unit test for findLargeProperties_

* Remove if block that stops recursion

* Add a test for save with a `name/value` pair

* Add some test code that ensures name/value encoded

Against the current source code, this test fails because the code throws an error, but it should pass.

* Add header

* Remove only

* Add a test that saves a long string value property

* Add a unit test checking longString for value

* Remove only

* TODO is done

* refactor: Add separate function for adding large properties to the excludeFromIndexes list (#1349)

* Add some test cases for findLargeProperties_

* Add a test for arrays and skip it.

* Add a test for the complex case wrapped in an arra

* Add a test case for an array without name/value.

* Add another test for name/value pair

* Change the test case involving a name/value pair

* Add two simple test cases for excludeFromIndexes

* Add another unit test for findLargeProperties_

* Remove if block that stops recursion

* Add a test for save with a `name/value` pair

* Add some test code that ensures name/value encoded

Against the current source code, this test fails because the code throws an error, but it should pass.

* Add header

* Remove only

* Add a test that saves a long string value property

* Add a unit test checking longString for value

* Remove only

* TODO is done

* Refactor the save function

Pull out the code that extends the excludeFromIndexes list

* Add TODO

* Write unit tests for the new excludeFromIndexes fn

* Add the header

* Document parameter for extendExcludeFromIndexes

* Remove redundant check

* refactor: Add a separate function for building the entity proto (#1350)

* Add some test cases for findLargeProperties_

* Add a test for arrays and skip it.

* Add a test for the complex case wrapped in an arra

* Add a test case for an array without name/value.

* Add another test for name/value pair

* Change the test case involving a name/value pair

* Add two simple test cases for excludeFromIndexes

* Add another unit test for findLargeProperties_

* Remove if block that stops recursion

* Add a test for save with a `name/value` pair

* Add some test code that ensures name/value encoded

Against the current source code, this test fails because the code throws an error, but it should pass.

* Add header

* Remove only

* Add a test that saves a long string value property

* Add a unit test checking longString for value

* Remove only

* TODO is done

* Refactor the save function

Pull out the code that extends the excludeFromIndexes list

* Add TODO

* Write unit tests for the new excludeFromIndexes fn

* Add the header

* Refactor the code for building the entity proto

* Rename it buildEntityProto

* The tests need another mock due to the structure

The structure change requires another mock.

* First two tests for buildEntityProto

* Reintroduce extendExcludeFromIndexes.ts tests

* Adjust the test to match expected proto

* Add another test for an entity in an array

* Add headers

* Add another header

* Eliminate space. Simplify diff

* test: Ensure that using extendExcludeFromIndexes and then buildEntityProto always excludes large properties from indexing (#1355)

* Add some test cases for findLargeProperties_

* Add a test for arrays and skip it.

* Add a test for the complex case wrapped in an arra

* Add a test case for an array without name/value.

* Add another test for name/value pair

* Change the test case involving a name/value pair

* Add two simple test cases for excludeFromIndexes

* Add another unit test for findLargeProperties_

* Remove if block that stops recursion

* Add a test for save with a `name/value` pair

* Add some test code that ensures name/value encoded

Against the current source code, this test fails because the code throws an error, but it should pass.

* Add header

* Remove only

* Add a test that saves a long string value property

* Add a unit test checking longString for value

* Remove only

* TODO is done

* Refactor the save function

Pull out the code that extends the excludeFromIndexes list

* Add TODO

* Write unit tests for the new excludeFromIndexes fn

* Add the header

* Refactor the code for building the entity proto

* Rename it buildEntityProto

* The tests need another mock due to the structure

The structure change requires another mock.

* First two tests for buildEntityProto

* Reintroduce extendExcludeFromIndexes.ts tests

* Adjust the test to match expected proto

* Add another test for an entity in an array

* Add headers

* Add another header

* Add some tests for checkEntityProto

Make sure that checkEntityProto is working correctly.

* Empty commit

* Pull out complexCaseEntities

* Add a few more tests to excludeIndexesAndBuildProto

* Add generated test cases ensuring completeness

* Add a few comments that explain behaviour of block

* Add comments explaining code blocks

* Add a bunch of other generated tests

* Make this test more meaningful for name/value chek

* Fix the generator

* Remove console logs

* Add better test name descriptions

* Add TODO

* Add a comment about the array of arrays test case

* Increase the max depth

* Add headers to the codebase

* Update parameter descriptions

* Add a bunch of parameter documentation

Correct only as well.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: datastore Issues related to the googleapis/nodejs-datastore API. size: xl Pull request size is extra large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants