Skip to content

Commit

Permalink
update createAll orderly
Browse files Browse the repository at this point in the history
  • Loading branch information
Xicheng Guo committed Dec 27, 2023
1 parent daf822b commit 9515429
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 201 deletions.
120 changes: 68 additions & 52 deletions src/__tests__/book-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
import { PlainObject } from "../storage-lib/base/plain-object";
import { BookModel } from "./helper/book-model";
import { Book } from "./helper/book-storage";

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

const bookList: PlainObject<BookModel>[] = [
{
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
}
];

beforeAll(async () => {
await Book.deleteAll();
});
Expand Down Expand Up @@ -79,58 +134,19 @@ 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
}
]);
const result = await Book.createAll(bookList);
expect(result.length).toEqual(10);

// 因为是并行创建,所以批量新增的数据是无序的
expect((await Book.find()).map(item=>item.book_name)).not.toEqual(bookList.map(item=>item.book_name));
});

test('Test createAll Book orderly', async () => {
await Book.deleteAll();
const result = await Book.createAll(bookList, true);
expect(result.length).toEqual(10);

// 因为是顺序创建,所以批量新增的数据是有序的
expect((await Book.find()).map(item=>item.book_name)).toEqual(bookList.map(item=>item.book_name));
});
});
221 changes: 119 additions & 102 deletions src/__tests__/chat-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,113 @@
import { PlainObject } from "../storage-lib/base/plain-object";
import { ChatModel } from "./helper/chat-model";
import { Chat } from "./helper/chat-storage";

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

const chatList: PlainObject<ChatModel>[] = [
{
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'
}
]
}
];


beforeAll(async () => {
await Chat.deleteAll();
});
Expand Down Expand Up @@ -90,108 +196,19 @@ 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'
}
]
}
]);
const result = await Chat.createAll(chatList);
expect(result.length).toEqual(10);

// 因为是并行创建,所以批量新增的数据是无序的
expect((await Chat.find()).map(item=>item.participants)).not.toEqual(chatList.map(item=>item.participants).reverse());
});

test('Test createAll Chat orderly', async ()=>{
await Chat.deleteAll();
const result = await Chat.createAll(chatList, true);
expect(result.length).toEqual(10);

// 因为是顺序创建,所以批量新增的数据是有序的
expect((await Chat.find()).map(item=>item.participants)).toEqual(chatList.map(item=>item.participants).reverse());
})
});
3 changes: 3 additions & 0 deletions src/__tests__/helper/book-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ class BookStorage extends GithubStorage<BookModel> {
}
}

/**
* test github api with a book storage instance.
*/
export const Book = BookStorage.getInstance();
3 changes: 3 additions & 0 deletions src/__tests__/helper/chat-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ class ChatStorage extends GitlabStorage<ChatModel> {
}
}

/**
* test gitlab api with a chat storage instance.
*/
export const Chat = ChatStorage.getInstance();
3 changes: 3 additions & 0 deletions src/__tests__/helper/user-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ export class UserStorage extends GiteeStorage<UserModel> {

}

/**
* test gitee api with a user storage instance.
*/
export const User = UserStorage.getInstance();
Loading

0 comments on commit 9515429

Please sign in to comment.