Skip to content

Commit

Permalink
Merge branch 'master' into client-certs
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesatheyDDS committed Jan 7, 2019
2 parents adacd6f + ec930ad commit 5b90514
Show file tree
Hide file tree
Showing 6 changed files with 434 additions and 0 deletions.
13 changes: 13 additions & 0 deletions migrations/20181220224717_create_fuel_eia_diesel_prices.up.fizz
@@ -0,0 +1,13 @@
sql("CREATE TABLE public.fuel_eia_diesel_prices (
id uuid NOT NULL,
pub_date date NOT NULL,
rate_start_date date NOT NULL,
rate_end_date date NOT NULL,
eia_price_per_gallon_millicents integer NOT NULL,
baseline_rate integer NOT NULL,
CHECK(baseline_rate > -1),
CHECK(baseline_rate < 101),
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
CONSTRAINT no_overlapping_rates EXCLUDE using gist (daterange(rate_start_date,rate_end_date,'[]') WITH &&)
);")
@@ -0,0 +1,6 @@
sql("INSERT INTO public.fuel_eia_diesel_prices (id, pub_date, rate_start_date, rate_end_date, eia_price_per_gallon_millicents, baseline_rate, created_at, updated_at)
VALUES
('6c16cbf5-04ad-11e9-955d-acde48001122', '2018-12-03', '2018-12-15', '2019-01-14', 320700, 6, now(), now()),
('71b32ffa-04ad-11e9-baf4-acde48001122', '2018-11-05', '2018-11-15', '2018-12-14', 333800, 7, now(), now()),
('722aad14-04ad-11e9-92df-acde48001122', '2018-10-01', '2018-10-15', '2018-11-14', 331300, 7, now(), now()),
('72da9e17-04ad-11e9-b643-acde48001122', '2018-09-03', '2018-09-15', '2018-10-14', 325200, 6, now(), now());")
68 changes: 68 additions & 0 deletions pkg/models/fuel_eia_diesel_price.go
@@ -0,0 +1,68 @@
package models

import (
"encoding/json"
"github.com/gobuffalo/pop"
"github.com/gobuffalo/uuid"
"github.com/gobuffalo/validate"
"github.com/gobuffalo/validate/validators"
"github.com/transcom/mymove/pkg/unit"
"time"
)

// FuelEIADieselPrice used to hold data from the SDDC Fuel Surcharge information
// found at https://etops.sddc.army.mil/pls/ppcig_camp/fsc.output to calculate a
// shipment's fuel surcharge
type FuelEIADieselPrice struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
PubDate time.Time `json:"pub_date" db:"pub_date"`
RateStartDate time.Time `json:"rate_start_date" db:"rate_start_date"`
RateEndDate time.Time `json:"rate_end_date" db:"rate_end_date"`
EIAPricePerGallonMillicents unit.Millicents `json:"eia_price_per_gallon_millicents" db:"eia_price_per_gallon_millicents"`
BaselineRate int64 `json:"baseline_rate" db:"baseline_rate"`
}

// String is not required by pop and may be deleted
func (f FuelEIADieselPrice) String() string {
jf, _ := json.Marshal(f)
return string(jf)
}

// FuelEIADieselPrices is not required by pop and may be deleted
type FuelEIADieselPrices []FuelEIADieselPrice

// String is not required by pop and may be deleted
func (f FuelEIADieselPrices) String() string {
jf, _ := json.Marshal(f)
return string(jf)
}

// Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method.
// This method is not required and may be deleted.
func (f *FuelEIADieselPrice) Validate(tx *pop.Connection) (*validate.Errors, error) {
return validate.Validate(
&validators.TimeIsPresent{Field: f.PubDate, Name: "PubDate"},
&validators.TimeIsPresent{Field: f.RateStartDate, Name: "RateStartDate"},
&validators.TimeIsPresent{Field: f.RateEndDate, Name: "RateEndDate"},
&validators.IntIsGreaterThan{Field: f.EIAPricePerGallonMillicents.Int(), Name: "EIAPricePerGallonMillicents", Compared: 0},
&validators.IntIsGreaterThan{Field: int(f.BaselineRate), Name: "BaselineRate", Compared: -1},
&validators.IntIsLessThan{Field: int(f.BaselineRate), Name: "BaselineRate", Compared: 101},
&validators.TimeAfterTime{
FirstTime: f.RateEndDate, FirstName: "RateEndDate",
SecondTime: f.RateStartDate, SecondName: "RateStartDate"},
), nil
}

// ValidateCreate gets run every time you call "pop.ValidateAndCreate" method.
// This method is not required and may be deleted.
func (f *FuelEIADieselPrice) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) {
return validate.NewErrors(), nil
}

// ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method.
// This method is not required and may be deleted.
func (f *FuelEIADieselPrice) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
return validate.NewErrors(), nil
}
151 changes: 151 additions & 0 deletions pkg/models/fuel_eia_diesel_price_test.go
@@ -0,0 +1,151 @@
package models_test

import (
"github.com/gobuffalo/uuid"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/testdatagen"
"github.com/transcom/mymove/pkg/unit"
"testing"
"time"
)

