chore: prepare project for server deployment
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
const Entity = require('../src/models/Entity');
|
||||
const DirectedRelationship = require('../src/models/DirectedRelationship');
|
||||
const { getDb, closeDb } = require('../src/config/database');
|
||||
const { migrate } = require('../src/database/migrate');
|
||||
|
||||
beforeAll(() => {
|
||||
migrate();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
closeDb();
|
||||
});
|
||||
|
||||
describe('DirectedRelationship Model', () => {
|
||||
let entity1Id, entity2Id, relId;
|
||||
|
||||
beforeAll(() => {
|
||||
// 创建测试实体
|
||||
const entity1 = Entity.create({
|
||||
name: '实体1',
|
||||
definition: '第一个测试实体'
|
||||
});
|
||||
entity1Id = entity1.entity_id;
|
||||
|
||||
const entity2 = Entity.create({
|
||||
name: '实体2',
|
||||
definition: '第二个测试实体'
|
||||
});
|
||||
entity2Id = entity2.entity_id;
|
||||
});
|
||||
|
||||
test('应该创建关系', () => {
|
||||
const relationship = DirectedRelationship.create({
|
||||
source_entity_id: entity1Id,
|
||||
relation_type: '包含',
|
||||
target_entity_id: entity2Id,
|
||||
rel_status: 'active',
|
||||
authority_level: 3,
|
||||
is_public: 1
|
||||
});
|
||||
|
||||
expect(relationship).toBeDefined();
|
||||
expect(relationship.rel_id).toBeDefined();
|
||||
expect(relationship.source_entity_id).toBe(entity1Id);
|
||||
expect(relationship.relation_type).toBe('包含');
|
||||
expect(relationship.target_entity_id).toBe(entity2Id);
|
||||
expect(relationship.rel_status).toBe('active');
|
||||
expect(relationship.authority_level).toBe(3);
|
||||
expect(relationship.is_public).toBe(1);
|
||||
|
||||
relId = relationship.rel_id;
|
||||
});
|
||||
|
||||
test('应该根据ID查找关系', () => {
|
||||
const relationship = DirectedRelationship.findById(relId);
|
||||
expect(relationship).toBeDefined();
|
||||
expect(relationship.rel_id).toBe(relId);
|
||||
expect(relationship.source_entity_id).toBe(entity1Id);
|
||||
});
|
||||
|
||||
test('应该根据源实体查找关系', () => {
|
||||
const relationships = DirectedRelationship.findBySourceEntity(entity1Id);
|
||||
expect(Array.isArray(relationships)).toBe(true);
|
||||
expect(relationships.length).toBeGreaterThan(0);
|
||||
expect(relationships[0].source_entity_id).toBe(entity1Id);
|
||||
});
|
||||
|
||||
test('应该根据目标实体查找关系', () => {
|
||||
const relationships = DirectedRelationship.findByTargetEntity(entity2Id);
|
||||
expect(Array.isArray(relationships)).toBe(true);
|
||||
expect(relationships.length).toBeGreaterThan(0);
|
||||
expect(relationships[0].target_entity_id).toBe(entity2Id);
|
||||
});
|
||||
|
||||
test('应该更新关系', () => {
|
||||
const updatedRelationship = DirectedRelationship.update(relId, {
|
||||
relation_type: '定位为',
|
||||
authority_level: 5
|
||||
});
|
||||
|
||||
expect(updatedRelationship.relation_type).toBe('定位为');
|
||||
expect(updatedRelationship.authority_level).toBe(5);
|
||||
});
|
||||
|
||||
test('应该获取所有关系', () => {
|
||||
const relationships = DirectedRelationship.findAll();
|
||||
expect(Array.isArray(relationships)).toBe(true);
|
||||
expect(relationships.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test('应该删除关系', () => {
|
||||
const result = DirectedRelationship.delete(relId);
|
||||
expect(result.changes).toBe(1);
|
||||
|
||||
const relationship = DirectedRelationship.findById(relId);
|
||||
expect(relationship).toBeUndefined();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
// 清理测试实体
|
||||
Entity.delete(entity1Id);
|
||||
Entity.delete(entity2Id);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user