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

[awattar] Feature/push code coverage #17752

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

tl-photography
Copy link
Contributor

PR does not change behaviour.

Only pushes code coverage and code maintainability.

@tl-photography tl-photography marked this pull request as draft November 15, 2024 23:44
@jlaur jlaur changed the title Feature/push code coverage [awattar] Feature/push code coverage Nov 15, 2024
@jlaur
Copy link
Contributor

jlaur commented Nov 16, 2024

@tl-photography - thanks. Did you intend to include the two commits from #17729? In that case, we need to add label "awaiting other PR" to mention this dependency.

@tl-photography
Copy link
Contributor Author

Ah ok, I marked it as draft only.

Can I set this label too, otherwise please do so.

@jlaur jlaur added the awaiting other PR Depends on another PR label Nov 16, 2024
Copy link

@Wolfgang1966 Wolfgang1966 left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for your work and your patience!

@lolodomo
Copy link
Contributor

I just merged #17729

@tl-photography tl-photography marked this pull request as ready for review November 30, 2024 20:29
@tl-photography
Copy link
Contributor Author

tl-photography commented Nov 30, 2024

@lolodomo

Thanks! PR should be good. 👍

Edit: Should I rebase?

@lolodomo lolodomo removed the awaiting other PR Depends on another PR label Dec 1, 2024
@jlaur
Copy link
Contributor

jlaur commented Dec 3, 2024

Edit: Should I rebase?

Yes, please.

@tl-photography tl-photography force-pushed the feature/push-code-coverage branch from 074b441 to 88312d1 Compare December 3, 2024 18:36
@tl-photography
Copy link
Contributor Author

Edit: Should I rebase?

Yes, please.

done

@@ -163,7 +162,7 @@ public void refreshChannel(ChannelUID channelUID) {
long diff;
switch (channelId) {
case CHANNEL_ACTIVE:
state = OnOffType.from(result.isActive());
state = OnOffType.from(result.isActive(getNow(zoneId).toInstant()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Here and other places below: This can be simplified, there is no reason to go through the trouble of getting a ZonedDateTime just to discard to time-zone again when converting back to Instant:

Suggested change
state = OnOffType.from(result.isActive(getNow(zoneId).toInstant()));
state = OnOffType.from(result.isActive(Instant.now()));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main reason for this change is to access the instant in the utests for stubbing.
I did not find a way with the available framework to stub the const Instant.now() method.

Copy link
Contributor Author

@tl-photography tl-photography Dec 7, 2024

Choose a reason for hiding this comment

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

hwo about this change in 622cfc0

would this work for you?

Copy link
Contributor

Choose a reason for hiding this comment

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

You might also be able to provide a Clock object for mocking this. See for example:

private final Clock clock;
public PriceListParser() {
this(Clock.system(EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
}
public PriceListParser(Clock clock) {
this.clock = clock;
}
public Map<Instant, BigDecimal> toHourly(Collection<DatahubPricelistRecord> records) {
Instant firstHourStart = Instant.now(clock)
.minus(ElectricityPriceSubscriptionCache.NUMBER_OF_HISTORIC_HOURS, ChronoUnit.HOURS)
.truncatedTo(ChronoUnit.HOURS);
Instant lastHourStart = Instant.now(clock).truncatedTo(ChronoUnit.HOURS).plus(2, ChronoUnit.DAYS)
.truncatedTo(ChronoUnit.DAYS);
return toHourly(records, firstHourStart, lastHourStart);
}

and:

@Test
void toHourlyNoChanges() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2023-01-23T12:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistN1.json", DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser.toHourly(Arrays.stream(records.records()).toList());
assertThat(tariffMap.size(), is(60));
assertThat(tariffMap.get(Instant.parse("2023-01-23T15:00:00Z")), is(equalTo(new BigDecimal("0.432225"))));
assertThat(tariffMap.get(Instant.parse("2023-01-23T16:00:00Z")), is(equalTo(new BigDecimal("1.05619"))));
assertThat(tariffMap.get(Instant.parse("2023-01-24T15:00:00Z")), is(equalTo(new BigDecimal("0.432225"))));
assertThat(tariffMap.get(Instant.parse("2023-01-24T16:00:00Z")), is(equalTo(new BigDecimal("1.05619"))));
}

Copy link
Contributor Author

@tl-photography tl-photography Dec 22, 2024

Choose a reason for hiding this comment

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

I just had a look and it seems the example you send me implements a Constructor where the clock object is initialized exactly for the reason of testing.
In the production code, only the constructor without the parameter is used.

I can change the code, but i think there is not much difference between the two approaches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i have worked on it and changed most of the code to a clock object to have the same look and feel.

@tl-photography tl-photography force-pushed the feature/push-code-coverage branch from c2c8b60 to c5eec4e Compare December 22, 2024 22:20
@tl-photography
Copy link
Contributor Author

i had conflicts and resolved them via rebase

@tl-photography
Copy link
Contributor Author

I also added now tests for the refresh logic, since this was easy to add after the clock rework.

@tl-photography
Copy link
Contributor Author

Coverage is now at 57% instr respectively 51% branch coverage.

image

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

Successfully merging this pull request may close these issues.

5 participants