Skip to content
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

{PROD4POD-1931} Add tests for poly-importer #1204

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft

Conversation

JJ
Copy link
Contributor

@JJ JJ commented Oct 3, 2022

✍️ Description

Increase coverage of core tests. poly-importer is used by all features, and coverage was rather dim

Captura de pantalla de 2022-10-03 10-10-26

The scope of this PR will be defined on the run. For the time being, it's 🚧 and tries to increase it beyond original 13.68%

🏗️ Fixes 1931

ℹ️ Other information

Refactoring will be made as needed. For starters, it needed configuration of the jest jsdom environment.

♥️ Thank you!

And set up properly jest to work in a jsdom environment
@JJ JJ marked this pull request as draft October 3, 2022 08:12
Juan Julián (JJ) Merelo added 6 commits October 3, 2022 10:26
Up to 20% coverage now
Seems the right one for an abstract base class

:white_check_mark: too
Added dep for testing
@JJ JJ changed the title {PROD4POD-1930} Add tests for poly-importer {PROD4POD-1931} Add tests for poly-importer Oct 4, 2022
Juan Julián (JJ) Merelo added 2 commits October 10, 2022 09:28
But this is going to need some more mock code, so will leave it like this
JJ added a commit that referenced this pull request Oct 13, 2022
# ✍️ Description

Assigning a value to an attribute violated encapsulation, which is easy
to do in JS, but still. This is a cleaner API, with a private
`changeListener` which is assigned when the object is built.

### 🏗️ Works with PROD4POD-1931

Still cleaning up and refactoring the importer, looking at improving
coverage. Related to #1209 and #1204, but wanted it to be considered on
its own.

Also it's a change of API, which is better if considered on its own.
Copy link
Contributor

@fhd fhd left a comment

Choose a reason for hiding this comment

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

Pretty good start I'd say!

@@ -2,26 +2,24 @@ class PolyImportError extends Error {
constructor(type, cause) {
super(`Failed to ${type} file`);
this.cause = cause;
this.name = this.constructor.name;
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

});
});

describe("Can remove archives ", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at the code - it doesn't remove any archives, does it? Isn't it more it("ignores non-existent archives")? (I also think this is more of an it to "File storage" rather than its own describe, but that part is a nit.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's quite incomplete at the moment, I'm afraid. But yes, this specific one could be renamed and instantiated as you say.

@@ -3,6 +3,7 @@
*
* @class
*/

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: We usually don't have empty lines between JSDoc comment and declaration.

JJ added a commit that referenced this pull request Oct 14, 2022
# ✍️ Description

The mock for zip-files is more useful in the common `poly-import`
module. That way it can also be used in the module itself to mock
zipfiles, and imported from any module that needs to mock zips.

While we were at it, boost a bit coverage of said test.

### 🏗️ Fixes PROD4POD-1931

It is a precondition for #1204, since `Storage` needs zip files to be
tested.

## ℹ️ Other information

Some minor refactoring also made.

Might include some refactoring of tests, mainly to reduce boilerplate.
Not for the time being.

### Additional functionality

* ✅ adds tests in parts that were not covered, for
instance, file name analysis in Facebook
});
});

describe("File storage", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Figure these two describe blocks could be merged, but not that important.

@@ -0,0 +1,11 @@
import { Importer } from "../src";

describe("Importer can't be instantiated ", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: What's with the trailing space here in the string? Also below.

@@ -44,6 +44,7 @@ describe("Report metadata analysis", () => {
),
],
]);
console.log(zipFile);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Forgotten?

