Coverage for app/services/create_world.py: 16%

17 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-02 23:27 +0000

1from app.db.models import World, WorldSettings 

2from app.core.exceptions import BadRequestError, DatabaseSaveError 

3 

4def create_world(input_name: str, input_description: str, user_id, db): 

5 if len(input_name) > 100: 

6 raise BadRequestError("Name must be less than 100 characters") 

7 

8 try: 

9 #make new world 

10 new_world = World( 

11 name = input_name, 

12 description = input_description, 

13 created_by = user_id 

14 ) 

15 

16 db.add(new_world) 

17 db.commit() 

18 db.refresh(new_world) 

19 

20 #Attach world setting to new world 

21 new_world_settings = WorldSettings( 

22 world_id = new_world.id 

23 ) 

24 

25 db.add(new_world_settings) 

26 db.commit() 

27 db.refresh(new_world_settings) 

28 

29 return { 

30 "world": new_world.id, 

31 "world_settings": new_world_settings.id 

32 } 

33 

34 except Exception as e: 

35 raise DatabaseSaveError(str(e))