forked from IMAP-Science-Operations-Center/sds-data-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
118 lines (102 loc) · 4.04 KB
/
Copy pathconftest.py
File metadata and controls
118 lines (102 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Setup items for all test types."""
import os
from contextlib import contextmanager
from unittest.mock import patch
import boto3
import pytest
from moto import mock_dynamodb
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sds_data_manager.lambda_code.SDSCode.database import database as db
from sds_data_manager.lambda_code.SDSCode.database.models import Base
@contextmanager
def in_memory_session(engine=None):
"""Yield an in-memory SQLite session with ``db.Session`` patched to it.
Shared scaffolding for the DB-backed ``session`` fixtures. Pass a pre-built
``engine`` (e.g. with event listeners already attached) or let it build a
default in-memory engine.
"""
if engine is None:
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
with patch.object(db, "Session") as mock_session:
session = sessionmaker(bind=engine)()
mock_session.return_value = session
try:
yield session
finally:
session.rollback()
session.close()
Base.metadata.drop_all(engine)
@pytest.fixture
def setup_dynamodb():
"""Initialize DynamoDB resource and create table."""
os.environ["AWS_DEFAULT_REGION"] = "us-west-2"
os.environ["INGEST_TABLE"] = "imap-ingest-table"
os.environ["ALGORITHM_TABLE"] = "ialirt-algorithm-table"
with mock_dynamodb():
# Initialize DynamoDB resource
dynamodb = boto3.resource("dynamodb", region_name="us-west-2")
algorithm_table = dynamodb.create_table(
TableName=os.environ["ALGORITHM_TABLE"],
KeySchema=[
# Partition key
{"AttributeName": "apid", "KeyType": "HASH"},
# Sort key
{"AttributeName": "met", "KeyType": "RANGE"},
],
AttributeDefinitions=[
{"AttributeName": "apid", "AttributeType": "N"},
{"AttributeName": "met", "AttributeType": "N"},
{"AttributeName": "met_in_utc", "AttributeType": "S"},
{"AttributeName": "last_modified", "AttributeType": "S"},
],
GlobalSecondaryIndexes=[
{
"IndexName": "met_in_utc", # Unique index name
"KeySchema": [
{"AttributeName": "apid", "KeyType": "HASH"},
{"AttributeName": "met_in_utc", "KeyType": "RANGE"},
],
"Projection": {"ProjectionType": "ALL"},
},
{
"IndexName": "last_modified", # Unique index name
"KeySchema": [
{"AttributeName": "apid", "KeyType": "HASH"},
{"AttributeName": "last_modified", "KeyType": "RANGE"},
],
"Projection": {"ProjectionType": "ALL"},
},
],
BillingMode="PAY_PER_REQUEST",
)
yield {
"algorithm_table": algorithm_table,
}
@pytest.fixture
def setup_data_table():
"""Initialize DynamoDB resource and create table."""
os.environ["AWS_DEFAULT_REGION"] = "us-west-2"
os.environ["INGEST_TABLE"] = "imap-ingest-table"
os.environ["DATA_TABLE"] = "imap-data-table"
with mock_dynamodb():
# Initialize DynamoDB resource
dynamodb = boto3.resource("dynamodb", region_name="us-west-2")
data_table = dynamodb.create_table(
TableName=os.environ["DATA_TABLE"],
KeySchema=[
# Partition key
{"AttributeName": "instrument", "KeyType": "HASH"},
# Sort key
{"AttributeName": "time_utc", "KeyType": "RANGE"},
],
AttributeDefinitions=[
{"AttributeName": "instrument", "AttributeType": "S"},
{"AttributeName": "time_utc", "AttributeType": "S"},
],
BillingMode="PAY_PER_REQUEST",
)
yield {
"data_table": data_table,
}