def test_success(self, mock_course_presistence): course_api = CourseApi(mock_course_presistence) course = fake_course() mock_course_presistence.create_course = MagicMock( return_value=Ok(course)) assert course_api.create_course(course.course_code).unwrap() == course mock_course_presistence.create_course.assert_called_once_with(course)
def test_get_section(mock_course_presistence, expected): course_api = CourseApi(mock_course_presistence) expected_identity = expected.map_or(lambda section: section.identity, None) mock_course_presistence.get_section = MagicMock(return_value=expected) assert course_api.get_section(expected_identity) == expected mock_course_presistence.get_section.assert_called_once_with( expected_identity)
def test_fail(self, mock_course_presistence): course_api = CourseApi(mock_course_presistence) course = fake_course() err = Err(fake.pystr()) mock_course_presistence.create_course = MagicMock(return_value=err) assert course_api.create_course(course.course_code) == err mock_course_presistence.create_course.assert_called_once_with(course)
def test_get_course(mock_course_persistence, mock_meeting_persistence, expected): course_api = CourseApi(mock_course_persistence, mock_meeting_persistence) mock_course_persistence.get_course = MagicMock(return_value=expected) course_code = expected.map_or(lambda c: c.course_code, None) assert course_api.get_course(course_code) == expected mock_course_persistence.get_course.assert_called_once_with(course_code)
def test_fail(self, mock_course_presistence): course_api = CourseApi(mock_course_presistence) section = fake_section() err = Err(fake.pystr()) mock_course_presistence.create_section = MagicMock(return_value=err) assert (course_api.create_section( section.course, section.year, section.semester, section.section_code, section.taught_by, section.num_students, ) == err) mock_course_presistence.create_section.assert_called_once_with(section)
def test_success(self, mock_course_presistence): course_api = CourseApi(mock_course_presistence) section = fake_section() mock_course_presistence.create_section = MagicMock( return_value=Ok(section)) assert (course_api.create_section( section.course, section.year, section.semester, section.section_code, section.taught_by, section.num_students, ).unwrap() == section) mock_course_presistence.create_section.assert_called_once_with(section)
def test_query_sections(mock_course_persistence, mock_meeting_persistence, filters, expected): course_api = CourseApi(mock_course_persistence, mock_meeting_persistence) def assert_called_correctly(_filters): for key, val in _filters.items(): if val is None: assert key not in filters else: assert val == filters[key] return expected mock_course_persistence.query_sections.side_effect = assert_called_correctly assert course_api.query_sections(**filters) == expected mock_course_persistence.query_sections.assert_called_once()
def make_app(cls, name, secret, restrictions, user_type): flask_app = Flask(name) CORS(flask_app) gql_controller = GraphqlController(build_schema(restrictions)) course_persistence = CoursePersistence(get_connection) instructor_persistence = InstructorPersistence(get_connection) student_persistence = StudentPersistence(get_connection) meeting_persistence = MeetingPersistence(get_connection) token_auth = JwtAuthenticator(secret) if user_type == Instructor: password_auth = PasswordAuthenticator(instructor_persistence) else: password_auth = PasswordAuthenticator(student_persistence) ohs_api = OhsApi( course_api=CourseApi(course_persistence, meeting_persistence), instructor_api=InstructorApi(instructor_persistence, password_auth, token_auth), student_api=StudentApi(student_persistence, password_auth, token_auth), meeting_api=MeetingApi(meeting_persistence), ) connection_manager = ConnectionManager( 1, 100, { "host": os.getenv("OHS_DB_HOST"), "dbname": os.getenv("OHS_DB_NAME"), "user": os.getenv("OHS_DB_USER"), "password": os.getenv("OHS_DB_PASSWORD"), }, ) return cls(flask_app, gql_controller, ohs_api, connection_manager)
def test_query_sections(mock_course_presistence, filters, expected): course_api = CourseApi(mock_course_presistence) mock_course_presistence.query_sections = MagicMock(return_value=expected) assert course_api.query_sections(filters) == expected mock_course_presistence.query_sections.assert_called_once_with(filters)
class InstructorService(App[Context]): def __init__(self, flask_app_, graphql_controller, api): self.api = api super().__init__(flask_app_, graphql_controller) def create_context(self, request) -> Context: return Context(self.api, self.authenticate_instructor(request).unwrap()) def authenticate_instructor(self, request) -> Result[Instructor, str]: # TODO: implement authentication return Ok(None) flask_app = Flask("Instructor Service") CORS(flask_app) gql_controller = GraphqlController( build_schema([SchemaRestriction.ALL, SchemaRestriction.INSTRUCTOR])) course_presistence = CoursePresistence(lambda: flask.g.connection) ohs_instructor_api = OhsApi(CourseApi(course_presistence), InstructorApi()) app = InstructorService(flask_app, gql_controller, ohs_instructor_api) if __name__ == "__main__": try: port = int(sys.argv[1]) except IndexError: port = 8000 flask_app.run(debug=True, port=port)