Error running simulation — An unknown error occurred - google-cloud-firestore

{
"__name__":"/databases/(default)/documents/users/mfBBtfSX91YS9mXrkmEDTksA0712/sold/mfBBtfSX91YS9mXrkmEDTksA0712-hanSW0s5MxOOENWnc5T1cLinekj2/invoice/WhdEi2W83GtjDjowi6MV",
"id":"WhdEi2W83GtjDjowi6MV",
"data":{
"id":"db1a01a0-4da5-11ec-a579-c31f34aa6119",
"total":80,
"quotations":[
{
"id":"c595e610-46ab-11ec-b8ad-d56159e1778c",
"title":"anda",
"measuremntUnit":"kg",
"rate":35,
"quantity":2,
"isSelected":false,
"index":1
},
{
"id":"c595e610-46ab-11ec-b8ad-d56159e1778c",
"title":"biscuit",
"measuremntUnit":"packet",
"rate":10,
"quantity":1,
"isSelected":true,
"index":7}
],
"sellerEmail":"ku1438693#gmail.com",
"sellerUserId":"mfBBtfSX91YS9mXrkmEDTksA0712",
"sellerDisplayName":" Lila Kunwar",
"buyerUserId":"hanSW0s5MxOOENWnc5T1cLinekj2",
"buyerDisplayName":"saugat thapa",
"buyerEmail":"nexus.saugat26#gmail.com",
"sellerPhotoUrl":"https://lh3.googleusercontent.com/a/AATXAJ",
"buyerPhotUrl":"https://lh3.googleusercontent.com/a/AATXAJ"
}}
i am trying to check whether above data can insert into firestore given that the following is the security rule. however i get error Error running simulation — An unknown error occurred.
match /users/{userId}{
match /sold/{soldId}/{document=**}{
function isAuthed(){
return request.auth != null && request.auth.uid == userId;
}
function isSeller(){
return request.auth.uid == resource.data.sellerUserId ||
request.auth.uid == resource.data.buyerUserId;
}
function isApproved() {
return request.resource.data.isApproved == false;
// || request.resource.data.reviewed == "false"
}
function quotations(){
return request.resource.data.quotations;
}
function isOtherThanQuotations(){
return
isValidStringInput(request.resource.data.buyerUserId) &&
isValidStringInput(request.resource.data.buyerEmail) &&
isValidStringInput(request.resource.data.buyerPhotoUrl) &&
isValidStringInput(request.resource.data.buyerDisplayName) &&
isValidStringInput(request.resource.data.sellerUserId)&&
isValidStringInput(request.resource.data.buyerEmail) &&
isValidStringInput(request.resource.data.buyerPhotoUrl) &&
isValidStringInput(request.resource.data.buyerDisplayName) &&
isValidNumber(request.resource.data.total) &&
// isApproved() &&
request.resource.data.createdAt == request.time;
}
function isSelected() {
return request.resource.data.isSelected == false;
// || request.resource.data.reviewed == "false"
}
function isValidQuotation(item) {
return quotations()[item].keys().hasAll(
[
'title',
'rate',
'quantity',
'measuremntUnit',
'isSelected',
'index',
])
&& isValidStringInput(quotations()[item].title, 200)
&& isValidNumber(quotations()[item].rate)
&& isValidNumber(quotations()[item].quantity)
&& isValidStringInput(quotations()[item].measuremntUnit, 200)
&& isValidNumber(quotations()[item].index)
&& isSelected();
}
function isValidStringInput(input, maxSize) {
return input is string
&& input.size() > 0
&& input.size() <= maxSize;
}
function isValidNumber(input) {
return input is int
|| input.matches('^[0-9]+$');
}
allow read: if isSeller();
allow write: if isAuthed()
&& quotations().size() >= 1
&& quotations().size() <= 10
&& isOtherThanQuotations()
&& isValidQuotation(0)
&& (quotations().size() < 2 || isValidquotation(1) )
&& (quotations().size() < 3 || isValidquotation(2) )
&& (quotations().size() < 4 || isValidquotation(3) )
&& (quotations().size() < 5 || isValidquotation(4) )
&& (quotations().size() < 6 || isValidquotation(5) )
&& (quotations().size() < 7 || isValidquotation(6) )
&& (quotations().size() < 8 || isValidquotation(7) )
&& (quotations().size() < 9 || isValidquotation(8) )
&& (quotations().size() < 10 || isValidquotation(9) )
&& (quotations().size() < 11 || isValidquotation(10) );
}
}
is it becase i have too too many functions? right now myconcern is with create only.
is it becasue the id doesnt have security rule to validate? i tried providing validation for id as well but the error still persist. i need more than 10 map in an array but this is for simplicity of testing process.

Related

How to test if a function is called using open/wc testing with sinon?

