Skip to content

Commit 8323514

Browse files
committed
fix conflicts not being handled
1 parent a8539a1 commit 8323514

File tree

5 files changed

+56
-27
lines changed

5 files changed

+56
-27
lines changed

components/SeedProvider.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useEffect, useState } from "react";
1111
import { useAsyncStorage } from "@react-native-async-storage/async-storage";
1212
import { ActivityIndicator } from "react-native";
1313
import { getRandomQuestion } from "../services/questions";
14-
import { useToastController } from "@tamagui/toast";
14+
import { Toast, useToastController } from "@tamagui/toast";
1515

1616
const seedNecessarySets = async () => {
1717
try {
@@ -31,6 +31,7 @@ const seedNecessarySets = async () => {
3131
(set) => !availableQuestionSetIds.has(set.id)
3232
)
3333
)
34+
.onConflictDoNothing()
3435
.execute();
3536

3637
const allValues = seedVals.questions.filter(
@@ -42,6 +43,7 @@ const seedNecessarySets = async () => {
4243
await db
4344
.insert(questions)
4445
.values(allValues.slice(i * 1000, (i + 1) * 1000) as unknown as any)
46+
.onConflictDoNothing()
4547
.execute();
4648
}
4749
} catch (e) {
@@ -58,9 +60,7 @@ export const resetAndReseed = async () => {
5860

5961
// try to restore answers
6062
await db.insert(answers).values(answerObjects).execute();
61-
} catch (e) {
62-
console.error(e);
63-
}
63+
} catch (e) {}
6464
};
6565

6666
export const checkShouldSeed = async () => {

components/StyledText.tsx

-5
This file was deleted.

components/__tests__/SeedProvider-test.js

+52-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ describe("SeedProvider", () => {
2929
migrate();
3030
});
3131

32+
beforeEach(async () => {
33+
jest.clearAllMocks();
34+
await reset();
35+
});
36+
3237
it("should render children after seed", async () => {
3338
render(
3439
<SeedProvider>
@@ -61,6 +66,53 @@ describe("SeedProvider", () => {
6166
});
6267
});
6368

69+
it("should succeed even if duplicates exist", async () => {
70+
await db.insert(questionSets).values({ id: 1, name: "First" }).execute();
71+
await db
72+
.insert(questionSets)
73+
.values({ id: 2, name: "Set Without Questions" })
74+
.execute();
75+
await db
76+
.insert(questions)
77+
.values({
78+
question: "What is the capital of France?",
79+
answers: ["Paris", "London", "Berlin", "Madrid"],
80+
correctAnswer: 0,
81+
level: "easy",
82+
category: "geography",
83+
questionSet: 1,
84+
})
85+
.execute();
86+
await db
87+
.insert(questions)
88+
.values({
89+
question: "What is the capital of France?",
90+
answers: ["Paris", "London", "Berlin", "Madrid"],
91+
correctAnswer: 0,
92+
level: "easy",
93+
category: "geography",
94+
questionSet: 1,
95+
})
96+
.execute();
97+
98+
render(<SeedProvider />);
99+
100+
await act(async () => {
101+
await jest.runAllTimers();
102+
});
103+
104+
await waitFor(async () => {
105+
expect(
106+
(
107+
await db
108+
.select({ num: count() })
109+
.from(questions)
110+
.where(eq(questions.questionSet, 2))
111+
)[0].num
112+
).toBeGreaterThan(0);
113+
});
114+
});
115+
64116
it("should seed missing question set", async () => {
65117
await db
66118
.insert(questionSets)
@@ -99,11 +151,6 @@ describe("SeedProvider", () => {
99151
).toBeGreaterThan(1);
100152
});
101153
});
102-
103-
beforeEach(async () => {
104-
jest.clearAllMocks();
105-
await reset();
106-
});
107154
});
108155

109156
describe("seed.json", () => {

components/__tests__/StyledText-test.js

-10
This file was deleted.

components/__tests__/__snapshots__/SeedProvider-test.js.snap

-3
This file was deleted.

0 commit comments

Comments
 (0)