Coverage for tests\conftest.py: 98%

37 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-12-20 00:39 +0000

1import json 

2import os 

3 

4import pytest 

5import sqlite_utils 

6from datasette.app import Datasette 

7 

8from datasette_reconcile.settings import SUPPORTED_API_VERSIONS 

9 

10SCHEMA_DIR = os.path.join( 

11 os.path.dirname(__file__), 

12 "../specs", 

13) 

14 

15 

16def create_db(tmp_path_factory): 

17 db_directory = tmp_path_factory.mktemp("dbs") 

18 db_path = db_directory / "test.db" 

19 db = sqlite_utils.Database(db_path) 

20 db["dogs"].insert_all( 

21 [ 

22 {"id": 1, "name": "Cleo", "age": 5, "status": "good dog"}, 

23 {"id": 2, "name": "Pancakes", "age": 4, "status": "bad dog"}, 

24 {"id": 3, "name": "Fido", "age": 3, "status": "bad dog"}, 

25 {"id": 4, "name": "Scratch", "age": 3, "status": "good dog"}, 

26 ], 

27 pk="id", 

28 ) 

29 return db_path 

30 

31 

32def plugin_metadata(metadata=None): 

33 to_return = {"databases": {"test": {"tables": {"dogs": {"title": "Some dogs"}}}}} 

34 if isinstance(metadata, dict): 

35 to_return["databases"]["test"]["tables"]["dogs"]["plugins"] = {"datasette-reconcile": metadata} 

36 return to_return 

37 

38 

39def get_schema(filename): 

40 schemas = {} 

41 for f in os.scandir(SCHEMA_DIR): 

42 if not f.is_dir(): 

43 continue 

44 if f.name not in SUPPORTED_API_VERSIONS: 

45 continue 

46 schema_path = os.path.join(f.path, "schemas", filename) 

47 if os.path.exists(schema_path): 47 ↛ 41line 47 didn't jump to line 41, because the condition on line 47 was never false

48 with open(schema_path, encoding="utf8") as schema_file: 

49 schemas[f.name] = json.load(schema_file) 

50 return schemas 

51 

52 

53@pytest.fixture(scope="session") 

54def ds(tmp_path_factory): 

55 ds = Datasette([create_db(tmp_path_factory)], metadata=plugin_metadata()) 

56 return ds 

57 

58 

59@pytest.fixture(scope="session") 

60def db_path(tmp_path_factory): 

61 return create_db(tmp_path_factory)