Best K6 code snippet using js.Teardown
query_facets_test.go
Source:query_facets_test.go
1/*2 * Copyright (C) 2017 Dgraph Labs, Inc. and Contributors3 *4 * This program is free software: you can redistribute it and/or modify5 * it under the terms of the GNU Affero General Public License as published by6 * the Free Software Foundation, either version 3 of the License, or7 * (at your option) any later version.8 *9 * This program is distributed in the hope that it will be useful,10 * but WITHOUT ANY WARRANTY; without even the implied warranty of11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12 * GNU Affero General Public License for more details.13 *14 * You should have received a copy of the GNU Affero General Public License15 * along with this program. If not, see <http://www.gnu.org/licenses/>.16 */17package query18import (19 "testing"20 "time"21 "github.com/stretchr/testify/require"22 "github.com/dgraph-io/dgraph/types"23 "github.com/dgraph-io/dgraph/x"24)25func populateGraphWithFacets(t *testing.T) {26 x.AssertTrue(ps != nil)27 // So, user we're interested in has uid: 1.28 // She has 5 friends: 23, 24, 25, 31, and 10129 friendFacets1 := map[string]string{"since": "2006-01-02T15:04:05"}30 friendFacets2 := map[string]string{31 "since": "2005-05-02T15:04:05", "close": "true", "family": "false", "age": "33"}32 friendFacets3 := map[string]string{33 "since": "2004-05-02T15:04:05", "close": "true", "family": "true", "tag": "\"Domain3\""}34 friendFacets4 := map[string]string{35 "since": "2007-05-02T15:04:05", "close": "false", "family": "true", "tag": "34"}36 addEdgeToUID(t, "friend", 1, 23, friendFacets1)37 addEdgeToUID(t, "friend", 1, 24, friendFacets3)38 addEdgeToUID(t, "friend", 1, 25, friendFacets4)39 addEdgeToUID(t, "friend", 1, 31, friendFacets1)40 addEdgeToUID(t, "friend", 1, 101, friendFacets2)41 addEdgeToUID(t, "friend", 31, 24, nil)42 addEdgeToUID(t, "friend", 23, 1, friendFacets1)43 addEdgeToUID(t, "schools", 33, 2433, nil)44 friendFacets5 := map[string]string{45 "games": `"football basketball chess tennis"`, "close": "false", "age": "35"}46 friendFacets6 := map[string]string{47 "games": `"football basketball hockey"`, "close": "false"}48 addEdgeToUID(t, "friend", 31, 1, friendFacets5)49 addEdgeToUID(t, "friend", 31, 25, friendFacets6)50 nameFacets := map[string]string{"origin": `"french"`}51 // Now let's add a few properties for the main user.52 addEdgeToValue(t, "name", 1, "Michonne", nameFacets)53 addEdgeToValue(t, "gender", 1, "female", nil)54 // Now let's add a name for each of the friends, except 101.55 addEdgeToTypedValue(t, "name", 23, types.StringID, []byte("Rick Grimes"), nameFacets)56 addEdgeToValue(t, "gender", 23, "male", nil)57 addEdgeToValue(t, "name", 24, "Glenn Rhee", nameFacets)58 addEdgeToValue(t, "name", 25, "Daryl Dixon", nil)59 addEdgeToValue(t, "name", 31, "Andrea", nil)60 addEdgeToValue(t, "name", 33, "Michale", nil)61 // missing name for 101 -- no name edge and no facets.62 addEdgeToLangValue(t, "name", 320, "Test facet", "en",63 map[string]string{"type": `"Test facet with lang"`})64 time.Sleep(5 * time.Millisecond)65}66// teardownGraphWithFacets removes friend edges otherwise tests in query_test.go67// are affected by populateGraphWithFacets.68func teardownGraphWithFacets(t *testing.T) {69 delEdgeToUID(t, "friend", 1, 23)70 delEdgeToUID(t, "friend", 1, 24)71 delEdgeToUID(t, "friend", 1, 25)72 delEdgeToUID(t, "friend", 1, 31)73 delEdgeToUID(t, "friend", 1, 101)74 delEdgeToUID(t, "friend", 31, 24)75 delEdgeToUID(t, "friend", 23, 1)76 delEdgeToUID(t, "friend", 31, 1)77 delEdgeToUID(t, "friend", 31, 25)78 delEdgeToUID(t, "schools", 33, 2433)79 delEdgeToLangValue(t, "name", 320, "Test facet", "en")80}81func TestRetrieveFacetsSimple(t *testing.T) {82 populateGraphWithFacets(t)83 defer teardownGraphWithFacets(t)84 query := `85 {86 me(func: uid(0x1)) {87 name @facets88 gender @facets89 }90 }91 `92 js := processToFastJsonNoErr(t, query)93 require.JSONEq(t,94 `{"data":{"me":[{"name|origin":"french","name":"Michonne","gender":"female"}]}}`,95 js)96}97func TestOrderFacets(t *testing.T) {98 populateGraphWithFacets(t)99 defer teardownGraphWithFacets(t)100 // to see how friend @facets are positioned in output.101 query := `102 {103 me(func: uid(1)) {104 friend @facets(orderasc:since) {105 name106 }107 }108 }109 `110 js := processToFastJsonNoErr(t, query)111 require.JSONEq(t,112 `{"data":{"me":[{"friend":[{"name":"Glenn Rhee","friend|since":"2004-05-02T15:04:05Z"},{"friend|since":"2005-05-02T15:04:05Z"},{"name":"Rick Grimes","friend|since":"2006-01-02T15:04:05Z"},{"name":"Andrea","friend|since":"2006-01-02T15:04:05Z"},{"name":"Daryl Dixon","friend|since":"2007-05-02T15:04:05Z"}]}]}}`,113 js)114}115func TestOrderdescFacets(t *testing.T) {116 populateGraphWithFacets(t)117 defer teardownGraphWithFacets(t)118 // to see how friend @facets are positioned in output.119 query := `120 {121 me(func: uid(1)) {122 friend @facets(orderdesc:since) {123 name124 }125 }126 }127 `128 js := processToFastJsonNoErr(t, query)129 require.JSONEq(t,130 `{"data":{"me":[{"friend":[{"name":"Daryl Dixon","friend|since":"2007-05-02T15:04:05Z"},{"name":"Rick Grimes","friend|since":"2006-01-02T15:04:05Z"},{"name":"Andrea","friend|since":"2006-01-02T15:04:05Z"},{"friend|since":"2005-05-02T15:04:05Z"},{"name":"Glenn Rhee","friend|since":"2004-05-02T15:04:05Z"}]}]}}`,131 js)132}133func TestOrderdescFacetsWithFilters(t *testing.T) {134 populateGraphWithFacets(t)135 defer teardownGraphWithFacets(t)136 query := `137 {138 var(func: uid(1)) {139 f as friend140 }141 me(func: uid(1)) {142 friend @filter(uid(f)) @facets(orderdesc:since) {143 name144 }145 }146 }147 `148 js := processToFastJsonNoErr(t, query)149 require.JSONEq(t,150 `{"data":{"me":[{"friend":[{"name":"Daryl Dixon","friend|since":"2007-05-02T15:04:05Z"},{"name":"Rick Grimes","friend|since":"2006-01-02T15:04:05Z"},{"name":"Andrea","friend|since":"2006-01-02T15:04:05Z"},{"friend|since":"2005-05-02T15:04:05Z"},{"name":"Glenn Rhee","friend|since":"2004-05-02T15:04:05Z"}]}]}}`,151 js)152}153func TestRetrieveFacetsAsVars(t *testing.T) {154 populateGraphWithFacets(t)155 defer teardownGraphWithFacets(t)156 // to see how friend @facets are positioned in output.157 query := `158 {159 var(func: uid(0x1)) {160 friend @facets(a as since)161 }162 me(func: uid( 23)) {163 name164 val(a)165 }166 }167 `168 js := processToFastJsonNoErr(t, query)169 require.JSONEq(t,170 `{"data": {"me":[{"name":"Rick Grimes","val(a)":"2006-01-02T15:04:05Z"}]}}`,171 js)172}173func TestRetrieveFacetsUidValues(t *testing.T) {174 populateGraphWithFacets(t)175 defer teardownGraphWithFacets(t)176 // to see how friend @facets are positioned in output.177 query := `178 {179 me(func: uid(0x1)) {180 friend @facets {181 name @facets182 }183 }184 }185 `186 js := processToFastJsonNoErr(t, query)187 require.JSONEq(t,188 `{"data":{"me":[{"friend":[{"name|origin":"french","name":"Rick Grimes","friend|since":"2006-01-02T15:04:05Z"},{"name|origin":"french","name":"Glenn Rhee","friend|close":true,"friend|family":true,"friend|since":"2004-05-02T15:04:05Z","friend|tag":"Domain3"},{"name":"Daryl Dixon","friend|close":false,"friend|family":true,"friend|since":"2007-05-02T15:04:05Z","friend|tag":34},{"name":"Andrea","friend|since":"2006-01-02T15:04:05Z"},{"friend|age":33,"friend|close":true,"friend|family":false,"friend|since":"2005-05-02T15:04:05Z"}]}]}}`,189 js)190}191func TestRetrieveFacetsAll(t *testing.T) {192 populateGraphWithFacets(t)193 defer teardownGraphWithFacets(t)194 query := `195 {196 me(func: uid(0x1)) {197 name @facets198 friend @facets {199 name @facets200 gender @facets201 }202 gender @facets203 }204 }205 `206 js := processToFastJsonNoErr(t, query)207 require.JSONEq(t,208 `{"data":{"me":[{"name|origin":"french","name":"Michonne","friend":[{"name|origin":"french","name":"Rick Grimes","gender":"male","friend|since":"2006-01-02T15:04:05Z"},{"name|origin":"french","name":"Glenn Rhee","friend|close":true,"friend|family":true,"friend|since":"2004-05-02T15:04:05Z","friend|tag":"Domain3"},{"name":"Daryl Dixon","friend|close":false,"friend|family":true,"friend|since":"2007-05-02T15:04:05Z","friend|tag":34},{"name":"Andrea","friend|since":"2006-01-02T15:04:05Z"},{"friend|age":33,"friend|close":true,"friend|family":false,"friend|since":"2005-05-02T15:04:05Z"}],"gender":"female"}]}}`,209 js)210}211func TestFacetsNotInQuery(t *testing.T) {212 populateGraphWithFacets(t)213 defer teardownGraphWithFacets(t)214 query := `215 {216 me(func: uid(0x1)) {217 name218 gender219 friend {220 name221 gender222 }223 }224 }225 `226 js := processToFastJsonNoErr(t, query)227 require.JSONEq(t,228 `{"data": {"me":[{"friend":[{"gender":"male","name":"Rick Grimes"},{"name":"Glenn Rhee"},{"name":"Daryl Dixon"},{"name":"Andrea"}],"gender":"female","name":"Michonne"}]}}`,229 js)230}231func TestSubjectWithNoFacets(t *testing.T) {232 populateGraphWithFacets(t)233 defer teardownGraphWithFacets(t)234 // id 33 does not have any facets associated with name and school235 query := `236 {237 me(func: uid(0x21)) {238 name @facets239 school @facets {240 name241 }242 }243 }244 `245 js := processToFastJsonNoErr(t, query)246 require.JSONEq(t,247 `{"data": {"me":[{"name":"Michale"}]}}`,248 js)249}250func TestFetchingFewFacets(t *testing.T) {251 populateGraphWithFacets(t)252 defer teardownGraphWithFacets(t)253 // only 1 friend of 1 has facet : "close" and she/he has no name254 query := `255 {256 me(func: uid(0x1)) {257 name258 friend @facets(close) {259 name260 }261 }262 }263 `264 js := processToFastJsonNoErr(t, query)265 require.JSONEq(t,266 `{"data":{"me":[{"name":"Michonne","friend":[{"name":"Rick Grimes"},{"name":"Glenn Rhee","friend|close":true},{"name":"Daryl Dixon","friend|close":false},{"name":"Andrea"},{"friend|close":true}]}]}}`,267 js)268}269func TestFetchingNoFacets(t *testing.T) {270 populateGraphWithFacets(t)271 defer teardownGraphWithFacets(t)272 // TestFetchingFewFacets but without the facet. Returns no facets.273 query := `274 {275 me(func: uid(0x1)) {276 name277 friend @facets() {278 name279 }280 }281 }282 `283 js := processToFastJsonNoErr(t, query)284 require.JSONEq(t,285 `{"data": {"me":[{"friend":[{"name":"Rick Grimes"},{"name":"Glenn Rhee"},{"name":"Daryl Dixon"},{"name":"Andrea"}],"name":"Michonne"}]}}`,286 js)287}288func TestFacetsSortOrder(t *testing.T) {289 populateGraphWithFacets(t)290 defer teardownGraphWithFacets(t)291 // order of facets in gql query should not matter.292 query := `293 {294 me(func: uid(0x1)) {295 name296 friend @facets(family, close) {297 name298 }299 }300 }301 `302 js := processToFastJsonNoErr(t, query)303 require.JSONEq(t,304 `{"data":{"me":[{"name":"Michonne","friend":[{"name":"Rick Grimes"},{"name":"Glenn Rhee","friend|close":true,"friend|family":true},{"name":"Daryl Dixon","friend|close":false,"friend|family":true},{"name":"Andrea"},{"friend|close":true,"friend|family":false}]}]}}`,305 js)306}307func TestUnknownFacets(t *testing.T) {308 populateGraphWithFacets(t)309 defer teardownGraphWithFacets(t)310 // uknown facets should be ignored.311 query := `312 {313 me(func: uid(0x1)) {314 name315 friend @facets(unknownfacets1, unknownfacets2) {316 name317 }318 }319 }320 `321 js := processToFastJsonNoErr(t, query)322 require.JSONEq(t,323 `{"data": {"me":[{"friend":[{"name":"Rick Grimes"},{"name":"Glenn Rhee"},{"name":"Daryl Dixon"},{"name":"Andrea"}],"name":"Michonne"}]}}`,324 js)325}326func TestFacetsMutation(t *testing.T) {327 populateGraphWithFacets(t)328 defer teardownGraphWithFacets(t)329 delEdgeToUID(t, "friend", 1, 24) // Delete friendship between Michonne and Glenn330 friendFacets := map[string]string{"since": "2001-11-10T00:00:00Z", "close": "false", "family": "false"}331 addEdgeToUID(t, "friend", 1, 101, friendFacets) // and 101 is not close friend now.332 query := `333 {334 me(func: uid(0x1)) {335 name336 friend @facets {337 name338 }339 }340 }341 `342 js := processToFastJsonNoErr(t, query)343 require.JSONEq(t,344 `{"data":{"me":[{"name":"Michonne","friend":[{"name":"Rick Grimes","friend|since":"2006-01-02T15:04:05Z"},{"name":"Daryl Dixon","friend|close":false,"friend|family":true,"friend|since":"2007-05-02T15:04:05Z","friend|tag":34},{"name":"Andrea","friend|since":"2006-01-02T15:04:05Z"},{"friend|close":false,"friend|family":false,"friend|since":"2001-11-10T00:00:00Z"}]}]}}`,345 js)346}347func TestFacetsFilterSimple(t *testing.T) {348 populateGraphWithFacets(t)349 defer teardownGraphWithFacets(t)350 // find close friends of 1351 query := `352 {353 me(func: uid(0x1)) {354 name355 friend @facets(eq(close, true)) {356 name357 uid358 }359 }360 }361 `362 js := processToFastJsonNoErr(t, query)363 // 0x65 does not have name.364 require.JSONEq(t,365 `{"data": {"me":[{"friend":[{"uid":"0x18","name":"Glenn Rhee"},{"uid":"0x65"}],"name":"Michonne"}]}}`,366 js)367}368func TestFacetsFilterSimple2(t *testing.T) {369 populateGraphWithFacets(t)370 defer teardownGraphWithFacets(t)371 // find close friends of 1372 query := `373 {374 me(func: uid(0x1)) {375 name376 friend @facets(eq(tag, "Domain3")) {377 name378 uid379 }380 }381 }382 `383 js := processToFastJsonNoErr(t, query)384 require.JSONEq(t,385 `{"data": {"me":[{"friend":[{"uid":"0x18","name":"Glenn Rhee"}],"name":"Michonne"}]}}`,386 js)387}388func TestFacetsFilterSimple3(t *testing.T) {389 populateGraphWithFacets(t)390 defer teardownGraphWithFacets(t)391 // find close friends of 1392 query := `393 {394 me(func: uid(0x1)) {395 name396 friend @facets(eq(tag, "34")) {397 name398 uid399 }400 }401 }402 `403 js := processToFastJsonNoErr(t, query)404 require.JSONEq(t,405 `{"data": {"me":[{"friend":[{"uid":"0x19","name":"Daryl Dixon"}],"name":"Michonne"}]}}`,406 js)407}408func TestFacetsFilterOr(t *testing.T) {409 populateGraphWithFacets(t)410 defer teardownGraphWithFacets(t)411 // find close or family friends of 1412 query := `413 {414 me(func: uid(0x1)) {415 name416 friend @facets(eq(close, true) OR eq(family, true)) {417 name418 uid419 }420 }421 }422 `423 js := processToFastJsonNoErr(t, query)424 // 0x65 (101) does not have name.425 require.JSONEq(t,426 `{"data": {"me":[{"friend":[{"uid":"0x18","name":"Glenn Rhee"},{"uid":"0x19","name":"Daryl Dixon"},{"uid":"0x65"}],"name":"Michonne"}]}}`,427 js)428}429func TestFacetsFilterAnd(t *testing.T) {430 populateGraphWithFacets(t)431 defer teardownGraphWithFacets(t)432 // unknown filters do not have any effect on results.433 query := `434 {435 me(func: uid(0x1)) {436 name437 friend @facets(eq(close, true) AND eq(family, false)) {438 name439 uid440 }441 }442 }443 `444 js := processToFastJsonNoErr(t, query)445 require.JSONEq(t,446 `{"data": {"me":[{"friend":[{"uid":"0x65"}],"name":"Michonne"}]}}`,447 js)448}449func TestFacetsFilterle(t *testing.T) {450 populateGraphWithFacets(t)451 defer teardownGraphWithFacets(t)452 // find friends of 1 below 36 years of age.453 query := `454 {455 me(func: uid(0x1)) {456 name457 friend @facets(le(age, 35)) {458 name459 uid460 }461 }462 }463 `464 js := processToFastJsonNoErr(t, query)465 require.JSONEq(t,466 `{"data": {"me":[{"friend":[{"uid":"0x65"}],"name":"Michonne"}]}}`,467 js)468}469func TestFacetsFilterge(t *testing.T) {470 populateGraphWithFacets(t)471 defer teardownGraphWithFacets(t)472 // find friends of 1 above 32 years of age.473 query := `474 {475 me(func: uid(0x1)) {476 name477 friend @facets(ge(age, 33)) {478 name479 uid480 }481 }482 }483 `484 js := processToFastJsonNoErr(t, query)485 require.JSONEq(t,486 `{"data": {"me":[{"friend":[{"uid":"0x65"}],"name":"Michonne"}]}}`,487 js)488}489func TestFacetsFilterAndOrle(t *testing.T) {490 populateGraphWithFacets(t)491 defer teardownGraphWithFacets(t)492 // find close or family friends of 1 before 2007-01-10493 query := `494 {495 me(func: uid(0x1)) {496 name497 friend @facets(eq(close, true) OR eq(family, true) AND le(since, "2007-01-10")) {498 name499 uid500 }501 }502 }503 `504 js := processToFastJsonNoErr(t, query)505 // 0x65 (101) does not have name.506 require.JSONEq(t,507 `{"data": {"me":[{"friend":[{"uid":"0x18","name":"Glenn Rhee"},{"uid":"0x65"}],"name":"Michonne"}]}}`,508 js)509}510func TestFacetsFilterAndOrge2(t *testing.T) {511 populateGraphWithFacets(t)512 defer teardownGraphWithFacets(t)513 // find close or family friends of 1 after 2007-01-10514 query := `515 {516 me(func: uid(0x1)) {517 name518 friend @facets(eq(close, false) OR eq(family, true) AND ge(since, "2007-01-10")) {519 name520 uid521 }522 }523 }524 `525 js := processToFastJsonNoErr(t, query)526 require.JSONEq(t,527 `{"data": {"me":[{"friend":[{"uid":"0x19","name":"Daryl Dixon"}],"name":"Michonne"}]}}`,528 js)529}530func TestFacetsFilterNotAndOrgeMutuallyExclusive(t *testing.T) {531 populateGraphWithFacets(t)532 defer teardownGraphWithFacets(t)533 // find Not (close or family friends of 1 after 2007-01-10)534 // Mutually exclusive of above result : TestFacetsFilterNotAndOrge535 query := `536 {537 me(func: uid(0x1)) {538 name539 friend @facets(not (eq(close, false) OR eq(family, true) AND ge(since, "2007-01-10"))) {540 name541 uid542 }543 }544 }545 `546 js := processToFastJsonNoErr(t, query)547 require.JSONEq(t,548 `{"data": {"me":[{"friend":[{"uid":"0x17","name":"Rick Grimes"},{"uid":"0x18","name":"Glenn Rhee"},{"uid":"0x1f","name":"Andrea"},{"uid":"0x65"}],"name":"Michonne"}]}}`,549 js)550}551func TestFacetsFilterUnknownFacets(t *testing.T) {552 populateGraphWithFacets(t)553 defer teardownGraphWithFacets(t)554 // unknown facets should filter out edges.555 query := `556 {557 me(func: uid(0x1)) {558 name559 friend @facets(ge(dob, "2007-01-10")) {560 name561 uid562 }563 }564 }565 `566 js := processToFastJsonNoErr(t, query)567 require.JSONEq(t,568 `{"data": {"me":[{"name":"Michonne"}]}}`,569 js)570}571func TestFacetsFilterUnknownOrKnown(t *testing.T) {572 populateGraphWithFacets(t)573 defer teardownGraphWithFacets(t)574 // unknown filters with OR do not have any effect on results575 query := `576 {577 me(func: uid(0x1)) {578 name579 friend @facets(ge(dob, "2007-01-10") OR eq(family, true)) {580 name581 uid582 }583 }584 }585 `586 js := processToFastJsonNoErr(t, query)587 require.JSONEq(t,588 `{"data": {"me":[{"friend":[{"uid":"0x18","name":"Glenn Rhee"},{"uid":"0x19","name":"Daryl Dixon"}],"name":"Michonne"}]}}`,589 js)590}591func TestFacetsFilterallofterms(t *testing.T) {592 populateGraphWithFacets(t)593 defer teardownGraphWithFacets(t)594 query := `595 {596 me(func: uid(31)) {597 name598 friend @facets(allofterms(games, "football chess tennis")) {599 name600 uid601 }602 }603 }604 `605 js := processToFastJsonNoErr(t, query)606 require.JSONEq(t,607 `{"data": {"me":[{"friend":[{"name":"Michonne","uid":"0x1"}],"name":"Andrea"}]}}`,608 js)609}610func TestFacetsFilterAllofMultiple(t *testing.T) {611 populateGraphWithFacets(t)612 defer teardownGraphWithFacets(t)613 query := `614 {615 me(func: uid(31)) {616 name617 friend @facets(allofterms(games, "football basketball")) {618 name619 uid620 }621 }622 }623 `624 js := processToFastJsonNoErr(t, query)625 require.JSONEq(t,626 `{"data": {"me":[{"friend":[{"name":"Michonne","uid":"0x1"}, {"name":"Daryl Dixon","uid":"0x19"}],"name":"Andrea"}]}}`,627 js)628}629func TestFacetsFilterAllofNone(t *testing.T) {630 populateGraphWithFacets(t)631 defer teardownGraphWithFacets(t)632 // nothing matches in allofterms633 query := `634 {635 me(func: uid(31)) {636 name637 friend @facets(allofterms(games, "football chess tennis cricket")) {638 name639 uid640 }641 }642 }643 `644 js := processToFastJsonNoErr(t, query)645 require.JSONEq(t,646 `{"data": {"me":[{"name":"Andrea"}]}}`,647 js)648}649func TestFacetsFilteranyofterms(t *testing.T) {650 populateGraphWithFacets(t)651 defer teardownGraphWithFacets(t)652 query := `653 {654 me(func: uid(31)) {655 name656 friend @facets(anyofterms(games, "tennis cricket")) {657 name658 uid659 }660 }661 }662 `663 js := processToFastJsonNoErr(t, query)664 require.JSONEq(t,665 `{"data": {"me":[{"friend":[{"uid":"0x1","name":"Michonne"}],"name":"Andrea"}]}}`,666 js)667}668func TestFacetsFilterAnyofNone(t *testing.T) {669 populateGraphWithFacets(t)670 defer teardownGraphWithFacets(t)671 query := `672 {673 me(func: uid(31)) {674 name675 friend @facets(anyofterms(games, "cricket")) {676 name677 uid678 }679 }680 }681 `682 js := processToFastJsonNoErr(t, query)683 require.JSONEq(t,684 `{"data": {"me":[{"name":"Andrea"}]}}`,685 js)686}687func TestFacetsFilterAllofanyofterms(t *testing.T) {688 populateGraphWithFacets(t)689 defer teardownGraphWithFacets(t)690 query := `691 {692 me(func: uid(31)) {693 name694 friend @facets(allofterms(games, "basketball hockey") OR anyofterms(games, "chess")) {695 name696 uid697 }698 }699 }700 `701 js := processToFastJsonNoErr(t, query)702 require.JSONEq(t,703 `{"data": {"me":[{"friend":[{"uid":"0x1","name":"Michonne"},{"uid":"0x19","name":"Daryl Dixon"}],"name":"Andrea"}]}}`,704 js)705}706func TestFacetsFilterAllofAndanyofterms(t *testing.T) {707 populateGraphWithFacets(t)708 defer teardownGraphWithFacets(t)709 query := `710 {711 me(func: uid(31)) {712 name713 friend @facets(allofterms(games, "hockey") AND anyofterms(games, "football basketball")) {714 name715 uid716 }717 }718 }719 `720 js := processToFastJsonNoErr(t, query)721 require.JSONEq(t,722 `{"data": {"me":[{"friend":[{"uid":"0x19","name":"Daryl Dixon"}],"name":"Andrea"}]}}`,723 js)724}725func TestFacetsFilterAtValueFail(t *testing.T) {726 populateGraphWithFacets(t)727 defer teardownGraphWithFacets(t)728 // facet filtering is not supported at value level.729 query := `730 {731 me(func: uid(1)) {732 friend {733 name @facets(eq(origin, "french"))734 }735 }736 }737`738 _, err := processToFastJson(t, query)739 require.Error(t, err)740}741func TestFacetsFilterAndRetrieval(t *testing.T) {742 populateGraphWithFacets(t)743 defer teardownGraphWithFacets(t)744 // Close should not be retrieved.. only used for filtering.745 query := `746 {747 me(func: uid(1)) {748 name749 friend @facets(eq(close, true)) @facets(family) {750 name751 uid752 }753 }754 }755 `756 js := processToFastJsonNoErr(t, query)757 require.JSONEq(t,758 `{"data":{"me":[{"name":"Michonne","friend":[{"name":"Glenn Rhee","uid":"0x18","friend|family":true},{"uid":"0x65","friend|family":false}]}]}}`,759 js)760}761func TestFacetWithLang(t *testing.T) {762 populateGraphWithFacets(t)763 defer teardownGraphWithFacets(t)764 query := `765 {766 me(func: uid(320)) {767 name@en @facets768 }769 }770 `771 js := processToFastJsonNoErr(t, query)772 require.JSONEq(t, `{"data":{"me":[{"name@en|type":"Test facet with lang","name@en":"Test facet"}]}}`, js)773}774func TestFilterUidFacetMismatch(t *testing.T) {775 populateGraphWithFacets(t)776 defer teardownGraphWithFacets(t)777 query := `778 {779 me(func: uid(0x1)) {780 friend @filter(uid(24, 101)) @facets {781 name782 }783 }784 }785 `786 js := processToFastJsonNoErr(t, query)787 require.JSONEq(t, `{"data":{"me":[{"friend":[{"name":"Glenn Rhee","friend|close":true,"friend|family":true,"friend|since":"2004-05-02T15:04:05Z","friend|tag":"Domain3"},{"friend|age":33,"friend|close":true,"friend|family":false,"friend|since":"2005-05-02T15:04:05Z"}]}]}}`, js)788}789func TestRecurseFacetOrder(t *testing.T) {790 populateGraphWithFacets(t)791 defer teardownGraphWithFacets(t)792 query := `793 {794 me(func: uid(1)) @recurse(depth: 2) {795 friend @facets(orderdesc: since)796 uid797 name798 }799 }800 `801 js := processToFastJsonNoErr(t, query)802 require.JSONEq(t, `{"data":{"me":[{"friend":[{"uid":"0x19","name":"Daryl Dixon","friend|since":"2007-05-02T15:04:05Z"},{"friend":[{"friend|since":"2006-01-02T15:04:05Z"}],"uid":"0x17","name":"Rick Grimes","friend|since":"2006-01-02T15:04:05Z"},{"uid":"0x1f","name":"Andrea","friend|since":"2006-01-02T15:04:05Z"},{"uid":"0x65","friend|since":"2005-05-02T15:04:05Z"},{"uid":"0x18","name":"Glenn Rhee","friend|since":"2004-05-02T15:04:05Z"}],"uid":"0x1","name":"Michonne"}]}}`, js)803 query = `804 {805 me(func: uid(1)) @recurse(depth: 2) {806 friend @facets(orderasc: since)807 uid808 name809 }810 }811 `812 js = processToFastJsonNoErr(t, query)813 require.JSONEq(t, `{"data":{"me":[{"friend":[{"uid":"0x18","name":"Glenn Rhee","friend|since":"2004-05-02T15:04:05Z"},{"uid":"0x65","friend|since":"2005-05-02T15:04:05Z"},{"friend":[{"friend|since":"2006-01-02T15:04:05Z"}],"uid":"0x17","name":"Rick Grimes","friend|since":"2006-01-02T15:04:05Z"},{"uid":"0x1f","name":"Andrea","friend|since":"2006-01-02T15:04:05Z"},{"uid":"0x19","name":"Daryl Dixon","friend|since":"2007-05-02T15:04:05Z"}],"uid":"0x1","name":"Michonne"}]}}`, js)814}815func TestFacetsAlias(t *testing.T) {816 populateGraphWithFacets(t)817 defer teardownGraphWithFacets(t)818 query := `819 {820 me(func: uid(0x1)) {821 name @facets(o: origin)822 friend @facets(family, tagalias: tag, since) {823 name @facets(o: origin)824 }825 }826 }827 `828 js := processToFastJsonNoErr(t, query)829 require.Equal(t, `{"data":{"me":[{"o":"french","name":"Michonne","friend":[{"o":"french","name":"Rick Grimes","friend|since":"2006-01-02T15:04:05Z"},{"o":"french","name":"Glenn Rhee","friend|family":true,"friend|since":"2004-05-02T15:04:05Z","tagalias":"Domain3"},{"name":"Daryl Dixon","friend|family":true,"friend|since":"2007-05-02T15:04:05Z","tagalias":34},{"name":"Andrea","friend|since":"2006-01-02T15:04:05Z"},{"friend|family":false,"friend|since":"2005-05-02T15:04:05Z"}]}]}}`, js)830}831func TestFacetsAlias2(t *testing.T) {832 populateGraphWithFacets(t)833 defer teardownGraphWithFacets(t)834 query := `835 {836 me2(func: uid(0x1)) {837 friend @facets(f: family, a as since, orderdesc: tag, close)838 }839 me(func: uid(23)) {840 name841 val(a)842 }843 }844 `845 js := processToFastJsonNoErr(t, query)846 require.JSONEq(t, `{"data":{"me2":[{"friend":[{"friend|close":true,"f":false,"friend|since":"2005-05-02T15:04:05Z"},{"friend|since":"2006-01-02T15:04:05Z"},{"friend|since":"2006-01-02T15:04:05Z"},{"friend|close":true,"f":true,"friend|since":"2004-05-02T15:04:05Z","friend|tag":"Domain3"},{"friend|close":false,"f":true,"friend|since":"2007-05-02T15:04:05Z","friend|tag":34}]}],"me":[{"name":"Rick Grimes", "val(a)":"2006-01-02T15:04:05Z"}]}}`, js)847}...
logWriter_test.go
Source:logWriter_test.go
1// Copyright 2019 ThoughtWorks, Inc.2/*----------------------------------------------------------------3 * Copyright (c) ThoughtWorks, Inc.4 * Licensed under the Apache License, Version 2.05 * See LICENSE in the project root for license information.6 *----------------------------------------------------------------*/7package logger8import (9 "io/ioutil"10 "os"11 "path/filepath"12 "strings"13 "sync"14 "testing"15 "github.com/getgauge/gauge/config"16)17func TestMain(m *testing.M) {18 exitCode := m.Run()19 os.RemoveAll(filepath.Join("_testdata", "logs"))20 os.Exit(exitCode)21}22func TestLogWriterToOutputInfoLogInCorrectFormat(t *testing.T) {23 defer tearDown(t)24 setupLogger("info")25 l := newLogWriter("js")26 if _, err := l.Stdout.Write([]byte("{\"logLevel\": \"info\", \"message\": \"Foo\"}\n")); err != nil {27 t.Fatalf("Unable to write to logWriter")28 }29 assertLogContains(t, []string{"[js] [INFO] Foo"})30}31func TestLogWriterToOutputInfoLogInCorrectFormatWhenNewLinesPresent(t *testing.T) {32 defer tearDown(t)33 setupLogger("info")34 l := newLogWriter("js")35 if _, err := l.Stdout.Write([]byte("{\"logLevel\": \"info\", \"message\": \"Foo\"}\n\r\n{\"logLevel\": \"info\", \"message\": \"Bar\"}\n{\"logLevel\": \"info\", \"message\": \"Baz\"}")); err != nil {36 t.Fatalf("Unable to write to logWriter")37 }38 assertLogContains(t, []string{"[js] [INFO] Foo", "[js] [INFO] Bar", "[js] [INFO] Baz"})39 assertLogDoesNotContains(t, []string{"[js] [INFO] \r"})40}41func TestLogWriterToOutputInfoLogWithMultipleLines(t *testing.T) {42 defer tearDown(t)43 setupLogger("debug")44 l := newLogWriter("js")45 if _, err := l.Stdout.Write([]byte("{\"logLevel\": \"info\", \"message\": \"Foo\"}\n{\"logLevel\": \"debug\", \"message\": \"Bar\"}\n")); err != nil {46 t.Fatalf("Unable to write to logWriter")47 }48 assertLogContains(t, []string{"[js] [INFO] Foo", "[js] [DEBUG] Bar"})49}50func TestLogWriterToLogPlainStrings(t *testing.T) {51 defer tearDown(t)52 setupLogger("debug")53 l := newLogWriter("js")54 if _, err := l.Stdout.Write([]byte("Foo Bar\n")); err != nil {55 t.Fatalf("Unable to write to logWriter")56 }57 assertLogContains(t, []string{"Foo Bar"})58}59func TestUnformattedLogWrittenToStderrShouldBePrefixedWithError(t *testing.T) {60 defer tearDown(t)61 setupLogger("debug")62 l := newLogWriter("js")63 if _, err := l.Stderr.Write([]byte("Foo Bar\n")); err != nil {64 t.Fatalf("Unable to write to logWriter")65 }66 assertLogContains(t, []string{"[ERROR]"})67}68func TestUnformattedLogWrittenToStdoutShouldBePrefixedWithInfo(t *testing.T) {69 defer tearDown(t)70 setupLogger("debug")71 l := newLogWriter("js")72 if _, err := l.Stdout.Write([]byte("Foo Bar\n")); err != nil {73 t.Fatalf("Unable to write to logWriter")74 }75 assertLogContains(t, []string{"[INFO]"})76}77func TestLoggingFromDifferentWritersAtSameTime(t *testing.T) {78 defer tearDown(t)79 setupLogger("info")80 j := newLogWriter("js")81 h := newLogWriter("html-report")82 var wg sync.WaitGroup83 var err error84 wg.Add(5)85 go func() {86 Debug(false, "debug msg")87 wg.Done()88 }()89 go func() {90 _, err = h.Stdout.Write([]byte("{\"logLevel\": \"warning\", \"message\": \"warning msg\"}\n{\"logLevel\": \"debug\", \"message\": \"debug msg\"}\n"))91 wg.Done()92 }()93 go func() {94 _, err = j.Stdout.Write([]byte("{\"logLevel\": \"info\", \"message\": \"info msg\"}\n{\"logLevel\": \"error\", \"message\": \"error msg\"}\n"))95 wg.Done()96 }()97 go func() {98 _, err = h.Stdout.Write([]byte("{\"logLevel\": \"info\", \"message\": \"info msg\"}\n{\"logLevel\": \"error\", \"message\": \"error msg\"}\n"))99 wg.Done()100 }()101 go func() {102 _, err = j.Stdout.Write([]byte("{\"logLevel\": \"warning\", \"message\": \"warning msg\"}\n{\"logLevel\": \"debug\", \"message\": \"debug msg\"}\n"))103 wg.Done()104 }()105 wg.Wait()106 if err != nil {107 t.Fatalf("Unable to write to logWriter")108 }109 assertLogContains(t, []string{110 "[js] [WARNING] warning msg",111 "[js] [ERROR] error msg",112 "[js] [INFO] info msg",113 "[js] [DEBUG] debug msg",114 "[html-report] [WARNING] warning msg",115 "[html-report] [ERROR] error msg",116 "[html-report] [INFO] info msg",117 "[html-report] [DEBUG] debug msg",118 "[Gauge] [DEBUG] debug msg",119 })120}121func tearDown(t *testing.T) {122 config.ProjectRoot = ""123 initialized = false124 if err := os.Truncate(ActiveLogFile, 0); err != nil {125 t.Logf("Could not truncate file")126 }127}128func setupLogger(level string) {129 config.ProjectRoot, _ = filepath.Abs("_testdata")130 Initialize(false, "info", CLI)131}132func newLogWriter(loggerID string) *LogWriter {133 f, _ := os.OpenFile(ActiveLogFile, os.O_RDWR, 0)134 return &LogWriter{135 Stderr: Writer{ShouldWriteToStdout: false, stream: 0, LoggerID: loggerID, File: f, isErrorStream: true},136 Stdout: Writer{ShouldWriteToStdout: false, stream: 0, LoggerID: loggerID, File: f},137 }138}139func assertLogContains(t *testing.T, want []string) {140 got, err := ioutil.ReadFile(ActiveLogFile)141 if err != nil {142 t.Fatalf("Unable to read log file. Error: %s", err.Error())143 }144 for _, w := range want {145 if !strings.Contains(string(got), w) {146 t.Errorf("Expected %s to contain %s.", string(got), w)147 }148 }149}150func assertLogDoesNotContains(t *testing.T, want []string) {151 got, err := ioutil.ReadFile(ActiveLogFile)152 if err != nil {153 t.Fatalf("Unable to read log file. Error: %s", err.Error())154 }155 for _, w := range want {156 if strings.Contains(string(got), w) {157 t.Errorf("Expected %s to not contain %s.", string(got), w)158 }159 }160}...
Teardown
Using AI Code Generation
1func main() {2 js.Global().Set("teardown", js.FuncOf(teardown))3}4func teardown(this js.Value, args []js.Value) interface{} {5}6func teardown(this js.Value, args []js.Value) interface{} {7}8func teardown(this js.Value, args []js.Value) interface{} {9}10func teardown(this js.Value, args []js.Value) interface{} {11}12func teardown(this js.Value, args []js.Value) interface{} {13}14func teardown(this js.Value, args []js.Value) interface{} {15}16func teardown(this js.Value, args []js.Value) interface{} {17}18func teardown(this js.Value, args []js.Value) interface{} {19}20func teardown(this js.Value, args []js.Value) interface{} {21}22func teardown(this js.Value, args []js.Value) interface{} {23}24func teardown(this js.Value, args []js.Value) interface{} {25}
Teardown
Using AI Code Generation
1func main() {2 js.Global().Set("teardown", js.FuncOf(teardown))3}4func teardown(this js.Value, args []js.Value) interface{} {5}6function teardown() {7 teardown = go.teardown;8 teardown();9}10 teardown();
Teardown
Using AI Code Generation
1import (2func main() {3 js.Global.Set("go", js.MakeWrapper(goWrapper{}))4}5type goWrapper struct{}6func (goWrapper) Teardown() {7 fmt.Println("Teardown")8}9import (10func main() {11 js.Global.Get("go").Call("Teardown")12}
Teardown
Using AI Code Generation
1js.Global().Get("Teardown").Invoke()2func Teardown() {3}4js.Global().Set("Teardown", js.FuncOf(func(this js.Value, args []js.Value) interface{} {5 Teardown()6}))
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!