@@ -1191,6 +1191,113 @@ func TestCreateListObservers(t *testing.T) {
11911191 }
11921192}
11931193
1194+ func TestCreateObservations (t * testing.T ) {
1195+ pivotTime := time .Date (2020 , 7 , 7 , 12 , 0 , 0 , 0 , time .UTC )
1196+ // Create a site to attach the observations to
1197+ siteResp , err := dc .CreateLocation (t .Context (), & pb.CreateLocationRequest {
1198+ LocationName : "test_create_observations_site" ,
1199+ GeometryWkt : "POINT(-0.1 51.5)" ,
1200+ EffectiveCapacityWatts : 1000000 ,
1201+ Metadata : & structpb.Struct {},
1202+ EnergySource : pb .EnergySource_ENERGY_SOURCE_SOLAR ,
1203+ LocationType : pb .LocationType_LOCATION_TYPE_SITE ,
1204+ ValidFromUtc : timestamppb .New (pivotTime .Add (- time .Hour * 4 )),
1205+ })
1206+ require .NoError (t , err )
1207+
1208+ // Update the capacity
1209+ updateResp , err := dc .UpdateLocationCapacity (t .Context (), & pb.UpdateLocationCapacityRequest {
1210+ LocationUuid : siteResp .LocationUuid ,
1211+ EnergySource : pb .EnergySource_ENERGY_SOURCE_SOLAR ,
1212+ NewEffectiveCapacityWatts : 2000000 ,
1213+ ValidFromUtc : timestamppb .New (pivotTime .Add (time .Hour * 1 )),
1214+ })
1215+ require .NoError (t , err )
1216+
1217+ // Create an observer to make the observations
1218+ obsResp , err := dc .CreateObserver (t .Context (), & pb.CreateObserverRequest {
1219+ Name : "test_create_observations_observer" ,
1220+ })
1221+ require .NoError (t , err )
1222+
1223+ validObservations := make ([]* pb.CreateObservationsRequest_Value , 10 )
1224+ for i := range validObservations {
1225+ value := 0.5 * float64 (siteResp .EffectiveCapacityWatts )
1226+ if i >= 2 {
1227+ value = 0.5 * float64 (updateResp .EffectiveCapacityWatts )
1228+ }
1229+ validObservations [i ] = & pb.CreateObservationsRequest_Value {
1230+ TimestampUtc : timestamppb .New (pivotTime .Add (time .Duration (i * 30 ) * time .Minute )),
1231+ ValueWatts : uint64 (value ),
1232+ }
1233+ }
1234+
1235+ invalidObservations := make ([]* pb.CreateObservationsRequest_Value , 10 )
1236+ for i := range invalidObservations {
1237+ value := 0.5 * float64 (siteResp .EffectiveCapacityWatts )
1238+ if i >= 2 {
1239+ value = 1.2 * float64 (updateResp .EffectiveCapacityWatts )
1240+ }
1241+ invalidObservations [i ] = & pb.CreateObservationsRequest_Value {
1242+ TimestampUtc : timestamppb .New (pivotTime .Add (time .Duration (i * 30 ) * time .Minute )),
1243+ ValueWatts : uint64 (value ),
1244+ }
1245+ }
1246+
1247+ testcases := []struct {
1248+ name string
1249+ req * pb.CreateObservationsRequest
1250+ }{
1251+ {
1252+ name : "Should create valid observations" ,
1253+ req : & pb.CreateObservationsRequest {
1254+ LocationUuid : siteResp .LocationUuid ,
1255+ EnergySource : pb .EnergySource_ENERGY_SOURCE_SOLAR ,
1256+ ObserverName : obsResp .ObserverName ,
1257+ Values : validObservations ,
1258+ },
1259+ },
1260+ {
1261+ name : "Shouldn't create invalid observations" ,
1262+ req : & pb.CreateObservationsRequest {
1263+ LocationUuid : siteResp .LocationUuid ,
1264+ EnergySource : pb .EnergySource_ENERGY_SOURCE_SOLAR ,
1265+ ObserverName : obsResp .ObserverName ,
1266+ Values : invalidObservations ,
1267+ },
1268+ },
1269+ {
1270+ name : "Shouldn't create observations for non-existent location" ,
1271+ req : & pb.CreateObservationsRequest {
1272+ LocationUuid : "non_existent_location" ,
1273+ EnergySource : pb .EnergySource_ENERGY_SOURCE_SOLAR ,
1274+ ObserverName : obsResp .ObserverName ,
1275+ Values : validObservations ,
1276+ },
1277+ },
1278+ {
1279+ name : "Shouldn't create observations for non-existent observer" ,
1280+ req : & pb.CreateObservationsRequest {
1281+ LocationUuid : siteResp .LocationUuid ,
1282+ EnergySource : pb .EnergySource_ENERGY_SOURCE_SOLAR ,
1283+ ObserverName : "non_existent_observer" ,
1284+ Values : validObservations ,
1285+ },
1286+ },
1287+ }
1288+
1289+ for _ , tc := range testcases {
1290+ t .Run (tc .name , func (t * testing.T ) {
1291+ _ , err := dc .CreateObservations (t .Context (), tc .req )
1292+ if strings .Contains (tc .name , "Shouldn't" ) {
1293+ require .Error (t , err )
1294+ } else {
1295+ require .NoError (t , err )
1296+ }
1297+ })
1298+ }
1299+ }
1300+
11941301func TestGetWeekAverageDeltas (t * testing.T ) {
11951302 pivotTime := time .Date (2025 , 1 , 1 , 12 , 0 , 0 , 0 , time .UTC )
11961303 metadata , err := structpb .NewStruct (map [string ]any {"source" : "test" })
0 commit comments