Skip to content

Commit

Permalink
Remove duplicate attribute names, don't set null on objects or instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Bogart committed Feb 19, 2021
1 parent bec21a9 commit f1de45e
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/attribute-assigner.ts
Expand Up @@ -3,6 +3,7 @@ import {Attribute} from "./attributes/attribute";
import {Evaluator} from "./evaluator";
import {FixtureRiveter} from "./fixture-riveter";
import {ModelConstructor} from "./types";
import {uniq} from "lodash";

export class AttributeAssigner<T> {
fixtureRiveter: FixtureRiveter;
Expand Down Expand Up @@ -53,18 +54,23 @@ export class AttributeAssigner<T> {

attributesForObject(): string[] {
const relationNames = this.relationNames();
return this.getAttributeNamesToAssign().filter((a) => !relationNames.includes(a));
return uniq(this.getAttributeNamesToAssign().filter((a) => !relationNames.includes(a)));
}

attributesForInstance(): string[] {
const invokedMethods = Array.from(this.evaluator.fetchedAttributes.keys());
return this.getAttributeNamesToAssign().filter((a) => !invokedMethods.includes(a));
return uniq(this.getAttributeNamesToAssign().filter((a) => !invokedMethods.includes(a)));
}

async toObject(): Promise<Record<string, any>> {
this.evaluator.instance = {};
for (const name of this.attributesForObject()) {
this.evaluator.instance[name] = await this._get(name);
const attribute = await this._get(name);
if (attribute === undefined || attribute === null) {
this.evaluator.instance[name] = undefined;
} else {
this.evaluator.instance[name] = attribute;
}
}

return this.evaluator.instance;
Expand All @@ -77,12 +83,12 @@ export class AttributeAssigner<T> {

for (const name of attributeNames) {
const attribute = await this._get(name);
if (attribute !== undefined && attribute !== null) {
if (relationNames.includes(name)) {
await this.adapter.relate(this.evaluator.instance, name, attribute, this.model);
} else {
this.adapter.set(this.evaluator.instance, name, attribute);
}
if (attribute === undefined || attribute === null) {
this.adapter.set(this.evaluator.instance, name, undefined);
} else if (relationNames.includes(name)) {
await this.adapter.relate(this.evaluator.instance, name, attribute, this.model);
} else {
this.adapter.set(this.evaluator.instance, name, attribute);
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/acceptance/adapters/sequelize-adapter.ts
Expand Up @@ -69,6 +69,35 @@ describe("Sequelize functionality", function() {
expect(user).to.be.an.instanceof(User);
expect(user.name).to.equal("Noah");
});

describe("relations set to null or undefined are set to undefined", function() {
specify("build", async function() {
expect((await fr.build("post", {user: null})).user).to.be.undefined;
expect((await fr.build("post", {user: undefined})).user).to.be.undefined;
});

specify("create", async function() {
expect((await fr.create("post", {user: null})).user).to.be.undefined;
expect((await fr.create("post", {user: undefined})).user).to.be.undefined;
});
});

describe("attributes set to null or undefined are changed to undefined", function() {
specify("attributesFor", async function() {
expect((await fr.attributesFor("user", {name: null})).name).to.be.undefined;
expect((await fr.attributesFor("user", {name: undefined})).name).to.be.undefined;
});

specify("build", async function() {
expect((await fr.build("user", {name: null})).name).to.be.undefined;
expect((await fr.build("user", {name: undefined})).name).to.be.undefined;
});

specify("create", async function() {
expect((await fr.create("user", {name: null})).name).to.be.undefined;
expect((await fr.create("user", {name: undefined})).name).to.be.undefined;
});
});
});

describe(
Expand Down
91 changes: 91 additions & 0 deletions test/unit/attribute-assigner.ts
@@ -0,0 +1,91 @@
import {FixtureRiveter} from "../../lib/fixture-riveter";
import {ObjectionAdapter} from "../../lib/adapters/objection-adapter";
import {Model} from "../support/model";
import {createTable} from "../support/define-helpers";

import {expect} from "chai";

describe("attribute-assigner", function() {
describe("oveerriding attributes with null and undefined", function() {
let fr: FixtureRiveter;

class User extends Model {
id: number;
name: string;

get props() {
return {name: "string"};
}
}

class Post extends Model {
static relationMappings = {
user: {
relation: Model.BelongsToOneRelation,
modelClass: User,
join: {
from: "posts.userId",
to: "users.id",
},
},
};

id: number;
userId: number;
user: User;

get props() {
return {
userId: "integer",
};
}
}

before(async function() {
await createTable(User);
await createTable(Post);
});

beforeEach(function() {
fr = new FixtureRiveter();
fr.setAdapter(new ObjectionAdapter());

fr.fixture("user", User, (f) => {
f.name(() => "Noah");
});

fr.fixture("post", Post, (f) => {
f.relation("user");
});
});

describe("relations set to null or undefined are set to undefined", function() {
specify("build", async function() {
expect((await fr.build("post", {user: null})).user).to.be.undefined;
expect((await fr.build("post", {user: undefined})).user).to.be.undefined;
});

specify("create", async function() {
expect((await fr.create("post", {user: null})).user).to.be.undefined;
expect((await fr.create("post", {user: undefined})).user).to.be.undefined;
});
});

describe("attributes set to null or undefined are changed to undefined", function() {
specify("attributesFor", async function() {
expect((await fr.attributesFor("user", {name: null})).name).to.be.undefined;
expect((await fr.attributesFor("user", {name: undefined})).name).to.be.undefined;
});

specify("build", async function() {
expect((await fr.build("user", {name: null})).name).to.be.undefined;
expect((await fr.build("user", {name: undefined})).name).to.be.undefined;
});

specify("create", async function() {
expect((await fr.create("user", {name: null})).name).to.be.null;
expect((await fr.create("user", {name: undefined})).name).to.be.null;
});
});
});
});

0 comments on commit f1de45e

Please sign in to comment.