Skip to content

Commit

Permalink
Cleaner Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FlashOnFire committed Oct 20, 2023
1 parent f4e62b7 commit 97c532a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:equatable/equatable.dart';

class Student extends Equatable {
final String name;
final String id;
final int id;

Student(this.name, this.id);

Expand All @@ -14,14 +14,14 @@ class Student extends Equatable {
}

class StudentColloscope extends Equatable {
final int studentId;
final Student student;
final int trinomeId;
final List kholles;

StudentColloscope(this.studentId, this.trinomeId, this.kholles);
StudentColloscope(this.student, this.trinomeId, this.kholles);

@override
List<Object?> get props => [studentId, trinomeId, kholles];
List<Object?> get props => [student, trinomeId, kholles];

@override
bool? get stringify => true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:beautiful_soup_dart/beautiful_soup.dart';
import 'package:collection/collection.dart';
import 'package:html/dom.dart';
import 'package:requests_plus/requests_plus.dart';

Expand Down Expand Up @@ -30,27 +31,42 @@ class PolytechColloscopeClient {
var link = e.getAttrValue("href");
var id = RegExp(r"id_etudiant=(\d+)").firstMatch(link!)?.group(1);

students.add(Student(name, id!));
students.add(Student(name, int.parse(id!)));
});

return students;
}

Future<StudentColloscope> getColloscope(Year year, int studentId) async {
Future<Student?> fetchStudent(Year year, String name, String surname) async {
if (name.isEmpty || surname.isEmpty) {
return null;
}

String match =
"${name.substring(0, 1).toUpperCase()}. ${surname.toUpperCase()}";

var students = await fetchStudents(year);
return students.firstWhereOrNull((s) => s.name == match);
}

Future<StudentColloscope> getColloscope(Year year, Student student) async {
// TODO : Add a check to verify the studentid (look 5 lines below)

var page = await RequestsPlus.get(
Consts.khollesStudentURL[year]!
.replaceFirst(":id", studentId.toString()),
.replaceFirst(":id", student.id.toString()),
userName: username,
password: password);

BeautifulSoup bs = BeautifulSoup(page.body);

// We can use this to check if the studentid is valid
var trinomeStr = RegExp(r"trinôme (\d+)")
.firstMatch(bs.find("h3.colles")!.innerHtml)
?.group(1);
var header = bs.find("h3.colles");
if (header == null) {
throw StateError("Invalid student id");
}

var trinomeStr = RegExp(r"trinôme (\d+)").firstMatch(header.innerHtml)?.group(1);

var trinome = int.parse(trinomeStr!);

Expand All @@ -62,7 +78,7 @@ class PolytechColloscopeClient {
kholles.add(parseKholle(e));
});

return StudentColloscope(studentId, trinome, kholles);
return StudentColloscope(student, trinome, kholles);
}

static Kholle parseKholle(Bs4Element e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,75 @@ import 'package:test/test.dart';

Future<void> main() async {
DotEnv env = DotEnv(includePlatformEnvironment: true);
late PolytechColloscopeClient client;

env.load();
final String username = env['USERNAME'] ?? "";
final String password = env['PASSWORD'] ?? "";
late final int id;

if (username.isEmpty || password.isEmpty) {
fail("username or password were empty. check your envt variables");
}
late final String name;
late final String surname;

PolytechColloscopeClient client =
PolytechColloscopeClient(username, password);
setUpAll(() {
env.load();
final String username = env['USERNAME'] ?? "";
final String password = env['PASSWORD'] ?? "";

var students = await client.fetchStudents(Year.first);
print(students);
id = int.parse(env['ID'] ?? "0");

var colloscope = await client.getColloscope(Year.first, 828);
print(colloscope);
name = env['NAME'] ?? "";
surname = env['SURNAME'] ?? "";

if (username.isEmpty || password.isEmpty) {
fail("username or password were empty. check your envt variables");
}

client = PolytechColloscopeClient(username, password);
});

test('Fetch all Students IDs (first year)',
() async => {expect(await client.fetchStudents(Year.first), isNotEmpty)});

test(
'Fetch all Students IDs (second year)',
() async =>
{expect(await client.fetchStudents(Year.second), isNotEmpty)});

test(
'Get Colloscope wrong ID',
() async => {
expect(client.getColloscope(Year.second, Student("", 651635)),
throwsStateError)
});

test(
'Get Colloscope',
() async => {
expect(await client.getColloscope(Year.second, Student("", id)),
isNotNull)
});

test('Get Colloscope', () async {
final StudentColloscope colloscope =
await client.getColloscope(Year.second, Student("", id));
expect(colloscope, isNotNull);
expect(colloscope.student, isNotNull);
expect(colloscope.trinomeId, isNotNull);
expect(colloscope.kholles, isNotEmpty);
});

test(
"Fetch a student",
() async => {
expect(await client.fetchStudent(Year.second, name, surname),
isNotNull)
});

test('Fetch a student', () async {
final StudentColloscope colloscope =
await client.getColloscope(Year.second, Student("", id));
expect(colloscope, isNotNull);
expect(colloscope.student, isNotNull);
expect(colloscope.student.id, isNotNull);
expect(colloscope.trinomeId, isNotNull);
expect(colloscope.kholles, isNotEmpty);
});
}

0 comments on commit 97c532a

Please sign in to comment.