[BROKE] Load order issue on many tests

pull/1048/head
Rick Carlino 2018-11-20 17:09:59 -06:00
parent a166021860
commit 49a9cc1f91
6 changed files with 52 additions and 23 deletions

View File

@ -8,6 +8,7 @@ import {
import * as _ from "lodash";
import { resourceReducer, emptyState } from "../resources/reducer";
import { resourceReady } from "../sync/actions";
import { threeWayComparison as c3 } from "../util/move";
export function fakeDevice(): TaggedDevice {
return {
"kind": "Device",
@ -20,6 +21,7 @@ export function fakeDevice(): TaggedDevice {
"uuid": "Device.415.0"
};
}
const tr1: TaggedResource = {
"kind": "User",
"body": {
@ -315,13 +317,51 @@ const log: TaggedLog = {
export let FAKE_RESOURCES: TaggedResource[] = [tr1, fakeDevice(), tr2, tr3, tr4,
tr5, tr6, tr7, tr8, tr9, tr10, tr11, tr12, tr13, tr14, tr15, log];
const KIND: keyof TaggedResource = "kind"; // Safety first, kids.
type ResourceGroupNumber = 0 | 1 | 2 | 3 | 4;
type ResourceLookupTable = Record<TaggedResource["kind"], ResourceGroupNumber>;
/** In the real app, resources are loaded in a particular order.
* This table serves as a reference to prevent referential integrity issues. */
const GROUPS: ResourceLookupTable = {
// GROUP 0
Device: 0,
FarmwareEnv: 0,
FarmwareInstallation: 0,
FbosConfig: 0,
FirmwareConfig: 0,
WebAppConfig: 0,
// Group 1
Peripheral: 1,
Point: 1,
SensorReading: 1,
Sensor: 1,
Tool: 1,
// Group 2
Sequence: 2,
// Group 3
PinBinding: 3,
Regimen: 3,
// Group 4
FarmEvent: 4,
DiagnosticDump: 4,
Image: 4,
Log: 4,
PlantTemplate: 4,
SavedGarden: 4,
User: 4,
WebcamFeed: 4,
Crop: 4,
};
export function buildResourceIndex(resources: TaggedResource[] = FAKE_RESOURCES,
state = emptyState()) {
return _(resources)
const sortedResources = resources
.sort((l, r) => c3(GROUPS[l.kind], GROUPS[r.kind]));
type K = keyof typeof GROUPS;
return _(sortedResources)
.groupBy(KIND)
.toPairs()
.sort((l, r) => c3(GROUPS[l[0] as K || 4], GROUPS[r[0] as K || 4]))
.map((x: [TaggedResource["kind"], TaggedResource[]]) => x)
.map(y => resourceReady(y[0], y[1]))
.map((y) => resourceReady(y[0], y[1]))
.reduce(resourceReducer, state);
}

View File

@ -20,7 +20,7 @@ describe("<AddFarmEvent />", () => {
function fakeProps(): AddEditFarmEventProps {
const sequence = fakeSequence();
sequence.body.id = 1;
const farmEvent = fakeFarmEvent("Sequence", 1);
const farmEvent = fakeFarmEvent("Sequence", sequence.body.id);
farmEvent.uuid = "FarmEvent";
return {
deviceTimezone: "",

View File

@ -25,12 +25,12 @@ describe("<EditFarmEvent />", () => {
dispatch: jest.fn(),
regimensById: {},
sequencesById: { "1": sequence },
farmEventsById: { "1": fakeFarmEvent("Sequence", 1) },
farmEventsById: { "1": fakeFarmEvent("Sequence", sequence.body.id) },
executableOptions: [],
repeatOptions: [],
handleTime: jest.fn(),
farmEvents: [],
getFarmEvent: () => fakeFarmEvent("Sequence", 1),
getFarmEvent: () => fakeFarmEvent("Sequence", sequence.body.id || 0),
findExecutable: () => sequence,
timeOffset: 0,
autoSyncEnabled: false,

View File

@ -24,12 +24,12 @@ describe("mapStateToProps()", () => {
time_offset: 28800000
}];
const sequenceFarmEvent = fakeFarmEvent("Sequence", 1);
const sequenceFarmEvent = fakeFarmEvent("Sequence", sequence.body.id);
sequenceFarmEvent.body.id = 1;
sequenceFarmEvent.body.start_time = "2222-02-22T02:00:00.000Z";
sequenceFarmEvent.body.end_time = "2222-02-22T02:03:00.000Z";
const regimenFarmEvent = fakeFarmEvent("Regimen", 1);
const regimenFarmEvent = fakeFarmEvent("Regimen", sequence.body.id);
regimenFarmEvent.body.id = 2;
regimenFarmEvent.body.start_time = "2222-02-23T02:00:00.000Z";
regimenFarmEvent.body.end_time = "2222-02-23T02:03:00.000Z";
@ -106,7 +106,7 @@ describe("mapResourcesToCalendar(): sequence farm events", () => {
sequence.body.id = 1;
sequence.body.body = [{ kind: "take_photo", args: {} }];
const sequenceFarmEvent = fakeFarmEvent("Sequence", 1);
const sequenceFarmEvent = fakeFarmEvent("Sequence", sequence.body.id);
sequenceFarmEvent.body.id = 1;
sequenceFarmEvent.body.start_time = props.start_time;
sequenceFarmEvent.body.end_time = props.end_time;
@ -187,7 +187,7 @@ describe("mapResourcesToCalendar(): regimen farm events", () => {
time_offset: 288660000
}];
const regimenFarmEvent = fakeFarmEvent("Regimen", 1);
const regimenFarmEvent = fakeFarmEvent("Regimen", sequence.body.id);
regimenFarmEvent.body.id = 2;
regimenFarmEvent.body.start_time = "2017-12-20T01:02:00.000Z";
regimenFarmEvent.body.end_time = "2017-12-20T01:05:00.000Z";

View File

@ -22,7 +22,6 @@ import { selectAllFarmEvents, findByKindAndId, selectAllLogs } from "./selectors
import { ExecutableType } from "farmbot/dist/resources/api_resources";
import { betterCompact } from "../util";
// NONSENSE CAUSED BY CIRCULAR DEPS (TODO: FIX LATER RC) =======================
export function findByUuid(index: ResourceIndex, uuid: string): TaggedResource {
const x = index.references[uuid];
if (x && isTaggedResource(x)) {
@ -31,7 +30,6 @@ export function findByUuid(index: ResourceIndex, uuid: string): TaggedResource {
throw new Error("BAD UUID- CANT FIND RESOURCE: " + uuid);
}
}
// END NONSENSE ================================================================
type IndexDirection = "up" | "down";
type IndexerCallback = (self: TaggedResource, index: ResourceIndex) => void;
@ -112,13 +110,6 @@ const reindexAllSequences = (i: ResourceIndex) => {
})).map(mapper);
};
// const SEQUENCE_STUFF: Indexer = {
// up(_, _i) { },
// down(_, i) {
// reindexAllSequences(i);
// },
// };
export function reindexAllFarmEventUsage(i: ResourceIndex) {
i.inUse["Regimen.FarmEvent"] = {};
i.inUse["Sequence.FarmEvent"] = {};
@ -126,6 +117,7 @@ export function reindexAllFarmEventUsage(i: ResourceIndex) {
"Regimen": i.inUse["Regimen.FarmEvent"],
"Sequence": i.inUse["Sequence.FarmEvent"],
};
// Which FarmEvents use which resource?
selectAllFarmEvents(i)
.map(fe => ({
@ -146,7 +138,6 @@ export const INDEXERS: Indexer[] = [
ALL,
BY_KIND,
BY_KIND_AND_ID,
// SEQUENCE_STUFF
];
type IndexerHook = Partial<Record<TaggedResource["kind"], Reindexer>>;
@ -205,9 +196,7 @@ const BEFORE_HOOKS: IndexerHook = {
};
const AFTER_HOOKS: IndexerHook = {
Regimen(_index, _strategy) {
// reindexAllFarmEventUsage()
},
FarmEvent: reindexAllFarmEventUsage,
Sequence: reindexAllSequences,
};

View File

@ -1,7 +1,7 @@
enum Comparison { LOW = -1, EQ = 0, HIGH = 1 }
/** Emulates the "spaceship operator" seen in languages like Ruby, PHP, etc..
* -1 => "too low", 0 => "identical", 1 => "too high" */
const threeWayComparison = (l: number, r: number): Comparison => {
export const threeWayComparison = (l: number, r: number): Comparison => {
if (l == r) {
return Comparison.EQ;
}