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

hackathon #34

Merged
merged 1 commit into from
Dec 3, 2023
Merged
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
31 changes: 29 additions & 2 deletions bananalyzer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from typing import List
from urllib.parse import urlparse

import requests

from bananalyzer import AgentRunner
from bananalyzer.data.examples import (
download_examples,
Expand Down Expand Up @@ -291,10 +293,35 @@ def main() -> int:
tests = [generator.generate_test(example) for example in filtered_examples]

# Run the tests
return run_tests(
exit_code, report_path = run_tests(
tests, agent, args.pytest_args, args.headless, args.single_browser_instance
)

with open(report_path, "r") as fp:
res = requests.post(
"http://localhost:3000/api/rest/upload",
headers={
"Authorization": f"Bearer",
"Content-Type": "text/plain",
},
data=fp.read(),
)
res.raise_for_status()



return exit_code


if __name__ == "__main__":
main()
with open("/Users/awtkns/PycharmProjects/bananalyzer/.banana_cache/tmp81wthtxa_report.html", "r") as fp:
res = requests.post(
"http://localhost:3000/api/rest/upload",
headers={
"Authorization": f"Bearer",
"Content-Type": "application/text",
},
data=fp.read(),
)
res.raise_for_status()
# main()
6 changes: 3 additions & 3 deletions bananalyzer/runner/runner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import tempfile
from pathlib import Path
from typing import Awaitable, Callable, List
from typing import Awaitable, Callable, List, Tuple

import pytest
from pydantic import BaseModel
Expand Down Expand Up @@ -95,7 +95,7 @@ def run_tests(
pytest_args: PytestArgs,
headless: bool = False,
single_browser_instance: bool = False,
) -> int:
) -> Tuple[int, Path]:
"""
Create temporary test files based on intent, run them, and then delete them
"""
Expand Down Expand Up @@ -133,4 +133,4 @@ def run_tests(
+ [f"--junitxml={str(report_path)}"]
)

return pytest.main(args, plugins=[(BananalyzerPytestPlugin())])
return pytest.main(args, plugins=[(BananalyzerPytestPlugin())]), report_path
18 changes: 6 additions & 12 deletions next/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "sqlite"
// NOTE: When using mysql or sqlserver, uncomment the @db.Text annotations in model Account below
// Further reading:
// https://next-auth.js.org/adapters/prisma#create-the-prisma-schema
// https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#string
url = env("DATABASE_URL")
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}

model Post {
Expand All @@ -33,12 +27,12 @@ model Account {
type String
provider String
providerAccountId String
refresh_token String? // @db.Text
access_token String? // @db.Text
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? // @db.Text
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

Expand Down
48 changes: 21 additions & 27 deletions next/src/app/api/rest/upload/route.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
import { createTRPCContext } from "~/server/api/trpc";
import { appRouter } from "~/server/api/root";
import { NextResponse } from "next/server";
import { parse, type TestSuites } from 'junit2json'
import fs from 'fs/promises';
import { parse, type TestSuites } from "junit2json";
import { TestSuiteSchema } from "~/schemas";
import { headers } from "next/headers";

const PATH
= '/Users/awtkns/PycharmProjects/bananalyzer/.banana_cache/tmpha02dk07_report.html';
export async function POST(req: Request) {
const test_report = await req.text();

const handler = async (req: Request) => {
let userToken = headers().get("Authorization");

const xmlData = await fs.readFile(PATH, 'utf8');


// Create context and caller
const headers = Object.entries(req.headers).reduce((acc, [key, value]) => {
if (typeof value === "string") {
acc.append(key, value);
}
return acc;
}, new Headers());
if (!userToken) {
return NextResponse.json("Unauthorized", {
status: 403,
});
}
userToken = userToken.replace("Bearer ", "");

const ctx = await createTRPCContext({
headers: headers,
headers: headers(),
});

const caller = appRouter.createCaller(ctx);
const x = await parse(xmlData) as TestSuites;
const x = (await parse(test_report)) as TestSuites;

const suite = x?.testsuite?.[0];
if (!suite) {
return
return;
}

const testSuite = {
userId: userToken,
name: suite.name,
tests: suite.tests,
failures: suite.failures,
Expand All @@ -42,27 +39,24 @@ const handler = async (req: Request) => {
time: suite.time,
timestamp: suite.timestamp,
hostname: suite.hostname,
testCases: (suite.testcase ?? []).map(testcase => {
return ({
testCases: (suite.testcase ?? []).map((testcase) => {
return {
name: testcase.name,
classname: testcase.classname,
time: testcase.time,
status: "passed", // TODO: testcase.status,
// TODO: Fix this line properties: ((testcase?.properties ?? []) as { name: string, value: string }[])
properties: ((testcase?.properties ?? []) as { name: string, value: string }[])
});
})
}

const result = await TestSuiteSchema.safeParseAsync(testSuite);
if (!result.success) {
return NextResponse.json(result.error, {
status: 422
status: 422,
});

}

await caller.evaluations.create(result.data)
await caller.evaluations.create(result.data);
return NextResponse.json("ok");
};

export { handler as GET };
}
14 changes: 8 additions & 6 deletions next/src/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import {z} from "zod";
import { z } from "zod";

export const TestCaseSchema = z.object({
name: z.string(),
classname: z.string().optional(),
time: z.number(),
status: z.enum(["passed", "failed", "skipped"]),
message: z.string().optional(),
properties: z.array(z.object({
name: z.string(),
value: z.string()
}))
properties: z.array(
z.object({
name: z.string(),
value: z.string(),
}),
),
});

export const TestSuiteSchema = z.object({
userId: z.string().cuid(),
name: z.string(),
testCases: z.array(TestCaseSchema),
tests: z.number().int().gte(0),
Expand All @@ -23,6 +26,5 @@ export const TestSuiteSchema = z.object({
hostname: z.string(),
});


export type TestCase = z.infer<typeof TestCaseSchema>;
export type TestSuite = z.infer<typeof TestSuiteSchema>;
28 changes: 13 additions & 15 deletions next/src/server/api/routers/evaluations.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {z} from "zod";

import {createTRPCRouter, publicProcedure,} from "~/server/api/trpc";
import {TestSuiteSchema} from "~/schemas";

import { z } from "zod";

import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { TestSuiteSchema } from "~/schemas";

export const evalRouter = createTRPCRouter({
create: publicProcedure
Expand All @@ -18,9 +16,9 @@ export const evalRouter = createTRPCRouter({
errors: input.errors,
time: input.time,
hostname: input.hostname,
createdById: "clpofluzg0000m7vjkmz7dxvv",
}
})
createdById: input.userId,
},
});

for (const test of input.testCases) {
const testCase = await ctx.db.testCase.create({
Expand All @@ -31,20 +29,20 @@ export const evalRouter = createTRPCRouter({
time: test.time,
status: test.status,
message: test.message,
}
})
},
});

for (const property of test.properties) {
await ctx.db.testCaseProperty.create({
data: {
testCaseId: testCase.id,
name: property.name,
value: property.value,
}
})
},
});
}
}


// return suite
})});
// return suite
}),
});
Loading
Loading