Skip to content

Commit

Permalink
Setup basic integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StarDylan committed Dec 11, 2024
1 parent 098d67d commit af51921
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 46 deletions.
2 changes: 1 addition & 1 deletion integration-test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ RUN pip install --no-cache-dir -r requirements.txt
COPY . .

# Default command to run tests
CMD ["pytest", "-s"]
CMD ["pytest"]
21 changes: 12 additions & 9 deletions integration-test/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Integration Tests

## Env Vars
Integration tests for fEMR. This project will bring up the entire fEMR stack with docker then run tests against it.

```bash
docker run -it --rm -v $(pwd):/app -w /app femr-femr pandoc README.md -o README.md
```
Assumes that the femr docker container's name is `femr-femr`, otherwise you will need to re-build it with the new name.

## Run with Docker

## Running Tests
Ensure that the femr container is built with name `femr-femr`, before running the tests.

In the `integration-test` directory, run the tests with:

```bash
docker-compose -f docker-compose.test.yml up --build
docker compose up --build
```

## Running Tests with Coverage
## Build Image

In the root of the fEMR repository:

```bash
docker-compose -f docker-compose.test.yml -f docker-compose.coverage.yml up --build
docker compose build
```

31 changes: 14 additions & 17 deletions integration-test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from testcontainers.mysql import MySqlContainer
from testcontainers.selenium import BrowserWebDriverContainer
from selenium.webdriver import DesiredCapabilities
from testcontainers.core.container import DockerContainer
from testcontainers.core.network import Network
from testcontainers.core.waiting_utils import wait_for_logs
Expand All @@ -12,20 +14,6 @@
import os
import re


### Environment Variables




# def find_free_port():
# with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
# s.bind(('', 0))
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# return s.getsockname()[1]


# # my_sql_port = find_free_port()
client = docker.from_env()

try:
Expand All @@ -45,7 +33,7 @@

import pytest

@pytest.fixture(scope='function', autouse=True)
@pytest.fixture(scope='module', autouse=True)
def run_before_and_after_tests(request):
"""Fixture to execute asserts before and after a test is run"""

Expand All @@ -60,15 +48,18 @@ def cleanup():
with sql_container_spec as mysql:
network.connect(mysql._container.id, aliases=["db"])

femr_container_spec = DockerContainer(femr_image, network=network_name)\
femr_container_spec = DockerContainer(femr_image)\
.with_bind_ports("9000", "9000")\
.with_env("DB_URL", f'jdbc:mysql://db:3306/femr_db?characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true')\
.with_env("DB_USER", 'femr')\
.with_env("DB_PASS", 'password')\
.with_env("IS_DOCKER",'true')\
.with_env("IS_DOCKER",'true')



with femr_container_spec as femr_container:
network.connect(femr_container_spec._container.id, aliases=["femr"])

wait_for_logs(femr_container, re.compile(".*Listening for HTTP on.*", flags=re.DOTALL | re.MULTILINE).search)

print("Femr Started")
Expand All @@ -78,3 +69,9 @@ def cleanup():

yield


@pytest.fixture(scope='session', autouse=True)
def setup_selenium_container():
with BrowserWebDriverContainer(DesiredCapabilities.CHROME) as selenium_container:
os.environ["SELENIUM_ADDRESS"] = selenium_container.get_connection_url()
yield
3 changes: 2 additions & 1 deletion integration-test/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
testcontainers==4.9.0
pytest==8.3.4
pytest==8.3.4
selenium==4.27.1
47 changes: 47 additions & 0 deletions integration-test/tests/test_basic_functionality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import os
import requests


def test_femr_is_alive():
femr_address = os.getenv("FEMR_ADDRESS")

assert femr_address is not None, "FEMR_ADDRESS environment variable not set"

response = requests.get(femr_address)
assert response.status_code == 200

def test_can_login_and_logout_to_admin():
femr_address = os.getenv("FEMR_ADDRESS")

assert femr_address is not None, "FEMR_ADDRESS environment variable not set"

driver_address = os.getenv("SELENIUM_ADDRESS")

assert driver_address is not None, "SELENIUM_ADDRESS environment variable not set"

driver = webdriver.Remote(command_executor=driver_address, options=webdriver.ChromeOptions())

driver.get(f"{femr_address}/")

# Test Login
driver.set_window_size(1361, 1157)
driver.find_element(By.NAME, "email").click()
driver.find_element(By.NAME, "email").send_keys("admin")
driver.find_element(By.NAME, "password").send_keys("admin")
driver.find_element(By.CSS_SELECTOR, "input:nth-child(4)").click()
assert "Welcome to fEMR" in driver.find_element(By.ID, "home_index_h2_Welcome").text

# Test Logout
driver.find_element(By.CSS_SELECTOR, ".glyphicon-log-out").click()
assert driver.find_element(By.CSS_SELECTOR, "h1").text == "Please sign in"

18 changes: 0 additions & 18 deletions integration-test/tests/test_something.py

This file was deleted.

0 comments on commit af51921

Please sign in to comment.