I am new to web components, and am using LIT framework. I have been breaking my head trying to figure this out.
This is my function.
createObj() {
this.financingObj = [];
if (this.dropDownValue === common.ONCE) {
if (this.ammountEntered >= 2000 && this.ammountEntered <= 999999) {
this.financingObj.push(this.createFinancingStructure('BusinessLoan'));
}
if (this.ammountEntered >= 10000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(this.createFinancingStructure('Leasing'));
}
if (this.ammountEntered >= 250000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(this.createFinancingStructure('InvestmentLoan'));
}
if (this.ammountEntered >= 2500000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj = this.financingObj.filter(
item => item !== this.createFinancingStructure('BusinessLoan'),
);
this.financingObj.push(this.createFinancingStructure('RollOverTermLoan'));
}
} else if (this.dropDownValue === common.MULTIPLE_TIMES) {
if (this.ammountEntered >= 250000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(this.createFinancingStructure('InvestmentLoan'));
}
if (this.ammountEntered >= 2500000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(
this.createFinancingStructure('RollOverTermLoan'),
this.createFinancingStructure('RollOverRevolving'),
);
}
}
}
And this is my test case
it('should call createObjForInvest when invest product is selected', () => {
const spy = sinon.spy(element, 'createObj');
spy();
sinon.assert.called(spy);
});
I see the function gets called, but I am getting the following error.

My logic for seeing if a user has won a tic-tac-toe match isn't working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Below is some code to check if somebody has won on a tic tac toe board. (board[0] - board[8] represent the tic tac toe board from top to bottom, left to right):
func checkWin(board: [Int]) -> Bool{
if board[0] != 0 {
if board[0] == board[1] && board[1] == board[2] {
return true
} else if board[0] == board[3] && board[3] == board[6] {
return true
}
} else if board[4] != 0 {
if board[1] == board[4] && board[4] == board[7] {
return true
} else if board[3] == board[4] && board[4] == board[5] {
return true
} else if board[2] == board[4] && board[4] == board[6] {
return true
} else if board[0] == board[4] && board[4] == board[8] { //
return true
}
} else if board[8] != 0 {
if board[2] == board[5] && board[5] == board[8] { //
return true
} else if board[6] == board[7] && board[7] == board[8] { //
return true
}
}
return false
}
However, the three lines with // at the end will not return true even if the conditions are met. I have noticed that they all share board[8], however I know this is not a problem with the storyboard, as if I hard code a win that satisfies one of faulty conditions it still doesn't work. Can anyone see what's going wrong?
Your logic is flawed. Once you check with the outer ifs, you have committed to just checking a few of the possible cases.
For instance, if space 0 is not empty, then you are only checking cases 0-1-2 and 0-3-6, but you aren't checking 0-4-8 so you'll miss that possibility. The 0-4-8 case is handled inside the first else if case, but you'll never get there if board[0] != 0.
You can fix this by using 3 ifs instead of the else ifs.
func checkWin(board: [Int]) -> Bool{
print(board)
if board[0] != 0 {
if board[0] == board[1] && board[1] == board[2] {
return true
} else if board[0] == board[3] && board[3] == board[6] {
return true
}
}
if board[4] != 0 {
if board[1] == board[4] && board[4] == board[7] {
return true
} else if board[3] == board[4] && board[4] == board[5] {
return true
} else if board[2] == board[4] && board[4] == board[6] {
return true
} else if board[0] == board[4] && board[4] == board[8] { //
return true
}
}
if board[8] != 0 {
if board[2] == board[5] && board[5] == board[8] { //
return true
} else if board[6] == board[7] && board[7] == board[8] { //
return true
}
}
return false
}
a little bit more compact:
func checkWin(board: [Int]) -> Bool{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return true
}
}
return false
}
better to get also the winner-id or 0 for no winner:
func getWinner(board: [Int]) -> Int{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return board[check[0]]
}
}
return 0
}
or if you like to put it in an enum:
enum Winner {
case none
case player(id: Int)
}
func checkWin(board: [Int]) -> Winner{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return .player(id: board[check[0]])
}
}
return .none
}

EF other way to Sum() new Column

