Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add cypress tests to github actions #2285

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,14 @@ jobs:
uses: sonarsource/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}


cypress-run:
needs: npm-build
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Cypress run
uses: cypress-io/github-action@v6
1 change: 1 addition & 0 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,7 @@ def _create_edit_study_command(
# Can happen with data with only one column. In this case, we don't care about the delimiter.
delimiter = "\t"
df = pd.read_csv(io.BytesIO(data), delimiter=delimiter, header=None).replace(",", ".", regex=True)
df = df.dropna(axis=1, how="all") # We want to remove columns full of NaN at the import
matrix = df.to_numpy(dtype=np.float64)
matrix = matrix.reshape((1, 0)) if matrix.size == 0 else matrix
return ReplaceMatrix(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,7 @@ def get_file_content(self) -> OriginalFile:
else:
content = self.config.path.read_bytes()
return OriginalFile(content=content, suffix=suffix, filename=filename)

@override
def get_default_empty_matrix(self) -> t.Optional[npt.NDArray[np.float64]]:
return self.default_empty
26 changes: 22 additions & 4 deletions antarest/study/storage/rawstudy/model/filesystem/matrix/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from pathlib import Path
from typing import List, Optional, Union, cast

import numpy as np
import pandas as pd
from numpy import typing as npt
from typing_extensions import override

from antarest.core.model import JSON
Expand Down Expand Up @@ -127,16 +129,25 @@ def load(
formatted: bool = True,
) -> Union[bytes, JSON]:
file_path, tmp_dir = self._get_real_file_path()
if not formatted:
if file_path.exists():
return file_path.read_bytes()

if formatted:
return self.parse_as_json(file_path)

if not file_path.exists():
logger.warning(f"Missing file {self.config.path}")
if tmp_dir:
tmp_dir.cleanup()
return b""

return self.parse_as_json(file_path)
file_content = file_path.read_bytes()
if file_content != b"":
return file_content

# If the content is empty, we should return the default matrix to do the same as `parse_as_json()`
default_matrix = self.get_default_empty_matrix()
if default_matrix is None:
return b""
return default_matrix.tobytes()

@abstractmethod
def parse_as_json(self, file_path: Optional[Path] = None) -> JSON:
Expand All @@ -145,6 +156,13 @@ def parse_as_json(self, file_path: Optional[Path] = None) -> JSON:
"""
raise NotImplementedError()

@abstractmethod
def get_default_empty_matrix(self) -> Optional[npt.NDArray[np.float64]]:
"""
Returns the default matrix to return when the existing one is empty
"""
raise NotImplementedError()

@override
def dump(
self,
Expand Down
9 changes: 9 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "cypress";

export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
5 changes: 5 additions & 0 deletions cypress/e2e/spec.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('template spec', () => {
it('passes', () => {
cy.visit('https://example.cypress.io')
})
})
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
37 changes: 37 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference types="cypress" />
// ***********************************************
// This example commands.ts shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
//
// declare global {
// namespace Cypress {
// interface Chainable {
// login(email: string, password: string): Chainable<void>
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }
17 changes: 17 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ***********************************************************
// This example support/e2e.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'
8 changes: 8 additions & 0 deletions cypress/support/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"]
},
"include": ["**/*.ts"]
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"devDependencies": {
"@commitlint/cli": "^19.2.1",
"@commitlint/config-conventional": "^19.1.0"
},
"dependencies": {
"cypress": "^13.17.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,20 @@ def test_get_study_data(self, client: TestClient, user_access_token: str, intern
b"\xef\xbb\xbf1;1;1;1;1\r\n1;1;1;1;1",
b"1;1;1;1;1\r1;1;1;1;1",
b"0,000000;0,000000;0,000000;0,000000\n0,000000;0,000000;0,000000;0,000000",
b"1;2;3;;;\n4;5;6;;;\n",
],
["\t", "\t", ",", "\t", ";", ";", ";"],
["\t", "\t", ",", "\t", ";", ";", ";", ";"],
):
res = client.put(raw_url, params={"path": matrix_path}, files={"file": io.BytesIO(content)})
assert res.status_code == 204, res.json()
res = client.get(raw_url, params={"path": matrix_path})
written_data = res.json()["data"]
if not content.decode("utf-8"):
# For some reason the `GET` returns the default matrix when it's empty
# The `GET` returns the default matrix when it's empty
expected = 8760 * [[0]] if study_type == "raw" else [[]]
else:
df = pd.read_csv(io.BytesIO(content), delimiter=delimiter, header=None).replace(",", ".", regex=True)
df = df.dropna(axis=1, how="all") # We want to remove columns full of NaN at the import
expected = df.to_numpy(dtype=np.float64).tolist()
assert written_data == expected

Expand Down
Loading
Loading