Skip to content

Commit

Permalink
Merge pull request #164 from ehhc/fix_163
Browse files Browse the repository at this point in the history
fixes #163
  • Loading branch information
matthiasbalke authored Sep 1, 2017
2 parents 5d9aeb4 + f58bbad commit 0894952
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.wickedsource.budgeteer.persistence.record;

import java.util.Date;
import java.util.List;

import org.joda.money.Money;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -11,9 +14,6 @@
import org.wickedsource.budgeteer.persistence.person.PersonEntity;
import org.wickedsource.budgeteer.persistence.project.ProjectEntity;

import java.util.Date;
import java.util.List;

public interface WorkRecordRepository extends CrudRepository<WorkRecordEntity, Long>, QueryDslPredicateExecutor<WorkRecordEntity>, RecordRepository , JpaSpecificationExecutor {

/**
Expand All @@ -31,7 +31,7 @@ public interface WorkRecordRepository extends CrudRepository<WorkRecordEntity, L
* @param budgetId ID of the budget whose average daily rate to calculate
* @return monetary value of the average daily rate in cents.
*/
@Query("select sum(record.dailyRate * record.minutes) / sum(record.minutes) from WorkRecordEntity record where record.budget.id=:budgetId")
@Query("select case when (sum(record.minutes) = 0) then 0 else (sum(record.dailyRate * record.minutes) / sum(record.minutes)) end from WorkRecordEntity record where record.budget.id=:budgetId")
Double getAverageDailyRate(@Param("budgetId") long budgetId);

@Query("select max(record.date) from WorkRecordEntity record where record.budget.id=:budgetId")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.wickedsource.budgeteer.persistence.record;

import com.github.springtestdbunit.annotation.DatabaseOperation;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -11,12 +15,9 @@
import org.wickedsource.budgeteer.persistence.budget.BudgetEntity;
import org.wickedsource.budgeteer.persistence.person.PersonEntity;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.github.springtestdbunit.annotation.DatabaseOperation;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;

public class WorkRecordRepositoryTest extends IntegrationTestTemplate {

Expand All @@ -41,6 +42,14 @@ public void testGetAverageDailyRate() throws Exception {
Assert.assertEquals(56666d, value, 1d);
}

@Test
@DatabaseSetup("getAverageDailyRateZeroMinutes.xml")
@DatabaseTearDown(value = "getAverageDailyRate.xml", type = DatabaseOperation.DELETE_ALL)
public void testGetAverageDailyRateZeroMinutes() throws Exception {
double value = repository.getAverageDailyRate(1l);
Assert.assertEquals(0, value, 0);
}

@Test
@DatabaseSetup("getLastWorkRecordDate.xml")
@DatabaseTearDown(value = "getLastWorkRecordDate.xml", type = DatabaseOperation.DELETE_ALL)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<dataset>

<PROJECT id="1" name="project1"/>

<BUDGET id="1" name="Budget 1" total="100000" import_key="budget1" project_id="1"/>
<BUDGET_TAG budget_id="1" tag="Tag 1"/>
<BUDGET_TAG budget_id="1" tag="Tag 2"/>
<BUDGET_TAG budget_id="1" tag="Tag 3"/>

<PERSON id="1" name="person1" import_key="person1" project_id="1"/>

<IMPORT id="1" import_date="2015-01-01" start_date="2015-01-01" end_date="2015-01-01" import_type="Testimport" project_id="1"/>

<WORK_RECORD id="1" person_id="1" budget_id="1" record_date="2015-01-01" record_year="2015" record_week="1" record_month="0" record_day="1" minutes="480" daily_rate="50000" import_id="1"/>
<WORK_RECORD id="2" person_id="1" budget_id="1" record_date="2015-08-15" record_year="2015" record_week="33" record_month="7" record_day="15" minutes="-480" daily_rate="60000" import_id="1"/>

</dataset>

0 comments on commit 0894952

Please sign in to comment.