Skip to content

Commit

Permalink
fix: Combine scalar filters on nullable list
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed May 13, 2021
1 parent ce0b82a commit 8f306e8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 60 deletions.
13 changes: 4 additions & 9 deletions src/handlers/combine-scalar-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,10 @@ function isScalarFilter(inputType: InputType) {
return false;
}
let result = false;
// Check `not` field
// OPTIMIZE: Field not usually in the end of fields array
const notField = inputType.fields.find(f => f.name === 'not');
if (notField) {
result = notField.inputTypes.every(x => {
return (
String(x.type).includes(inputType.name) ||
['enumTypes', 'scalar'].includes(x.location)
);
const equals = inputType.fields.find(f => f.name === 'equals');
if (equals) {
result = equals.inputTypes.every(x => {
return ['enumTypes', 'scalar'].includes(x.location);
});
}
return result;
Expand Down
90 changes: 40 additions & 50 deletions src/test/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1113,117 +1113,107 @@ describe('combine scalar filters', () => {

describe('user where input', () => {
before(() => {
sourceFile = project.getSourceFile(s =>
s.getFilePath().endsWith('user-where.input.ts'),
)!;
setSourceFile('user-where.input.ts');
});

it('count', () => {
expect(getPropertyStructure(sourceFile, 'count')?.type).toBe('IntFilter');
expect(p('count')?.type).toBe('IntFilter');
});

it('bio', () => {
expect(getPropertyStructure(sourceFile, 'bio')?.type).toBe('StringFilter');
expect(p('bio')?.type).toBe('StringFilter');
});

it('money', () => {
expect(getPropertyStructure(sourceFile, 'money')?.type).toBe(
'DecimalFilter',
);
expect(p('money')?.type).toBe('DecimalFilter');
});

it('rating', () => {
expect(getPropertyStructure(sourceFile, 'rating')?.type).toBe(
'FloatFilter',
);
expect(p('rating')?.type).toBe('FloatFilter');
});

it('born', () => {
expect(getPropertyStructure(sourceFile, 'born')?.type).toBe(
'DateTimeFilter',
);
expect(p('born')?.type).toBe('DateTimeFilter');
});

it('humanoid', () => {
expect(getPropertyStructure(sourceFile, 'humanoid')?.type).toBe(
'BoolFilter',
);
expect(p('humanoid')?.type).toBe('BoolFilter');
});

it('role', () => {
expect(getPropertyStructure(sourceFile, 'role')?.type).toBe(
'EnumRoleFilter',
);
expect(p('role')?.type).toBe('EnumRoleFilter');
});

// it('^', () => console.log(sourceFile.getText()));
});

describe('user scalar where with aggregates', () => {
before(() => {
sourceFile = project.getSourceFile(s =>
s.getFilePath().endsWith('user-scalar-where-with-aggregates.input.ts'),
)!;
setSourceFile('user-scalar-where-with-aggregates.input.ts');
});

// it('^', () => console.log(sourceFile.getText()));

it('id', () => {
expect(getPropertyStructure(sourceFile, 'id')?.type).toBe(
'StringWithAggregatesFilter',
);
expect(p('id')?.type).toBe('StringWithAggregatesFilter');
});

it('bio', () => {
expect(getPropertyStructure(sourceFile, 'bio')?.type).toBe(
'StringWithAggregatesFilter',
);
expect(p('bio')?.type).toBe('StringWithAggregatesFilter');
});

it('count', () => {
expect(getPropertyStructure(sourceFile, 'count')?.type).toBe(
'IntWithAggregatesFilter',
);
expect(p('count')?.type).toBe('IntWithAggregatesFilter');
});

it('rating', () => {
expect(getPropertyStructure(sourceFile, 'rating')?.type).toBe(
'FloatWithAggregatesFilter',
);
expect(p('rating')?.type).toBe('FloatWithAggregatesFilter');
});

it('born', () => {
expect(getPropertyStructure(sourceFile, 'born')?.type).toBe(
'DateTimeWithAggregatesFilter',
);
expect(p('born')?.type).toBe('DateTimeWithAggregatesFilter');
});

it('humanoid', () => {
expect(getPropertyStructure(sourceFile, 'humanoid')?.type).toBe(
'BoolWithAggregatesFilter',
);
expect(p('humanoid')?.type).toBe('BoolWithAggregatesFilter');
});

it('money', () => {
expect(getPropertyStructure(sourceFile, 'money')?.type).toBe(
'DecimalWithAggregatesFilter',
);
expect(p('money')?.type).toBe('DecimalWithAggregatesFilter');
});

it('data', () => {
expect(getPropertyStructure(sourceFile, 'data')?.type).toBe(
'JsonWithAggregatesFilter',
);
expect(p('data')?.type).toBe('JsonWithAggregatesFilter');
});

it('role', () => {
expect(getPropertyStructure(sourceFile, 'role')?.type).toBe(
'EnumRoleWithAggregatesFilter',
);
expect(p('role')?.type).toBe('EnumRoleWithAggregatesFilter');
});
});
});

describe('combine scalar filters on array', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
schema: `
model User {
id String @id
str String[]
int Int?
}
`,
options: [
`outputFilePattern = "{name}.{type}.ts"`,
`combineScalarFilters = true`,
],
}));
});

it('smoke', () => {
'no errors';
});
});

describe('hide field', () => {
describe('scalar field', () => {
before(async () => {
Expand Down
18 changes: 17 additions & 1 deletion src/test/test-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,23 @@ export async function testGenerate(args: {
const filePath = sourceFile.getFilePath();
const text = sourceFile.getText();
if (!text) {
throw `Project should not contain empty files: ${filePath}`;
let message = `Project should not contain empty files: ${filePath}`;
const fileLower = sourceFile
.getBaseNameWithoutExtension()
.replace(/-/g, '')
.split('.')[0];
const sources = sourceFiles.filter(s =>
s
.getClass(() => true)
?.getProperties()
.find(p =>
String(p.getStructure().type).toLowerCase().includes(fileLower),
),
);
if (sources.length > 0) {
message += `, reference: ${sources.map(s => s.getBaseName())}`;
}
throw message;
}
const imports = sourceFile
.getImportDeclarations()
Expand Down

0 comments on commit 8f306e8

Please sign in to comment.