FlutterFlow / flutterflow-issues

A community issue tracker for FlutterFlow.
113 stars 19 forks source link

Custom functions behave differently between UI testing and functional testing #2371

Open LuckyKoni opened 6 months ago

LuckyKoni commented 6 months ago

Has your issue been reported?

Current Behavior

I created a custom function to check if the requested booking period is conflicting with any existing bookings of the product. Here is the code that is really doing the checking

List<ProductscopyRecord> output = productsFiltered.where((p) {
    bool hasBooking = queryBookingRecords.any((b) =>
        startDate.isBefore(b.bookingEndDatetime!) &&
        endDate.isAfter(b.bookingStartDatetime!));

    return !hasBooking;
  }).toList();

return output.length.toString();

I use the same input data for Function test, the function works as expected. The output length is 0, since the requested booking conflicts with an existing booking.

But output length is displaying as 1 during the UI testing.

image

image

Expected Behavior

I expect the result of Function testing and UI testing are the same. In this example, since there is a conflict, both should show 0

Steps to Reproduce

  1. Create a new page
  2. Use the ItemMapViewCopy widget in my project
  3. Check the getProductsTest function
  4. Create 2 dummy records in productscopy document, one product listing record, one booking record, listing and booking are differentiated by the "isBooking" field
  5. Here are the input required for the getProductsTest function, 1. productscopy document you created, a location, distance as double input, startDate, endDate

Reproducible from Blank

Bug Report Code (Required)

ITFXl8rqw5ZOm8RE1ZDIaflaqDwwGUALa+dI0tZ+TQ0jF5D4OooiYsjSRElvTcyzaH5mGFal8WodpfzOkvPfF/cVNkydRrV8zpFQVhPPbECueJTSDKuSZXYkI+dVGUDFyLbUkx1vAPhpcXg47Ga1fufJcz6Cf9qOYwx5e6fDbOY=

Context

I created a custom function to check if the requested booking period is conflicting with any existing bookings of the product.

Visual documentation

image

image

image

image

Additional Info

No response

Environment

- FlutterFlow version: 3.13.7
- Platform: Web
- Browser name and version:Chrome (121.0.6167.139 (Official Build) (arm64))
- Operating system and version affected: macOS 14.3 (23D56)
hariprasadms commented 6 months ago

Hi @LuckyKoni - Thank you for submitting the issue. Given the nature of the issue and its association with the involved data, it appears to be more project-specific, which may require additional time for thorough investigation on our end. To expedite the resolution process, I recommend creating a sample function with mock data (utilizing app state to store the data) to replicate the issue. Share the details with us. it will greatly help us in swiftly investigating and resolving the issue.

LuckyKoni commented 6 months ago

Thanks @hariprasadms I have just created a minimal project to demo my problem. The Bug Report Code: IT4slsmAsI1Nobhe+KryKPl7/SE4H202bY08scp/bxIgGIDzPOw+ZPWldhdoOemgTGxUI0SmmnsEw/DrjuPuUsI5OTmAbqZzws99FzrKIUC5QZOBDJC7OEFSGfhbJlDH1sKK2iNvLs1YLXdh7TuYeuetEzTAJLKzImdISq/LZO4=

Function code

import 'dart:convert';
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

String? getProductsTest(
  List<ProductscopyRecord> products,
  DateTime startDate,
  DateTime endDate,
) {
  /// MODIFY CODE ONLY BELOW THIS LINE

  List<ProductscopyRecord> productsPost = products.where((product) {
    return !product.isBooking;
  }).toList();

  List<ProductscopyRecord> queryBookingRecords = products.where((product) {
    return product.isBooking &&
        productsPost.map((p) => p.pid).contains(product.pid);
  }).toList();

  List<ProductscopyRecord> output = productsPost.where((p) {
    bool hasBooking = queryBookingRecords.any((b) =>
        startDate.isBefore(b.bookingEndDatetime!) &&
        endDate.isAfter(b.bookingStartDatetime!));

    return !hasBooking;
  }).toList();

  return output.length.toString();

  /// MODIFY CODE ONLY ABOVE THIS LINE
}

Test function output: 0 image

Test function input data: image

image

UI test output: 1 (Which is unexpected and inconsistent with function test) image

The app state used: image

Please let me know if you need more info.

LuckyKoni commented 6 months ago

And also here are the Document: image image

hariprasadms commented 6 months ago

Hey @LuckyKoni - Thanks for the details. That is super helpful to investigate the issue. I just looked at test run and see I get result as '1'. Is that what you are expecting ?

image
LuckyKoni commented 6 months ago

@hariprasadms I just update my screenshots in my previous comments to make it more clear. So the expectation is to the UI shows "0" instead of 1.

To give you more context, the first record "isBooking" means there is a booking from 10 March 2024 to 12 March 2024. The second record is a listing record, listing the product from 1 March 2024 to 1 April 2024. The request period of the new booking in this example is from 8th March 2024 to 15 March 2024, which conflicts with the existing booking, thus the number of available product is expected to be 0. The Test function gives me the expected result 0, but the UI text using the function and app state gives me a 1.

hariprasadms commented 6 months ago

Hi @LuckyKoni - Before delving into the intricacies of function code logic, I'd like to point out that during the function test run, the result displayed at my end is '1', which is consistent with what is shown on the UI. In light of this, it seems that the issue may be related to your logic implementation rather than a flaw in the function test run feature itself. Would you like to taking another thorough look at the logic behind function implementation?

hariprasadms commented 6 months ago

To add more info, the result of function run and UI is consistent across real devices and debug run mode. Have a look at the logic please.

LuckyKoni commented 6 months ago

Hi @hariprasadms, the function test result is "0" please see my screenshot. Can you please share your screenshot and the data used for UI and Function testing. I don't believe there is anything wrong with the code logic - if it is wrong, i should get the same wrong result on UI and function test. image

LuckyKoni commented 6 months ago

Hey @LuckyKoni - Thanks for the details. That is super helpful to investigate the issue. I just looked at test run and see I get result as '1'. Is that what you are expecting ?

image

Hi @hariprasadms In your test, can you please change the start date to 3/8/2024 instead of the current time, end date to 3/15/2024 instead of the current time. Then apply 3/8/2024 and 3/15/2024 as the start date and end date in App state for UI testing. This is when I got inconsistent result.

LuckyKoni commented 6 months ago

Hi @hariprasadms, did you manage to reproduce the issue?

hariprasadms commented 6 months ago

Hi @LuckyKoni - Sorry for the delayed response. Are you still experiencing the issue, or have there been any changes with the latest FlutterFlow updates?

LuckyKoni commented 6 months ago

Hi @hariprasadms. I just test again and the issue persists. Please help.

hariprasadms commented 6 months ago

I'm looking at it now. Have you made any changed to function recently?

LuckyKoni commented 6 months ago

@hariprasadms No recent change on this function. The project has the minimum setup with the collection, function, UI and app state to reproduce the issue.

hariprasadms commented 6 months ago

With the above dates you mentioned, the function returns '0'. Is that correct as per latest date in the list ?

hariprasadms commented 6 months ago

When I login to app I get below error. Is this affecting the function result ? or should I look into test app you setup earlier? the code below.

IT4slsmAsI1Nobhe+KryKPl7/SE4H202bY08scp/bxIgGIDzPOw+ZPWldhdoOemgTGxUI0SmmnsEw/DrjuPuUsI5OTmAbqZzws99FzrKIUC5QZOBDJC7OEFSGfhbJlDH1sKK2iNvLs1YLXdh7TuYeuetEzTAJLKzImdISq/LZO4=

image image
LuckyKoni commented 6 months ago

The expected return is 0. Test function return 0. The UI return 1.

image image

LuckyKoni commented 6 months ago

Can you please use this project report code instead. As your previous requested, this project has the minimum setup. IT4slsmAsI1Nobhe+KryKPl7/SE4H202bY08scp/bxIgGIDzPOw+ZPWldhdoOemgTGxUI0SmmlIEpcmKid/1LcIDHE2aG5hFzZFUTw6WQnORQZOBDJC7OEFSGfhbJlDH1sKK2iNvLs1YLXdh7TuYeuetEzTAJLKzImdISq/LZO4=

hariprasadms commented 6 months ago

I checked the downloaded code and went through how the code runs. I noticed that the downloaded code and the test run mode show the same result when it's '1'. However, there's a different result for the same code in the function test, the result is 0.

image
hariprasadms commented 6 months ago

I guess it can be issue with date stored in the app state. Can I ask you to store the date in string formate and convert it before use. Try this please.

LuckyKoni commented 6 months ago

@hariprasadms @takashiff Thanks for confirming the bug. Can you please let me know the ETA of this bug fix?

LuckyKoni commented 6 months ago

Hi @agreaves can you please advise on the bug fix update?

LuckyKoni commented 5 months ago

Hi team @agreaves @hariprasadms @takashiff any update on fixing this bug?

hariprasadms commented 5 months ago

Hi @LuckyKoni - Its with dev team now. It will be worked on based on current priority. We will update the status once it is ready. Thanks