Commit c336f5aa authored by Mycha de Vrees's avatar Mycha de Vrees
Browse files

refs #3 added fixtures and updated booktransformer unittest

parent 62df911e
Pipeline #355 passed with stage
in 1 minute and 21 seconds
import Transformer from './Transformer'
import _ from 'lodash'
/**
* Fetch all linked information to a book
* @param book
* @param response
* @param type
* @param typeAlias
* @param manytomany
* @returns {Object|Array}
* @private
*/
function _fetchBooksIncludes ({ book, response, type, typeAlias = null, manytomany = false }) {
if (!typeAlias) {
typeAlias = type
......@@ -28,6 +38,13 @@ function _fetchBooksIncludes ({ book, response, type, typeAlias = null, manytoma
return {}
}
/**
* Fetch multiple books (limited by perPage in table pagination)
* Optionally include and map specific linked information
* like authors, series and genres
* @param response
* @returns {Array}
*/
function fetchMultipleBooks (response) {
const books = []
response.data.data.map((book) => {
......@@ -51,6 +68,12 @@ function fetchMultipleBooks (response) {
return books
}
/**
* Transform the response of a single book
* Includes author attributes
* @param response
* @returns {Object}
*/
function fetchSingleBook (response) {
const authors = []
const obj = {
......@@ -87,6 +110,12 @@ class BookTransformer extends Transformer {
// or just fetch a single book.
return fetchSingleBook(response)
}
/**
* Fetch book author
* @param response
* @returns {Array}
*/
static fetchBookAuthors (response) {
const authors = []
if (Array.isArray(response.data.data)) {
......
import Vue from 'vue'
import BookTransformer from '@/transformers/BookTransformer'
describe('BookTransformer fetch', () => {
const body = {
data: {
data: [
{
id: 1,
attributes: {
language: null,
serie_id: null,
sortable_title: null,
title: 'Het kraaienmeisje'
},
relationships: {
authors: {
data: [
{
id: 1,
name: 'Test author'
}
]
}
}
}
],
included: [
{
id: 1,
attributes: {
title: 'Test author'
},
type: 'author'
}
]
}
}
const transform = BookTransformer.fetch(body)
describe('BookTransformer fetch multibody', () => {
const multibody = require('./BookTransformer/fixtures/multibody.json')
const transform = BookTransformer.fetch(multibody)
it('should contain the correct data format', () => {
expect(transform[0].id).toEqual(1)
})
})
describe('BookTransformer fetch singlebody', () => {
const singlebody = require('./BookTransformer/fixtures/singlebody.json')
const transform = BookTransformer.fetch(singlebody)
it('should contain the correct data format', () => {
expect(transform.id).toEqual(1)
})
})
describe('BookTrasnformer fetch authors', () => {
const authors = require('./BookTransformer/fixtures/authors.json')
const transformAuthors = BookTransformer.fetchBookAuthors(authors)
it('should contain the correct data format', () => {
expect(transformAuthors[0].title).toEqual('Jerker Eriksson')
})
})
{
"data": {
"data": [
{
"attributes": {
"goodreads_id": "5039034",
"name": "Jerker Eriksson",
"sortable_name": null
},
"id": 1,
"links": {
"self": "http://localhost:5000/author/1/"
},
"relationships": {
"books": {
"data": null,
"links": {
"self": "http://localhost:5000/author/1/books"
}
}
},
"type": "author"
},
{
"attributes": {
"goodreads_id": "7795892",
"name": "Håkan Axlander Sundquist",
"sortable_name": null
},
"id": 2,
"links": {
"self": "http://localhost:5000/author/2/"
},
"relationships": {
"books": {
"data": null,
"links": {
"self": "http://localhost:5000/author/2/books"
}
}
},
"type": "author"
},
{
"attributes": {
"goodreads_id": "7160538",
"name": "Erik Axl Sund",
"sortable_name": null
},
"id": 3,
"links": {
"self": "http://localhost:5000/author/3/"
},
"relationships": {
"books": {
"data": null,
"links": {
"self": "http://localhost:5000/author/3/books"
}
}
},
"type": "author"
}
]
}
}
{
"data": {
"data": [
{
"id": 1,
"attributes": {
"language": null,
"serie_id": null,
"sortable_title": null,
"title": "Het kraaienmeisje"
},
"relationships": {
"authors": {
"data": [
{
"id": 1,
"name": "Test author"
}
]
},
"genres": {
"data": [
{
"id": 1,
"type": "genre"
}
]
},
"serie": {
"data": {
"id": 1,
"type": "book_serie"
}
}
}
}
],
"included": [
{
"id": 1,
"attributes": {
"title": "Test author"
},
"type": "author"
},
{
"id": 1,
"attributes": {
"language": "NL",
"name": "Testserie"
},
"type": "book_serie"
},
{
"id": 1,
"attributes": {
"fiction": false,
"name": "Art"
},
"type": "genre"
}
]
}
}
{
"data": {
"data": {
"attributes": {
"language": null,
"serie_id": 1,
"sortable_title": null,
"title": "Het kraaienmeisje (Victoria Bergmans svaghet #1)"
},
"id": 1,
"links": {
"self": "http://localhost:5000/book/1/"
},
"relationships": {
"authors": {
"data": [
{
"id": 1,
"type": "author"
},
{
"id": 2,
"type": "author"
},
{
"id": 3,
"type": "author"
}
],
"links": {
"self": "http://localhost:5000/book/1/authors"
},
"meta": {
"count": 3,
"direction": "MANYTOMANY",
"limit": 10
}
},
"editions": {
"data": null,
"links": {
"self": "http://localhost:5000/book/1/editions"
},
"meta": {
"direction": "ONETOMANY"
}
},
"genres": {
"data": [
{
"id": 1,
"type": "genre"
}
],
"links": {
"self": "http://localhost:5000/book/1/genres"
},
"meta": {
"count": 1,
"direction": "MANYTOMANY",
"limit": 10
}
},
"serie": {
"data": {
"id": 1,
"type": "book_serie"
},
"links": {
"self": "http://localhost:5000/book/1/serie"
},
"meta": {
"direction": "MANYTOONE"
}
}
},
"type": "book"
},
"included": [
{
"attributes": {
"goodreads_id": "7795892",
"name": "Håkan Axlander Sundquist",
"sortable_name": null
},
"id": 2,
"links": {
"self": "http://localhost:5000/author/2/"
},
"relationships": {
"books": {
"data": null,
"links": {
"self": "http://localhost:5000/author/2/books"
}
}
},
"type": "author"
},
{
"attributes": {
"goodreads_id": "7160538",
"name": "Erik Axl Sund",
"sortable_name": null
},
"id": 3,
"links": {
"self": "http://localhost:5000/author/3/"
},
"relationships": {
"books": {
"data": null,
"links": {
"self": "http://localhost:5000/author/3/books"
}
}
},
"type": "author"
},
{
"attributes": {
"language": "NL",
"name": "Testserie"
},
"id": 1,
"links": {
"self": "http://localhost:5000/book_serie/1/"
},
"relationships": {
"books": {
"data": null,
"links": {
"self": "http://localhost:5000/book_serie/1/books"
}
}
},
"type": "book_serie"
},
{
"attributes": {
"fiction": false,
"name": "Art"
},
"id": 1,
"links": {
"self": "http://localhost:5000/genre/1/"
},
"relationships": {
"books": {
"data": null,
"links": {
"self": "http://localhost:5000/genre/1/books"
}
}
},
"type": "genre"
},
{
"attributes": {
"goodreads_id": "5039034",
"name": "Jerker Eriksson",
"sortable_name": null
},
"id": 1,
"links": {
"self": "http://localhost:5000/author/1/"
},
"relationships": {
"books": {
"data": null,
"links": {
"self": "http://localhost:5000/author/1/books"
}
}
},
"type": "author"
}
],
"jsonapi": {
"version": "1.0"
},
"links": {
"related": "http://localhost:5000/book/1/?page[limit]=10&include=editions%2Cauthors%2Cgenres%2Cserie&fields[book]=title%2Csortable_title%2Clanguage%2Cserie_id&sort=title%2Csortable_title%2Clanguage%2Cserie_id",
"self": "http://localhost:5000/book/1/"
},
"meta": {
"count": 1,
"instance_meta": {},
"limit": 10
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment