refactor: adapt for typeorm 0.3

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-03-20 22:40:41 +01:00
parent a07b5f54d1
commit c4975e4783
21 changed files with 131 additions and 69 deletions

View file

@ -120,8 +120,8 @@ describe('AliasService', () => {
jest
.spyOn(noteRepo, 'save')
.mockImplementationOnce(async (note: Note): Promise<Note> => note);
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
jest.spyOn(aliasRepo, 'findOne').mockResolvedValueOnce(undefined);
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(null);
jest.spyOn(aliasRepo, 'findOne').mockResolvedValueOnce(null);
const savedAlias = await service.addAlias(note, alias);
expect(savedAlias.name).toEqual(alias);
expect(savedAlias.primary).toBeTruthy();
@ -131,8 +131,8 @@ describe('AliasService', () => {
jest
.spyOn(noteRepo, 'save')
.mockImplementationOnce(async (note: Note): Promise<Note> => note);
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
jest.spyOn(aliasRepo, 'findOne').mockResolvedValueOnce(undefined);
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(null);
jest.spyOn(aliasRepo, 'findOne').mockResolvedValueOnce(null);
const savedAlias = await service.addAlias(note, alias2);
expect(savedAlias.name).toEqual(alias2);
expect(savedAlias.primary).toBeFalsy();
@ -230,7 +230,7 @@ describe('AliasService', () => {
it('mark the alias as primary', async () => {
jest
.spyOn(aliasRepo, 'findOne')
.spyOn(aliasRepo, 'findOneBy')
.mockResolvedValueOnce(alias)
.mockResolvedValueOnce(alias2);
jest

View file

@ -45,7 +45,7 @@ export class AliasService {
const foundAlias = await this.aliasRepository.findOne({
where: { name: alias },
});
if (foundAlias !== undefined) {
if (foundAlias !== null) {
this.logger.debug(`The alias '${alias}' is already used.`, 'addAlias');
throw new AlreadyInDBError(`The alias '${alias}' is already used.`);
}
@ -53,7 +53,7 @@ export class AliasService {
const foundNote = await this.noteRepository.findOne({
where: { publicId: alias },
});
if (foundNote !== undefined) {
if (foundNote !== null) {
this.logger.debug(
`The alias '${alias}' is already a public id.`,
'addAlias',
@ -113,8 +113,12 @@ export class AliasService {
throw new NotInDBError(`The alias '${alias}' is not used by this note.`);
}
const oldPrimary = await this.aliasRepository.findOne(oldPrimaryId);
const newPrimary = await this.aliasRepository.findOne(newPrimaryId);
const oldPrimary = await this.aliasRepository.findOneBy({
id: oldPrimaryId,
});
const newPrimary = await this.aliasRepository.findOneBy({
id: newPrimaryId,
});
if (!oldPrimary || !newPrimary) {
throw new Error('This should not happen!');

View file

@ -6,7 +6,7 @@
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { DataSource, EntityManager, Repository } from 'typeorm';
import { AuthToken } from '../auth/auth-token.entity';
import { Author } from '../authors/author.entity';
@ -121,7 +121,16 @@ describe('NotesService', () => {
* the overrideProvider call, as otherwise we have two instances
* and the mock of createQueryBuilder replaces the wrong one
* **/
userRepo = new Repository<User>();
userRepo = new Repository<User>(
'',
new EntityManager(
new DataSource({
type: 'sqlite',
database: ':memory:',
}),
),
undefined,
);
const module: TestingModule = await Test.createTestingModule({
providers: [
NotesService,
@ -202,7 +211,7 @@ describe('NotesService', () => {
const note = Note.create(user, alias) as Note;
it('with no note', async () => {
jest.spyOn(noteRepo, 'find').mockResolvedValueOnce(undefined);
jest.spyOn(noteRepo, 'find').mockResolvedValueOnce(null);
const notes = await service.getUserNotes(user);
expect(notes).toEqual([]);
});
@ -374,7 +383,7 @@ describe('NotesService', () => {
where: () => createQueryBuilder,
orWhere: () => createQueryBuilder,
setParameter: () => createQueryBuilder,
getOne: () => undefined,
getOne: () => null,
};
jest
.spyOn(noteRepo, 'createQueryBuilder')

View file

@ -5,7 +5,7 @@
*/
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Equal, Repository } from 'typeorm';
import noteConfiguration, { NoteConfig } from '../config/note.config';
import {
@ -56,7 +56,7 @@ export class NotesService {
*/
async getUserNotes(user: User): Promise<Note[]> {
const notes = await this.noteRepository.find({
where: { owner: user },
where: { owner: Equal(user) },
relations: [
'owner',
'userPermissions',
@ -65,7 +65,7 @@ export class NotesService {
'aliases',
],
});
if (notes === undefined) {
if (notes === null) {
return [];
}
return notes;
@ -188,7 +188,7 @@ export class NotesService {
.setParameter('noteIdOrAlias', noteIdOrAlias)
.getOne();
if (note === undefined) {
if (note === null) {
this.logger.debug(
`Could not find note '${noteIdOrAlias}'`,
'getNoteByIdOrAlias',