fileStorage = new FeatureFileStorage(pod);
});
it("ignores non-existent archives", () => {
expect(fileUri).toMatch(polyProtocolRegex);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why export the regex and test it? I think it'd be better to leave it as an implementation detail and write a dedicated test for handling of invalid URLs. That regex is never used directly in production code, so no point testing it directly IMHO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, testing the fileUri against the protocol. The problem with leaving it as an implementation details is that we might end up having incompatible implementation details, and causing issues such as this one https://jira.polypoly.eu/browse/PROD4POD-1133 So it's not so much an implementation details as a single source of truth, because eventually we will be interchanging files between different implementations.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a proposed API change - quite a different story. If we make that change we can test it, because the API would guarantee us to local paths. Right now it gives us internal URLs. You can test that it's a valid URL, but that's really as far as we should go. Testing that some internal regex used in the code matches the output makes little sense to me. What if the regex is wrong, and that causes the issue? I'd be OK with duplicating the regex in the tests, that way we at least don't have to expose implementation details...

const polyProtocol = "polypod://";
const polyProtocolRegex = new RegExp(`^${polyProtocol}`);
export const polyProtocol = "polypod://";
export const polyProtocolRegex = new RegExp(`^${polyProtocol}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

See above - I'm not a fan of exposing things for test purposes, the ability to mock components not under test being the only exception. Otherwise, things should be as private as possible and tests should make an effort to test code paths through the public API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really for testing; maybe I should have factored out this change as I have done with the rest. Main intention here is to make the polyProtocol a single source for all the literals that are used all over.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just realised that you could just call isPolypodUri with no need to export these internals.

That said, I'm still not sure why the unit tests for poly-import need to ensure that importArchive works as expected... They can't even, only in the test feature can we test API across platforms. The poly-import tests are for testing code in this library, nothing outside it.

JJ added a commit that referenced this pull request Oct 18, 2022
# ✍️ Description

While doing #1204 a bit of refactoring is needed; and I realized this
return value is never used. Giving it its own PR since it's a change in
API, and want it to be tested with the rest.

Also, some refactoring mainly eliminating `await` and `return` where
they're not actually needed.

### 🏗️ Fixes PROD4PROD-1931

It contributes to cleaning the repository, and improve test coverage in
`importer`; eliminating code that's not used will no doubt contribute to
that.
JJ added a commit that referenced this pull request Oct 25, 2022
# ✍️ Description

While doing #1204, the need to refactor mocks arose. It became a bit
hefty for an already heavy PR, so it's better taken outside.

### 🏗️ Working with PROD4POD-1931

## ℹ️ Other information

I'll leave it as draft for the time being, since some functions might be
unneeded after this. Feedback welcome anyway.
feature-utils/poly-import/test/importer.test.js Outdated Show resolved Hide resolved
feature-utils/poly-import/test/polyIn-errors.test.js Outdated Show resolved Hide resolved
Comment on lines +9 to +13
it("Returns increasing elapsed time", () => {
const oldTime = telemetry.elapsedTime();
expect(oldTime).toBeGreaterThan(0);
expect(telemetry.elapsedTime()).toBeGreaterThan(oldTime);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: just for its readability

Suggested change
it("Returns increasing elapsed time", () => {
const oldTime = telemetry.elapsedTime();
expect(oldTime).toBeGreaterThan(0);
expect(telemetry.elapsedTime()).toBeGreaterThan(oldTime);
});
test("elapsedTime() returns increasing elapsed time", () => {
const oldTime = telemetry.elapsedTime();
expect(oldTime).toBeGreaterThan(0);
const newTime = telemetry.elapsedTime();
expect(newTime).toBeGreaterThan(oldTime);
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really... In this case I don't think readability would add much to it, just verbosity.

Copy link
Contributor

Choose a reason for hiding this comment

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

mostly meant it for the test title - not just the separated var for newTime...

feature-utils/poly-import/test/storage.test.js Outdated Show resolved Hide resolved
Comment on lines +11 to +12
classData.forEach(([testClass, testMsg]) => {
const testCause = "test";
Copy link
Contributor

Choose a reason for hiding this comment

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

as this checks the classData (defined here and not in the real implementation), it seems to me it creates more tech-debt and potential issues if Error types are refactored at some point or more are added.
We might want to fill this object dynamically from PolyImportError somehow and not on our own? (or any other alternative)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, it checks that it's effectively closed for modification, as in the open/closed principle. I'll check anyway if it can be improved through encapsulation. And any way thanks for the suggestion, since this is still a draft.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants