This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
192 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,18 @@ | ||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; | ||
import { Article, Articles } from '../../types/article'; | ||
import { Article } from '../../types/article'; | ||
|
||
export const historyApi = createApi({ | ||
reducerPath: 'historyApi', | ||
baseQuery: fetchBaseQuery({ | ||
baseUrl: 'http://localhost:5000/', | ||
}), | ||
endpoints: (builder) => ({ | ||
getAllHistoryArticleLink: builder.query<Array<Articles>, void>({ | ||
query: () => ({ | ||
url: '/', | ||
}), | ||
}), | ||
getHistoryArticleById: builder.query<Article, string>({ | ||
query: (id: string) => `history/${id}`, | ||
}), | ||
}), | ||
}); | ||
|
||
export const { | ||
useGetAllHistoryArticleLinkQuery, | ||
useGetHistoryArticleByIdQuery, | ||
} = historyApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ReactNode } from 'react'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
import fetchMock from 'jest-fetch-mock'; | ||
import { store } from '../store'; | ||
import { useGetHomepageDataQuery } from './homepageSlice'; | ||
|
||
function Wrapper({ children }: { children: ReactNode }) { | ||
return ( | ||
<Provider store={store}> | ||
{children} | ||
</Provider> | ||
); | ||
} | ||
|
||
describe('useGetAllHistoryArticleLinkQuery', () => { | ||
const endpointName = 'getHomepageData'; | ||
const data = { | ||
tunnellers: [ | ||
{ id: 1, image: 'image-1.jpg' }, | ||
{ id: 2, image: 'image-2.jpg' }, | ||
{ id: 3, image: 'image-3.jpg' }, | ||
], | ||
historyChapters: [ | ||
{ url: 'test-article-1', title: 'test-article-1', chapter: 1 }, | ||
{ url: 'test-article-2', title: 'test-article-2', chapter: 2 }, | ||
{ url: 'test-article-3', title: 'test-article-3', chapter: 3 }, | ||
], | ||
}; | ||
|
||
beforeEach(() => { | ||
fetchMock.resetMocks(); | ||
fetchMock.mockOnceIf('http://localhost:5000/', () => Promise.resolve({ | ||
status: 200, | ||
body: JSON.stringify({ data }), | ||
})); | ||
}); | ||
|
||
it('renders hook', async () => { | ||
const { result } = renderHook(() => useGetHomepageDataQuery(), { | ||
wrapper: Wrapper, | ||
}); | ||
|
||
expect(result.current).toMatchObject({ | ||
status: 'pending', | ||
endpointName, | ||
isLoading: true, | ||
isSuccess: false, | ||
isError: false, | ||
isFetching: true, | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
expect(fetchMock).toBeCalledTimes(1); | ||
|
||
expect(result.current).toMatchObject({ | ||
status: 'fulfilled', | ||
endpointName, | ||
data: { data }, | ||
isLoading: false, | ||
isSuccess: true, | ||
isError: false, | ||
currentData: {}, | ||
isFetching: false, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; | ||
import { Articles } from '../../types/article'; | ||
|
||
export const homepageApi = createApi({ | ||
reducerPath: 'homepageApi', | ||
baseQuery: fetchBaseQuery({ | ||
baseUrl: 'http://localhost:5000/', | ||
}), | ||
endpoints: (builder) => ({ | ||
getHomepageData: builder.query<Record<string, Array<Articles>>, void>({ | ||
query: () => ({ | ||
url: '/', | ||
}), | ||
}), | ||
}), | ||
}); | ||
|
||
export const { | ||
useGetHomepageDataQuery, | ||
} = homepageApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
from dataclasses import dataclass | ||
|
||
|
||
from .article import ArticleReference | ||
|
||
|
||
@dataclass | ||
class TunnellerImage: | ||
id: int | ||
image: str | ||
|
||
def __getitem__(self, key: str): | ||
return getattr(self, key) | ||
|
||
|
||
@dataclass | ||
class Homepage: | ||
tunnellers: list[TunnellerImage] | ||
history_chapters: list[ArticleReference] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
# flask_mysqldb does not have stub files | ||
from flask_mysqldb import MySQL # type: ignore | ||
|
||
|
||
from ..models.homepage import Homepage, TunnellerImage | ||
|
||
from ..db.models.ArticleData import ( | ||
ArticleReferenceData, | ||
FileData, | ||
) | ||
|
||
from ..models.helpers.article_helpers import ( | ||
get_images, | ||
get_next_chapter, | ||
) | ||
|
||
|
||
from ..models.article import ArticleReference | ||
from ..repositories.queries.article_query import ( | ||
next_article_query, | ||
images_query, | ||
) | ||
from ..repositories.queries.tunnellers_images_query import tunnellers_images_query | ||
|
||
from ..db.run_sql import run_sql | ||
|
||
|
||
def get_homepage(mysql: MySQL) -> Homepage: | ||
tunnellers: list[TunnellerImage] = [] | ||
|
||
images_sql: str = tunnellers_images_query() | ||
images_results: tuple[TunnellerImage, ...] = run_sql(images_sql, mysql, None) | ||
|
||
for index, row in enumerate(images_results): | ||
image = TunnellerImage( | ||
row["id"], | ||
row["image"], | ||
) | ||
tunnellers.append(image) | ||
|
||
articles: list[ArticleReference] = [] | ||
|
||
articles_sql: str = next_article_query() | ||
articles_results: tuple[ArticleReferenceData, ...] = run_sql( | ||
articles_sql, mysql, None | ||
) | ||
images_sql = images_query() | ||
image_result: tuple[FileData, ...] = run_sql(images_sql, mysql, None) | ||
next_sql = next_article_query() | ||
next_result: tuple[ArticleReferenceData, ...] = run_sql(next_sql, mysql, None) | ||
|
||
for index, row in enumerate(articles_results): | ||
article = ArticleReference( | ||
row["id"], | ||
row["chapter"], | ||
row["title"], | ||
get_images(image_result, index), | ||
get_next_chapter(row["chapter"], next_result), | ||
) | ||
articles.append(article) | ||
|
||
return Homepage(tunnellers, articles) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
def tunnellers_images_query() -> str: | ||
return """SELECT | ||
t.id | ||
, t.image | ||
FROM tunneller t WHERE t.image IS NOT NULL""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters