2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-17 20:55:26 +00:00

Merge pull request #135 from inventree/unit-testing

Unit testing

(cherry picked from commit d55f594342)
This commit is contained in:
Oliver
2022-05-22 10:11:36 +10:00
committed by Oliver Walters
parent 10b435f4fa
commit cfc9f09b80
35 changed files with 893 additions and 317 deletions

141
test/api_test.dart Normal file
View File

@ -0,0 +1,141 @@
/*
* Unit tests for the InvenTree API code
*/
import "package:test/test.dart";
import "package:inventree/api.dart";
import "package:inventree/user_profile.dart";
void main() {
setUp(() async {
if (! await UserProfileDBManager().profileNameExists("Test Profile")) {
// Create and select a profile to user
print("TEST: Creating profile for user 'testuser'");
await UserProfileDBManager().addProfile(UserProfile(
name: "Test Profile",
server: "http://localhost:12345",
username: "testuser",
password: "testpassword",
selected: true,
));
}
var prf = await UserProfileDBManager().getSelectedProfile();
// Ensure that the server settings are correct by default,
// as they can get overwritten by subsequent tests
if (prf != null) {
prf.name = "Test Profile";
prf.server = "http://localhost:12345";
prf.username = "testuser";
prf.password = "testpassword";
await UserProfileDBManager().updateProfile(prf);
}
// Ensure the profile is selected
assert(! await UserProfileDBManager().selectProfileByName("Missing Profile"));
assert(await UserProfileDBManager().selectProfileByName("Test Profile"));
});
group("Login Tests:", () {
test("Disconnected", () async {
// Test that calling disconnect() does the right thing
var api = InvenTreeAPI();
api.disconnectFromServer();
// Check expected values
expect(api.isConnected(), equals(false));
expect(api.isConnecting(), equals(false));
expect(api.hasToken, equals(false));
});
test("Login Failure", () async {
// Tests for various types of login failures
var api = InvenTreeAPI();
// Incorrect server address
var profile = await UserProfileDBManager().getSelectedProfile();
assert(profile != null);
if (profile != null) {
profile.server = "http://localhost:5555";
await UserProfileDBManager().updateProfile(profile);
bool result = await api.connectToServer();
assert(!result);
// TODO: Test the the right 'error message' is returned
// TODO: The request above should throw a 'SockeException'
// Test incorrect login details
profile.server = "http://localhost:12345";
profile.username = "invalidusername";
await UserProfileDBManager().updateProfile(profile);
await api.connectToServer();
assert(!result);
// TODO: Test that the connection attempt above throws an authentication error
assert(!api.checkConnection());
} else {
assert(false);
}
});
test("Login Success", () async {
// Test that we can login to the server successfully
var api = InvenTreeAPI();
// Attempt to connect
final bool result = await api.connectToServer();
// Check expected values
assert(result);
assert(api.hasToken);
expect(api.baseUrl, equals("http://localhost:12345/"));
assert(api.isConnected());
assert(!api.isConnecting());
assert(api.checkConnection());
});
test("Version Checks", () async {
// Test server version information
var api = InvenTreeAPI();
assert(await api.connectToServer());
// Check supported functions
assert(api.apiVersion >= 50);
assert(api.supportsSettings);
assert(api.supportsNotifications);
assert(api.supportsModernStockTransactions);
assert(api.supportsPoReceive);
// Check available permissions
assert(api.checkPermission("part", "change"));
assert(api.checkPermission("stocklocation", "delete"));
assert(api.checkPermission("part", "weirdpermission"));
assert(api.checkPermission("blah", "bloo"));
});
});
}

View File

@ -0,0 +1,30 @@
/*
* Unit tests for the preferences manager
*/
import "package:test/test.dart";
import "package:inventree/preferences.dart";
void main() {
setUp(() async {
});
group("Settings Tests:", () {
test("Default Values", () async {
// Boolean values
expect(await InvenTreeSettingsManager().getBool("test", false), equals(false));
expect(await InvenTreeSettingsManager().getBool("test", true), equals(true));
// String values
expect(await InvenTreeSettingsManager().getValue("test", "x"), equals("x"));
});
test("Set value", () async {
await InvenTreeSettingsManager().setValue("abc", "xyz");
expect(await InvenTreeSettingsManager().getValue("abc", "123"), equals("xyz"));
});
});
}

125
test/user_profile_test.dart Normal file
View File

@ -0,0 +1,125 @@
/*
* Unit tests for the API class
*/
import "package:test/test.dart";
import "package:inventree/user_profile.dart";
void main() {
setUp(() async {
// Ensure we have a user profile available
// This profile will match the dockerized InvenTree setup, running locally
// To start with, there should not be *any* profiles available
var profiles = await UserProfileDBManager().getAllProfiles();
for (var prf in profiles) {
await UserProfileDBManager().deleteProfile(prf);
}
// Check that there are *no* profiles in the database
profiles = await UserProfileDBManager().getAllProfiles();
expect(profiles.length, equals(0));
// Now, create one!
bool result = await UserProfileDBManager().addProfile(UserProfile(
name: "Test Profile",
username: "testuser",
password: "testpassword""",
server: "http://localhost:12345",
selected: true,
));
expect(result, equals(true));
// Ensure we have one profile available
// expect(profiles.length, equals(1));
profiles = await UserProfileDBManager().getAllProfiles();
expect(profiles.length, equals(1));
int key = -1;
// Find the first available profile
for (var p in profiles) {
if (p.key != null) {
key = p.key ?? key;
break;
}
}
// Select the profile
await UserProfileDBManager().selectProfile(key);
});
// Run a set of tests for user profile functionality
group("Profile Tests:", () {
test("Add Invalid Profiles", () async {
// Add a profile with missing data
bool result = await UserProfileDBManager().addProfile(
UserProfile(
username: "what",
password: "why",
)
);
expect(result, equals(false));
// Add a profile with a name that already exists
result = await UserProfileDBManager().addProfile(
UserProfile(
name: "Test Profile",
username: "xyz",
password: "hunter42",
)
);
expect(result, equals(false));
// Check that the number of protocols available is still the same
var profiles = await UserProfileDBManager().getAllProfiles();
expect(profiles.length, equals(1));
});
test("Profile Name Check", () async {
bool result = await UserProfileDBManager().profileNameExists("doesnotexist");
expect(result, equals(false));
result = await UserProfileDBManager().profileNameExists("Test Profile");
expect(result, equals(true));
});
test("Select Profile", () async {
// Ensure that we can select a user profile
final prf = await UserProfileDBManager().getSelectedProfile();
expect(prf, isNot(null));
if (prf != null) {
UserProfile p = prf;
expect(p.name, equals("Test Profile"));
expect(p.username, equals("testuser"));
expect(p.password, equals("testpassword"));
expect(p.server, equals("http://localhost:12345"));
expect(p.toString(), equals("<${p.key}> Test Profile : http://localhost:12345 - testuser:testpassword"));
// Test that we can update the profile
p.name = "different name";
bool result = await UserProfileDBManager().updateProfile(p);
expect(result, equals(true));
// Trying to update with an invalid value will fail!
p.password = "";
result = await UserProfileDBManager().updateProfile(p);
expect(result, equals(false));
}
});
});
}

View File

@ -5,9 +5,9 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import "package:flutter_test/flutter_test.dart";
// import "package:flutter_test/flutter_test.dart";
void main() {
testWidgets("Counter increments smoke test", (WidgetTester tester) async {
});
// testWidgets("Counter increments smoke test", (WidgetTester tester) async {
// });
}