photoprism/frontend/tests/unit/model/rest_test.js

239 lines
7.4 KiB
JavaScript
Raw Normal View History

import "../fixtures";
import Rest from "model/rest";
2019-08-09 10:54:05 +02:00
import Album from "model/album";
import Label from "model/label";
2020-07-03 18:13:21 +02:00
import Link from "model/link";
2019-08-09 10:54:05 +02:00
2020-07-02 10:03:00 +02:00
let chai = require("chai/chai");
2019-08-13 08:01:16 +02:00
let assert = chai.assert;
2019-08-09 10:54:05 +02:00
describe("model/abstract", () => {
it("should set values", () => {
const values = { id: 5, Name: "Black Cat", Slug: "black-cat" };
const label = new Label(values);
assert.equal(label.Name, "Black Cat");
assert.equal(label.Slug, "black-cat");
label.setValues();
assert.equal(label.Name, "Black Cat");
assert.equal(label.Slug, "black-cat");
const values2 = { id: 6, Name: "White Cat", Slug: "white-cat" };
label.setValues(values2);
assert.equal(label.Name, "White Cat");
assert.equal(label.Slug, "white-cat");
});
it("should get values", () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
const result = album.getValues();
assert.equal(result.Name, "Christmas 2019");
assert.equal(result.UID, 66);
});
it("should get id", () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
const result = album.getId();
assert.equal(result, 66);
});
it("should test if id exists", () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
const result = album.hasId();
assert.equal(result, true);
});
it("should get model name", () => {
const result = Rest.getModelName();
assert.equal(result, "Item");
});
it("should update album", async () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
assert.equal(album.Description, undefined);
2021-09-19 13:35:44 +02:00
album.Name = "Christmas 2020";
await album.update();
assert.equal(album.Description, "Test description");
});
it("should save album", async () => {
const values = { UID: "abc", Name: "Christmas 2019", Slug: "christmas-2019" };
const album = new Album(values);
2021-09-19 13:35:44 +02:00
album.Name = "Christmas 2020";
assert.equal(album.Description, undefined);
await album.save();
assert.equal(album.Description, "Test description");
2021-10-06 16:42:53 +02:00
const values2 = { Name: "Christmas 2019", Slug: "christmas-2019" };
const album2 = new Album(values2);
album.Name = "Christmas 2020";
assert.equal(album2.Description, undefined);
await album2.save().then((response) => {
assert.equal(response.success, "ok");
});
assert.equal(album2.Description, undefined);
});
it("should remove album", async () => {
const values = { UID: "abc", Name: "Christmas 2019", Slug: "christmas-2019" };
const album = new Album(values);
assert.equal(album.Name, "Christmas 2019");
await album.remove();
});
it("should get edit form", async () => {
const values = { UID: "abc", Name: "Christmas 2019", Slug: "christmas-2019" };
const album = new Album(values);
const result = await album.getEditForm();
assert.equal(result.definition.foo, "edit");
});
it("should get create form", async () => {
const result = await Album.getCreateForm();
assert.equal(result.definition.foo, "bar");
});
it("should get search form", async () => {
const result = await Album.getSearchForm();
assert.equal(result.definition.foo, "bar");
});
it("should search label", async () => {
const result = await Album.search();
assert.equal(result.data.ID, 51);
assert.equal(result.data.Name, "tabby cat");
});
it("should get collection resource", () => {
assert.equal(Rest.getCollectionResource(), "");
});
it("should get slug", () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
const result = album.getSlug();
assert.equal(result, "christmas-2019");
});
it("should get slug", () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
const result = album.clone();
assert.equal(result.Slug, "christmas-2019");
assert.equal(result.Name, "Christmas 2019");
assert.equal(result.id, 5);
});
it("should find album", async () => {
const values = { Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
return album
.find(5)
.then((response) => {
assert.equal(response.UID, "5");
return Promise.resolve();
})
.catch((error) => {
return Promise.reject(error);
});
});
it("should get entity name", () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
const result = album.getEntityName();
assert.equal(result, "christmas-2019");
});
it("should return model name", () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
const result = album.modelName();
assert.equal(result, "Album");
});
it("should return limit", () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
const result = Rest.limit();
assert.equal(result, 3333);
assert.equal(album.constructor.limit(), 3333);
});
it("should create link", async () => {
const values = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values);
album
.createLink("passwd", 8000)
.then((response) => {
assert.equal(response.Slug, "christmas-2019");
return Promise.resolve();
})
.catch((error) => {
return Promise.reject(error);
});
});
it("should update link", async () => {
const values = {
UID: 5,
Password: "passwd",
Slug: "friends",
Expires: 80000,
UpdatedAt: "2012-07-08T14:45:39Z",
2021-10-08 17:48:31 +02:00
Token: "abchhgftryue2345",
};
const link = new Link(values);
const values2 = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values2);
return album
.updateLink(link)
.then((response) => {
assert.equal(response.Slug, "friends");
return Promise.resolve();
})
.catch((error) => {
return Promise.reject(error);
});
});
it("should remove link", async () => {
const values = {
UID: 5,
Password: "passwd",
Slug: "friends",
Expires: 80000,
UpdatedAt: "2012-07-08T14:45:39Z",
};
const link = new Link(values);
const values2 = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values2);
return album
.removeLink(link)
.then((response) => {
assert.equal(response.Success, "ok");
return Promise.resolve();
})
.catch((error) => {
return Promise.reject(error);
});
});
it("should return links", async () => {
const values2 = { id: 5, Name: "Christmas 2019", Slug: "christmas-2019", UID: 66 };
const album = new Album(values2);
return album
.links()
.then((response) => {
assert.equal(response.count, 2);
assert.equal(response.models.length, 2);
return Promise.resolve();
})
.catch((error) => {
return Promise.reject(error);
});
});
});