Skip to content

Commit

Permalink
Merge pull request #52 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
add createAll
  • Loading branch information
GuoXiCheng authored Dec 26, 2023
2 parents 84b2891 + daf822b commit 92f372c
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 3 deletions.
62 changes: 61 additions & 1 deletion src/__tests__/book-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { Book } from "./helper/book-storage";

describe('Test Book Storage', () => {

test('Test deleteAll Book', async () => {
beforeAll(async () => {
await Book.deleteAll();
});

test('Test find Book', async () => {
const detail = await Book.find();
expect(detail.length).toEqual(0);
});
Expand Down Expand Up @@ -73,4 +76,61 @@ describe('Test Book Storage', () => {
});
}
});

test('Test createAll Book', async () => {
await Book.deleteAll();
const result = await Book.createAll([
{
book_name: 'test-book-1',
book_author: 'test-author-1',
book_price: 100
},
{
book_name: 'test-book-2',
book_author: 'test-author-2',
book_price: 200
},
{
book_name: 'test-book-3',
book_author: 'test-author-3',
book_price: 300
},
{
book_name: 'test-book-4',
book_author: 'test-author-4',
book_price: 400
},
{
book_name: 'test-book-5',
book_author: 'test-author-5',
book_price: 500
},
{
book_name: 'test-book-6',
book_author: 'test-author-6',
book_price: 600
},
{
book_name: 'test-book-7',
book_author: 'test-author-7',
book_price: 700
},
{
book_name: 'test-book-8',
book_author: 'test-author-8',
book_price: 800
},
{
book_name: 'test-book-9',
book_author: 'test-author-9',
book_price: 900
},
{
book_name: 'test-book-10',
book_author: 'test-author-10',
book_price: 1000
}
]);
expect(result.length).toEqual(10);
});
});
112 changes: 111 additions & 1 deletion src/__tests__/chat-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { Chat } from "./helper/chat-storage";

describe('Use Gitlab Test Chat Storage', () => {

test('Test deleteAll Chat', async () => {
beforeAll(async () => {
await Chat.deleteAll();
});

test('Test find Chat', async () => {
const detail = await Chat.find();
expect(detail.length).toEqual(0);
});
Expand Down Expand Up @@ -84,4 +87,111 @@ describe('Use Gitlab Test Chat Storage', () => {
});
}
});

test('Test createAll Chat', async () => {
await Chat.deleteAll();
const result = await Chat.createAll([
{
participants: ['from-user-1', 'to-user-1'],
messages: [
{
from: 'from-user-1',
to: 'to-user-1',
message: 'hello'
}
]
},
{
participants: ['from-user-2', 'to-user-2'],
messages: [
{
from: 'from-user-2',
to: 'to-user-2',
message: 'hello'
}
]
},
{
participants: ['from-user-3', 'to-user-3'],
messages: [
{
from: 'from-user-3',
to: 'to-user-3',
message: 'hello'
}
]
},
{
participants: ['from-user-4', 'to-user-4'],
messages: [
{
from: 'from-user-4',
to: 'to-user-4',
message: 'hello'
}
]
},
{
participants: ['from-user-5', 'to-user-5'],
messages: [
{
from: 'from-user-5',
to: 'to-user-5',
message: 'hello'
}
]
},
{
participants: ['from-user-6', 'to-user-6'],
messages: [
{
from: 'from-user-6',
to: 'to-user-6',
message: 'hello'
}
]
},
{
participants: ['from-user-7', 'to-user-7'],
messages: [
{
from: 'from-user-7',
to: 'to-user-7',
message: 'hello'
}
]
},
{
participants: ['from-user-8', 'to-user-8'],
messages: [
{
from: 'from-user-8',
to: 'to-user-8',
message: 'hello'
}
]
},
{
participants: ['from-user-9', 'to-user-9'],
messages: [
{
from: 'from-user-9',
to: 'to-user-9',
message: 'hello'
}
]
},
{
participants: ['from-user-10', 'to-user-10'],
messages: [
{
from: 'from-user-10',
to: 'to-user-10',
message: 'hello'
}
]
}
]);
expect(result.length).toEqual(10);
});
});
51 changes: 50 additions & 1 deletion src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { User } from "./helper/user-storage";

describe('Test User Storage', () => {

test('Test deleteAll User', async () => {
beforeAll(async () => {
await User.deleteAll();
});

test('Test find User', async () => {
const detail = await User.find();
expect(detail.length).toEqual(0);
});
Expand Down Expand Up @@ -60,4 +63,50 @@ describe('Test User Storage', () => {
expect(error.response.data).toEqual({ message: '404 Not Found' });
}
});

test('Test createAll User', async () => {
const result = await User.createAll([
{
name: 'test-user-1',
age: 18
},
{
name: 'test-user-2',
age: 20
},
{
name: 'test-user-3',
age: 22
},
{
name: 'test-user-4',
age: 24
},
{
name: 'test-user-5',
age: 26
},
{
name: 'test-user-6',
age: 28
},
{
name: 'test-user-7',
age: 30
},
{
name: 'test-user-8',
age: 32
},
{
name: 'test-user-9',
age: 34
},
{
name: 'test-user-10',
age: 36
}
]);
expect(result.length).toEqual(10);
});
});
9 changes: 9 additions & 0 deletions src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ export abstract class BaseStorage<T extends BaseModel> {
return this.deserialize<T>(response);
}

/**
* Creates multiple items in the storage.
* @param data An array of objects representing the items to be created.
* @returns A promise that resolves to an array of created items.
*/
async createAll(data: PlainObject<T>[]): Promise<T[]> {
return Promise.all(data.map((item) => this.create(item)));
}

/**
* Updates a record by its ID.
* @param id - The ID of the record to update.
Expand Down

0 comments on commit 92f372c

Please sign in to comment.