Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colodenn committed Dec 29, 2024
1 parent 0e007bd commit acc3b84
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
3 changes: 0 additions & 3 deletions src/utils/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ export const getModels = async (
const results = transaction.formatResults<Model>(rawResults, false);
const models = 'records' in results[0] ? results[0].records : [];

console.log(rawResults);
console.log(models.map((model) => model.triggers));

return models.map((model) => ({
...model,
fields: model.fields?.filter((field) => !IGNORED_FIELDS.includes(field.slug)),
Expand Down
68 changes: 31 additions & 37 deletions tests/utils/apply.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('apply', () => {
expect(models[0].triggers).toBeDefined();
});

test.only('update model triggers', async () => {
test('update model triggers', async () => {
const definedModels: Array<Model> = [TestE, TestC];
const existingModels: Array<Model> = [TestD];

Expand All @@ -217,92 +217,87 @@ describe('apply', () => {
await protocol.convertToQueryObjects();

const statements = protocol.getSQLStatements(models);
console.log(statements);
await db.query(statements);

const newModels = await getModels(db);

expect(newModels[1]?.triggers?.[0]?.action).toBe('DELETE');

Check failure on line 224 in tests/utils/apply.test.ts

View workflow job for this annotation

GitHub Actions / validate / Testing

error: expect(received).toBe(expected)

Expected: "DELETE" Received: undefined at /home/runner/work/cli/cli/tests/utils/apply.test.ts:224:49
});

test('complex model transformation with indexes and triggers', async () => {
const definedModels: Array<Model> = [TestE, TestB];
const existingModels: Array<Model> = [TestD, TestA];

const modelDiff = await diffModels(definedModels, existingModels);
const db = await queryEphemeralDatabase(existingModels);
const models = await getModels(db);

const modelDiff = await diffModels(definedModels, models);

const protocol = new Protocol(modelDiff);
await protocol.convertToQueryObjects();

const statements = protocol.getSQLStatements(existingModels);
const db = await queryEphemeralDatabase(existingModels);
const statements = protocol.getSQLStatements(models);
await db.query(statements);

const models = await getModels(db);
expect(models).toHaveLength(2);
const newModels = await getModels(db);
expect(newModels).toHaveLength(2);
});

test('rename model with existing relationships', async () => {
const definedModels: Array<Model> = [AccountNew, Profile];
const existingModels: Array<Model> = [Account, Profile];

const modelDiff = await diffModels(definedModels, existingModels, true);
const db = await queryEphemeralDatabase(existingModels);
const models = await getModels(db);

const modelDiff = await diffModels(definedModels, models, true);

const protocol = new Protocol(modelDiff);
await protocol.convertToQueryObjects();

const statements = protocol.getSQLStatements(existingModels);
const db = await queryEphemeralDatabase(existingModels);
const statements = protocol.getSQLStatements(models);
await db.query(statements);

const models = await getModels(db);
expect(models.find((m) => m.slug === 'account_new')).toBeDefined();
const newModels = await getModels(db);
expect(newModels.find((m) => m.slug === 'account_new')).toBeDefined();
});

test('drop multiple models with dependencies', async () => {
const definedModels: Array<Model> = [];
const existingModels: Array<Model> = [Account, Profile, TestA];

const modelDiff = await diffModels(definedModels, existingModels);
const db = await queryEphemeralDatabase(existingModels);
const models = await getModels(db);

const modelDiff = await diffModels(definedModels, models);

const protocol = new Protocol(modelDiff);
await protocol.convertToQueryObjects();

const statements = protocol.getSQLStatements(existingModels);
const db = await queryEphemeralDatabase(existingModels);
const statements = protocol.getSQLStatements(models);
await db.query(statements);

const models = await getModels(db);
expect(models).toHaveLength(0);
const newModels = await getModels(db);
expect(newModels).toHaveLength(0);
});

test('complex model update with multiple changes', async () => {
const definedModels: Array<Model> = [TestE, TestB, Account];
const existingModels: Array<Model> = [TestD, TestA, AccountNew];

const modelDiff = await diffModels(definedModels, existingModels, true);
const protocol = new Protocol(modelDiff);
await protocol.convertToQueryObjects();

const statements = protocol.getSQLStatements(existingModels);
const db = await queryEphemeralDatabase(existingModels);
await db.query(statements);

const models = await getModels(db);
expect(models).toHaveLength(3);
});

test('update model with complex index changes', async () => {
const definedModels: Array<Model> = [TestB, TestF];
const existingModels: Array<Model> = [TestA];
const modelDiff = await diffModels(definedModels, models, true);

const modelDiff = await diffModels(definedModels, existingModels);
const protocol = new Protocol(modelDiff);
await protocol.convertToQueryObjects();

const statements = protocol.getSQLStatements(existingModels);
const db = await queryEphemeralDatabase(existingModels);
const statements = protocol.getSQLStatements(models);
await db.query(statements);

const models = await getModels(db);
expect(models.length).toBeGreaterThan(0);
const newModels = await getModels(db);
expect(newModels).toHaveLength(3);
});

test('create and update models with mixed operations', async () => {
Expand All @@ -313,8 +308,7 @@ describe('apply', () => {
const models = await getModels(db);

const modelDiff = await diffModels(definedModels, models);
console.log(JSON.stringify(models, null, 2));
console.log(modelDiff);

const protocol = new Protocol(modelDiff);
await protocol.convertToQueryObjects();

Expand Down
2 changes: 1 addition & 1 deletion tests/utils/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('queries', () => {
},
];
const customQueries: Array<string> = ['get.model("user")'];
const result = createTempModelQuery('user', fields, [], customQueries);
const result = createTempModelQuery('user', fields, [], [], customQueries);
expect(result).toEqual([
"create.model({slug:'RONIN_TEMP_user',fields:[{slug:'username', type:'string', name:'Username', unique:true, required:true}]})",
'add.RONIN_TEMP_user.to(() => get.user())',
Expand Down

0 comments on commit acc3b84

Please sign in to comment.