Skip to content

Commit

Permalink
test: add unit tests for relationships module in database package (#407)
Browse files Browse the repository at this point in the history
* test: add unit tests for relationships module in database package

* fix: update reaction types to new enum value in test-app

* refactor: alter control flow in relationship #get() to improve coverage

* refactor: more control flow changes

* fix: typo in relationship.test.js
  • Loading branch information
zacharygolba authored Sep 21, 2016
1 parent ade16b5 commit 233d4a7
Show file tree
Hide file tree
Showing 17 changed files with 515 additions and 127 deletions.
63 changes: 31 additions & 32 deletions src/packages/database/relationship/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,42 @@ export async function get(
key: string
): Promise<Array<Model> | ?Model> {
const opts = owner.constructor.relationshipFor(key);
let value = null;

if (!opts) {
return null;
}

const related = relatedFor(owner);
const { type } = opts;
let { foreignKey } = opts;
let value = related.get(key);

foreignKey = camelize(foreignKey, true);
if (opts) {
const related = relatedFor(owner);
const { type } = opts;
let { foreignKey } = opts;

if (!value) {
switch (type) {
case 'hasOne':
value = await getHasOne(owner, {
...opts,
foreignKey
});
break;
value = related.get(key);
foreignKey = camelize(foreignKey, true);

case 'hasMany':
value = await getHasMany(owner, {
...opts,
foreignKey
});
break;
if (!value) {
switch (type) {
case 'hasOne':
value = await getHasOne(owner, {
...opts,
foreignKey
});
break;

case 'hasMany':
value = await getHasMany(owner, {
...opts,
foreignKey
});
break;

case 'belongsTo':
value = await getBelongsTo(owner, {
...opts,
foreignKey
});
break;
}

case 'belongsTo':
value = await getBelongsTo(owner, {
...opts,
foreignKey
});
break;
set(owner, key, value);
}

set(owner, key, value);
}

return value;
Expand Down
32 changes: 15 additions & 17 deletions src/packages/database/relationship/utils/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,26 @@ async function getHasManyThrough(owner: Model, {
foreignKey: baseKey
}: Relationship$opts): Promise<Array<Model>> {
const inverseOpts = model.relationshipFor(inverse);
let value = [];

if (through && inverseOpts) {
let { foreignKey } = inverseOpts;
const foreignKey = camelize(inverseOpts.foreignKey, true);
const records = await through
.select(baseKey, foreignKey)
.where({
[baseKey]: owner.getPrimaryKey()
});

foreignKey = camelize(foreignKey, true);

const records = await through.select(baseKey, foreignKey).where({
[baseKey]: owner.getPrimaryKey()
});

if (!records.length) {
return [];
if (records.length) {
value = await model.where({
[model.primaryKey]: records
.map(record => Reflect.get(record, foreignKey))
.filter(Boolean)
});
}

return await model.where({
[model.primaryKey]: records
.map(record => Reflect.get(record, foreignKey))
.filter(Boolean)
});
} else {
return [];
}

return value;
}

/**
Expand Down
Loading

0 comments on commit 233d4a7

Please sign in to comment.