Skip to content

Commit

Permalink
fix merge issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 committed Mar 13, 2024
1 parent 8b0a5fc commit 1af9beb
Showing 1 changed file with 82 additions and 66 deletions.
148 changes: 82 additions & 66 deletions packages/schema/tests/schema/validation/datamodel-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ describe('Data Model Validation Tests', () => {

it('duplicated fields', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
id String @id
x Int
x String
}
`)
`);

expect(result).toMatchObject(errorLike('Duplicated declaration name "x"'));
});
Expand Down Expand Up @@ -128,14 +128,14 @@ describe('Data Model Validation Tests', () => {

it('should error when there are no unique fields', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int
@@allow('all', x > 0)
}
`)
`);
expect(result).toMatchObject(errorLike(err));
})
});

it('should should use @unique when there is no @id', async () => {
const result = await safelyLoadModel(`
Expand All @@ -147,24 +147,24 @@ describe('Data Model Validation Tests', () => {
}
`);
expect(result).toMatchObject({ status: 'fulfilled' });
})
});

// @@unique used as id
it('should suceed when @@unique used as id', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int
@@unique([x])
@@allow('all', x > 0)
}
`);
expect(result).toMatchObject({ status: 'fulfilled' });
})
});

it('should succeed when @id is an enum type', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
enum E {
A
B
Expand All @@ -174,11 +174,11 @@ describe('Data Model Validation Tests', () => {
}
`);
expect(result).toMatchObject({ status: 'fulfilled' });
})
});

it('should succeed when @@id is an enum type', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
enum E {
A
B
Expand All @@ -190,143 +190,145 @@ describe('Data Model Validation Tests', () => {
}
`);
expect(result).toMatchObject({ status: 'fulfilled' });
})
});

it('should error when there are no id fields, even when denying access', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int
@@deny('all', x <= 0)
}
`)
`);

expect(result).toMatchObject(errorLike(err));
})
});

it('should error when there are not id fields, without access restrictions', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int @gt(0)
}
`)
`);

expect(result).toMatchObject(errorLike(err));
})
});

it('should error when there is more than one field marked as @id', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int @id
y Int @id
}
`)
expect(result).toMatchObject(errorLike(`Model can include at most one field with @id attribute`))
})
`);
expect(result).toMatchObject(errorLike(`Model can include at most one field with @id attribute`));
});

it('should error when both @id and @@id are used', async () => {
it('should error when both @id and @@id are used', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int @id
y Int
@@id([x, y])
}
`)
expect(result).toMatchObject(errorLike(`Model cannot have both field-level @id and model-level @@id attributes`))
})
`);
expect(result).toMatchObject(
errorLike(`Model cannot have both field-level @id and model-level @@id attributes`)
);
});

it('should error when @id used on optional field', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int? @id
}
`)
expect(result).toMatchObject(errorLike(`Field with @id attribute must not be optional`))
})
`);
expect(result).toMatchObject(errorLike(`Field with @id attribute must not be optional`));
});

it('should error when @@id used on optional field', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int?
@@id([x])
}
`)
expect(result).toMatchObject(errorLike(`Field with @id attribute must not be optional`))
})
`);
expect(result).toMatchObject(errorLike(`Field with @id attribute must not be optional`));
});

it('should error when @id used on list field', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int[] @id
}
`)
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`))
})
`);
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`));
});

it('should error when @@id used on list field', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Int[]
@@id([x])
}
`)
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`))
})
`);
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`));
});

it('should error when @id used on a Json field', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Json @id
}
`)
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`))
})
`);
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`));
});

it('should error when @@id used on a Json field', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model M {
x Json
@@id([x])
}
`)
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`))
})
`);
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`));
});

it('should error when @id used on a reference field', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model Id {
id String @id
}
model M {
myId Id @id
}
`)
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`))
})
`);
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`));
});

it('should error when @@id used on a reference field', async () => {
const result = await safelyLoadModel(`
${ prelude }
${prelude}
model Id {
id String @id
}
model M {
myId Id
@@id([myId])
}
`)
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`))
})
`);
expect(result).toMatchObject(errorLike(`Field with @id attribute must be of scalar or enum type`));
});
});

it('relation', async () => {
Expand Down Expand Up @@ -390,7 +392,9 @@ describe('Data Model Validation Tests', () => {
id String @id
}
`)
).toMatchObject(errorLike(`The relation field "b" on model "A" is missing an opposite relation field on model "B"`));
).toMatchObject(
errorLike(`The relation field "b" on model "A" is missing an opposite relation field on model "B"`)
);

// one-to-one ambiguous
expect(
Expand Down Expand Up @@ -492,7 +496,11 @@ describe('Data Model Validation Tests', () => {
aId String
}
`)
).toMatchObject(errorLike(`Field "aId" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute`));
).toMatchObject(
errorLike(
`Field "aId" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute`
)
);

// missing @relation
expect(
Expand All @@ -508,7 +516,11 @@ describe('Data Model Validation Tests', () => {
a A
}
`)
).toMatchObject(errorLike(`Field for one side of relation must carry @relation attribute with both "fields" and "references" fields`));
).toMatchObject(
errorLike(
`Field for one side of relation must carry @relation attribute with both "fields" and "references" fields`
)
);

// wrong relation owner field type
expect(
Expand Down Expand Up @@ -672,7 +684,9 @@ describe('Data Model Validation Tests', () => {
}
`);

expect(errors).toMatchObject(errorLike(`Model A cannot be extended because it's not abstract`));
expect(errors).toMatchObject(
errorLike(`Model A cannot be extended because it's neither abstract nor marked as "@@delegate"`)
);

// relation incomplete from multiple level inheritance
expect(
Expand All @@ -696,6 +710,8 @@ describe('Data Model Validation Tests', () => {
a String
}
`)
).toMatchObject(errorLike(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`));
).toMatchObject(
errorLike(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`)
);
});
});

0 comments on commit 1af9beb

Please sign in to comment.