func (suite *ModelSuite) TestBasicFuelEIADieselPriceInstantiation() {
testCases := map[string]struct {
fuelEIADP models.FuelEIADieselPrice
expectedErrs map[string][]string
}{
"Successful Create": {
fuelEIADP: models.FuelEIADieselPrice{
ID: uuid.Must(uuid.NewV4()),
PubDate: time.Now(),
RateStartDate: time.Now(),
RateEndDate: time.Now().AddDate(0, 1, -1),
EIAPricePerGallonMillicents: 320700,
BaselineRate: 6,
},
expectedErrs: nil,
},

"Empty Fields": {
fuelEIADP: models.FuelEIADieselPrice{},
expectedErrs: map[string][]string{
"pub_date": {"PubDate can not be blank."},
"rate_start_date": {"RateStartDate can not be blank."},
"rate_end_date": {"RateEndDate can not be blank."},
"e_i_a_price_per_gallon_millicents": {"0 is not greater than 0."},
},
},

"Bad baseline rate": {
fuelEIADP: models.FuelEIADieselPrice{
ID: uuid.Must(uuid.NewV4()),
PubDate: time.Now(),
RateStartDate: time.Now(),
RateEndDate: time.Now(),
EIAPricePerGallonMillicents: 320700,
BaselineRate: 102,
},
expectedErrs: map[string][]string{
"baseline_rate": {"102 is not less than 101."},
},
},

"Bad RateEndDate": {
fuelEIADP: models.FuelEIADieselPrice{
ID: uuid.Must(uuid.NewV4()),
PubDate: time.Now(),
RateStartDate: time.Now(),
RateEndDate: time.Now().AddDate(0, -1, 0),
EIAPricePerGallonMillicents: 320700,
BaselineRate: 6,
},
expectedErrs: map[string][]string{
"rate_end_date": {"RateEndDate must be after RateStartDate."},
},
},
}

for name, test := range testCases {
suite.T().Run(name, func(t *testing.T) {
suite.verifyValidationErrors(&test.fuelEIADP, test.expectedErrs)
})
}

}

func (suite *ModelSuite) TestFuelEIADieselPriceOverlappingDatesConstraint() {
now := time.Now()
id := uuid.Must(uuid.NewV4())

suite.T().Run("Overlapping Dates Constraint Test", func(t *testing.T) {

// Test for overalapping start and end dates
newFuelPrice := models.FuelEIADieselPrice{
ID: id,
PubDate: now,
RateStartDate: time.Date(2017, time.November, 15, 0, 0, 0, 0, time.UTC),
RateEndDate: time.Date(2017, time.December, 14, 0, 0, 0, 0, time.UTC),
EIAPricePerGallonMillicents: 320700,
BaselineRate: 6,
}
verrs, err := suite.db.ValidateAndCreate(&newFuelPrice)

id = uuid.Must(uuid.NewV4())
newFuelPrice = models.FuelEIADieselPrice{
ID: id,
PubDate: now,
RateStartDate: time.Date(2017, time.December, 15, 0, 0, 0, 0, time.UTC),
RateEndDate: time.Date(2018, time.January, 14, 0, 0, 0, 0, time.UTC),
EIAPricePerGallonMillicents: 320700,
BaselineRate: 6,
}
verrs, err = suite.db.ValidateAndCreate(&newFuelPrice)

// Overlapping record should cause en error
id = uuid.Must(uuid.NewV4())
newFuelPrice = models.FuelEIADieselPrice{
ID: id,
PubDate: now,
RateStartDate: time.Date(2017, time.December, 20, 0, 0, 0, 0, time.UTC),
RateEndDate: time.Date(2018, time.January, 14, 0, 0, 0, 0, time.UTC),
EIAPricePerGallonMillicents: 320700,
BaselineRate: 6,
}

verrs, err = suite.db.ValidateAndCreate(&newFuelPrice)
suite.EqualError(err, "pq: conflicting key value violates exclusion constraint \"no_overlapping_rates\"")
suite.Empty(verrs.Error())

})

}

// Create multiple records covering a range of dates
// Can change the dates for start and end ranges and
// can create a default baseline and price to use via assertions
func (suite *ModelSuite) TestMakeFuelEIADieselPrices() {
testdatagen.MakeFuelEIADieselPrices(suite.db, testdatagen.Assertions{})
// or call testdatagen.MakeDefaultFuelEIADieselPrices(suite.db)
// to change the date range:
// assertions testdatagen.Assertions{}
// assertions.assertions.FuelEIADieselPrice.RateStartDate = time.Date(testdatagen.TestYear-1, time.July, 15, 0, 0, 0, 0, time.UTC)
// assertions.assertions.FuelEIADieselPrice.RateEndDate = time.Date(testdatagen.TestYear+1, time.July, 14, 0, 0, 0, 0, time.UTC)
// testdatagen.MakeFuelEIADieselPrices(suite.db, assertions)
}

// Create 1 record for the shipment date provided and use assertions
func (suite *ModelSuite) TestMakeFuelEIADieselPriceForDate() {
rateStartDate := time.Date(2017, time.July, 15, 0, 0, 0, 0, time.UTC)
assertions := testdatagen.Assertions{}
assertions.FuelEIADieselPrice.RateStartDate = rateStartDate
assertions.FuelEIADieselPrice.EIAPricePerGallonMillicents = unit.Millicents(695700)
shipmentDate := assertions.FuelEIADieselPrice.RateStartDate.AddDate(0, 0, 10)

testdatagen.MakeFuelEIADieselPriceForDate(suite.db, shipmentDate, assertions)
}

// Create 1 record for the shipment date provided
func (suite *ModelSuite) TestMakeDefaultFuelEIADieselPriceForDate() {
shipmentDate := time.Now()
testdatagen.MakeDefaultFuelEIADieselPriceForDate(suite.db, shipmentDate)
}

0 comments on commit 5b90514

Please sign in to comment.