Example #1
0
func checkTweets(t *testing.T, rc *importer.RunContext, expectedPostGroups ...map[string]string) {
	postsNode, err := imptest.GetRequiredChildPathObj(rc.RootNode(), "tweets")
	if err != nil {
		t.Fatal(err)
	}

	childRefs, err := imptest.FindChildRefs(postsNode)
	if err != nil {
		t.Fatal(err)
	}

	// Merges groups, last wins
	expectedPosts := map[string]string{}
	for _, posts := range expectedPostGroups {
		for k, v := range posts {
			expectedPosts[k] = v
		}
	}

	if len(childRefs) != len(expectedPosts) {
		t.Fatalf("After import, found %d child refs, want %d: %v", len(childRefs), len(expectedPosts), childRefs)
	}

	for _, ref := range childRefs {
		childNode, err := rc.Host.ObjectFromRef(ref)
		if err != nil {
			t.Fatal(err)
		}
		foundID := childNode.Attr("twitterId")
		expectedContent, ok := expectedPosts[foundID]
		if !ok {
			t.Fatalf("Found unexpected child node %v with id %q", childNode, foundID)
		}
		foundContent := childNode.Attr(nodeattr.Content)
		if foundContent != expectedContent {
			t.Fatalf("Found unexpected child node %v with content %q when we want %q", childNode, foundContent, expectedContent)
		}
		delete(expectedPosts, foundID)
	}
	if len(expectedPosts) != 0 {
		t.Fatalf("The following entries were expected but not found: %#v", expectedPosts)
	}
}
Example #2
0
// Verify that a batch import of 3 posts works
func TestIntegrationRun(t *testing.T) {
	const authToken = "gina:foo"
	const attrKey = "key"
	const attrValue = "value"

	responder := httputil.FileResponder("testdata/batchresponse.json")
	transport, err := httputil.NewRegexpFakeTransport([]*httputil.Matcher{
		&httputil.Matcher{`^https\://api\.pinboard\.in/v1/posts/all\?auth_token=gina:foo&format=json&results=10000&todt=\d\d\d\d.*`, responder},
	})
	if err != nil {
		t.Fatal(err)
	}

	imptest.ImporterTest(t, "pinboard", transport, func(rc *importer.RunContext) {
		err = rc.AccountNode().SetAttrs(attrAuthToken, authToken)
		if err != nil {
			t.Fatal(err)
		}

		testee := imp{}
		if err := testee.Run(rc); err != nil {
			t.Fatal(err)
		}

		postsNode, err := imptest.GetRequiredChildPathObj(rc.RootNode(), "posts")
		if err != nil {
			t.Fatal(err)
		}

		childRefs, err := imptest.FindChildRefs(postsNode)
		if err != nil {
			t.Fatal(err)
		}

		expectedPosts := map[string]string{
			`https://wiki.archlinux.org/index.php/xorg#Display_size_and_DPI`:                   "Xorg - ArchWiki",
			`http://www.harihareswara.net/sumana/2014/08/17/0`:                                 "One Way Confidence Will Look",
			`http://www.wikiart.org/en/marcus-larson/fishing-near-the-fjord-by-moonlight-1862`: "Fishing Near The Fjord By Moonlight - Marcus Larson - WikiArt.org",
		}

		if len(childRefs) != len(expectedPosts) {
			t.Fatalf("After import, found %d child refs, want %d: %v", len(childRefs), len(expectedPosts), childRefs)
		}

		for _, ref := range childRefs {
			childNode, err := rc.Host.ObjectFromRef(ref)
			if err != nil {
				t.Fatal(err)
			}
			foundURL := childNode.Attr(nodeattr.URL)
			expectedTitle, ok := expectedPosts[foundURL]
			if !ok {
				t.Fatalf("Found unexpected child node %v with url %q", childNode, foundURL)
			}
			foundTitle := childNode.Attr(nodeattr.Title)
			if foundTitle != expectedTitle {
				t.Fatalf("Found unexpected child node %v with title %q when we want %q", childNode, foundTitle, expectedTitle)
			}
			delete(expectedPosts, foundURL)
		}
		if len(expectedPosts) != 0 {
			t.Fatalf("The following entries were expected but not found: %#v", expectedPosts)
		}
	})
}