refactor: Add a separate function for building the entity proto#1350
Conversation
…into 1242-write-tests-for-findLargeProperties-revisit
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
…tps://github.com/googleapis/nodejs-datastore into 376497482-separate-function-for-adding-large-properties
The structure change requires another mock.
…into 376498249-separate-function-for-building-the-entity-proto
# Conflicts: # src/index.ts # src/utils/entity/extendExcludeFromIndexes.ts
| } else { | ||
| // This code builds the right entityProto from the entityObject | ||
| entityProto = entity.entityToEntityProto(entityObject); | ||
| } |
There was a problem hiding this comment.
This entire code block has been pulled into the buildEntityProto function.
| * designed to be used for non-array entities. | ||
| * | ||
| */ | ||
| export function buildEntityProto(entityObject: Entity) { |
There was a problem hiding this comment.
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', () => { | |||
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
Import the fixed constants that were moved out of test/entity.ts.
| { | ||
| '../../entity.js': {entity: fakeEntity}, | ||
| } | ||
| ); |
There was a problem hiding this comment.
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.
daniel-sanche
left a comment
There was a problem hiding this comment.
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. |
#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.
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
buildEntityProtobecause 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
src/index.ts, code inside the save function is moved out intobuildEntityProto.tsso that it can be unit tested.src/utils/entity/buildEntityProto.tsthebuildEntityProtofunction is created that now contains the code that used to be insrc/index.ts.test/entity.ts, some constants in a test are moved out into afixturesfile so that they can be reused by another test.test/entity/buildEntityProto.ts, some tests have been created for thebuildEntityProtofunction and some of those tests use the constants that have been moved to thefixturesfile.test/fixtures/entityObjectAndProto.ts, constants are added that used to be intest/entity.ts.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 insrc/utils/entity/buildEntityProto.ts. So mocks that used to apply forsrc/index.tswill now apply for../src/utils/entity/buildEntityProto.js.Next steps:
Next we need to write tests to ensure that for any input, whenever
extendExcludeFromIndexesis used and thenbuildEntityProtois used, the resulting entity proto always has excludeFromIndexes: true in all the places of the entity proto where the large properties are.