Skip to content

Commit

Permalink
fix serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
gskorokhod committed Aug 4, 2024
1 parent 5f2a4bd commit 268eb5b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 25 deletions.
95 changes: 90 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,92 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect } from "vitest";
import type { Shift, Location, Person, Task, Skill } from "$lib/types";
import * as devalue from "devalue";
import {
generateLocation,
generatePerson,
generateShift,
generateSkill,
generateTask
} from "$lib/testing.ts";

describe('sum test', () => {
it('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
describe("Serialization", () => {
it("should serialize a location", () => {
const location: Location = generateLocation();

const serialized = devalue.stringify(location);
console.log(serialized);

expect(serialized).toBeDefined();

const deserialized = devalue.parse(serialized);
console.log(deserialized);

expect(deserialized).toBeDefined();
expect(deserialized).toBeTypeOf("object");
expect(deserialized).toEqual(location);
});

it("should serialize a person", () => {
const person: Person = generatePerson();

const serialized = devalue.stringify(person);
console.log(serialized);

expect(serialized).toBeDefined();

const deserialized = devalue.parse(serialized);
console.log(deserialized);

expect(deserialized).toBeDefined();
expect(deserialized).toBeTypeOf("object");
expect(deserialized).toEqual(person);
});

it("should serialize a task", () => {
const task: Task = generateTask();

const serialized = devalue.stringify(task);
console.log(serialized);

expect(serialized).toBeDefined();

const deserialized = devalue.parse(serialized);
console.log(deserialized);

expect(deserialized).toBeDefined();
expect(deserialized).toBeTypeOf("object");
expect(deserialized).toEqual(task);
});

it("should serialize a shift", () => {
const shift: Shift = generateShift();

const serialized = devalue.stringify(shift);
console.log(serialized);

expect(serialized).toBeDefined();

const deserialized = devalue.parse(serialized);
console.log(deserialized);

expect(deserialized).toBeDefined();
expect(deserialized).toBeTypeOf("object");
expect(deserialized).toEqual(shift);
});

it("should serialize a skill", () => {
const skill: Skill = generateSkill();

const serialized = devalue.stringify(skill);
console.log(serialized);

expect(serialized).toBeDefined();

const deserialized = devalue.parse(serialized);
console.log(deserialized);

expect(deserialized).toBeDefined();
expect(deserialized).toBeTypeOf("object");
expect(deserialized).toEqual(skill);
});
});
4 changes: 2 additions & 2 deletions src/lib/components/elements/shift/shift-card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Task from "$lib/components/elements/task/task-card.svelte";
import { ClockIcon, MapPinIcon } from "lucide-svelte";
import { capitalize } from "$lib/utils.ts";
import { fmtZonedDateTime } from "$lib/utils.js";
import { fmtDateTime } from "$lib/utils.js";
let shift: Shift;
Expand All @@ -21,7 +21,7 @@
<Card.Content class="flex flex-col items-start justify-start gap-3">
<div class="flex flex-row items-center justify-start gap-2 w-full">
<ClockIcon />
{fmtZonedDateTime(shift.start_date_time)} - {fmtZonedDateTime(shift.end_date_time)}
{fmtDateTime(shift.start_date_time)} - {fmtDateTime(shift.end_date_time)}
</div>
<div class="flex flex-row items-center justify-start gap-2 w-full">
<MapPinIcon />
Expand Down
26 changes: 16 additions & 10 deletions src/lib/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ export function generateLocations(n: number): Location[] {
return Array.from({ length: n }, generateLocation);
}

export function generateShift(): Shift | undefined {
const loc = sampleLocation();
if (loc === undefined) return undefined;
export function generateShift(): Shift {
let loc = sampleLocation();
if (loc === undefined) loc = generateLocation();

return createShift({
name: faker.lorem.words(),
description: faker.lorem.sentence(),
start_date_time: fromDate(faker.date.recent(), "UTC"),
end_date_time: fromDate(faker.date.soon(), "UTC"),
start_date_time: faker.date.recent(),
end_date_time: faker.date.soon(),
location: loc,
tasks: sampleTasks(faker.number.int({ min: 1, max: 3 }))
});
Expand Down Expand Up @@ -214,7 +214,9 @@ export function generateConstraintForLocation(loc: Location): Constraint {
}
];

return sampleOne(CONSTRAINT_GENERATORS)();
const gen = sampleOne(CONSTRAINT_GENERATORS);
if (gen === undefined) throw new Error("No constraint generator found");
else return gen();
}

export function generateConstraintForTask(task: Task): Constraint {
Expand Down Expand Up @@ -242,7 +244,9 @@ export function generateConstraintForPerson(person: Person): Constraint {
}
];

return sampleOne(CONSTRAINT_GENERATORS)();
const gen = sampleOne(CONSTRAINT_GENERATORS);
if (gen === undefined) throw new Error("No constraint generator found");
else return gen();
}

export function generateConstraintForRandomLocation(): Constraint | undefined {
Expand Down Expand Up @@ -270,7 +274,9 @@ export function generateConstraints(n: number): Constraint[] {
generateConstraintForRandomPerson
];

return Array.from({ length: n }, () => sampleOne(GENERATORS)()).filter(
(c) => c !== undefined
) as Constraint[];
return Array.from({ length: n }, () => {
const gen = sampleOne(GENERATORS);
if (gen === undefined) throw new Error("No constraint generator found");
return gen();
}).filter((c) => c !== undefined) as Constraint[];
}
5 changes: 2 additions & 3 deletions src/lib/types/shift.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ZonedDateTime } from "@internationalized/date";
import type { Task } from "$lib/types/task.ts";
import type { Person } from "$lib/types/person.ts";
import type { Location } from "$lib/types/location.ts";
Expand All @@ -7,8 +6,8 @@ import { v4 as uuidv4 } from "uuid";
export interface ShiftProps {
name: string;
description: string;
start_date_time: ZonedDateTime;
end_date_time: ZonedDateTime;
start_date_time: Date;
end_date_time: Date;
location: Location;
tasks: Task[];
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export function getInitials(name: string): string {
.join("");
}

export function fmtZonedDateTime(dt: ZonedDateTime): string {
export function fmtDateTime(dt: ZonedDateTime | Date): string {
if (dt instanceof Date) return dt.toLocaleString([], { hour: "2-digit", minute: "2-digit" });
return dt.toDate().toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
}
5 changes: 2 additions & 3 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,17 @@
fields: ["name", "description", "location", "tasks", "people", "start_time", "end_time"],
storeFields: ["name"]
});
let all_shifts: Shift[];
let search: string = "";
$: search_ids = miniSearch.search(search, {
prefix: true,
fuzzy: 0.2
}).map((res) => res.id);
$: filtered_shifts = search === "" ? all_shifts : all_shifts.filter((shift) => search_ids.includes(shift.uuid));
$: filtered_shifts = search === "" ? $shifts : $shifts.filter((shift) => search_ids.includes(shift.uuid));
shifts.subscribe(value => {
miniSearch.removeAll();
miniSearch.addAllAsync(value);
all_shifts = value;
});
</script>

Expand Down
2 changes: 1 addition & 1 deletion src/routes/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
locations.set(generateLocations(10));
employees.set(generatePeople(15));
tasks.set(generateTasks(5));
shifts.set(generateShifts(5));
constraints.set(generateConstraints(10));
shifts.set(generateShifts(5));
}}>
Generate dummy data
</Button>
Expand Down

0 comments on commit 268eb5b

Please sign in to comment.