i got stuck on this query that calculate the new column.
i cannot explain briefly just see the snippet code below.
from user in context.Table
select new
{
Total = user.Total,
Paid = user.Paid,
Balance = //should be Total - Paid to assign result
}
i have tried this query
var result = from a in context.EnrollmentRequests
where a.SchoolYear == SchoolYear
select new
{
a.StudentID,
Name = a.Student.FirstName + " " + a.Student.MiddleName + " " + a.Student.LastName,
Tuition = context.Miscs.Where(m => m.YearLevel == a.YearLevel && m.SchoolYear == SchoolYear && m.Term == a.Term && m.CourseID == a.CourseID)
.Select(ms => new { Amount = ms.Amount })
.Union(context.StudentCharges
.Where(s => s.YearLevel == a.YearLevel && s.SchoolYear == SchoolYear && s.Term == a.Term && s.CourseID == a.CourseID && s.StudentID == a.StudentID)
.Select(ss => new { Amount = ss.Amount }))
.Union(context.StudentSubjectTakes
.Where(st => st.StudentID == a.StudentID && st.SchoolYear == a.SchoolYear && st.Term == a.Term && st.YearLevel == a.YearLevel && st.EducationalLevel == a.Student.StudentAdvanceEducations.FirstOrDefault().EducationLevel)
.Select(st => new
{
Amount = context.SubjectOfferedFees
.Where(f => f.SubjectsOfferedID == st.SubjectsOfferedID).Sum(w => (decimal?)w.Cost ?? 0)
}))
.Select(f => f.Amount).Sum(),
PaymentMade = context.Payments.Where(p => p.SchoolYear == SchoolYear && p.Term == a.Term && p.StudentID == a.StudentID && p.PaymentDes == "Tuition Fee").Sum(sm => (decimal?)sm.Amount),
Balance = Tuition - PaymentMade //Does not exist on current context
};
but doesn't work it says that does not exist on current context.
how could this possible.
thanks. this will be helpful to anyone.
Balance = user.Total - user.Paid

Swift: Error when updated Xcode: 'could not find overload for '&&' that accepts the supplied arguments'

I just updated to the non beta version of Xcode 6 (finally) and, coming from beta 5, got a few errors that I didn't get before, one being "could not find overload for '&&' that accepts the supplied arguments"
I am following a tutorial here and from another question, I know that this error is because "the expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions."
I'm a beginner; how do I break up an expression into sub-expressions?
My code:
func checkForWin(){
//first row across
var youWin = 1
var theyWin = 0
var whoWon = ["Lost":0,"Won":1]
for (key,value) in whoWon {
if ((plays[6] == value && plays[7] == value && plays[8] == value) || //across the bottom
(plays[3] == value && plays[4] == value && plays[5] == value) || //across the middle
(plays[0] == value && plays[1] == value && plays[2] == value) || //across the top
(plays[6] == value && plays[3] == value && plays[0] == value) || //down the left side
(plays[7] == value && plays[4] == value && plays[1] == value) || //down the middle
(plays[8] == value && plays[5] == value && plays[2] == value) || //down the right side
(plays[6] == value && plays[4] == value && plays[2] == value) || //diagonal
(plays[8] == value && plays[4] == value && plays[0] == value)){//diagonal
userMessage.hidden = false
youLabel.hidden = false
userMessage.text = "\(key)!"
done = true;
}
}
}
To break it up, you just need to add more ()s. Like this:
if (((plays[6] == value) && (plays[7] == value) && (plays[8] == value)) ||
((plays[3] == value) && (plays[4] == value) && (plays[5] == value)) ||
((plays[0] == value) && (plays[1] == value) && (plays[2] == value)) ||
((plays[8] == value) && (plays[4] == value) && (plays[0] == value)))

Entity framework better way to reduce lambda condition WHERE?

I had the code below that provide search, 4 field, the
lambda will increase huge lot for each field included in, and it is time consuming and not efficient to have such a combination search, any better way to write a proper search?
var result = db.ValuePairs
.Where(c=>
(isStatus && isText && isCode && isGroupCd)
?
c.Status == status && c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText && isCode)
?
c.Status == status && c.Text == text && c.Code == code
:
(isStatus && isText && isGroupCd)
?
c.Status == status && c.Text == text && c.GroupCode == groupCd
:
(isStatus && isCode && isGroupCd)
?
c.Status == status && c.Code == code && c.GroupCode == groupCd
:
(isText && isCode && isGroupCd)
?
c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText)
?
c.Status == status && c.Text == text
:
(isStatus && isCode)
?
c.Status == status && c.Code == code
:
(isStatus && isGroupCd)
?
c.Status == status && c.GroupCode == groupCd
:
(isText && isCode)
?
c.Text == text && c.Code == code
:
(isText && isGroupCd)
?
c.Text == text && c.GroupCode == groupCd
:
(isCode && isGroupCd)
?
c.Code == code && c.GroupCode == groupCd
:
(isStatus)
?
c.Status == status
:
(isText)
?
c.Text == text
:
(isCode)
?
c.Code == code
:
(isGroupCd)
?
c.GroupCode == groupCd
:
groupCd != ""
);
Pretty simple boolean logic there...
.Where(c =>
(!isStatus || c.Status == status)
&& (!isCode || c.Code == code)
&& (!isGroupCd || c.GroupCode == groupCd)
);