fix: empty operator with association field (#4189)

* test: empty operator with association field

* fix: test

* fix: test
This commit is contained in:
ChengLei Shao 2024-04-27 14:40:05 +08:00 committed by GitHub
parent def7530d14
commit 69baf99b66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 79 additions and 7 deletions

View File

@ -7,7 +7,7 @@ describe('empty operator', () => {
let db: Database; let db: Database;
let User: Collection; let User: Collection;
let Profile: Collection;
afterEach(async () => { afterEach(async () => {
await db.close(); await db.close();
}); });
@ -16,7 +16,23 @@ describe('empty operator', () => {
db = mockDatabase({}); db = mockDatabase({});
User = db.collection({ User = db.collection({
name: 'users', name: 'users',
fields: [{ type: 'string', name: 'name' }], fields: [
{ type: 'string', name: 'name' },
{
type: 'hasOne',
name: 'profile',
},
],
});
Profile = db.collection({
name: 'profiles',
fields: [
{
type: 'string',
name: 'address',
},
],
}); });
await db.sync({ await db.sync({
@ -74,4 +90,56 @@ describe('empty operator', () => {
expect(result.length).toEqual(2); expect(result.length).toEqual(2);
}); });
test('string not empty', async () => {
const u1 = await User.repository.create({
values: {
name: 'u1',
},
});
const u2 = await User.repository.create({
values: {
name: '',
},
});
const result = await User.repository.find({
filter: {
'name.$notEmpty': true,
},
});
expect(result.length).toEqual(1);
expect(result[0].get('id')).toEqual(u1.get('id'));
});
test('string not empty with association', async () => {
const u1 = await User.repository.create({
values: {
name: 'u1',
profile: {
address: 'a1',
},
},
});
const u2 = await User.repository.create({
values: {
name: 'u2',
profile: {
address: '',
},
},
});
const result = await User.repository.find({
filter: {
'profile.address.$notEmpty': true,
},
});
expect(result.length).toEqual(1);
expect(result[0].get('id')).toEqual(u1.get('id'));
});
}); });

View File

@ -18,11 +18,15 @@ const findFilterFieldType = (ctx) => {
const associationPath = path; const associationPath = path;
for (const association of associationPath) { for (const association of associationPath) {
if (lodash.isNumber(parseInt(association)) || association.startsWith('$')) { if (lodash.isFinite(parseInt(association)) || association.startsWith('$')) {
continue; continue;
} }
model = model.associations[association].target; const modelAssociation = model.associations[association];
if (!modelAssociation) {
break;
}
model = modelAssociation.target;
} }
const collection = db.modelCollection.get(model); const collection = db.modelCollection.get(model);

View File

@ -21,8 +21,8 @@ describe('view collection', () => {
db = app.db; db = app.db;
collectionRepository = app.db.getCollection('collections').repository; collectionRepository = app.db.getRepository('collections');
fieldsRepository = app.db.getCollection('fields').repository; fieldsRepository = app.db.getRepository('fields');
agent = app.agent(); agent = app.agent();
testViewName = `view_${uid(6)}`; testViewName = `view_${uid(6)}`;
@ -283,7 +283,7 @@ SELECT * FROM numbers;
}); });
it('should list collections fields with source interface', async () => { it('should list collections fields with source interface', async () => {
await app.db.getCollection('collections').repository.create({ await app.db.getRepository('collections').create({
values: { values: {
name: 'users', name: 'users',
fields: [ fields: [